Skip to content

Support returning Future<ExtensionType> #802

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/src/builder.dart
Original file line number Diff line number Diff line change
@@ -1866,7 +1866,7 @@ class _MockClassInfo {
_dummyFakedValue(dartType, invocation),
ExtensionTypeElement2(:final typeErasure)
when !typeErasure.containsPrivateName =>
_dummyValue(typeErasure, invocation),
_dummyValue(typeErasure, invocation).asA(_typeReference(dartType)),
ExtensionTypeElement2() => _dummyValueFallbackToRuntime(
dartType,
invocation,
18 changes: 15 additions & 3 deletions test/builder/auto_mocks_test.dart
Original file line number Diff line number Diff line change
@@ -4180,14 +4180,26 @@ void main() {

test('are supported as return types', () async {
await expectSingleNonNullableOutput(
dedent('''
dedent('''
extension type E(int v) {}
class Foo {
E get v;
}
'''),
decodedMatches(allOf(contains('E get v'), contains('returnValue: 0'))),
);
decodedMatches(allOf(
contains('E get v'), contains('returnValue: (0 as _i2.E)'))));
});

test('are supported as Future return types', () async {
await expectSingleNonNullableOutput(
dedent('''
extension type E(int v) {}
class Foo {
Future<E> get v;
}
'''),
decodedMatches(allOf(contains('_i3.Future<_i2.E> get v'),
contains('returnValue: _i3.Future<_i2.E>.value((0 as _i2.E))'))));
});
});
group('build_extensions support', () {
3 changes: 3 additions & 0 deletions test/end2end/foo.dart
Original file line number Diff line number Diff line change
@@ -74,4 +74,7 @@ class UsesExtTypes {
Ext extTypeReturn(int _) => Ext(42);
bool privateExtTypeArg(ExtOfPrivate _) => true;
ExtOfPrivate privateExtTypeReturn(int _) => ExtOfPrivate(private);
Future<Ext> futureExtTypeReturn(int _) => Future.value(Ext(43));
Future<ExtOfPrivate> futureExtOfPrivateTypeReturn(int _) =>
Future.value(ExtOfPrivate(private));
}
9 changes: 9 additions & 0 deletions test/end2end/generated_mocks_test.dart
Original file line number Diff line number Diff line change
@@ -365,6 +365,15 @@ void main() {
expect(usesExtTypes.extTypeReturn(2), equals(Ext(42)));
expect(usesExtTypes.extTypeReturn(42), equals(Ext(0)));
});

test('a method using extension type as a return type can be stubbed',
() async {
when(usesExtTypes.futureExtTypeReturn(2))
.thenAnswer((_) => Future.value(Ext(42)));
expect(await usesExtTypes.futureExtTypeReturn(2), equals(Ext(42)));
expect(await usesExtTypes.futureExtTypeReturn(42), equals(Ext(0)));
expect(await usesExtTypes.futureExtTypeReturn(42), equals(0));
});
});
});