Skip to content

Commit 67c3237

Browse files
srawlinscopybara-github
authored andcommitted
Prepare mockito for the 'wildcard-variables' language feature.
We generally preserve positional parameter names, but in the case of wildcard parameters, we must rename them. We cannot override a wildcard parameter with a wildcard parameter, since we do use the parameter (we pass it to `super.noSuchMethod` in a call to `Invocation.method`). This change introduces some renaming logic for wildcard parameters, using new method-unique parameter names. PiperOrigin-RevId: 696158750
1 parent f72791d commit 67c3237

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

lib/src/builder.dart

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,14 +1318,21 @@ class _MockClassInfo {
13181318
if (parameter.isRequiredPositional || parameter.isOptionalPositional) {
13191319
final superParameterType =
13201320
_escapeCovariance(parameter, position: position);
1321-
final matchingParameter = _matchingParameter(parameter,
1322-
superParameterType: superParameterType, forceNullable: true);
1321+
final matchingParameter = _matchingParameter(
1322+
parameter,
1323+
superParameterType: superParameterType,
1324+
// A parameter in the overridden method may be a wildcard, in which
1325+
// case we need to rename it, as we use the parameter when we pass
1326+
// it to `Invocation.method`.
1327+
defaultName: '_$position',
1328+
forceNullable: true,
1329+
);
13231330
if (parameter.isRequiredPositional) {
13241331
builder.requiredParameters.add(matchingParameter);
13251332
} else {
13261333
builder.optionalParameters.add(matchingParameter);
13271334
}
1328-
invocationPositionalArgs.add(refer(parameter.displayName));
1335+
invocationPositionalArgs.add(refer(matchingParameter.name));
13291336
position++;
13301337
} else if (parameter.isNamed) {
13311338
final superParameterType =
@@ -1712,12 +1719,14 @@ class _MockClassInfo {
17121719
{required analyzer.DartType superParameterType,
17131720
String? defaultName,
17141721
bool forceNullable = false}) {
1722+
final parameterHasName = parameter.name.isNotEmpty && parameter.name != '_';
17151723
assert(
1716-
parameter.name.isNotEmpty || defaultName != null,
1717-
'parameter must have a non-empty name, or non-null defaultName must be '
1718-
'passed, but parameter name is "${parameter.name}" and defaultName is '
1719-
'$defaultName');
1720-
final name = parameter.name.isEmpty ? defaultName! : parameter.name;
1724+
parameterHasName || defaultName != null,
1725+
'parameter must have a non-empty name, or non-null defaultName must be '
1726+
'passed, but parameter name is "${parameter.name}" and defaultName is '
1727+
'$defaultName',
1728+
);
1729+
final name = !parameterHasName ? defaultName! : parameter.name;
17211730
return Parameter((pBuilder) {
17221731
pBuilder.name = name;
17231732
if (!superParameterType.containsPrivateName) {
@@ -1980,8 +1989,13 @@ class _MockClassInfo {
19801989

19811990
assert(setter.parameters.length == 1);
19821991
final parameter = setter.parameters.single;
1992+
// The parameter in the overridden setter may be a wildcard, in which case
1993+
// we need to rename it, as we use the parameter when we pass it to
1994+
// `Invocation.setter`.
1995+
final parameterName =
1996+
parameter.displayName == '_' ? '_value' : parameter.displayName;
19831997
builder.requiredParameters.add(Parameter((pBuilder) {
1984-
pBuilder.name = parameter.displayName;
1998+
pBuilder.name = parameterName;
19851999
if (!parameter.type.containsPrivateName) {
19862000
pBuilder.type = _typeReference(parameter.type,
19872001
forceNullable: true, overrideVoid: true);
@@ -2012,7 +2026,7 @@ class _MockClassInfo {
20122026
final invocation =
20132027
referImported('Invocation', 'dart:core').property('setter').call([
20142028
refer('#$name'),
2015-
refer(parameter.displayName),
2029+
refer(parameterName),
20162030
]);
20172031
final returnNoSuchMethod = refer('super')
20182032
.property('noSuchMethod')

test/builder/auto_mocks_test.dart

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,6 @@ void main() {
13641364
test('prefixes parameter type on generic function-typed parameter', () async {
13651365
await expectSingleNonNullableOutput(
13661366
dedent(r'''
1367-
import 'dart:async';
13681367
class Foo {
13691368
dynamic m(void Function(Foo f) a) {}
13701369
}
@@ -1377,7 +1376,6 @@ void main() {
13771376
test('prefixes return type on generic function-typed parameter', () async {
13781377
await expectSingleNonNullableOutput(
13791378
dedent(r'''
1380-
import 'dart:async';
13811379
class Foo {
13821380
void m(Foo Function() a) {}
13831381
}
@@ -1389,7 +1387,6 @@ void main() {
13891387
test('prefixes parameter type on function-typed parameter', () async {
13901388
await expectSingleNonNullableOutput(
13911389
dedent(r'''
1392-
import 'dart:async';
13931390
class Foo {
13941391
void m(void a(Foo f)) {}
13951392
}
@@ -1402,7 +1399,6 @@ void main() {
14021399
test('prefixes return type on function-typed parameter', () async {
14031400
await expectSingleNonNullableOutput(
14041401
dedent(r'''
1405-
import 'dart:async';
14061402
class Foo {
14071403
void m(Foo a()) {}
14081404
}
@@ -1411,6 +1407,20 @@ void main() {
14111407
);
14121408
});
14131409

1410+
test('renames wildcard parameters', () async {
1411+
await expectSingleNonNullableOutput(
1412+
dedent(r'''
1413+
class Foo {
1414+
void m(int _, int _) {}
1415+
}
1416+
'''),
1417+
_containsAllOf(
1418+
'void m(int? _0, int? _1) => super.noSuchMethod(Invocation.method(',
1419+
'Invocation.method(#m, [_0, _1])',
1420+
),
1421+
);
1422+
});
1423+
14141424
test('widens the type of parameters to be nullable', () async {
14151425
await expectSingleNonNullableOutput(
14161426
dedent(r'''
@@ -2046,6 +2056,23 @@ void main() {
20462056
);
20472057
});
20482058

2059+
test('overrides nullable instance setters with wildcard parameters',
2060+
() async {
2061+
await expectSingleNonNullableOutput(
2062+
dedent('''
2063+
class Foo {
2064+
void set m(int? _) {}
2065+
}
2066+
'''),
2067+
_containsAllOf(dedent2('''
2068+
set m(int? _value) => super.noSuchMethod(
2069+
Invocation.setter(#m, _value),
2070+
returnValueForMissingStub: null,
2071+
);
2072+
''')),
2073+
);
2074+
});
2075+
20492076
test('overrides inherited non-nullable instance setters', () async {
20502077
await expectSingleNonNullableOutput(
20512078
dedent('''

0 commit comments

Comments
 (0)