Skip to content

Commit dc3eb65

Browse files
Ilya Yanokcopybara-github
authored andcommitted
Generate valid functions with optional non-nullable arguments
If a method of a mocked class returns a function with optional yet non-nullable argument, Mockito currently produces something like ```dart (int x, {int y}) => /* doesn't matter */ ``` which is not a valid Dart, since `y` must either be `required` or have a default value. `required` won't fit into the desired type and coming up with a default value is not easy, since it has to be a constant, but `dummyValue` doesn't always produce one. Fortunately, we can just widen the type here and make it nullable. PiperOrigin-RevId: 582669927
1 parent fcb9779 commit dc3eb65

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

lib/src/builder.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,12 +1580,13 @@ class _MockClassInfo {
15801580
} else if (parameter.isOptionalPositional) {
15811581
final matchingParameter = _matchingParameter(parameter,
15821582
superParameterType: parameter.type,
1583-
defaultName: '__p$position');
1583+
defaultName: '__p$position',
1584+
forceNullable: true);
15841585
b.optionalParameters.add(matchingParameter);
15851586
position++;
15861587
} else if (parameter.isOptionalNamed) {
15871588
final matchingParameter = _matchingParameter(parameter,
1588-
superParameterType: parameter.type);
1589+
superParameterType: parameter.type, forceNullable: true);
15891590
b.optionalParameters.add(matchingParameter);
15901591
} else if (parameter.isRequiredNamed) {
15911592
final matchingParameter = _matchingParameter(parameter,

test/builder/auto_mocks_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ void main() {
24142414
_containsAllOf('''
24152415
returnValue: (
24162416
int __p0, [
2417-
String __p1,
2417+
String? __p1,
24182418
]) {},'''),
24192419
);
24202420
});
@@ -2431,7 +2431,7 @@ void main() {
24312431
_containsAllOf('''
24322432
returnValue: (
24332433
_i2.Foo __p0, {
2434-
bool b,
2434+
bool? b,
24352435
}) {},'''),
24362436
);
24372437
});

test/end2end/foo.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Foo<T> {
2222
Future<void>? returnsNullableFutureVoid() => Future.value();
2323
Future<T> returnsFuture(T x) => Future.value(x);
2424
Bar returnsBar(int arg) => Bar();
25+
String Function(int x, [String s]) returnsFunction() => (x, [s = '']) => '';
26+
String Function(int x, {String s}) returnsFunctionNamed() =>
27+
(x, {s = ''}) => '';
2528
}
2629

2730
class Bar {

test/end2end/generated_mocks_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ void main() {
163163
when(foo.returnsFuture(any)).thenAnswer((_) async => 1);
164164
expect(await foo.returnsFuture(0), 1);
165165
});
166+
167+
test(
168+
'a method returning a function with optional non-nullable argument '
169+
'can be stubbed', () {
170+
when(foo.returnsFunction()).thenReturn((x, [s = 'x']) => s + s);
171+
expect(foo.returnsFunction()(1), equals('xx'));
172+
expect(foo.returnsFunction()(1, 'y'), equals('yy'));
173+
});
174+
175+
test(
176+
'a method returning a function with named optional non-nullable '
177+
'argument can be stubbed', () {
178+
when(foo.returnsFunctionNamed()).thenReturn((x, {s = 'x'}) => s + s);
179+
expect(foo.returnsFunctionNamed()(1), equals('xx'));
180+
expect(foo.returnsFunctionNamed()(1, s: 'y'), equals('yy'));
181+
});
166182
});
167183

168184
group('for a generated mock using unsupportedMembers', () {

0 commit comments

Comments
 (0)