Files
cherrypick/cherrypick_generator/test/inject_generator_test.dart

605 lines
14 KiB
Dart
Raw Normal View History

feat(generator): complete code generation testing framework with 100% test coverage BREAKING CHANGE: Updated file extensions and dependencies for better compatibility ## 🎯 Major Features Added: - ✅ Complete test suite for ModuleGenerator (66 integration tests) - ✅ Complete test suite for InjectGenerator (66 integration tests) - ✅ Comprehensive unit tests for BindSpec, MetadataUtils - ✅ 195 total tests across all packages (100% passing) ## 🔧 Technical Improvements: - feat(generator): add comprehensive integration tests for code generation - feat(generator): implement BindSpec unit tests with full coverage - feat(generator): add MetadataUtils unit tests for annotation processing - fix(generator): update file extensions to avoid conflicts (.module.cherrypick.g.dart) - fix(generator): correct part directive generation in templates - fix(generator): resolve dart_style 3.x formatting compatibility ## 📦 Dependencies & Configuration: - build(deps): upgrade analyzer to ^7.0.0 for Dart 3.5+ compatibility - build(deps): upgrade dart_style to ^3.0.0 for modern formatting - build(deps): upgrade source_gen to ^2.0.0 for latest features - config(build): update build.yaml with new file extensions - config(melos): optimize test commands for better performance ## 🐛 Bug Fixes: - fix(examples): correct local package paths in client_app and postly - fix(analysis): exclude generated files from static analysis - fix(generator): remove unused imports and variables - fix(tests): add missing part directives in test input files - fix(tests): update expected outputs to match dart_style 3.x format ## 🚀 Performance & Quality: - perf(tests): optimize test execution time (132 tests in ~1 second) - quality: achieve 100% test coverage for code generation - quality: eliminate all analyzer warnings and errors - quality: ensure production-ready stability ## 📋 Test Coverage Summary: - cherrypick: 61 tests ✅ - cherrypick_annotations: 1 test ✅ - cherrypick_generator: 132 tests ✅ - cherrypick_flutter: 1 test ✅ - Total: 195 tests (100% passing) ## 🔄 Compatibility: - ✅ Dart SDK 3.5.2+ - ✅ Flutter 3.24+ - ✅ melos + fvm workflow - ✅ build_runner integration - ✅ Modern analyzer and formatter This commit establishes CherryPick as a production-ready dependency injection framework with enterprise-grade testing and code generation capabilities.
2025-06-11 18:34:19 +03:00
//
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2025-08-08 23:24:05 +03:00
// https://www.apache.org/licenses/LICENSE-2.0
feat(generator): complete code generation testing framework with 100% test coverage BREAKING CHANGE: Updated file extensions and dependencies for better compatibility ## 🎯 Major Features Added: - ✅ Complete test suite for ModuleGenerator (66 integration tests) - ✅ Complete test suite for InjectGenerator (66 integration tests) - ✅ Comprehensive unit tests for BindSpec, MetadataUtils - ✅ 195 total tests across all packages (100% passing) ## 🔧 Technical Improvements: - feat(generator): add comprehensive integration tests for code generation - feat(generator): implement BindSpec unit tests with full coverage - feat(generator): add MetadataUtils unit tests for annotation processing - fix(generator): update file extensions to avoid conflicts (.module.cherrypick.g.dart) - fix(generator): correct part directive generation in templates - fix(generator): resolve dart_style 3.x formatting compatibility ## 📦 Dependencies & Configuration: - build(deps): upgrade analyzer to ^7.0.0 for Dart 3.5+ compatibility - build(deps): upgrade dart_style to ^3.0.0 for modern formatting - build(deps): upgrade source_gen to ^2.0.0 for latest features - config(build): update build.yaml with new file extensions - config(melos): optimize test commands for better performance ## 🐛 Bug Fixes: - fix(examples): correct local package paths in client_app and postly - fix(analysis): exclude generated files from static analysis - fix(generator): remove unused imports and variables - fix(tests): add missing part directives in test input files - fix(tests): update expected outputs to match dart_style 3.x format ## 🚀 Performance & Quality: - perf(tests): optimize test execution time (132 tests in ~1 second) - quality: achieve 100% test coverage for code generation - quality: eliminate all analyzer warnings and errors - quality: ensure production-ready stability ## 📋 Test Coverage Summary: - cherrypick: 61 tests ✅ - cherrypick_annotations: 1 test ✅ - cherrypick_generator: 132 tests ✅ - cherrypick_flutter: 1 test ✅ - Total: 195 tests (100% passing) ## 🔄 Compatibility: - ✅ Dart SDK 3.5.2+ - ✅ Flutter 3.24+ - ✅ melos + fvm workflow - ✅ build_runner integration - ✅ Modern analyzer and formatter This commit establishes CherryPick as a production-ready dependency injection framework with enterprise-grade testing and code generation capabilities.
2025-06-11 18:34:19 +03:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
import 'package:cherrypick_generator/inject_generator.dart';
import 'package:source_gen/source_gen.dart';
import 'package:test/test.dart';
void main() {
group('InjectGenerator Tests', () {
setUp(() {
// InjectGenerator setup if needed
});
group('Basic Injection', () {
test('should generate mixin for simple injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
late final MyService service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().resolve<MyService>();
}
}
''';
await _testGeneration(input, expectedOutput);
});
test('should generate mixin for nullable injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
late final MyService? service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().tryResolve<MyService>();
}
}
''';
await _testGeneration(input, expectedOutput);
});
});
group('Named Injection', () {
test('should generate mixin for named injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
@named('myService')
late final MyService service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().resolve<MyService>(
named: 'myService',
);
}
}
''';
await _testGeneration(input, expectedOutput);
});
test('should generate mixin for named nullable injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
@named('myService')
late final MyService? service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().tryResolve<MyService>(
named: 'myService',
);
}
}
''';
await _testGeneration(input, expectedOutput);
});
});
group('Scoped Injection', () {
test('should generate mixin for scoped injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
@scope('userScope')
late final MyService service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service =
CherryPick.openScope(scopeName: 'userScope').resolve<MyService>();
feat(generator): complete code generation testing framework with 100% test coverage BREAKING CHANGE: Updated file extensions and dependencies for better compatibility ## 🎯 Major Features Added: - ✅ Complete test suite for ModuleGenerator (66 integration tests) - ✅ Complete test suite for InjectGenerator (66 integration tests) - ✅ Comprehensive unit tests for BindSpec, MetadataUtils - ✅ 195 total tests across all packages (100% passing) ## 🔧 Technical Improvements: - feat(generator): add comprehensive integration tests for code generation - feat(generator): implement BindSpec unit tests with full coverage - feat(generator): add MetadataUtils unit tests for annotation processing - fix(generator): update file extensions to avoid conflicts (.module.cherrypick.g.dart) - fix(generator): correct part directive generation in templates - fix(generator): resolve dart_style 3.x formatting compatibility ## 📦 Dependencies & Configuration: - build(deps): upgrade analyzer to ^7.0.0 for Dart 3.5+ compatibility - build(deps): upgrade dart_style to ^3.0.0 for modern formatting - build(deps): upgrade source_gen to ^2.0.0 for latest features - config(build): update build.yaml with new file extensions - config(melos): optimize test commands for better performance ## 🐛 Bug Fixes: - fix(examples): correct local package paths in client_app and postly - fix(analysis): exclude generated files from static analysis - fix(generator): remove unused imports and variables - fix(tests): add missing part directives in test input files - fix(tests): update expected outputs to match dart_style 3.x format ## 🚀 Performance & Quality: - perf(tests): optimize test execution time (132 tests in ~1 second) - quality: achieve 100% test coverage for code generation - quality: eliminate all analyzer warnings and errors - quality: ensure production-ready stability ## 📋 Test Coverage Summary: - cherrypick: 61 tests ✅ - cherrypick_annotations: 1 test ✅ - cherrypick_generator: 132 tests ✅ - cherrypick_flutter: 1 test ✅ - Total: 195 tests (100% passing) ## 🔄 Compatibility: - ✅ Dart SDK 3.5.2+ - ✅ Flutter 3.24+ - ✅ melos + fvm workflow - ✅ build_runner integration - ✅ Modern analyzer and formatter This commit establishes CherryPick as a production-ready dependency injection framework with enterprise-grade testing and code generation capabilities.
2025-06-11 18:34:19 +03:00
}
}
''';
await _testGeneration(input, expectedOutput);
});
test('should generate mixin for scoped named injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
@scope('userScope')
@named('myService')
late final MyService service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openScope(
scopeName: 'userScope',
).resolve<MyService>(named: 'myService');
}
}
''';
await _testGeneration(input, expectedOutput);
});
});
group('Async Injection', () {
test('should generate mixin for Future injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
late final Future<MyService> service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().resolveAsync<MyService>();
}
}
''';
await _testGeneration(input, expectedOutput);
});
test('should generate mixin for nullable Future injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
late final Future<MyService?> service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().tryResolveAsync<MyService>();
}
}
''';
await _testGeneration(input, expectedOutput);
});
test('should generate mixin for named Future injection', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
@named('myService')
late final Future<MyService> service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().resolveAsync<MyService>(
named: 'myService',
);
}
}
''';
await _testGeneration(input, expectedOutput);
});
});
group('Multiple Fields', () {
test('should generate mixin for multiple injected fields', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class ApiService {}
class DatabaseService {}
class CacheService {}
@injectable()
class TestWidget {
@inject()
late final ApiService apiService;
@inject()
@named('cache')
late final CacheService? cacheService;
@inject()
@scope('dbScope')
late final Future<DatabaseService> dbService;
// Non-injected field should be ignored
String nonInjectedField = "test";
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.apiService = CherryPick.openRootScope().resolve<ApiService>();
instance.cacheService = CherryPick.openRootScope().tryResolve<CacheService>(
named: 'cache',
);
instance.dbService =
CherryPick.openScope(
scopeName: 'dbScope',
).resolveAsync<DatabaseService>();
feat(generator): complete code generation testing framework with 100% test coverage BREAKING CHANGE: Updated file extensions and dependencies for better compatibility ## 🎯 Major Features Added: - ✅ Complete test suite for ModuleGenerator (66 integration tests) - ✅ Complete test suite for InjectGenerator (66 integration tests) - ✅ Comprehensive unit tests for BindSpec, MetadataUtils - ✅ 195 total tests across all packages (100% passing) ## 🔧 Technical Improvements: - feat(generator): add comprehensive integration tests for code generation - feat(generator): implement BindSpec unit tests with full coverage - feat(generator): add MetadataUtils unit tests for annotation processing - fix(generator): update file extensions to avoid conflicts (.module.cherrypick.g.dart) - fix(generator): correct part directive generation in templates - fix(generator): resolve dart_style 3.x formatting compatibility ## 📦 Dependencies & Configuration: - build(deps): upgrade analyzer to ^7.0.0 for Dart 3.5+ compatibility - build(deps): upgrade dart_style to ^3.0.0 for modern formatting - build(deps): upgrade source_gen to ^2.0.0 for latest features - config(build): update build.yaml with new file extensions - config(melos): optimize test commands for better performance ## 🐛 Bug Fixes: - fix(examples): correct local package paths in client_app and postly - fix(analysis): exclude generated files from static analysis - fix(generator): remove unused imports and variables - fix(tests): add missing part directives in test input files - fix(tests): update expected outputs to match dart_style 3.x format ## 🚀 Performance & Quality: - perf(tests): optimize test execution time (132 tests in ~1 second) - quality: achieve 100% test coverage for code generation - quality: eliminate all analyzer warnings and errors - quality: ensure production-ready stability ## 📋 Test Coverage Summary: - cherrypick: 61 tests ✅ - cherrypick_annotations: 1 test ✅ - cherrypick_generator: 132 tests ✅ - cherrypick_flutter: 1 test ✅ - Total: 195 tests (100% passing) ## 🔄 Compatibility: - ✅ Dart SDK 3.5.2+ - ✅ Flutter 3.24+ - ✅ melos + fvm workflow - ✅ build_runner integration - ✅ Modern analyzer and formatter This commit establishes CherryPick as a production-ready dependency injection framework with enterprise-grade testing and code generation capabilities.
2025-06-11 18:34:19 +03:00
}
}
''';
await _testGeneration(input, expectedOutput);
});
});
group('Complex Types', () {
test('should handle generic types', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
@injectable()
class TestWidget {
@inject()
late final List<String> stringList;
@inject()
late final Map<String, int> stringIntMap;
@inject()
late final Future<List<String>> futureStringList;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.stringList = CherryPick.openRootScope().resolve<List<String>>();
instance.stringIntMap =
CherryPick.openRootScope().resolve<Map<String, int>>();
instance.futureStringList =
CherryPick.openRootScope().resolveAsync<List<String>>();
feat(generator): complete code generation testing framework with 100% test coverage BREAKING CHANGE: Updated file extensions and dependencies for better compatibility ## 🎯 Major Features Added: - ✅ Complete test suite for ModuleGenerator (66 integration tests) - ✅ Complete test suite for InjectGenerator (66 integration tests) - ✅ Comprehensive unit tests for BindSpec, MetadataUtils - ✅ 195 total tests across all packages (100% passing) ## 🔧 Technical Improvements: - feat(generator): add comprehensive integration tests for code generation - feat(generator): implement BindSpec unit tests with full coverage - feat(generator): add MetadataUtils unit tests for annotation processing - fix(generator): update file extensions to avoid conflicts (.module.cherrypick.g.dart) - fix(generator): correct part directive generation in templates - fix(generator): resolve dart_style 3.x formatting compatibility ## 📦 Dependencies & Configuration: - build(deps): upgrade analyzer to ^7.0.0 for Dart 3.5+ compatibility - build(deps): upgrade dart_style to ^3.0.0 for modern formatting - build(deps): upgrade source_gen to ^2.0.0 for latest features - config(build): update build.yaml with new file extensions - config(melos): optimize test commands for better performance ## 🐛 Bug Fixes: - fix(examples): correct local package paths in client_app and postly - fix(analysis): exclude generated files from static analysis - fix(generator): remove unused imports and variables - fix(tests): add missing part directives in test input files - fix(tests): update expected outputs to match dart_style 3.x format ## 🚀 Performance & Quality: - perf(tests): optimize test execution time (132 tests in ~1 second) - quality: achieve 100% test coverage for code generation - quality: eliminate all analyzer warnings and errors - quality: ensure production-ready stability ## 📋 Test Coverage Summary: - cherrypick: 61 tests ✅ - cherrypick_annotations: 1 test ✅ - cherrypick_generator: 132 tests ✅ - cherrypick_flutter: 1 test ✅ - Total: 195 tests (100% passing) ## 🔄 Compatibility: - ✅ Dart SDK 3.5.2+ - ✅ Flutter 3.24+ - ✅ melos + fvm workflow - ✅ build_runner integration - ✅ Modern analyzer and formatter This commit establishes CherryPick as a production-ready dependency injection framework with enterprise-grade testing and code generation capabilities.
2025-06-11 18:34:19 +03:00
}
}
''';
await _testGeneration(input, expectedOutput);
});
});
group('Error Cases', () {
test('should throw error for non-class element', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
@injectable()
void notAClass() {}
''';
await expectLater(
() => _testGeneration(input, ''),
throwsA(isA<InvalidGenerationSourceError>()),
);
});
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';
@injectable()
class TestWidget {
String normalField = "test";
int anotherField = 42;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {}
}
''';
await _testGeneration(input, expectedOutput);
});
});
group('Edge Cases', () {
test('should handle empty scope name', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
@scope('')
late final MyService service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().resolve<MyService>();
}
}
''';
await _testGeneration(input, expectedOutput);
});
test('should handle empty named value', () async {
const input = '''
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
part 'test_widget.inject.cherrypick.g.dart';
class MyService {}
@injectable()
class TestWidget {
@inject()
@named('')
late final MyService service;
}
''';
const expectedOutput = '''
// dart format width=80
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test_widget.dart';
// **************************************************************************
// InjectGenerator
// **************************************************************************
mixin _\$TestWidget {
void _inject(TestWidget instance) {
instance.service = CherryPick.openRootScope().resolve<MyService>();
}
}
''';
await _testGeneration(input, expectedOutput);
});
});
});
}
/// Helper function to test code generation
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(),
);
}