Skip to content

Commit

Permalink
Change void to dynamic when overriding method arguments
Browse files Browse the repository at this point in the history
We pass the arguments further to `noSuchMethod`, so to make
the compiler happy we need to override the type.

Also reverts the previous attempt to fix it by suppressing
analyzer diagnostic.

PiperOrigin-RevId: 509240249
  • Loading branch information
srawlins committed Feb 16, 2023
1 parent 71e41b9 commit 781752c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ class MockBuilder implements Builder {
// The generator appends a suffix to fake classes
b.body.add(Code('// ignore_for_file: camel_case_types\n'));
// The generator has to occasionally implement sealed classes
b.body.add(Code('// ignore_for_file: subtype_of_sealed_class\n'));
// The generator has to use `void`-typed arguments.
b.body.add(Code('// ignore_for_file: use_of_void_result\n\n'));
b.body.add(Code('// ignore_for_file: subtype_of_sealed_class\n\n'));
b.body.addAll(mockLibraryInfo.fakeClasses);
b.body.addAll(mockLibraryInfo.mockClasses);
});
Expand Down Expand Up @@ -1676,8 +1674,8 @@ class _MockClassInfo {
return Parameter((pBuilder) {
pBuilder.name = name;
if (!superParameterType.containsPrivateName) {
pBuilder.type =
_typeReference(superParameterType, forceNullable: forceNullable);
pBuilder.type = _typeReference(superParameterType,
forceNullable: forceNullable, overrideVoid: true);
}
if (parameter.isNamed) pBuilder.named = true;
if (parameter.isRequiredNamed && sourceLibIsNonNullable) {
Expand Down Expand Up @@ -1935,7 +1933,8 @@ class _MockClassInfo {
builder.requiredParameters.add(Parameter((pBuilder) {
pBuilder.name = parameter.displayName;
if (!parameter.type.containsPrivateName) {
pBuilder.type = _typeReference(parameter.type, forceNullable: true);
pBuilder.type = _typeReference(parameter.type,
forceNullable: true, overrideVoid: true);
}
}));

Expand Down Expand Up @@ -1997,7 +1996,10 @@ class _MockClassInfo {
// TODO(srawlins): Contribute this back to a common location, like
// package:source_gen?
Reference _typeReference(analyzer.DartType type,
{bool forceNullable = false}) {
{bool forceNullable = false, bool overrideVoid = false}) {
if (overrideVoid && type.isVoid) {
return TypeReference((b) => b..symbol = 'dynamic');
}
if (type is analyzer.InterfaceType) {
return TypeReference((b) {
b
Expand Down
18 changes: 18 additions & 0 deletions test/builder/auto_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3542,6 +3542,24 @@ void main() {
);
});
});
test('Void in argument type gets overriden to dynamic', () async {
final mocksContent = await buildWithNonNullable({
...annotationsAsset,
'foo|lib/foo.dart': dedent(r'''
class Foo {
void m(void x) {}
}
'''),
'foo|test/foo_test.dart': dedent(r'''
import 'package:foo/foo.dart';
import 'package:mockito/annotations.dart';
@GenerateMocks([Foo])
void main() {}
'''),
});
expect(mocksContent, contains('void m(dynamic x)'));
});
}

TypeMatcher<List<int>> _containsAllOf(a, [b]) => decodedMatches(
Expand Down
18 changes: 18 additions & 0 deletions test/builder/custom_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,24 @@ void main() {
);
});
});
test('Void in argument type coming from type arg becomes dynamic', () async {
final mocksContent = await buildWithNonNullable({
...annotationsAsset,
'foo|lib/foo.dart': dedent(r'''
class Foo<T> {
T m(T x) => x;
}
'''),
'foo|test/foo_test.dart': dedent(r'''
import 'package:foo/foo.dart';
import 'package:mockito/annotations.dart';
@GenerateMocks([], customMocks: [MockSpec<Foo<void>>()])
void main() {}
'''),
});
expect(mocksContent, contains('void m(dynamic x)'));
});
}

TypeMatcher<List<int>> _containsAllOf(a, [b]) => decodedMatches(
Expand Down

0 comments on commit 781752c

Please sign in to comment.