Skip to content

Commit

Permalink
Do not use private type alias names in generated code.
Browse files Browse the repository at this point in the history
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 #396.

PiperOrigin-RevId: 370949384
  • Loading branch information
srawlins committed Apr 28, 2021
1 parent c06ec0b commit e620734
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
7 changes: 4 additions & 3 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1259,9 +1259,10 @@ class _MockLibraryInfo {
..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 =
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.6';
const packageVersion = '5.0.7';
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.6
version: 5.0.7

description: A mock framework inspired by Mockito.
homepage: https://github.com/dart-lang/mockito
Expand Down
67 changes: 63 additions & 4 deletions test/builder/auto_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,22 @@ void main() {
);
});

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(
Expand Down Expand Up @@ -1959,8 +1975,8 @@ void main() {
});

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';
Expand All @@ -1976,8 +1992,8 @@ void main() {
});

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';
Expand Down Expand Up @@ -2121,6 +2137,49 @@ void main() {
);
});

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 {
Expand Down

0 comments on commit e620734

Please sign in to comment.