From bfb4801eda97dc875a3c9188eb2a6fbde5b82f7a Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Tue, 21 May 2024 00:47:11 +0800 Subject: [PATCH] Migrate from outdated code_gen_tester --- codegen_build/pubspec.yaml | 5 --- codegen_build/test/np_log_test.dart | 57 ++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/codegen_build/pubspec.yaml b/codegen_build/pubspec.yaml index fd1b4813..94f2ba2d 100644 --- a/codegen_build/pubspec.yaml +++ b/codegen_build/pubspec.yaml @@ -16,11 +16,6 @@ dependencies: dev_dependencies: build_test: ^2.1.5 - code_gen_tester: - git: - url: https://github.com/rrousselGit/functional_widget - ref: v0.10.0 - path: packages/code_gen_tester np_lints: path: ../np_lints path: any diff --git a/codegen_build/test/np_log_test.dart b/codegen_build/test/np_log_test.dart index 68f1c9b1..8d848248 100644 --- a/codegen_build/test/np_log_test.dart +++ b/codegen_build/test/np_log_test.dart @@ -1,26 +1,63 @@ -import 'package:code_gen_tester/code_gen_tester.dart'; import 'package:np_codegen_build/src/np_log_generator.dart'; import 'package:test/test.dart'; +import 'util.dart'; + // dummy class to free us from importing the actual logger library class Logger { Logger(String name); } -void main() { - final tester = SourceGenTester.fromPath("test/src/np_log.dart"); - const generator = NpLogGenerator(); - Future expectGen(String name, Matcher matcher) async => - expectGenerateNamed(await tester, name, generator, matcher); +void main() async { + await resolveCompilationUnit("test/src/np_log.dart"); + tearDown(() { + // Increment this after each test so the next test has it's own package + _pkgCacheCount++; + }); test("NpLog", () async { - await expectGen("Test", completion(""" -extension _\$TestNpLog on Test { + final src = _genSrc(""" +@npLog +class Foo {} +"""); + final expected = _genExpected(r""" +extension _$FooNpLog on Foo { // ignore: unused_element Logger get _log => log; - static final log = Logger("np_log.Test"); + static final log = Logger("test.Foo"); } -""")); +"""); + return _buildTest(src, expected); }); } + +String _genSrc(String src) { + return """ +import 'package:np_codegen/np_codegen.dart'; +part 'test.g.dart'; +$src +"""; +} + +String _genExpected(String src) { + return """// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'test.dart'; + +// ************************************************************************** +// NpLogGenerator +// ************************************************************************** + +$src"""; +} + +Future _buildTest(String src, String expected) => buildTest( + generators: [const NpLogGenerator()], + pkgName: _pkgName, + src: src, + expected: expected, + ); + +String get _pkgName => 'pkg$_pkgCacheCount'; +int _pkgCacheCount = 1;