Skip to content

Commit b75ffbd

Browse files
committed
Support returning Future<ExtensionType>
1 parent 32683f5 commit b75ffbd

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

lib/src/builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ class _MockClassInfo {
16661666
_dummyFakedValue(dartType, invocation),
16671667
ExtensionTypeElement(:final typeErasure)
16681668
when !typeErasure.containsPrivateName =>
1669-
_dummyValue(typeErasure, invocation),
1669+
_dummyValue(typeErasure, invocation).asA(_typeReference(dartType)),
16701670
ExtensionTypeElement() =>
16711671
_dummyValueFallbackToRuntime(dartType, invocation),
16721672
_ => throw StateError(

test/builder/auto_mocks_test.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3775,8 +3775,20 @@ void main() {
37753775
E get v;
37763776
}
37773777
'''),
3778-
decodedMatches(
3779-
allOf(contains('E get v'), contains('returnValue: 0'))));
3778+
decodedMatches(allOf(
3779+
contains('E get v'), contains('returnValue: (0 as _i2.E)'))));
3780+
});
3781+
3782+
test('are supported as Future return types', () async {
3783+
await expectSingleNonNullableOutput(
3784+
dedent('''
3785+
extension type E(int v) {}
3786+
class Foo {
3787+
Future<E> get v;
3788+
}
3789+
'''),
3790+
decodedMatches(allOf(contains('_i3.Future<_i2.E> get v'),
3791+
contains('returnValue: _i3.Future<_i2.E>.value((0 as _i2.E))'))));
37803792
});
37813793
});
37823794
group('build_extensions support', () {

test/end2end/foo.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@ class UsesExtTypes {
7474
Ext extTypeReturn(int _) => Ext(42);
7575
bool privateExtTypeArg(ExtOfPrivate _) => true;
7676
ExtOfPrivate privateExtTypeReturn(int _) => ExtOfPrivate(private);
77+
Future<Ext> futureExtTypeReturn(int _) => Future.value(Ext(43));
78+
Future<ExtOfPrivate> futureExtOfPrivateTypeReturn(int _) =>
79+
Future.value(ExtOfPrivate(private));
7780
}

test/end2end/generated_mocks_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ void main() {
365365
expect(usesExtTypes.extTypeReturn(2), equals(Ext(42)));
366366
expect(usesExtTypes.extTypeReturn(42), equals(Ext(0)));
367367
});
368+
369+
test('a method using extension type as a return type can be stubbed',
370+
() async {
371+
when(usesExtTypes.futureExtTypeReturn(2))
372+
.thenAnswer((_) => Future.value(Ext(42)));
373+
expect(await usesExtTypes.futureExtTypeReturn(2), equals(Ext(42)));
374+
expect(await usesExtTypes.futureExtTypeReturn(42), equals(Ext(0)));
375+
expect(await usesExtTypes.futureExtTypeReturn(42), equals(0));
376+
});
368377
});
369378
});
370379

0 commit comments

Comments
 (0)