Skip to content

Instantly share code, notes, and snippets.

@utamori
Created July 13, 2022 10:27
Show Gist options
  • Save utamori/a615f1f354392e76e48579b0af85960f to your computer and use it in GitHub Desktop.
Save utamori/a615f1f354392e76e48579b0af85960f to your computer and use it in GitHub Desktop.
tdt.dart
int function(int input) {
if (input == 0) {
return 0;
}
if (input % 2 == 0) {
return 2;
} else {
return 1;
}
}
typedef _Input = int;
typedef _Expected = int;
void main() async {
<String, Tuple2<_Input, _Expected>>{
'value is zero': Tuple2(0, 0),
'value is odd': Tuple2(5, 0),
'value is even': Tuple2(2, 2),
}.forEach((title, testCase) {
final input = testCase.item1;
final expected = testCase.item2;
test('function $title', () {
expect(function(input), expected);
});
});
}
class Tuple2<T1, T2> {
Tuple2(this.item1, this.item2);
final T1 item1;
final T2 item2;
}
void test(String title, void Function() body) {
print('TEST: $title');
body();
}
void expect(dynamic actual, dynamic expected) {
if (actual != expected) {
print('FAILED ACTUAL: $actual, EXPECTED: $expected');
} else {
print('PASSED');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment