Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 10: Testing Futures #10

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Calculator - [Caster.IO](https://caster.io) project to showcase testing techniqu

[Lesson 9: Sharing code between tests with setUp() and tearDown()](https://caster.io/lessons/lesson-9-sharing-code-between-tests-with-setup-and-teardown) 👉🏾 [Branch](https://github.com/samuelematias/calculator_app/tree/lesson9)

[Lesson 10: Testing Futures](https://caster.io/lessons/lesson-10-testing-futures) 👉🏾 [Branch](https://github.com/samuelematias/calculator_app/tree/lesson10)

## License

```
Expand Down
3 changes: 3 additions & 0 deletions packages/calculator/lib/src/calculator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ class Calculator {
if (b == 0) throw ArgumentError('You cannot divide by zero');
return a / b;
}

Future<double> powerOfTwo(double a) =>
Future.delayed(const Duration(seconds: 1), () => a * a);
}
66 changes: 37 additions & 29 deletions packages/calculator/test/calculator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,55 @@ void main() {
calculator = Calculator();
});

group('add', () {
test('the calculator returns 4 when adding 2 and 2', () {
expect(calculator.add(2, 2), 4);
});
group('Calculator', () {
group('add', () {
test('returns 4 when adding 2 and 2', () {
expect(calculator.add(2, 2), 4);
});

test('the calculator returns 40 when adding 20 and 20', () {
expect(calculator.add(20, 20), 40);
test('returns 40 when adding 20 and 20', () {
expect(calculator.add(20, 20), 40);
});
});
});

group('substract', () {
test('the calculator returns 10 when substracting 10 to 20', () {
expect(calculator.substract(20, 10), 10);
});
group('substract', () {
test('returns 10 when substracting 10 to 20', () {
expect(calculator.substract(20, 10), 10);
});

test('the calculator returns -4 when substracting 8 to 4', () {
expect(calculator.substract(4, 8), -4);
test('returns -4 when substracting 8 to 4', () {
expect(calculator.substract(4, 8), -4);
});
});
});

group('multiple', () {
test('the calculator returns 45 when multiplying 5 tby 9', () {
expect(calculator.multiply(5, 9), 45);
});
group('multiple', () {
test('returns 45 when multiplying 5 tby 9', () {
expect(calculator.multiply(5, 9), 45);
});

test('the calculator returns 18 when multiplying 25 tby 9', () {
expect(calculator.multiply(2, 9), 18);
test('returns 18 when multiplying 25 tby 9', () {
expect(calculator.multiply(2, 9), 18);
});
});
});

group('divide', () {
test('the calculator returns 9 when dividing 27 by 3', () {
expect(calculator.divide(27, 3), 9);
});
group('divide', () {
test('returns 9 when dividing 27 by 3', () {
expect(calculator.divide(27, 3), 9);
});

test('returns 1 when dividing 27 by 27', () {
expect(calculator.divide(27, 27), 1);
});

test('the calculator returns 1 when dividing 27 by 27', () {
expect(calculator.divide(27, 27), 1);
test('throws an ArgumentError when dividing by zero', () {
expect(() => calculator.divide(27, 0), throwsArgumentError);
});
});

test('the calculator throws an ArgumentError when dividing by zero', () {
expect(() => calculator.divide(27, 0), throwsArgumentError);
group('powerOfTwo', () {
test('returns 81 when the input is 9', () async {
expect(await calculator.powerOfTwo(9), 81);
});
});
});
}