Skip to content

Commit cd0bee3

Browse files
committed
✅ Added extra test for parameterized_error
1 parent f2ebca4 commit cd0bee3

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

lib/src/errors/parameterized_error.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class ParameterizedError extends Error {
88

99
/// Creates a [ParameterizedError] from a [TypeError].
1010
/// When arguments types don't match the function arguments types.
11-
factory ParameterizedError.fromTypeError(
11+
factory ParameterizedError.forTypeError(
1212
List<dynamic> value,
1313
Function body,
1414
) {
15-
final bodyClosure = _extractFunctionArgumentsSignature(body.toString());
15+
final bodyClosure = extractFunctionArgumentsSignature(body.toString());
1616

1717
return ParameterizedError(
1818
"Provided value(s) didn't match the function arguments types.\n"
@@ -28,7 +28,7 @@ class ParameterizedError extends Error {
2828
NoSuchMethodError e,
2929
List<dynamic> value,
3030
) {
31-
final bodyClosure = _extractFunctionArgumentsSignature(e.toString());
31+
final bodyClosure = extractFunctionArgumentsSignature(e.toString());
3232
final positionalArgumentsCount = ','.allMatches(bodyClosure).length + 1;
3333

3434
return ParameterizedError(
@@ -44,7 +44,9 @@ class ParameterizedError extends Error {
4444
/// Error message.
4545
final String message;
4646

47-
static String _extractFunctionArgumentsSignature(String body) {
47+
/// Helper function to extract the function arguments signature from
48+
/// Exception string.
49+
static String extractFunctionArgumentsSignature(String body) {
4850
const closure = 'Closure: (';
4951
final closureIndex = body.indexOf(closure);
5052
final endIndex = body.indexOf(')', closureIndex);

lib/src/parameterized_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class ParameterizedTestImpl implements ParameterizedTest {
168168
}
169169
//ignore: avoid_catching_errors
170170
on TypeError {
171-
throw ParameterizedError.fromTypeError(value, body);
171+
throw ParameterizedError.forTypeError(value, body);
172172
}
173173
},
174174
testOn: testOptions?.testOn,

test/parameterized_error_test.dart

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import 'package:parameterized_test/src/errors/parameterized_error.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
group('Test formatting of error messages', () {
6+
test('ParameterizedError from NoSuchMethodError', () {
7+
try {
8+
Function.apply((int value) => value, [1, 2]);
9+
//ignore: avoid_catching_errors
10+
} on NoSuchMethodError catch (e) {
11+
final result = ParameterizedError.fromNoSuchMethodError(e, [1, 2]);
12+
expect(
13+
result.message,
14+
"Provided value(s) didn't match the function arguments "
15+
'count.\n'
16+
'Amount of provided values: 2\n'
17+
'Expected function arguments: 1\n'
18+
'Test values: [1, 2]\n'
19+
'Provided types: (int, int)\n'
20+
'Expected types: (int)',
21+
);
22+
}
23+
try {
24+
Function.apply((int value, int value2) => value + value2, [1]);
25+
//ignore: avoid_catching_errors
26+
} on NoSuchMethodError catch (e) {
27+
final result = ParameterizedError.fromNoSuchMethodError(e, [1]);
28+
expect(
29+
result.message,
30+
"Provided value(s) didn't match the function arguments "
31+
'count.\n'
32+
'Amount of provided values: 1\n'
33+
'Expected function arguments: 2\n'
34+
'Test values: [1]\n'
35+
'Provided types: (int)\n'
36+
'Expected types: (int, int)',
37+
);
38+
}
39+
});
40+
41+
test('ParameterizedError for TypeError', () {
42+
final result = ParameterizedError.forTypeError(
43+
[1],
44+
(String value) => value,
45+
);
46+
expect(
47+
result.message,
48+
"Provided value(s) didn't match the function arguments types.\n"
49+
'Test values: [1]\n'
50+
'Provided types: (int)\n'
51+
'Expected types: (String)',
52+
);
53+
});
54+
});
55+
56+
test('extractFunctionArgumentsSignature tests', () {
57+
final testValues = [
58+
((int value) => Null, 'int'),
59+
((int value) => String, 'int'),
60+
((int value, String string) => Null, 'int, String'),
61+
((int value, String string) => double, 'int, String'),
62+
((Map<int, String> value, String string) => Null, 'Map, String'),
63+
(
64+
(Map<int, String> value, String string) => Map<double, bool>,
65+
'Map, String'
66+
),
67+
(
68+
(Map<int, Map<double, bool>> value, String string) => Null,
69+
'Map, String'
70+
),
71+
((int value, String string) async => Future<String>, 'int, String'),
72+
];
73+
74+
for (final e in testValues) {
75+
final result = ParameterizedError.extractFunctionArgumentsSignature(
76+
e.$1.toString(),
77+
);
78+
79+
expect(result, e.$2);
80+
}
81+
});
82+
}
File renamed without changes.

0 commit comments

Comments
 (0)