Skip to content

Commit 6601e70

Browse files
committed
⬆️ Updated lints dependencies and updated formatting.
Also added small explaining on Future.error usage.
1 parent 4db5930 commit 6601e70

15 files changed

+307
-401
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ parameterizedTest('Example of CSV data',
256256
});
257257
```
258258

259+
### Testing with `Future.error` values
260+
When using `Future.error` directly in your parameterized test values, this might leads to not executing tests at all. In order to handle this correctly your can provide the `Future` as a callback function.
261+
But also need to make sure that the when the future is awaited its also catch. `Future.error` will throw the provided object. See [23](https://github.com/DutchCodingCompany/parameterized_test/issues/23) for more information.
262+
259263
## Additional information 💡
260264

261265
It's just a simple wrapper to easily execute tests multiple times with different values. Feel free to

analysis_options.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
include: package:very_good_analysis/analysis_options.yaml
22

3+
formatter:
4+
trailing_commas: automate
5+
36
analyzer:
47
errors:
58
unawaited_futures: warning

example/parameterized_test_example.dart

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ignore this lint for example reasons.
12
// ignore_for_file: avoid_print
23

34
import 'package:csv/csv.dart';
@@ -6,46 +7,44 @@ import 'package:test/test.dart';
67

78
void main() {
89
// Simple test containing a list of single values
10+
parameterizedTest('Example of list of single values', [1, 2, 3], (int value) {
11+
final result = value < 4;
12+
expect(result, true);
13+
});
14+
15+
// Simple test containing a list of multiple values
916
parameterizedTest(
10-
'Example of list of single values',
17+
'Example of list of multiple values',
1118
[
12-
1,
13-
2,
14-
3,
19+
[0, 1, 1],
20+
[1, 1, 2],
21+
[1, 2, 3],
22+
[2, 2, 4],
1523
],
16-
(int value) {
17-
final result = value < 4;
18-
expect(result, true);
24+
(int value1, int value2, int sum) {
25+
expect(value1 + value2, sum);
1926
},
2027
);
2128

22-
// Simple test containing a list of multiple values
23-
parameterizedTest('Example of list of multiple values', [
24-
[0, 1, 1],
25-
[1, 1, 2],
26-
[1, 2, 3],
27-
[2, 2, 4],
28-
], (int value1, int value2, int sum) {
29-
expect(value1 + value2, sum);
30-
});
31-
3229
// Test containing a list with complex objects
33-
parameterizedTest('Example of a list with complex object', [
34-
[DateTime(2024, 4, 12), 5],
35-
[DateTime(1969, 07, 20), 7],
36-
], (DateTime dateTime, int expectedWeekday) {
37-
expect(dateTime.weekday, expectedWeekday);
38-
});
39-
40-
// Test containing a list of enums
4130
parameterizedTest(
42-
'Example using enum as value',
43-
FruitEnum.values,
44-
(FruitEnum testEnum) {
45-
expect(testEnum.name.length, testEnum.wordLength);
31+
'Example of a list with complex object',
32+
[
33+
[DateTime(2024, 4, 12), 5],
34+
[DateTime(1969, 07, 20), 7],
35+
],
36+
(DateTime dateTime, int expectedWeekday) {
37+
expect(dateTime.weekday, expectedWeekday);
4638
},
4739
);
4840

41+
// Test containing a list of enums
42+
parameterizedTest('Example using enum as value', FruitEnum.values, (
43+
FruitEnum testEnum,
44+
) {
45+
expect(testEnum.name.length, testEnum.wordLength);
46+
});
47+
4948
// Test retreiving the list of values from a function
5049
List<dynamic> provideData() {
5150
return [
@@ -56,13 +55,13 @@ void main() {
5655
];
5756
}
5857

59-
parameterizedTest(
60-
'Example of list of values from function',
61-
provideData(),
62-
(int value1, int value2, int sum) {
63-
expect(value1 + value2, sum);
64-
},
65-
);
58+
parameterizedTest('Example of list of values from function', provideData(), (
59+
int value1,
60+
int value2,
61+
int sum,
62+
) {
63+
expect(value1 + value2, sum);
64+
});
6665

6766
// Simple test with setup and teardown
6867
parameterizedTest(
@@ -88,28 +87,24 @@ void main() {
8887
// But this is not a good practice to use a delay like
8988
// this in a test. Running this test will take longer. This could be
9089
// fixed by using a package like fake_async.
91-
parameterizedTest(
92-
'Example using a async test',
93-
[
94-
100,
95-
200,
96-
300,
97-
],
98-
(int value) async {
99-
final millis = DateTime.now().millisecondsSinceEpoch;
100-
await Future<void>.delayed(Duration(milliseconds: value));
101-
final passed = DateTime.now().millisecondsSinceEpoch - millis;
90+
parameterizedTest('Example using a async test', [100, 200, 300], (
91+
int value,
92+
) async {
93+
final millis = DateTime.now().millisecondsSinceEpoch;
94+
await Future<void>.delayed(Duration(milliseconds: value));
95+
final passed = DateTime.now().millisecondsSinceEpoch - millis;
10296

103-
expect(passed >= value, true);
104-
},
105-
);
97+
expect(passed >= value, true);
98+
});
10699

107100
// Test with CSV data
108-
parameterizedTest('Example of CSV data',
109-
const CsvToListConverter().convert('kiwi,4\r\napple,5\r\nbanana,6'),
110-
(String fruit, int length) {
111-
expect(fruit.length, length);
112-
});
101+
parameterizedTest(
102+
'Example of CSV data',
103+
const CsvToListConverter().convert('kiwi,4\r\napple,5\r\nbanana,6'),
104+
(String fruit, int length) {
105+
expect(fruit.length, length);
106+
},
107+
);
113108
}
114109

115110
enum FruitEnum {

lib/parameterized_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// A library that simplifies writing parameterized tests in Dart.
2-
library parameterized_test;
2+
library;
33

44
export '/src/parameterized_test.dart'
55
show parameterizedGroup, parameterizedTest;

lib/src/errors/parameterized_error.dart

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ 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.forTypeError(
12-
List<dynamic> value,
13-
Function body,
14-
) {
11+
factory ParameterizedError.forTypeError(List<dynamic> value, Function body) {
1512
final bodyClosure = extractFunctionArgumentsSignature(body.toString());
1613

1714
return ParameterizedError(
18-
"Provided value(s) didn't match the function arguments types.\n"
19-
'Test values: $value\n'
20-
'Provided types: '
21-
'(${value.map((e) => e.runtimeType).join(', ')})\n'
22-
'Expected types: ($bodyClosure)');
15+
"Provided value(s) didn't match the function arguments types.\n"
16+
'Test values: $value\n'
17+
'Provided types: '
18+
'(${value.map((e) => e.runtimeType).join(', ')})\n'
19+
'Expected types: ($bodyClosure)',
20+
);
2321
}
2422

2523
/// Creates a [ParameterizedError] from a [TypeError].
@@ -32,13 +30,14 @@ class ParameterizedError extends Error {
3230
final positionalArgumentsCount = ','.allMatches(bodyClosure).length + 1;
3331

3432
return ParameterizedError(
35-
"Provided value(s) didn't match the function arguments count.\n"
36-
'Amount of provided values: ${value.length}\n'
37-
'Expected function arguments: $positionalArgumentsCount\n'
38-
'Test values: $value\n'
39-
'Provided types: '
40-
'(${value.map((e) => e.runtimeType).join(', ')})\n'
41-
'Expected types: ($bodyClosure)');
33+
"Provided value(s) didn't match the function arguments count.\n"
34+
'Amount of provided values: ${value.length}\n'
35+
'Expected function arguments: $positionalArgumentsCount\n'
36+
'Test values: $value\n'
37+
'Provided types: '
38+
'(${value.map((e) => e.runtimeType).join(', ')})\n'
39+
'Expected types: ($bodyClosure)',
40+
);
4241
}
4342

4443
/// Error message.
@@ -51,10 +50,7 @@ class ParameterizedError extends Error {
5150
final closureIndex = body.indexOf(closure);
5251
final endIndex = body.indexOf(')', closureIndex);
5352

54-
final bodyClosure = body.substring(
55-
closureIndex + closure.length,
56-
endIndex,
57-
);
53+
final bodyClosure = body.substring(closureIndex + closure.length, endIndex);
5854

5955
return bodyClosure.replaceAll(RegExp('<(.*)>'), '');
6056
}

lib/src/errors/stack_trace_extension.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ extension StackTraceExtension on StackTrace {
2727

2828
final frames = trace.frames.take(index).toList();
2929

30-
return !frames.every(
31-
(f) => f.package == 'parameterized_test' || f.isCore,
32-
);
30+
return !frames.every((f) => f.package == 'parameterized_test' || f.isCore);
3331
}
3432
}

lib/src/parameterized_test.dart

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import 'package:parameterized_test/src/test_options/value_with_test_options.dart
55
import 'package:test/test.dart';
66

77
/// Callback signature for test and group functions.
8-
typedef TestFunc = void Function(
9-
Object? description,
10-
dynamic Function() body, {
11-
String? testOn,
12-
Timeout? timeout,
13-
Object? skip,
14-
Object? tags,
15-
Map<String, dynamic>? onPlatform,
16-
int? retry,
17-
});
8+
typedef TestFunc =
9+
void Function(
10+
Object? description,
11+
dynamic Function() body, {
12+
String? testOn,
13+
Timeout? timeout,
14+
Object? skip,
15+
Object? tags,
16+
Map<String, dynamic>? onPlatform,
17+
int? retry,
18+
});
1819

1920
/// Callback signature for setUp and tearDown functions.
2021
typedef SetupFunc = void Function(dynamic Function());
@@ -42,12 +43,8 @@ typedef SetupFunc = void Function(dynamic Function());
4243
/// ```
4344
/// {@endtemplate}
4445
@isTestGroup
45-
ParameterizedTest get parameterizedTest => const ParameterizedTestImpl(
46-
group,
47-
test,
48-
setUp,
49-
tearDown,
50-
);
46+
ParameterizedTest get parameterizedTest =>
47+
const ParameterizedTestImpl(group, test, setUp, tearDown);
5148

