refactor(generator): migrate cherrypick_generator to analyzer element2 API

- Fully migrated core cherrypick_generator and submodules to new analyzer element2 system:
  - Updated all GeneratorForAnnotation overrides to use Element2, ClassElement2, MethodElement2, FieldElement2 and new annotation/metadata access patterns.
  - Migrated signature and bodies for helpers, parsers, annotation validators, meta utils, and type parsers.
  - Fixed tests to use readerWriter instead of deprecated reader argument.
  - Refactored usage of now-absent 'metadata', 'parameters', 'fields', 'methods', 'source', and similar members to use correct *.firstFragment.* or API alternatives.
  - Cleaned up old imports and unused code.

test(generator): update generator integration tests

- Updated test calls to use correct TestReaderWriter type and bring test infra in line with current build_runner/testing API.

build: update dependencies and pubspec to support latest analyzer/build ecosystem

- Raised Dart SDK and package constraints as required for generated code and codegen plugins.
- Updated pubspecs in root/examples as needed by build warnings.

docs: add plots and assets (new files)

BREAKING CHANGE:
- Requires Dart 3.8+ and analyzer that supports element2.
- All downstream codegen/tests depending on Element API must migrate to Element2 signatures and data model.
This commit is contained in:
Sergey Penkovsky
2025-09-09 17:27:20 +03:00
parent c483d8c9e2
commit eb6d786600
21 changed files with 486 additions and 379 deletions

View File

