Skip to content

Commit

Permalink
Properly reference types in type arguments on a class-to-mock.
Browse files Browse the repository at this point in the history
Fixes #469

PiperOrigin-RevId: 395985920
  • Loading branch information
srawlins committed Sep 10, 2021
1 parent 2d22c17 commit 584b881
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 5.0.16-dev

* Fix type reference for nested, imported types found in type arguments on a
custom class-to-mock.
[#469](https://github.com/dart-lang/mockito/issues/469)

## 5.0.15

* Fix an issue generating the correct parameter default value given a
Expand Down
9 changes: 2 additions & 7 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ class MockBuilder implements Builder {
'// ignore_for_file: invalid_use_of_visible_for_testing_member\n'));
b.body.add(Code('// ignore_for_file: prefer_const_constructors\n'));
// The code_builder `asA` API unconditionally adds defensive parentheses.
b.body.add(Code('// ignore_for_file: unnecessary_parenthesis\n'));
// The generator appends a suffix to fake classes
b.body.add(Code('// ignore_for_file: camel_case_types\n\n'));
b.body.add(Code('// ignore_for_file: unnecessary_parenthesis\n\n'));
b.body.addAll(mockLibraryInfo.fakeClasses);
b.body.addAll(mockLibraryInfo.mockClasses);
});
Expand Down Expand Up @@ -823,10 +821,7 @@ class _MockClassInfo {
// implements the mock target with said type arguments. For example:
// `class MockFoo extends Mock implements Foo<int> {}`
for (var typeArgument in typeToMock.typeArguments) {
typeArguments.add(referImported(
typeArgument.getDisplayString(
withNullability: sourceLibIsNonNullable),
_typeImport(typeArgument.element)));
typeArguments.add(_typeReference(typeArgument));
}
} else if (classToMock.typeParameters != null) {
// [typeToMock] is a simple reference to a generic type (for example:
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const packageVersion = '5.0.15';
const packageVersion = '5.0.16-dev';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mockito
version: 5.0.15
version: 5.0.16-dev

description: >-
A mock framework inspired by Mockito with APIs for Fakes, Mocks,
Expand Down
21 changes: 21 additions & 0 deletions test/builder/custom_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ void main() {
expect(mocksContent, contains('List<T> get f =>'));
});

test('generates a generic mock class with deep type arguments', () async {
var mocksContent = await buildWithNonNullable({
...annotationsAsset,
'foo|lib/foo.dart': dedent(r'''
class Foo<T> {}
class Bar {}
'''),
'foo|test/foo_test.dart': '''
import 'package:foo/foo.dart';
import 'package:mockito/annotations.dart';
@GenerateMocks(
[], customMocks: [MockSpec<Foo<List<Bar>>>(as: #MockFoo)])
void main() {}
'''
});
expect(
mocksContent,
contains(
'class MockFoo extends _i1.Mock implements _i2.Foo<List<_i2.Bar>>'));
});

test('generates a generic mock class with type arguments', () async {
var mocksContent = await buildWithNonNullable({
...annotationsAsset,
Expand Down

0 comments on commit 584b881

Please sign in to comment.