5249
/// {@template parameterizedGroup}
5350
// ignore: comment_references
@@ -76,12 +73,8 @@ ParameterizedTest get parameterizedTest => const ParameterizedTestImpl(
7673
/// ```
7774
/// {@endtemplate}
7875
@isTestGroup
79-
ParameterizedTest get parameterizedGroup => const ParameterizedTestImpl(
80-
group,
81-
group,
82-
setUp,
83-
tearDown,
84-
);
76+
ParameterizedTest get parameterizedGroup =>
77+
const ParameterizedTestImpl(group, group, setUp, tearDown);
8578

8679
/// {@template ParameterizedTest}
8780
/// Implementation of parameterized test.
@@ -159,17 +152,16 @@ class ParameterizedTestImpl implements ParameterizedTest {
159152
testDescription,
160153
() {
161154
try {
162-
return Function.apply(
163-
body,
164-
value,
165-
);
155+
return Function.apply(body, value);
166156
}
157+
// We want to catch these errors to provide better error messages
167158
//ignore: avoid_catching_errors
168159
on NoSuchMethodError catch (e, s) {
169160
if (s.isInsideTestBody) rethrow;
170161

171162
throw ParameterizedError.fromNoSuchMethodError(e, value);
172163
}
164+
// We want to catch these errors to provide better error messages
173165
//ignore: avoid_catching_errors
174166
on TypeError catch (e, s) {
175167
if (s.isInsideTestBody) rethrow;
@@ -206,9 +198,7 @@ class ParameterizedTestImpl implements ParameterizedTest {
206198
List<dynamic> value,
207199
CustomDescriptionBuilder? customDescriptionBuilder,
208200
) {
209-
final mappedValues = value.map(
210-
(e) => e is String ? "'$e'" : e.toString(),
211-
);
201+
final mappedValues = value.map((e) => e is String ? "'$e'" : e.toString());
212202

213203
return customDescriptionBuilder?.call(description, index, value) ??
214204
'[ ${mappedValues.join(', ')} ]';
@@ -217,9 +207,9 @@ class ParameterizedTestImpl implements ParameterizedTest {
217207
/// Unpack wrapped values and test options.
218208
/// Handle different types of values.
219209
ValueWithTestOptions parseValues(dynamic value) => switch (value) {
220-
ValueWithTestOptions() => value,
221-
List() => (values: value, testOptions: null),
222-
Iterable() => (values: value.toList(), testOptions: null),
223-
_ => (values: [value], testOptions: null),
224-
};
210+
ValueWithTestOptions() => value,
211+
List() => (values: value, testOptions: null),
212+
Iterable() => (values: value.toList(), testOptions: null),
213+
_ => (values: [value], testOptions: null),
214+
};
225215
}

0 commit comments

Comments
 (0)