@@ -11,13 +11,12 @@
// limitations under the License.
//
import 'dart:async';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
/// CherryPick DI field injector generator for codegen.
@@ -100,12 +99,12 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
/// }
/// ```
@override
FutureOr<String> generateForAnnotatedElement(
Element element,
dynamic generateForAnnotatedElement(
Element2 element,
ConstantReader annotation,
BuildStep buildStep,
) {
if (element is! ClassElement) {
if (element is! ClassElement2) {
throw InvalidGenerationSourceError(
'@injectable() can only be applied to classes.',
element: element,
@@ -113,7 +112,7 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
}
final classElement = element;
final className = classElement.name;
final className = classElement.firstFragment.name2;
final mixinName = '_\$$className';
final buffer = StringBuffer()
@@ -121,8 +120,9 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
..writeln(' void _inject($className instance) {');
// Collect and process all @inject fields
final injectFields =
classElement.fields.where(_isInjectField).map(_parseInjectField);
final injectFields = classElement.fields2
.where((f) => _isInjectField(f))
.map((f) => _parseInjectField(f));
for (final parsedField in injectFields) {
buffer.writeln(_generateInjectionLine(parsedField));
@@ -138,8 +138,8 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
/// Returns true if a field is annotated with `@inject`.
///
/// Used to detect which fields should be processed for injection.
static bool _isInjectField(FieldElement field) {
return field.metadata.any(
static bool _isInjectField(FieldElement2 field) {
return field.firstFragment.metadata2.annotations.any(
(m) => m.computeConstantValue()?.type?.getDisplayString() == 'inject',
);
}
@@ -149,11 +149,11 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
///
/// Converts Dart field declaration and all parameterizing injection-related
/// annotations into a [_ParsedInjectField] which is used for codegen.
static _ParsedInjectField _parseInjectField(FieldElement field) {
static _ParsedInjectField _parseInjectField(FieldElement2 field) {
String? scopeName;
String? namedValue;
for (final meta in field.metadata) {
for (final meta in field.firstFragment.metadata2.annotations) {
final DartObject? obj = meta.computeConstantValue();
final type = obj?.type?.getDisplayString();
if (type == 'scope') {
@@ -177,15 +177,15 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
}
// Determine nullability for field types like T? or Future<T?>
bool isNullable = dartType.nullabilitySuffix ==
NullabilitySuffix.question ||
bool isNullable =
dartType.nullabilitySuffix == NullabilitySuffix.question ||
(dartType is ParameterizedType &&
(dartType)
.typeArguments
.any((t) => t.nullabilitySuffix == NullabilitySuffix.question));
(dartType).typeArguments.any(
(t) => t.nullabilitySuffix == NullabilitySuffix.question,
));
return _ParsedInjectField(
fieldName: field.name,
fieldName: field.firstFragment.name2 ?? '',
coreType: coreTypeName.replaceAll('?', ''), // удаляем "?" на всякий
isFuture: isFuture,
isNullable: isNullable,
@@ -207,11 +207,11 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
String _generateInjectionLine(_ParsedInjectField field) {
final resolveMethod = field.isFuture
? (field.isNullable
? 'tryResolveAsync<${field.coreType}>'
: 'resolveAsync<${field.coreType}>')
? 'tryResolveAsync<${field.coreType}>'
: 'resolveAsync<${field.coreType}>')
: (field.isNullable
? 'tryResolve<${field.coreType}>'
: 'resolve<${field.coreType}>');
? 'tryResolve<${field.coreType}>'
: 'resolve<${field.coreType}>');
final openCall = (field.scopeName != null && field.scopeName!.isNotEmpty)
? "CherryPick.openScope(scopeName: '${field.scopeName}')"

View File

@@ -11,7 +11,7 @@
// limitations under the License.
//
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
@@ -79,12 +79,12 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
///
/// See file-level docs for usage and generated output example.
@override
String generateForAnnotatedElement(
Element element,
dynamic generateForAnnotatedElement(
Element2 element,
ConstantReader annotation,
BuildStep buildStep,
) {
if (element is! ClassElement) {
if (element is! ClassElement2) {
throw InvalidGenerationSourceError(
'@module() can only be applied to classes.',
element: element,

View File

@@ -12,6 +12,7 @@
//
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'exceptions.dart';
import 'metadata_utils.dart';
@@ -52,8 +53,10 @@ class AnnotationValidator {
/// - Parameter validation for method arguments.
///
/// Throws [AnnotationValidationException] on any violation.
static void validateMethodAnnotations(MethodElement method) {
final annotations = _getAnnotationNames(method.metadata);
static void validateMethodAnnotations(MethodElement2 method) {
final annotations = _getAnnotationNames(
method.firstFragment.metadata2.annotations,
);
_validateMutuallyExclusiveAnnotations(method, annotations);
_validateAnnotationCombinations(method, annotations);
@@ -68,8 +71,10 @@ class AnnotationValidator {
/// - Correct scope naming if present.
///
/// Throws [AnnotationValidationException] if checks fail.
static void validateFieldAnnotations(FieldElement field) {
final annotations = _getAnnotationNames(field.metadata);
static void validateFieldAnnotations(FieldElement2 field) {
final annotations = _getAnnotationNames(
field.firstFragment.metadata2.annotations,
);
_validateInjectFieldAnnotations(field, annotations);
}
@@ -82,8 +87,10 @@ class AnnotationValidator {
/// - Provides helpful context for error/warning reporting.
///
/// Throws [AnnotationValidationException] if checks fail.
static void validateClassAnnotations(ClassElement classElement) {
final annotations = _getAnnotationNames(classElement.metadata);
static void validateClassAnnotations(ClassElement2 classElement) {
final annotations = _getAnnotationNames(
classElement.firstFragment.metadata2.annotations,
);
_validateModuleClassAnnotations(classElement, annotations);
_validateInjectableClassAnnotations(classElement, annotations);
@@ -104,7 +111,7 @@ class AnnotationValidator {
///
/// For example, `@instance` and `@provide` cannot both be present.
static void _validateMutuallyExclusiveAnnotations(
MethodElement method,
MethodElement2 method,
List<String> annotations,
) {
// @instance and @provide are mutually exclusive
@@ -127,7 +134,7 @@ class AnnotationValidator {
/// - One of `@instance` or `@provide` must be present for a registration method
/// - Validates singleton usage
static void _validateAnnotationCombinations(
MethodElement method,
MethodElement2 method,
List<String> annotations,
) {
// @params can only be used with @provide
@@ -165,7 +172,7 @@ class AnnotationValidator {
/// Singleton-specific method annotation checks.
static void _validateSingletonUsage(
MethodElement method,
MethodElement2 method,
List<String> annotations,
) {
// Singleton with params might not make sense in some contexts
@@ -181,18 +188,17 @@ class AnnotationValidator {
'Singleton methods cannot return void',
element: method,
suggestion: 'Remove @singleton annotation or change return type',
context: {
'method_name': method.displayName,
'return_type': returnType,
},
context: {'method_name': method.displayName, 'return_type': returnType},
);
}
}
/// Validates extra requirements or syntactic rules for annotation arguments, like @named.
static void _validateAnnotationParameters(MethodElement method) {
static void _validateAnnotationParameters(MethodElement2 method) {
// Validate @named annotation parameters
final namedValue = MetadataUtils.getNamedValue(method.metadata);
final namedValue = MetadataUtils.getNamedValue(
method.firstFragment.metadata2.annotations,
);
if (namedValue != null) {
if (namedValue.isEmpty) {
throw AnnotationValidationException(
@@ -222,8 +228,10 @@ class AnnotationValidator {
}
// Validate method parameters for @params usage
for (final param in method.parameters) {
final paramAnnotations = _getAnnotationNames(param.metadata);
for (final param in method.formalParameters) {
final paramAnnotations = _getAnnotationNames(
param.firstFragment.metadata2.annotations,
);
if (paramAnnotations.contains('params')) {
_validateParamsParameter(param, method);
}
@@ -232,7 +240,9 @@ class AnnotationValidator {
/// Checks that @params is used with compatible parameter type.
static void _validateParamsParameter(
ParameterElement param, MethodElement method) {
FormalParameterElement param,
MethodElement2 method,
) {
// @params parameter should typically be dynamic or Map<String, dynamic>
final paramType = param.type.getDisplayString();
if (paramType != 'dynamic' &&
@@ -256,7 +266,7 @@ class AnnotationValidator {
/// Checks field-level annotation for valid injectable fields.
static void _validateInjectFieldAnnotations(
FieldElement field,
FieldElement2 field,
List<String> annotations,
) {
if (!annotations.contains('inject')) {
@@ -270,15 +280,12 @@ class AnnotationValidator {
'Cannot inject void type',
element: field,
suggestion: 'Use a concrete type instead of void',
context: {
'field_name': field.displayName,
'field_type': fieldType,
},
context: {'field_name': field.displayName, 'field_type': fieldType},
);
}
// Validate scope annotation if present
for (final meta in field.metadata) {
for (final meta in field.firstFragment.metadata2.annotations) {
final obj = meta.computeConstantValue();
final type = obj?.type?.getDisplayString();
if (type == 'scope') {
@@ -290,7 +297,7 @@ class AnnotationValidator {
/// Checks @module usage: must have at least one DI method, each with DI-annotation.
static void _validateModuleClassAnnotations(
ClassElement classElement,
ClassElement2 classElement,
List<String> annotations,
) {
if (!annotations.contains('module')) {
@@ -298,8 +305,9 @@ class AnnotationValidator {
}
// Check if class has public methods
final publicMethods =
classElement.methods.where((m) => m.isPublic).toList();
final publicMethods = classElement.methods2
.where((m) => m.isPublic)
.toList();
if (publicMethods.isEmpty) {
throw AnnotationValidationException(
'Module class must have at least one public method',
@@ -314,7 +322,9 @@ class AnnotationValidator {
// Validate that public methods have appropriate annotations
for (final method in publicMethods) {
final methodAnnotations = _getAnnotationNames(method.metadata);
final methodAnnotations = _getAnnotationNames(
method.firstFragment.metadata2.annotations,
);
if (!methodAnnotations.contains('instance') &&
!methodAnnotations.contains('provide')) {
throw AnnotationValidationException(
@@ -332,7 +342,7 @@ class AnnotationValidator {
/// Checks @injectable usage on classes and their fields.
static void _validateInjectableClassAnnotations(
ClassElement classElement,
ClassElement2 classElement,
List<String> annotations,
) {
if (!annotations.contains('injectable')) {
@@ -340,8 +350,10 @@ class AnnotationValidator {
}
// Check if class has injectable fields
final injectFields = classElement.fields.where((f) {
final fieldAnnotations = _getAnnotationNames(f.metadata);
final injectFields = classElement.fields2.where((f) {
final fieldAnnotations = _getAnnotationNames(
f.firstFragment.metadata2.annotations,
);
return fieldAnnotations.contains('inject');
}).toList();

View File

@@ -11,7 +11,7 @@
// limitations under the License.
//
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'bind_parameters_spec.dart';
import 'metadata_utils.dart';
@@ -25,7 +25,7 @@ enum BindingType {
instance,
/// Provider/factory function (@provide).
provide;
provide,
}
/// ---------------------------------------------------------------------------
@@ -155,7 +155,8 @@ class BindSpec {
switch (bindingType) {
case BindingType.instance:
throw StateError(
'Internal error: _generateWithParamsProvideClause called for @instance binding with @params.');
'Internal error: _generateWithParamsProvideClause called for @instance binding with @params.',
);
//return isAsyncInstance
// ? '.toInstanceAsync(($fnArgs) => $methodName($fnArgs))'
// : '.toInstance(($fnArgs) => $methodName($fnArgs))';
@@ -189,20 +190,24 @@ class BindSpec {
case BindingType.provide:
if (isAsyncProvide) {
if (needsMultiline) {
final lambdaIndent =
(isSingleton || named != null) ? indent + 6 : indent + 2;
final closingIndent =
(isSingleton || named != null) ? indent + 4 : indent;
final lambdaIndent = (isSingleton || named != null)
? indent + 6
: indent + 2;
final closingIndent = (isSingleton || named != null)
? indent + 4
: indent;
return '.toProvideAsync(\n${' ' * lambdaIndent}() => $methodName($argsStr),\n${' ' * closingIndent})';
} else {
return '.toProvideAsync(() => $methodName($argsStr))';
}
} else {
if (needsMultiline) {
final lambdaIndent =
(isSingleton || named != null) ? indent + 6 : indent + 2;
final closingIndent =
(isSingleton || named != null) ? indent + 4 : indent;
final lambdaIndent = (isSingleton || named != null)
? indent + 6
: indent + 2;
final closingIndent = (isSingleton || named != null)
? indent + 4
: indent;
return '.toProvide(\n${' ' * lambdaIndent}() => $methodName($argsStr),\n${' ' * closingIndent})';
} else {
return '.toProvide(() => $methodName($argsStr))';
@@ -246,7 +251,7 @@ class BindSpec {
/// print(bindSpec.returnType); // e.g., 'Logger'
/// ```
/// Throws [AnnotationValidationException] or [CodeGenerationException] if invalid.
static BindSpec fromMethod(MethodElement method) {
static BindSpec fromMethod(MethodElement2 method) {
try {
// Validate method annotations
AnnotationValidator.validateMethodAnnotations(method);
@@ -254,28 +259,44 @@ class BindSpec {
// Parse return type using improved type parser
final parsedReturnType = TypeParser.parseType(method.returnType, method);
final methodName = method.displayName;
final methodName = method.firstFragment.name2 ?? '';
// Check for @singleton annotation.
final isSingleton = MetadataUtils.anyMeta(method.metadata, 'singleton');
final isSingleton = MetadataUtils.anyMeta(
method.firstFragment.metadata2.annotations,
'singleton',
);
// Get @named value if present.
final named = MetadataUtils.getNamedValue(method.metadata);
final named = MetadataUtils.getNamedValue(
method.firstFragment.metadata2.annotations,
);
// Parse each method parameter.
final params = <BindParameterSpec>[];
bool hasParams = false;
for (final p in method.parameters) {
for (final p in method.formalParameters) {
final typeStr = p.type.getDisplayString();
final paramNamed = MetadataUtils.getNamedValue(p.metadata);
final isParams = MetadataUtils.anyMeta(p.metadata, 'params');
final paramNamed = MetadataUtils.getNamedValue(
p.firstFragment.metadata2.annotations,
);
final isParams = MetadataUtils.anyMeta(
p.firstFragment.metadata2.annotations,
'params',
);
if (isParams) hasParams = true;
params.add(BindParameterSpec(typeStr, paramNamed, isParams: isParams));
}
// Determine bindingType: @instance or @provide.
final hasInstance = MetadataUtils.anyMeta(method.metadata, 'instance');
final hasProvide = MetadataUtils.anyMeta(method.metadata, 'provide');
final hasInstance = MetadataUtils.anyMeta(
method.firstFragment.metadata2.annotations,
'instance',
);
final hasProvide = MetadataUtils.anyMeta(
method.firstFragment.metadata2.annotations,
'provide',
);
if (!hasInstance && !hasProvide) {
throw AnnotationValidationException(
@@ -290,8 +311,9 @@ class BindSpec {
);
}
final bindingType =
hasInstance ? BindingType.instance : BindingType.provide;
final bindingType = hasInstance
? BindingType.instance
: BindingType.provide;
// PROHIBIT @params with @instance bindings!
if (bindingType == BindingType.instance && hasParams) {

View File

@@ -11,7 +11,7 @@
// limitations under the License.
//
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:source_gen/source_gen.dart';
/// ---------------------------------------------------------------------------
@@ -48,21 +48,21 @@ class CherryPickGeneratorException extends InvalidGenerationSourceError {
CherryPickGeneratorException(
String message, {
required Element element,
required Element2 element,
required this.category,
this.suggestion,
this.context,
}) : super(
_formatMessage(message, category, suggestion, context, element),
element: element,
);
_formatMessage(message, category, suggestion, context, element),
element: element,
);
static String _formatMessage(
String message,
String category,
String? suggestion,
Map<String, dynamic>? context,
Element element,
Element2 element,
) {
final buffer = StringBuffer();
@@ -74,7 +74,9 @@ class CherryPickGeneratorException extends InvalidGenerationSourceError {
buffer.writeln('Context:');
buffer.writeln(' Element: ${element.displayName}');
buffer.writeln(' Type: ${element.runtimeType}');
buffer.writeln(' Location: ${element.source?.fullName ?? 'unknown'}');
buffer.writeln(
' Location: ${element.firstFragment.libraryFragment?.source.fullName ?? 'unknown'}',
);
// Try to show enclosing element info for extra context
try {

View File

@@ -12,6 +12,7 @@
//
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'bind_spec.dart';
/// ---------------------------------------------------------------------------
@@ -75,14 +76,11 @@ class GeneratedClass {
/// final gen = GeneratedClass.fromClassElement(classElement);
/// print(gen.generatedClassName); // e.g. $AppModule
/// ```
static GeneratedClass fromClassElement(ClassElement element) {
final className = element.displayName;
// Generated class name with '$' prefix (standard for generated Dart code).
static GeneratedClass fromClassElement(ClassElement2 element) {
final className = element.firstFragment.name2 ?? '';
final generatedClassName = r'$' + className;
// Get source file name
final sourceFile = element.source.shortName;
// Collect bindings for all non-abstract methods.
final binds = element.methods
final sourceFile = element.firstFragment.libraryFragment.source.shortName;
final binds = element.methods2
.where((m) => !m.isAbstract)
.map(BindSpec.fromMethod)
.toList();

View File

@@ -41,14 +41,16 @@ class MetadataUtils {
/// bool isSingleton = MetadataUtils.anyMeta(myMethod.metadata, 'singleton');
/// ```
static bool anyMeta(List<ElementAnnotation> meta, String typeName) {
return meta.any((m) =>
m
.computeConstantValue()
?.type
?.getDisplayString()
.toLowerCase()
.contains(typeName.toLowerCase()) ??
false);
return meta.any(
(m) =>
m
.computeConstantValue()
?.type
?.getDisplayString()
.toLowerCase()
.contains(typeName.toLowerCase()) ??
false,
);
}
/// Extracts the string value from a `@named('value')` annotation if present in [meta].

View File

@@ -11,7 +11,7 @@
// limitations under the License.
//
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'exceptions.dart';
@@ -45,7 +45,7 @@ class TypeParser {
/// final parsed = TypeParser.parseType(field.type, field);
/// if (parsed.isNullable) print('Field is nullable');
/// ```
static ParsedType parseType(DartType dartType, Element context) {
static ParsedType parseType(DartType dartType, Element2 context) {
try {
return _parseTypeInternal(dartType, context);
} catch (e) {
@@ -61,7 +61,7 @@ class TypeParser {
}
}
static ParsedType _parseTypeInternal(DartType dartType, Element context) {
static ParsedType _parseTypeInternal(DartType dartType, Element2 context) {
final displayString = dartType.getDisplayString();
final isNullable = dartType.nullabilitySuffix == NullabilitySuffix.question;
@@ -87,7 +87,10 @@ class TypeParser {
}
static ParsedType _parseFutureType(
DartType dartType, Element context, bool isNullable) {
DartType dartType,
Element2 context,
bool isNullable,
) {
if (dartType is! ParameterizedType || dartType.typeArguments.isEmpty) {
throw TypeParsingException(
'Future type must have a type argument',
@@ -112,7 +115,10 @@ class TypeParser {
}
static ParsedType _parseGenericType(
ParameterizedType dartType, Element context, bool isNullable) {
ParameterizedType dartType,
Element2 context,
bool isNullable,
) {
final typeArguments = dartType.typeArguments
.map((arg) => _parseTypeInternal(arg, context))
.toList();
@@ -138,7 +144,7 @@ class TypeParser {
/// final parsed = TypeParser.parseType(field.type, field);
/// TypeParser.validateInjectableType(parsed, field);
/// ```
static void validateInjectableType(ParsedType parsedType, Element context) {
static void validateInjectableType(ParsedType parsedType, Element2 context) {
// Check for void type
if (parsedType.coreType == 'void') {
throw TypeParsingException(

View File

@@ -15,20 +15,20 @@ topics:
- inversion-of-control
environment:
sdk: ">=3.6.0 <4.0.0"
sdk: ">=3.8.0 <4.0.0"
# Add regular dependencies here.
dependencies:
cherrypick_annotations: ^3.0.1
analyzer: ^7.7.1
cherrypick_annotations: ^3.0.0
analyzer: ">=7.5.9 <8.0.0"
dart_style: ^3.0.0
build: ^2.4.1
source_gen: ^2.0.0
build: ^3.0.0
source_gen: ^3.1.0
collection: ^1.18.0
dev_dependencies:
lints: ^5.1.1
lints: ^6.0.0
mockito: ^5.4.5
test: ^1.25.8
build_test: ^2.1.7
build_runner: ^2.4.13
build_test: ^3.0.0
build_runner: ^2.5.0

View File

@@ -480,9 +480,10 @@ void notAClass() {}
);
});
test('should generate empty mixin for class without @inject fields',
() async {
const input = '''
test(
'should generate empty mixin for class without @inject fields',
() async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
@@ -494,7 +495,7 @@ class TestWidget {
}
''';
const expectedOutput = '''
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
@@ -509,8 +510,9 @@ mixin _\$TestWidget {
}
''';
await _testGeneration(input, expectedOutput);
});
await _testGeneration(input, expectedOutput);
},
);
});
group('Edge Cases', () {
@@ -593,12 +595,8 @@ mixin _\$TestWidget {
Future<void> _testGeneration(String input, String expectedOutput) async {
await testBuilder(
injectBuilder(BuilderOptions.empty),
{
'a|lib/test_widget.dart': input,
},
outputs: {
'a|lib/test_widget.inject.cherrypick.g.dart': expectedOutput,
},
reader: await PackageAssetReader.currentIsolate(),
{'a|lib/test_widget.dart': input},
outputs: {'a|lib/test_widget.inject.cherrypick.g.dart': expectedOutput},
readerWriter: TestReaderWriter(),
);
}

View File

@@ -590,9 +590,10 @@ void notAClass() {}
);
});
test('should throw error for method without @instance or @provide',
() async {
const input = '''
test(
'should throw error for method without @instance or @provide',
() async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
import 'package:cherrypick/cherrypick.dart';
@@ -604,11 +605,12 @@ abstract class TestModule extends Module {
}
''';
await expectLater(
() => _testGeneration(input, ''),
throwsA(isA<InvalidGenerationSourceError>()),
);
});
await expectLater(
() => _testGeneration(input, ''),
throwsA(isA<InvalidGenerationSourceError>()),
);
},
);
test('should throw error for @params with @instance', () async {
const input = '''
@@ -637,12 +639,8 @@ abstract class TestModule extends Module {
Future<void> _testGeneration(String input, String expectedOutput) async {
await testBuilder(
moduleBuilder(BuilderOptions.empty),
{
'a|lib/test_module.dart': input,
},
outputs: {
'a|lib/test_module.module.cherrypick.g.dart': expectedOutput,
},
reader: await PackageAssetReader.currentIsolate(),
{'a|lib/test_module.dart': input},
outputs: {'a|lib/test_module.module.cherrypick.g.dart': expectedOutput},
readerWriter: TestReaderWriter(),
);
}

View File

@@ -1,7 +1,6 @@
import 'package:analyzer/dart/element/element2.dart';
import 'package:test/test.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/source/source.dart';
import 'package:cherrypick_generator/src/type_parser.dart';
import 'package:cherrypick_generator/src/exceptions.dart';
@@ -42,7 +41,9 @@ void main() {
expect(
() => TypeParser.validateInjectableType(
parsedType, _createMockElement()),
parsedType,
_createMockElement(),
),
throwsA(isA<TypeParsingException>()),
);
});
@@ -59,7 +60,9 @@ void main() {
expect(
() => TypeParser.validateInjectableType(
parsedType, _createMockElement()),
parsedType,
_createMockElement(),
),
throwsA(isA<TypeParsingException>()),
);
});
@@ -76,7 +79,9 @@ void main() {
expect(
() => TypeParser.validateInjectableType(
parsedType, _createMockElement()),
parsedType,
_createMockElement(),
),
returnsNormally,
);
});
@@ -159,19 +164,21 @@ void main() {
expect(parsedType.resolveMethodName, equals('resolveAsync'));
});
test('should return correct resolveMethodName for nullable async types',
() {
final parsedType = ParsedType(
displayString: 'Future<String?>',
coreType: 'String',
isNullable: true,
isFuture: true,
isGeneric: false,
typeArguments: [],
);
test(
'should return correct resolveMethodName for nullable async types',
() {
final parsedType = ParsedType(
displayString: 'Future<String?>',
coreType: 'String',
isNullable: true,
isFuture: true,
isGeneric: false,
typeArguments: [],
);
expect(parsedType.resolveMethodName, equals('tryResolveAsync'));
});
expect(parsedType.resolveMethodName, equals('tryResolveAsync'));
},
);
test('should implement equality correctly', () {
final parsedType1 = ParsedType(
@@ -216,19 +223,19 @@ void main() {
}
// Mock element for testing
Element _createMockElement() {
Element2 _createMockElement() {
return _MockElement();
}
class _MockElement implements Element {
class _MockElement implements Element2 {
@override
String get displayName => 'MockElement';
@override
String get name => 'MockElement';
@override
Source? get source => null;
//@override
//String get name => 'MockElement';
//
//@override
//Source? get source => null;
@override
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);