Skip to content

Commit

Permalink
Initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Oct 21, 2024
1 parent 9d0176a commit e37d3f7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ 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
with:
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
Expand Down
12 changes: 8 additions & 4 deletions hive_generator/lib/src/adapters_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<GenerateAdapters> {
@override
Expand Down Expand Up @@ -75,10 +82,7 @@ class AdaptersGenerator extends GeneratorForAnnotation<GenerateAdapters> {
.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''',
);

Expand Down
55 changes: 54 additions & 1 deletion hive_generator/test/adapters_generator_test.dart
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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:
Expand All @@ -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<Person2>()])
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
''',
},
);
Expand Down
9 changes: 4 additions & 5 deletions hive_generator/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> input,
required Map<String, String> 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,
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit e37d3f7

Please sign in to comment.