Do not use private type alias names in generated code.
We prefer generally to use type alias names in order to match the user's code; extracting out the type signature may surprise users. However, given a private type alias, we can recover the situation by extracting out the type signature.
Fixes https://github.com/dart-lang/mockito/issues/396.
PiperOrigin-RevId: 370949384
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4339bda..57fb6dc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,12 @@
+## 5.0.7
+
+* Properly refer to type parameter bounds with import prefixes.
+ [#389](https://github.com/dart-lang/mockito/issues/389)
+* Stop referring to private typedefs in generated code.
+ [#396](https://github.com/dart-lang/mockito/issues/396)
+* Ignore `prefer_const_constructors` and `avoid_redundant_argument_values` lint
+ rule violations in generated code.
+
## 5.0.6
* Support the 0.4.x releases of `test_api`.
diff --git a/lib/src/builder.dart b/lib/src/builder.dart
index c3f6afc..bc4480d 100644
--- a/lib/src/builder.dart
+++ b/lib/src/builder.dart
@@ -1259,9 +1259,10 @@
..types.addAll(type.typeArguments.map(_typeReference));
});
} else if (type is analyzer.FunctionType) {
- var element = type.aliasElement;
- if (element == null) {
- // [type] represents a FunctionTypedFormalParameter.
+ final element = type.aliasElement;
+ if (element == null || element.isPrivate) {
+ // [type] does not refer to a type alias, or it refers to a private type
+ // alias; we must instead write out its signature.
return FunctionType((b) {
b
..isNullable =
diff --git a/lib/src/version.dart b/lib/src/version.dart
index 9938457..5fd5c67 100644
--- a/lib/src/version.dart
+++ b/lib/src/version.dart
@@ -1 +1 @@
-const packageVersion = '5.0.6';
+const packageVersion = '5.0.7';
diff --git a/pubspec.yaml b/pubspec.yaml
index 52ec5c2..607cf6a 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: mockito
-version: 5.0.6
+version: 5.0.7
description: A mock framework inspired by Mockito.
homepage: https://github.com/dart-lang/mockito
diff --git a/test/builder/auto_mocks_test.dart b/test/builder/auto_mocks_test.dart
index 2daa076..8ca7e70 100644
--- a/test/builder/auto_mocks_test.dart
+++ b/test/builder/auto_mocks_test.dart
@@ -1925,6 +1925,22 @@
);
});
+ test(
+ 'creates a dummy non-null function-typed return value, with private type '
+ 'alias', () async {
+ await expectSingleNonNullableOutput(
+ dedent(r'''
+ typedef _Callback = Foo Function();
+ class Foo {
+ _Callback m() => () => Foo();
+ }
+ '''),
+ _containsAllOf(
+ '_i2.Foo Function() m() => (super.noSuchMethod(Invocation.method(#m, []),\n'
+ ' returnValue: () => _FakeFoo()) as _i2.Foo Function());'),
+ );
+ });
+
test('creates a dummy non-null generic function-typed return value',
() async {
await expectSingleNonNullableOutput(
@@ -1959,8 +1975,8 @@
});
test(
- 'creates a dummy non-null function-typed (with an imported parameter type) return value',
- () async {
+ 'creates a dummy non-null function-typed (with an imported parameter '
+ 'type) return value', () async {
await expectSingleNonNullableOutput(
dedent(r'''
import 'dart:io';
@@ -1976,8 +1992,8 @@
});
test(
- 'creates a dummy non-null function-typed (with an imported return type) return value',
- () async {
+ 'creates a dummy non-null function-typed (with an imported return type) '
+ 'return value', () async {
await expectSingleNonNullableOutput(
dedent(r'''
import 'dart:io';
@@ -2122,6 +2138,49 @@
});
test(
+ 'throws when GenerateMocks is given a class with a method with a '
+ 'type alias return type which refers to private types', () async {
+ _expectBuilderThrows(
+ assets: {
+ ...annotationsAsset,
+ ...simpleTestAsset,
+ 'foo|lib/foo.dart': dedent('''
+ abstract class Foo {
+ Callback m(int a);
+ }
+ class _Bar {}
+ typedef Callback = Function(_Bar?);
+ '''),
+ },
+ message: contains(
+ "The method 'Foo.m' features a private parameter type, '_Bar', and "
+ 'cannot be stubbed.'),
+ );
+ });
+
+ test(
+ 'throws when GenerateMocks is given a class with a method with a '
+ 'private type alias parameter type which refers to private types',
+ () async {
+ _expectBuilderThrows(
+ assets: {
+ ...annotationsAsset,
+ ...simpleTestAsset,
+ 'foo|lib/foo.dart': dedent('''
+ abstract class Foo {
+ void m(_Callback c);
+ }
+ class _Bar {}
+ typedef _Callback = Function(_Bar?);
+ '''),
+ },
+ message: contains(
+ "The method 'Foo.m' features a private parameter type, '_Bar', and "
+ 'cannot be stubbed.'),
+ );
+ });
+
+ test(
'throws when GenerateMocks is given a class with a method with a return '
'type with private type arguments', () async {
_expectBuilderThrows(