Testing Public Contracts
Lesson 41 of 48 in Coddy's Dart for JavaScript Developers course.
Tests should observe public behavior rather than private representation. Dart assert checks may be disabled, so the task drivers use explicit conditional throws for mandatory API probes. JavaScript console.assert only reports a failed assertion and is not a reliable grading failure mechanism. A small explicit helper can throw on a failed condition in both languages without external test packages. The example checks ASCII boundaries, including empty input, rather than disclosing the Store implementation in the task.
Complete JavaScript program:
function check(ok){if(!ok)throw new Error('failed');}function ascii(s){return [...s].every(c=>c.codePointAt(0)<128);}check(ascii(''));check(ascii('v2'));check(ascii('\u007f'));check(!ascii('\u0080'));console.log('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
EasyImplement 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));
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Dart for JavaScript Developers
1First Dart Programs
Run Dart FilesOutput and CommentsImports and LibrariesBindings and TypesNumeric OperationsRecap: Parcel Invoice4Collection Pipelines
Iterables and LazinessMaps and Missing KeysSets and MembershipClosures and CallbacksSorting and ComparatorsRecap: Word Inventory3Functions and Values
Functions and ReturnsNamed ParametersNull SafetyStrings and UnicodeLists and MutationRecap: Record Readings6Objects and State
Classes and InstancesConstructors and FactoriesProperties and PrivacyIdentity and CopiesComposition Across FilesRecap: Bounded Counter9Parcel Depot Project
Depot: Valid ItemsDepot: Register StockPractice on your own: Online Dart compiler