From e37d3f76667126e799e8030a8d43ed8777c67daa Mon Sep 17 00:00:00 2001 From: Rexios Date: Mon, 21 Oct 2024 12:04:36 -0400 Subject: [PATCH] Initial tests --- .github/workflows/test.yml | 6 +- .../lib/src/adapters_generator.dart | 12 ++-- .../test/adapters_generator_test.dart | 55 ++++++++++++++++++- hive_generator/test/test_utils.dart | 9 ++- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9ef59cc5..626c1942 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -85,7 +85,7 @@ jobs: flutter-channel: [stable, beta] defaults: run: - working-directory: hive_generator/example + working-directory: hive_generator steps: - uses: actions/checkout@v4 - uses: subosito/flutter-action@v2 @@ -93,8 +93,8 @@ jobs: channel: ${{ matrix.flutter-channel }} - name: Install dependencies run: dart pub get - - name: Generate build_runner output - run: dart pub run build_runner build --delete-conflicting-outputs + - name: Run tests + run: dart test ensure-codegen: runs-on: ubuntu-latest diff --git a/hive_generator/lib/src/adapters_generator.dart b/hive_generator/lib/src/adapters_generator.dart index fc7d7f0b..261d59ed 100644 --- a/hive_generator/lib/src/adapters_generator.dart +++ b/hive_generator/lib/src/adapters_generator.dart @@ -13,6 +13,13 @@ import 'package:yaml/yaml.dart'; import 'package:yaml_writer/yaml_writer.dart'; import 'package:path/path.dart' as path; +/// The comment placed at the top of the schema file +const schemaComment = ''' +# Generated by Hive CE +# Manual modifications may be necessary for certain migrations +# If edited manually, delete `.dart_tool` before regenerating +# Check into version control'''; + /// Builder that generates Hive adapters from a GenerateAdapters annotation class AdaptersGenerator extends GeneratorForAnnotation { @override @@ -75,10 +82,7 @@ class AdaptersGenerator extends GeneratorForAnnotation { .write(HiveSchema(nextTypeId: nextTypeId, types: newTypes).toJson()); schemaFile.writeAsStringSync( ''' -# Generated by Hive CE -# Manual modifications may be necessary for certain migrations -# If edited manually, delete `.dart_tool` before regenerating -# Check into version control +$schemaComment $yaml''', ); diff --git a/hive_generator/test/adapters_generator_test.dart b/hive_generator/test/adapters_generator_test.dart index c1c6e50a..739bcc5d 100644 --- a/hive_generator/test/adapters_generator_test.dart +++ b/hive_generator/test/adapters_generator_test.dart @@ -1,10 +1,11 @@ +import 'package:hive_ce_generator/src/adapters_generator.dart'; import 'package:test/test.dart'; import 'test_utils.dart'; void main() { group('adapters_generator', () { - test('clean generation', () async { + test('clean', () async { expectGeneration( input: { 'pubspec.yaml': pubspec, @@ -25,6 +26,7 @@ class Person { 'lib/hive/hive_adapters.g.dart': fileExists, 'lib/hive/hive_registrar.g.dart': fileExists, 'lib/hive/hive_schema.yaml': ''' +$schemaComment nextTypeId: 1 types: Person: @@ -35,6 +37,57 @@ types: index: 0 age: index: 1 +''', + }, + ); + }); + + test('add type', () async { + // Adding Person2 while removing Person should result in Person2 having a + // typeId of 1 + expectGeneration( + input: { + 'pubspec.yaml': pubspec, + 'lib/hive/hive_adapters.dart': ''' +import 'package:hive_ce/hive.dart'; +part 'hive_adapters.g.dart'; + +@GenerateAdapters([AdapterSpec()]) +class Person2 { + const Person2({required this.name, required this.age}); + + final String name; + final int age; +} +''', + 'lib/hive/hive_schema.yaml': ''' +nextTypeId: 1 +types: + Person: + typeId: 0 + nextIndex: 2 + fields: + name: + index: 0 + age: + index: 1 +''', + }, + output: { + 'lib/hive/hive_adapters.g.dart': fileExists, + 'lib/hive/hive_registrar.g.dart': fileExists, + 'lib/hive/hive_schema.yaml': ''' +$schemaComment +nextTypeId: 2 +types: + Person2: + typeId: 1 + nextIndex: 2 + fields: + name: + index: 0 + age: + index: 1 ''', }, ); diff --git a/hive_generator/test/test_utils.dart b/hive_generator/test/test_utils.dart index ff99844a..0dd8a7fa 100644 --- a/hive_generator/test/test_utils.dart +++ b/hive_generator/test/test_utils.dart @@ -9,15 +9,14 @@ const fileExists = ''; /// /// About [output] /// - An empty content string will result in checking for file existence only -/// - Files may contain additional content, but must contain the exact -/// string specified void expectGeneration({ required Map input, required Map output, }) { final projectRoot = createTestProject(input); - Process.runSync('dart', ['pub', 'get'], workingDirectory: projectRoot); - Process.runSync( + final result1 = + Process.runSync('dart', ['pub', 'get'], workingDirectory: projectRoot); + final result2 = Process.runSync( 'dart', ['pub', 'run', 'build_runner', 'build'], workingDirectory: projectRoot, @@ -30,7 +29,7 @@ void expectGeneration({ // Do not check content if value is empty if (value.isNotEmpty) { final content = file.readAsStringSync(); - expect(content, contains(value)); + expect(content, value); } } }