Skip to content

Commit

Permalink
Include type argument when returning a fallback value for an `Iterabl…
Browse files Browse the repository at this point in the history
…e<T>`.

Before this fix, we would only return `[]` which had an implicit dynamic: `<dynamic>[]`. The proper return value should explicitly include the expected type argument.

Existing test cases cover the desired behavior.

Also replace `var` with `final` in surrounding code.

Fixes #445

PiperOrigin-RevId: 384965296
  • Loading branch information
srawlins committed Jul 18, 2021
1 parent 00f605c commit 819f109
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 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.12-dev

* Use an empty list with a correct type argument for a fallback value for a
method which returns Iterable.
[#445](https://github.com/dart-lang/mockito/issues/445)

## 5.0.11

* Allow two mocks of the same class (with different type arguments) to be
Expand Down
14 changes: 6 additions & 8 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1075,26 +1075,24 @@ class _MockClassInfo {
.call(futureValueArguments);
} else if (type.isDartCoreInt) {
return literalNum(0);
} else if (type.isDartCoreIterable) {
return literalList([]);
} else if (type.isDartCoreList) {
} else if (type.isDartCoreIterable || type.isDartCoreList) {
assert(typeArguments.length == 1);
var elementType = _typeReference(typeArguments[0]);
final elementType = _typeReference(typeArguments[0]);
return literalList([], elementType);
} else if (type.isDartCoreMap) {
assert(typeArguments.length == 2);
var keyType = _typeReference(typeArguments[0]);
var valueType = _typeReference(typeArguments[1]);
final keyType = _typeReference(typeArguments[0]);
final valueType = _typeReference(typeArguments[1]);
return literalMap({}, keyType, valueType);
} else if (type.isDartCoreNum) {
return literalNum(0);
} else if (type.isDartCoreSet) {
assert(typeArguments.length == 1);
var elementType = _typeReference(typeArguments[0]);
final elementType = _typeReference(typeArguments[0]);
return literalSet({}, elementType);
} else if (type.element.declaration == typeProvider.streamElement) {
assert(typeArguments.length == 1);
var elementType = _typeReference(typeArguments[0]);
final elementType = _typeReference(typeArguments[0]);
return TypeReference((b) {
b
..symbol = 'Stream'
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.11';
const packageVersion = '5.0.12-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.11
version: 5.0.12-dev

description: >-
A mock framework inspired by Mockito with APIs for Fakes, Mocks,
Expand Down
11 changes: 6 additions & 5 deletions test/builder/auto_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ void main() {

/// Test [MockBuilder] on a single source file, in a package which has opted
/// into null safety, and with the non-nullable experiment enabled.
Future<void> expectSingleNonNullableOutput(String sourceAssetText,
Future<void> expectSingleNonNullableOutput(
String sourceAssetText,
/*String|Matcher<List<int>>*/ Object output) async {
await testWithNonNullable({
...metaAssets,
Expand Down Expand Up @@ -554,7 +555,7 @@ void main() {
'''),
_containsAllOf(dedent2('''
Iterable<int> m() =>
(super.noSuchMethod(Invocation.method(#m, []), returnValue: [])
(super.noSuchMethod(Invocation.method(#m, []), returnValue: <int>[])
as Iterable<int>);
''')),
);
Expand Down Expand Up @@ -1950,8 +1951,8 @@ void main() {
});

test(
'creates dummy non-null return values for Futures of known generic core classes',
() async {
'creates dummy non-null return values for Futures of known generic core '
'classes', () async {
await expectSingleNonNullableOutput(
dedent(r'''
class Foo {
Expand All @@ -1961,7 +1962,7 @@ void main() {
_containsAllOf(dedent2('''
_i3.Future<Iterable<bool>> m() =>
(super.noSuchMethod(Invocation.method(#m, []),
returnValue: Future<Iterable<bool>>.value([]))
returnValue: Future<Iterable<bool>>.value(<bool>[]))
as _i3.Future<Iterable<bool>>);
''')),
);
Expand Down

0 comments on commit 819f109

Please sign in to comment.