@@ -124,8 +124,7 @@ class MockBuilder implements Builder {
124124 // The source lib may be pre-null-safety because of an explicit opt-out
125125 // (`// @dart=2.9`), as opposed to living in a pre-null-safety package. To
126126 // allow for this situation, we must also add an opt-out comment here.
127- final dartVersionComment =
128- sourceLibIsNonNullable ? '// @dart=2.19' : '// @dart=2.9' ;
127+ final dartVersionComment = sourceLibIsNonNullable ? '' : '// @dart=2.9' ;
129128 final mockLibraryContent = DartFormatter ().format ('''
130129// Mocks generated by Mockito $packageVersion from annotations
131130// in ${entryLib .definingCompilationUnit .source .uri .path }.
@@ -1444,6 +1443,11 @@ class _MockClassInfo {
14441443 ]);
14451444 }
14461445
1446+ Expression _dummyValueFallbackToRuntime (
1447+ analyzer.DartType type, Expression invocation) =>
1448+ referImported ('dummyValue' , 'package:mockito/src/dummies.dart' )
1449+ .call ([refer ('this' ), invocation], {}, [_typeReference (type)]);
1450+
14471451 Expression _dummyValue (analyzer.DartType type, Expression invocation) {
14481452 // The type is nullable, just take a shortcut and return `null`.
14491453 if (typeSystem.isNullable (type)) {
@@ -1460,8 +1464,7 @@ class _MockClassInfo {
14601464 return literalNull;
14611465 }
14621466 // As a last resort, try looking for the correct value at run-time.
1463- return referImported ('dummyValue' , 'package:mockito/src/dummies.dart' )
1464- .call ([refer ('this' ), invocation], {}, [_typeReference (type)]);
1467+ return _dummyValueFallbackToRuntime (type, invocation);
14651468 }
14661469
14671470 final typeArguments = type.typeArguments;
@@ -1599,24 +1602,47 @@ class _MockClassInfo {
15991602 }).genericClosure;
16001603 }
16011604
1605+ Expression _dummyFakedValue (
1606+ analyzer.InterfaceType dartType, Expression invocation) {
1607+ final elementToFake = dartType.element;
1608+ final fakeName = mockLibraryInfo._fakeNameFor (elementToFake);
1609+ // Only make one fake class for each class that needs to be faked.
1610+ if (! mockLibraryInfo.fakedInterfaceElements.contains (elementToFake)) {
1611+ _addFakeClass (fakeName, elementToFake);
1612+ }
1613+ final typeArguments = dartType.typeArguments;
1614+ return TypeReference ((b) {
1615+ b
1616+ ..symbol = fakeName
1617+ ..types.addAll (typeArguments.map (_typeReference));
1618+ }).newInstance ([refer ('this' ), invocation]);
1619+ }
1620+
16021621 Expression _dummyValueImplementing (
16031622 analyzer.InterfaceType dartType, Expression invocation) {
16041623 final elementToFake = dartType.element;
16051624 if (elementToFake is EnumElement ) {
16061625 return _typeReference (dartType).property (
16071626 elementToFake.fields.firstWhere ((f) => f.isEnumConstant).name);
1627+ } else if (elementToFake is ClassElement ) {
1628+ if (elementToFake.isBase ||
1629+ elementToFake.isFinal ||
1630+ elementToFake.isSealed) {
1631+ // This class can't be faked, so try to call `dummyValue` to get
1632+ // a dummy value at run time.
1633+ // TODO(yanok): Consider checking subtypes, maybe some of them are
1634+ // implementable.
1635+ return _dummyValueFallbackToRuntime (dartType, invocation);
1636+ }
1637+ return _dummyFakedValue (dartType, invocation);
1638+ } else if (elementToFake is MixinElement ) {
1639+ // This is a mixin and not a class. This should not happen in Dart 3,
1640+ // since it is not possible to have a value of mixin type. But we
1641+ // have to support this for reverse comptatibility.
1642+ return _dummyFakedValue (dartType, invocation);
16081643 } else {
1609- final fakeName = mockLibraryInfo._fakeNameFor (elementToFake);
1610- // Only make one fake class for each class that needs to be faked.
1611- if (! mockLibraryInfo.fakedInterfaceElements.contains (elementToFake)) {
1612- _addFakeClass (fakeName, elementToFake);
1613- }
1614- final typeArguments = dartType.typeArguments;
1615- return TypeReference ((b) {
1616- b
1617- ..symbol = fakeName
1618- ..types.addAll (typeArguments.map (_typeReference));
1619- }).newInstance ([refer ('this' ), invocation]);
1644+ throw StateError ("Interface type '$dartType ' which is nether an enum, "
1645+ 'nor a class, nor a mixin. This case is unknown, please report a bug.' );
16201646 }
16211647 }
16221648
0 commit comments