Menu
CoddyTech

Testing Public Contracts

Lesson 41 of 48 in Coddy's Dart for Go Developers course.

Go has no assert statement: tests live in _test.go files and report through t.Errorf or t.Fatal, and a plain program fails a probe with an explicit panic. Dart has assert, but assertions run only when the runtime enables them (dart run --enable-asserts), so required behavioral probes throw explicitly. Go strings are UTF-8 bytes and Dart strings are UTF-16 code units, so the check reads bytes in Go and codeUnits in Dart; both treat any unit at or above 128 as non-ASCII. Boundary tests distinguish plausible incorrect implementations without depending on private fields.

Required probes use explicit throws, because Dart asserts can be disabled. Test public behavior and boundaries rather than inspecting source strings or private implementation details. The displayed ASCII checks do not implement the Store task.

Complete Go program:

package main

import "fmt"

func check(ok bool) {
	if !ok {
		panic("failed")
	}
}

func ascii(s string) bool {
	for i := 0; i < len(s); i++ {
		if s[i] >= 128 {
			return false
		}
	}
	return true
}

func main() {
	check(ascii(""))
	check(ascii("v2"))
	check(ascii("\u007f"))
	check(!ascii("\u0080"))
	fmt.Println("checks passed")
}

Complete Dart program:

void check(bool ok){if(!ok)throw StateError('failed');}
bool ascii(String s)=>s.codeUnits.every((u)=>u<128);
void main(){check(ascii(''));check(ascii('v2'));check(ascii('\u007f'));check(!ascii('\u0080'));print('checks passed');}
challenge icon

Challenge

Easy

Implement Store(int limit) with bool put(String key,int value), int? get(String key) and getter size. Insert new keys only while below capacity; replacement of an existing key succeeds even at capacity. Rejection leaves contents unchanged. Input is capacity then complete key value pairs; output accepted|size. Capacity is 0 through 100, values are integers. Independent probes also inspect stored values and rejection behavior.

Do not print from editable files. The supplied exercise.dart parses input and returns output; the locked driver independently probes the public APIs and prints the result with one newline.

Your code goes in Store.dart, exercise.dart. main.dart holds the supplied input/output code and cannot be edited.

Try it yourself

import 'dart:io';
import 'dart:convert';
import 'exercise.dart';
import 'Store.dart';
void check(bool condition){if(!condition)throw StateError('Public API contract failed');}
Future<void> main() async {
final raw=await stdin.transform(utf8.decoder).join();
final input=raw.replaceFirst(RegExp(r'\r?\n$'),'');
final s=Store(1);check(s.put('a',2));check(!s.put('b',3)&&s.get('b')==null);check(s.put('a',7)&&s.get('a')==7&&s.size==1);check(!Store(0).put('x',1));
stdout.writeln(solve(input));
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Dart for Go Developers

Practice on your own: Online Dart compiler