Skip to content

Instantly share code, notes, and snippets.

@mj-hd
Created November 26, 2021 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mj-hd/2ff63328ab092f2d19d8e78e32fb8971 to your computer and use it in GitHub Desktop.
Save mj-hd/2ff63328ab092f2d19d8e78e32fb8971 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