Skip to content

Fix code generation error on nested generic bounded type arguments #659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.4.3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 5.4.3-wip


* Fixed the bug with code generation on nested generic bounded type arguments. [#658](https://github.com/dart-lang/mockito/issues/658)

## 5.4.2

* Require code_builder 4.5.0.
Expand Down
15 changes: 14 additions & 1 deletion lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2051,7 +2051,20 @@ class _MockClassInfo {

T _withTypeParameters<T>(Iterable<TypeParameterElement> typeParameters,
T Function(Iterable<TypeReference>, Iterable<TypeReference>) body) {
final typeVars = {for (final t in typeParameters) t: _newTypeVar(t)};
final Map<TypeParameterElement, String> typeVars = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I wrote on the bug, I think it would be more logical to fix the equality on the analyzer side, if possible. Then, this won't be needed.

But even as a workaround, it has a few problems:

  1. It won't catch cases with more than one level of wrapping, like T extends Foo<Bar<T>>.
  2. It will create a wrong mapping for other type variables appearing in the bound, like
    class C<T> {
      X m<X extends Iterable<T>>() => [];
    }

for (final t in typeParameters) {
final typeVar = _newTypeVar(t);
typeVars[t] = typeVar;
final bound = t.bound;
if (bound != null && bound is analyzer.InterfaceType) {
for (final type in bound.typeArguments) {
final element = type.element;
if (element != null && element is TypeParameterElement) {
typeVars[element] = typeVar;
}
}
}
}
_typeVariableScopes.add(typeVars);
final typeRefsWithBounds = typeParameters.map(_typeParameterReference);
final typeRefs =
Expand Down
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.4.2
version: 5.4.3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-wip

description: >-
A mock framework inspired by Mockito with APIs for Fakes, Mocks,
behavior verification, and stubbing.
Expand Down
26 changes: 26 additions & 0 deletions test/builder/custom_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,32 @@ void main() {
expect(mocksContent, contains('Iterable<X1> m1<X1>(X1 Function(X)? f)'));
expect(mocksContent, contains('Iterable<X1?> m2<X1>(X1 Function(X)? f)'));
});

test('We preserve nested generic bounded type arguments', () async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the test! Could you please try adding test cases to cover the items in my comment above? Thanks!

final mocksContent = await buildWithNonNullable({
...annotationsAsset,
'foo|lib/foo.dart': dedent(r'''
class Foo<T> {}
abstract class Bar<T> {
Iterable<X> m1<X extends Foo<X>>(X Function(T) f);
}
abstract class FooBar<X> extends Bar<X> {}
'''),
'foo|test/foo_test.dart': '''
import 'package:foo/foo.dart';
import 'package:mockito/annotations.dart';

@GenerateNiceMocks([
MockSpec<Foo>(), MockSpec<Bar>(), MockSpec<FooBar>()
])
void main() {}
'''
});
expect(mocksContent, contains('class MockBar<T>'));
expect(mocksContent, contains('class MockFooBar<X>'));
expect(mocksContent,
contains('Iterable<X1> m1<X1 extends _i2.Foo<X1>>(X1 Function(X)? f)'));
});
}

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