Skip to content

Commit 070fd07

Browse files
committed
Add unit tests
1 parent f6af1d4 commit 070fd07

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

lib/utils/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export 'context.dart';
1+
export 'string_resources.dart';
Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,60 @@
1-
import 'dart:ffi';
2-
31
import 'package:flutter_bloc_app_template/data/network/api_result.dart';
42
import 'package:flutter_test/flutter_test.dart';
53

64
void main() {
75
group('ApiResult', () {
8-
test('success should contain correct data', () {
9-
final result = const ApiResult.success('Test Data');
6+
test('should return success result with correct data', () {
7+
const result = ApiResult.success(42);
8+
9+
final output = ApiResultWhen(result).when(
10+
success: (data) => 'Success with $data',
11+
error: (message) => 'Error: $message',
12+
loading: () => 'Loading...',
13+
);
1014

11-
expect(result, isA<Success<String>>());
12-
expect((result as Success<String>).data, 'Test Data');
15+
expect(output, 'Success with 42');
1316
});
1417

15-
test('error should contain correct message', () {
16-
final result = const ApiResult<String>.error('Test Error');
18+
test('should return error result with correct message', () {
19+
const result = ApiResult<String>.error('Something went wrong');
20+
21+
final output = ApiResultWhen(result).when(
22+
success: (data) => 'Success with $data',
23+
error: (message) => 'Error: $message',
24+
loading: () => 'Loading...',
25+
);
1726

18-
expect(result, isA<Error<String>>());
19-
expect((result as Error<String>).message, 'Test Error');
27+
expect(output, 'Error: Something went wrong');
2028
});
2129

22-
test('loading should be of type Loading', () {
23-
final result = const ApiResult<Void>.loading();
30+
test('should return loading result', () {
31+
const result = ApiResult<String>.loading();
2432

25-
expect(result, isA<Loading<Void>>());
33+
final output = ApiResultWhen(result).when(
34+
success: (data) => 'Success with $data',
35+
error: (message) => 'Error: $message',
36+
loading: () => 'Loading...',
37+
);
38+
39+
expect(output, 'Loading...');
40+
});
41+
42+
test('should throw assertion error for unknown type', () {
43+
final invalid = const _InvalidApiResult<int>();
44+
45+
expect(
46+
() => ApiResultWhen(invalid).when(
47+
success: (data) => 'Success with $data',
48+
error: (message) => 'Error: $message',
49+
loading: () => 'Loading...',
50+
),
51+
throwsA(isA<AssertionError>()),
52+
);
2653
});
2754
});
2855
}
56+
57+
/// A fake invalid implementation to test the fallback case in `when`
58+
class _InvalidApiResult<T> implements ApiResult<T> {
59+
const _InvalidApiResult();
60+
}

test/utils/pair_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'package:flutter_bloc_app_template/utils/pair.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('Pair', () {
6+
test('should store and expose first and second values', () {
7+
const pair = Pair<int, String>(1, 'one');
8+
9+
expect(pair.first, 1);
10+
expect(pair.second, 'one');
11+
});
12+
13+
test('should have correct toString representation', () {
14+
const pair = Pair<int, String>(1, 'one');
15+
16+
expect(pair.toString(), 'Pair(1, one)');
17+
});
18+
19+
test('should compare equal for identical values', () {
20+
const a = Pair<int, String>(1, 'one');
21+
const b = Pair<int, String>(1, 'one');
22+
23+
expect(a, equals(b));
24+
expect(a.hashCode, equals(b.hashCode));
25+
});
26+
27+
test('should not be equal if first or second values differ', () {
28+
const a = Pair<int, String>(1, 'one');
29+
const b = Pair<int, String>(2, 'one');
30+
const c = Pair<int, String>(1, 'two');
31+
32+
expect(a == b, isFalse);
33+
expect(a == c, isFalse);
34+
});
35+
36+
test('should be equal if identical (same instance)', () {
37+
const pair = Pair(1, 'one');
38+
39+
expect(pair == pair, isTrue);
40+
});
41+
42+
test('should not be equal to unrelated types', () {
43+
const pair = Pair(1, 'one');
44+
45+
expect(pair, isNot(equals('Pair(1, one)')));
46+
});
47+
});
48+
}

0 commit comments

Comments
 (0)