From 9db6c8df0fbdd0886ce256391eb65b0b0e95dcc7 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Tue, 15 Mar 2022 20:18:22 +0200 Subject: [PATCH 1/7] Move first generator test input/output to files --- generator/test/generator_test.dart | 59 ----------------- .../test/generator_tests_data/pinhole.dart | 9 +++ .../test/generator_tests_data/pinhole.g.dart | 38 +++++++++++ .../test/generator_tests_data/tests.dart | 10 +++ generator/test/test_util.dart | 64 +++++++++++++++++++ 5 files changed, 121 insertions(+), 59 deletions(-) create mode 100644 generator/test/generator_tests_data/pinhole.dart create mode 100644 generator/test/generator_tests_data/pinhole.g.dart create mode 100644 generator/test/generator_tests_data/tests.dart create mode 100644 generator/test/test_util.dart diff --git a/generator/test/generator_test.dart b/generator/test/generator_test.dart index 66b535954c..6f0111a1f2 100644 --- a/generator/test/generator_test.dart +++ b/generator/test/generator_test.dart @@ -3,65 +3,6 @@ import 'package:realm_generator/realm_generator.dart'; import 'package:test/test.dart'; void main() { - test('pinhole', () async { - await testBuilder( - generateRealmObjects(), - { - 'pkg|lib/src/test.dart': r''' -import 'package:realm_common/realm_common.dart'; - -part 'test.g.dart'; - -@RealmModel() -class _Foo { - int x = 0; -}''', - }, - outputs: { - 'pkg|lib/src/test.realm_objects.g.part': '// **************************************************************************\n' - '// RealmObjectGenerator\n' - '// **************************************************************************\n' - '\n' - 'class Foo extends _Foo with RealmEntity, RealmObject {\n' - ' static var _defaultsSet = false;\n' - '\n' - ' Foo({\n' - ' int x = 0,\n' - ' }) {\n' - ' if (!_defaultsSet) {\n' - ' _defaultsSet = RealmObject.setDefaults({\n' - ' \'x\': 0,\n' - ' });\n' - ' }\n' - ' RealmObject.set(this, \'x\', x);\n' - ' }\n' - '\n' - ' Foo._();\n' - '\n' - ' @override\n' - ' int get x => RealmObject.get(this, \'x\') as int;\n' - ' @override\n' - ' set x(int value) => RealmObject.set(this, \'x\', value);\n' - '\n' - ' @override\n' - ' Stream> get changes =>\n' - ' RealmObject.getChanges(this);\n' - '\n' - ' static SchemaObject get schema => _schema ??= _initSchema();\n' - ' static SchemaObject? _schema;\n' - ' static SchemaObject _initSchema() {\n' - ' RealmObject.registerFactory(Foo._);\n' - ' return const SchemaObject(Foo, [\n' - ' SchemaProperty(\'x\', RealmPropertyType.int),\n' - ' ]);\n' - ' }\n' - '}\n' - '', - }, - reader: await PackageAssetReader.currentIsolate(), - ); - }); - test('all types', () async { await testBuilder( generateRealmObjects(), diff --git a/generator/test/generator_tests_data/pinhole.dart b/generator/test/generator_tests_data/pinhole.dart new file mode 100644 index 0000000000..5aac5e299e --- /dev/null +++ b/generator/test/generator_tests_data/pinhole.dart @@ -0,0 +1,9 @@ +import 'package:realm_common/realm_common.dart'; +import '../../../lib/realm.dart'; + +part 'pinhole.g.dart'; + +@RealmModel() +class _Foo { + int x = 0; +} diff --git a/generator/test/generator_tests_data/pinhole.g.dart b/generator/test/generator_tests_data/pinhole.g.dart new file mode 100644 index 0000000000..ef442bdbb3 --- /dev/null +++ b/generator/test/generator_tests_data/pinhole.g.dart @@ -0,0 +1,38 @@ +// ************************************************************************** +// RealmObjectGenerator +// ************************************************************************** +part of 'pinhole.dart'; + +class Foo extends _Foo with RealmEntity, RealmObject { + static var _defaultsSet = false; + + Foo({ + int x = 0, + }) { + if (!_defaultsSet) { + _defaultsSet = RealmObject.setDefaults({ + 'x': 0, + }); + } + RealmObject.set(this, 'x', x); + } + + Foo._(); + + @override + int get x => RealmObject.get(this, 'x') as int; + @override + set x(int value) => RealmObject.set(this, 'x', value); + + @override + Stream> get changes => RealmObject.getChanges(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObject.registerFactory(Foo._); + return const SchemaObject(Foo, [ + SchemaProperty('x', RealmPropertyType.int), + ]); + } +} diff --git a/generator/test/generator_tests_data/tests.dart b/generator/test/generator_tests_data/tests.dart new file mode 100644 index 0000000000..c4f8e838aa --- /dev/null +++ b/generator/test/generator_tests_data/tests.dart @@ -0,0 +1,10 @@ +import 'package:test/test.dart'; +import '../test_util.dart'; + +void main() { + final folderName = 'generator_tests_data'; + + test('pinhole', () async { + await ioTtestBuilder(folderName, 'pinhole'); + }); +} diff --git a/generator/test/test_util.dart b/generator/test/test_util.dart new file mode 100644 index 0000000000..ca8e5990cb --- /dev/null +++ b/generator/test/test_util.dart @@ -0,0 +1,64 @@ +import 'dart:io'; +import 'dart:async'; +import 'dart:convert'; +import 'package:path/path.dart' as _path; +import 'package:dart_style/dart_style.dart'; +import 'package:build/build.dart'; +import 'package:build_test/build_test.dart'; +import 'package:realm_generator/realm_generator.dart'; + +String _stringReplacements(String content) { + final formatter = DartFormatter(); + var lines = LineSplitter.split(content); + String formattedContent = lines.where((element) => !element.startsWith("part of")).join('\n'); + return formatter.format(formattedContent); +} + +Future readFileAsFormattedString(String path) async { + var file = File(_path.join(Directory.current.path, path)); + return await file.readAsString(encoding: utf8).then((value) => _stringReplacements(value)); +} + +class _OutputFileWriter extends RecordingAssetWriter { + final _assets = >{}; + + @override + Map> get assets => _assets; + + @override + Future writeAsBytes(AssetId id, List bytes) { + return Future(() { + String content = _stringReplacements(utf8.decode(bytes)); + _assets.addAll({id: utf8.encode(content)}); + }); + } + + @override + Future writeAsString(AssetId id, String contents, {Encoding encoding = utf8}) { + return Future(() { + _assets.addAll({id: encoding.encode(_stringReplacements(contents))}); + }); + } +} + +Future> getInputFileAsset(String path) async { + String input = await readFileAsFormattedString(path); + return {'pkg|$path': input}; +} + +Future> getOutputFileAsset(String path) async { + String output = await readFileAsFormattedString(path); + String fileNameWithoutExtensions = _path.basenameWithoutExtension(_path.basenameWithoutExtension(path)); + var generatedFile = '${_path.dirname(path)}/$fileNameWithoutExtensions.realm_objects.g.part'; + return {'pkg|$generatedFile': output}; +} + +Future ioTtestBuilder(String directoryName, String testFilesPreffix) async { + return testBuilder( + generateRealmObjects(), + await getInputFileAsset('test/$directoryName/$testFilesPreffix.dart'), + outputs: await getOutputFileAsset('test/$directoryName/$testFilesPreffix.g.dart'), + reader: await PackageAssetReader.currentIsolate(), + writer: _OutputFileWriter(), + ); +} From 32aa0c7e70d6fca61a9b8b5727a9fdfef982c44f Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 16 Mar 2022 13:46:28 +0200 Subject: [PATCH 2/7] Tests restructure and fixes --- generator/test/generator_test.dart | 8 ++++++ .../pinhole.dart | 0 .../pinhole.g.dart | 0 .../test/generator_tests_data/tests.dart | 10 -------- generator/test/test_util.dart | 25 +++++++++++-------- 5 files changed, 22 insertions(+), 21 deletions(-) rename generator/test/{generator_tests_data => generator_test_io}/pinhole.dart (100%) rename generator/test/{generator_tests_data => generator_test_io}/pinhole.g.dart (100%) delete mode 100644 generator/test/generator_tests_data/tests.dart diff --git a/generator/test/generator_test.dart b/generator/test/generator_test.dart index 6f0111a1f2..875c8ca576 100644 --- a/generator/test/generator_test.dart +++ b/generator/test/generator_test.dart @@ -1,8 +1,16 @@ import 'package:build_test/build_test.dart'; import 'package:realm_generator/realm_generator.dart'; import 'package:test/test.dart'; +import 'test_util.dart'; void main() { + + final folderName = 'generator_test_io'; + + test('pinhole', () async { + await ioTtestBuilder(folderName, 'pinhole.dart'); + }); + test('all types', () async { await testBuilder( generateRealmObjects(), diff --git a/generator/test/generator_tests_data/pinhole.dart b/generator/test/generator_test_io/pinhole.dart similarity index 100% rename from generator/test/generator_tests_data/pinhole.dart rename to generator/test/generator_test_io/pinhole.dart diff --git a/generator/test/generator_tests_data/pinhole.g.dart b/generator/test/generator_test_io/pinhole.g.dart similarity index 100% rename from generator/test/generator_tests_data/pinhole.g.dart rename to generator/test/generator_test_io/pinhole.g.dart diff --git a/generator/test/generator_tests_data/tests.dart b/generator/test/generator_tests_data/tests.dart deleted file mode 100644 index c4f8e838aa..0000000000 --- a/generator/test/generator_tests_data/tests.dart +++ /dev/null @@ -1,10 +0,0 @@ -import 'package:test/test.dart'; -import '../test_util.dart'; - -void main() { - final folderName = 'generator_tests_data'; - - test('pinhole', () async { - await ioTtestBuilder(folderName, 'pinhole'); - }); -} diff --git a/generator/test/test_util.dart b/generator/test/test_util.dart index ca8e5990cb..5e127dae15 100644 --- a/generator/test/test_util.dart +++ b/generator/test/test_util.dart @@ -41,23 +41,26 @@ class _OutputFileWriter extends RecordingAssetWriter { } } -Future> getInputFileAsset(String path) async { - String input = await readFileAsFormattedString(path); - return {'pkg|$path': input}; +Future> getInputFileAsset(String inputFilePath) async { + var key = 'pkg|$inputFilePath'; + String inputContent = await readFileAsFormattedString(inputFilePath); + return {key: inputContent}; } -Future> getOutputFileAsset(String path) async { - String output = await readFileAsFormattedString(path); - String fileNameWithoutExtensions = _path.basenameWithoutExtension(_path.basenameWithoutExtension(path)); - var generatedFile = '${_path.dirname(path)}/$fileNameWithoutExtensions.realm_objects.g.part'; - return {'pkg|$generatedFile': output}; +Future> getOutputFileAsset(String inputFilePath, String outputFilePath) async { + var key = 'pkg|${_path.setExtension(inputFilePath, '.realm_objects.g.part')}'; + String outputContent = await readFileAsFormattedString(outputFilePath); + return {key: outputContent}; } -Future ioTtestBuilder(String directoryName, String testFilesPreffix) async { +Future ioTtestBuilder(String directoryName, String inputFileName, [String outputFileName = ""]) async { + if (outputFileName.isEmpty) { + outputFileName = _path.setExtension(inputFileName, '.g.dart'); + } return testBuilder( generateRealmObjects(), - await getInputFileAsset('test/$directoryName/$testFilesPreffix.dart'), - outputs: await getOutputFileAsset('test/$directoryName/$testFilesPreffix.g.dart'), + await getInputFileAsset('test/$directoryName/$inputFileName'), + outputs: await getOutputFileAsset('test/$directoryName/$inputFileName', 'test/$directoryName/$outputFileName'), reader: await PackageAssetReader.currentIsolate(), writer: _OutputFileWriter(), ); From 7c57db878175e404994c3e1eaa045a9413170278 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 16 Mar 2022 14:43:46 +0200 Subject: [PATCH 3/7] Test expected errors --- generator/test/generator_test.dart | 60 +++++-------------- .../test/generator_test_io/all_types.dart | 33 ++++++++++ .../generator_test_io/not_a_realm_type.dart | 8 +++ .../generator_test_io/not_a_realm_type.log | 15 +++++ generator/test/generator_test_io/pinhole.dart | 2 +- generator/test/test_util.dart | 26 +++++--- 6 files changed, 90 insertions(+), 54 deletions(-) create mode 100644 generator/test/generator_test_io/all_types.dart create mode 100644 generator/test/generator_test_io/not_a_realm_type.dart create mode 100644 generator/test/generator_test_io/not_a_realm_type.log diff --git a/generator/test/generator_test.dart b/generator/test/generator_test.dart index 875c8ca576..82c4e5972c 100644 --- a/generator/test/generator_test.dart +++ b/generator/test/generator_test.dart @@ -4,59 +4,30 @@ import 'package:test/test.dart'; import 'test_util.dart'; void main() { - final folderName = 'generator_test_io'; test('pinhole', () async { - await ioTtestBuilder(folderName, 'pinhole.dart'); + await ioTestBuilder(folderName, 'pinhole.dart', 'pinhole.g.dart'); }); test('all types', () async { - await testBuilder( - generateRealmObjects(), - { - 'pkg|lib/src/test.dart': r''' -import 'dart:typed_data'; - -import 'package:realm_common/realm_common.dart'; - -part 'test.g.dart'; - -@RealmModel() -@MapTo('Fooo') -class _Foo { - int x = 0; -} - -@RealmModel() -class _Bar { - @PrimaryKey() - late String id; - late bool aBool, another; - var data = Uint8List(16); - // late RealmAny any; // not supported yet - @MapTo('tidspunkt') - var timestamp = DateTime.now(); - var aDouble = 0.0; - // late Decimal128 decimal; // not supported yet - _Foo? foo; - // late ObjectId id; - // late Uuid uuid; // not supported yet - @Ignored() - var theMeaningOfEverything = 42; - var list = [0]; // list of ints with default value - // late Set set; // not supported yet - // late map = {}; // not supported yet + await ioTestBuilder(folderName, 'all_types.dart'); + }); - @Indexed() - String? anOptionalString; -}''' - }, - reader: await PackageAssetReader.currentIsolate(), + test('not a realm type', () async { + await expectLater( + () async => await ioTestErrorBuilder(folderName, 'not_a_realm_type.dart'), + throwsA( + isA().having( + (e) => e.format(), + 'format()', + await readFileAsString('test/$folderName/not_a_realm_type.log'), + ), + ), ); }); - test('not a realm type', () async { + test('not a realm type old', () async { await expectLater( () async => await testBuilder( generateRealmObjects(), @@ -920,7 +891,7 @@ class _Person { ); }); - test('map unsupported', () async { + test('map unsupported', () async { await expectLater( () async => await testBuilder( generateRealmObjects(), @@ -955,5 +926,4 @@ class _Person { )), ); }); - } diff --git a/generator/test/generator_test_io/all_types.dart b/generator/test/generator_test_io/all_types.dart new file mode 100644 index 0000000000..1a5bbd01da --- /dev/null +++ b/generator/test/generator_test_io/all_types.dart @@ -0,0 +1,33 @@ +import 'dart:typed_data'; + +import 'package:realm_common/realm_common.dart'; + +@RealmModel() +@MapTo('Fooo') +class _Foo { + int x = 0; +} + +@RealmModel() +class _Bar { + @PrimaryKey() + late String id; + late bool aBool, another; + var data = Uint8List(16); + // late RealmAny any; // not supported yet + @MapTo('tidspunkt') + var timestamp = DateTime.now(); + var aDouble = 0.0; + // late Decimal128 decimal; // not supported yet + _Foo? foo; + // late ObjectId id; + // late Uuid uuid; // not supported yet + @Ignored() + var theMeaningOfEverything = 42; + var list = [0]; // list of ints with default value + // late Set set; // not supported yet + // late map = {}; // not supported yet + + @Indexed() + String? anOptionalString; +} diff --git a/generator/test/generator_test_io/not_a_realm_type.dart b/generator/test/generator_test_io/not_a_realm_type.dart new file mode 100644 index 0000000000..8d0cd58df2 --- /dev/null +++ b/generator/test/generator_test_io/not_a_realm_type.dart @@ -0,0 +1,8 @@ +import 'package:realm_common/realm_common.dart'; + +class NonRealm {} + +@RealmModel() +class _Bad { + late NonRealm notARealmType; +} diff --git a/generator/test/generator_test_io/not_a_realm_type.log b/generator/test/generator_test_io/not_a_realm_type.log new file mode 100644 index 0000000000..cc9e0c8013 --- /dev/null +++ b/generator/test/generator_test_io/not_a_realm_type.log @@ -0,0 +1,15 @@ +Not a realm type + + in: package:pkg/src/test.dart:9:8 + ╷ + 5 │ class NonRealm {} + │ ━━━━━━━━ + ... │ + 7 │ @RealmModel() + 8 │ class _Bad { + │ ━━━━ in realm model for \'Bad\' + 9 │ late NonRealm notARealmType; + │ ^^^^^^^^ NonRealm is not a realm model type + ╵ + Add a @RealmModel annotation on \'NonRealm\', or an @Ignored annotation on \'notARealmType\'. + \ No newline at end of file diff --git a/generator/test/generator_test_io/pinhole.dart b/generator/test/generator_test_io/pinhole.dart index 5aac5e299e..e723af9a7f 100644 --- a/generator/test/generator_test_io/pinhole.dart +++ b/generator/test/generator_test_io/pinhole.dart @@ -1,5 +1,5 @@ import 'package:realm_common/realm_common.dart'; -import '../../../lib/realm.dart'; +import '../../../lib/realm.dart'; //For partial file references part 'pinhole.g.dart'; diff --git a/generator/test/test_util.dart b/generator/test/test_util.dart index 5e127dae15..1bfd8f3733 100644 --- a/generator/test/test_util.dart +++ b/generator/test/test_util.dart @@ -14,11 +14,16 @@ String _stringReplacements(String content) { return formatter.format(formattedContent); } -Future readFileAsFormattedString(String path) async { +Future readFileAsDartFormattedString(String path) async { var file = File(_path.join(Directory.current.path, path)); return await file.readAsString(encoding: utf8).then((value) => _stringReplacements(value)); } +Future readFileAsString(String path) async { + var file = File(_path.join(Directory.current.path, path)); + return await file.readAsString(encoding: utf8).then((value) => LineSplitter.split(value).join('\n')); +} + class _OutputFileWriter extends RecordingAssetWriter { final _assets = >{}; @@ -43,25 +48,30 @@ class _OutputFileWriter extends RecordingAssetWriter { Future> getInputFileAsset(String inputFilePath) async { var key = 'pkg|$inputFilePath'; - String inputContent = await readFileAsFormattedString(inputFilePath); + String inputContent = await readFileAsDartFormattedString(inputFilePath); return {key: inputContent}; } Future> getOutputFileAsset(String inputFilePath, String outputFilePath) async { var key = 'pkg|${_path.setExtension(inputFilePath, '.realm_objects.g.part')}'; - String outputContent = await readFileAsFormattedString(outputFilePath); + String outputContent = await readFileAsDartFormattedString(outputFilePath); return {key: outputContent}; } -Future ioTtestBuilder(String directoryName, String inputFileName, [String outputFileName = ""]) async { - if (outputFileName.isEmpty) { - outputFileName = _path.setExtension(inputFileName, '.g.dart'); - } +Future ioTestBuilder(String directoryName, String inputFileName, [String outputFileName = ""]) async { return testBuilder( generateRealmObjects(), await getInputFileAsset('test/$directoryName/$inputFileName'), - outputs: await getOutputFileAsset('test/$directoryName/$inputFileName', 'test/$directoryName/$outputFileName'), + outputs: outputFileName.isEmpty ? null : await getOutputFileAsset('test/$directoryName/$inputFileName', 'test/$directoryName/$outputFileName'), reader: await PackageAssetReader.currentIsolate(), writer: _OutputFileWriter(), ); } + +Future ioTestErrorBuilder(String directoryName, String inputFileName) async { + return testBuilder( + generateRealmObjects(), + await getInputFileAsset('test/$directoryName/$inputFileName'), + reader: await PackageAssetReader.currentIsolate(), + ); +} From 5cfa889c25082db1feed7ef836360c7fa1e9f566 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 16 Mar 2022 15:37:10 +0200 Subject: [PATCH 4/7] Error test fixes --- .../generator_test_io/not_a_realm_type.dart | 2 ++ .../generator_test_io/not_a_realm_type.log | 26 +++++++++---------- generator/test/test_util.dart | 4 +-- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/generator/test/generator_test_io/not_a_realm_type.dart b/generator/test/generator_test_io/not_a_realm_type.dart index 8d0cd58df2..3d7e0ae285 100644 --- a/generator/test/generator_test_io/not_a_realm_type.dart +++ b/generator/test/generator_test_io/not_a_realm_type.dart @@ -1,5 +1,7 @@ import 'package:realm_common/realm_common.dart'; +//part 'not_a_realm_type.g.dart'; + class NonRealm {} @RealmModel() diff --git a/generator/test/generator_test_io/not_a_realm_type.log b/generator/test/generator_test_io/not_a_realm_type.log index cc9e0c8013..4357d811a9 100644 --- a/generator/test/generator_test_io/not_a_realm_type.log +++ b/generator/test/generator_test_io/not_a_realm_type.log @@ -1,15 +1,15 @@ Not a realm type - in: package:pkg/src/test.dart:9:8 - ╷ - 5 │ class NonRealm {} - │ ━━━━━━━━ - ... │ - 7 │ @RealmModel() - 8 │ class _Bad { - │ ━━━━ in realm model for \'Bad\' - 9 │ late NonRealm notARealmType; - │ ^^^^^^^^ NonRealm is not a realm model type - ╵ - Add a @RealmModel annotation on \'NonRealm\', or an @Ignored annotation on \'notARealmType\'. - \ No newline at end of file +in: asset:pkg/test/generator_test_io/not_a_realm_type.dart:9:8 + , +5 | class NonRealm {} + | ======== +... | +7 | @RealmModel() +8 | class _Bad { + | ==== in realm model for 'Bad' +9 | late NonRealm notARealmType; + | ^^^^^^^^ NonRealm is not a realm model type + ' +Add a @RealmModel annotation on 'NonRealm', or an @Ignored annotation on 'notARealmType'. + diff --git a/generator/test/test_util.dart b/generator/test/test_util.dart index 1bfd8f3733..36ac8cf72c 100644 --- a/generator/test/test_util.dart +++ b/generator/test/test_util.dart @@ -24,7 +24,7 @@ Future readFileAsString(String path) async { return await file.readAsString(encoding: utf8).then((value) => LineSplitter.split(value).join('\n')); } -class _OutputFileWriter extends RecordingAssetWriter { +class _OutputDartFileWriter extends RecordingAssetWriter { final _assets = >{}; @override @@ -64,7 +64,7 @@ Future ioTestBuilder(String directoryName, String inputFileName, [Strin await getInputFileAsset('test/$directoryName/$inputFileName'), outputs: outputFileName.isEmpty ? null : await getOutputFileAsset('test/$directoryName/$inputFileName', 'test/$directoryName/$outputFileName'), reader: await PackageAssetReader.currentIsolate(), - writer: _OutputFileWriter(), + writer: _OutputDartFileWriter(), ); } From 7a57c2d7170e8bdc63b1869ff816a63c7106bd5f Mon Sep 17 00:00:00 2001 From: desistefanova Date: Wed, 16 Mar 2022 15:49:14 +0200 Subject: [PATCH 5/7] mas symbols --- .../generator_test_io/not_a_realm_type.log | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/generator/test/generator_test_io/not_a_realm_type.log b/generator/test/generator_test_io/not_a_realm_type.log index 4357d811a9..e76e4c38b8 100644 --- a/generator/test/generator_test_io/not_a_realm_type.log +++ b/generator/test/generator_test_io/not_a_realm_type.log @@ -1,15 +1,15 @@ Not a realm type in: asset:pkg/test/generator_test_io/not_a_realm_type.dart:9:8 - , -5 | class NonRealm {} - | ======== -... | -7 | @RealmModel() -8 | class _Bad { - | ==== in realm model for 'Bad' -9 | late NonRealm notARealmType; - | ^^^^^^^^ NonRealm is not a realm model type - ' + ╷ +5 │ class NonRealm {} + │ ━━━━━━━━ +... │ +7 │ @RealmModel() +8 │ class _Bad { + │ ━━━━ in realm model for 'Bad' +9 │ late NonRealm notARealmType; + │ ^^^^^^^^ NonRealm is not a realm model type + ╵ Add a @RealmModel annotation on 'NonRealm', or an @Ignored annotation on 'notARealmType'. From e25bf0a15c7152f0dc0ded05bea0157b31726cae Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 17 Mar 2022 00:01:57 +0200 Subject: [PATCH 6/7] Mac to Windows symbols conversion --- generator/test/generator_test.dart | 45 +--------------------------- generator/test/test_util.dart | 47 +++++++++--------------------- 2 files changed, 14 insertions(+), 78 deletions(-) diff --git a/generator/test/generator_test.dart b/generator/test/generator_test.dart index 82c4e5972c..afdd33ed06 100644 --- a/generator/test/generator_test.dart +++ b/generator/test/generator_test.dart @@ -21,50 +21,7 @@ void main() { isA().having( (e) => e.format(), 'format()', - await readFileAsString('test/$folderName/not_a_realm_type.log'), - ), - ), - ); - }); - - test('not a realm type old', () async { - await expectLater( - () async => await testBuilder( - generateRealmObjects(), - { - 'pkg|lib/src/test.dart': r''' -import 'package:realm_common/realm_common.dart'; - -part 'test.g.dart'; - -class NonRealm {} - -@RealmModel() -class _Bad { - late NonRealm notARealmType; -}''' - }, - reader: await PackageAssetReader.currentIsolate(), - ), - throwsA( - isA().having( - (e) => e.format(), - 'format()', - 'Not a realm type\n' - '\n' - 'in: package:pkg/src/test.dart:9:8\n' - ' ╷\n' - '5 │ class NonRealm {}\n' - ' │ ━━━━━━━━ \n' - '... │\n' - '7 │ @RealmModel()\n' - '8 │ class _Bad {\n' - ' │ ━━━━ in realm model for \'Bad\'\n' - '9 │ late NonRealm notARealmType;\n' - ' │ ^^^^^^^^ NonRealm is not a realm model type\n' - ' ╵\n' - 'Add a @RealmModel annotation on \'NonRealm\', or an @Ignored annotation on \'notARealmType\'.\n' - '', + await readFileAsErrorFormattedString(folderName, 'not_a_realm_type.log'), ), ), ); diff --git a/generator/test/test_util.dart b/generator/test/test_util.dart index 36ac8cf72c..0dc66b6f4c 100644 --- a/generator/test/test_util.dart +++ b/generator/test/test_util.dart @@ -3,7 +3,6 @@ import 'dart:async'; import 'dart:convert'; import 'package:path/path.dart' as _path; import 'package:dart_style/dart_style.dart'; -import 'package:build/build.dart'; import 'package:build_test/build_test.dart'; import 'package:realm_generator/realm_generator.dart'; @@ -16,34 +15,18 @@ String _stringReplacements(String content) { Future readFileAsDartFormattedString(String path) async { var file = File(_path.join(Directory.current.path, path)); - return await file.readAsString(encoding: utf8).then((value) => _stringReplacements(value)); + String content = await file.readAsString(encoding: utf8); + return _stringReplacements(content); } -Future readFileAsString(String path) async { - var file = File(_path.join(Directory.current.path, path)); - return await file.readAsString(encoding: utf8).then((value) => LineSplitter.split(value).join('\n')); -} - -class _OutputDartFileWriter extends RecordingAssetWriter { - final _assets = >{}; - - @override - Map> get assets => _assets; - - @override - Future writeAsBytes(AssetId id, List bytes) { - return Future(() { - String content = _stringReplacements(utf8.decode(bytes)); - _assets.addAll({id: utf8.encode(content)}); - }); - } - - @override - Future writeAsString(AssetId id, String contents, {Encoding encoding = utf8}) { - return Future(() { - _assets.addAll({id: encoding.encode(_stringReplacements(contents))}); - }); - } +Future readFileAsErrorFormattedString(String directoryName, String logFileName) async { + var file = File(_path.join(Directory.current.path, 'test/$directoryName/$logFileName')); + String content = await file.readAsString(encoding: utf8); + var macToWinSymbols = {'╷': ',', '━': '=', '╵': '\'', '│': '|'}; + macToWinSymbols.forEach((key, value) { + content = Platform.isWindows ? content.replaceAll(key, value) : content.replaceAll(value, key); + }); + return LineSplitter.split(content).join('\n'); } Future> getInputFileAsset(String inputFilePath) async { @@ -59,13 +42,9 @@ Future> getOutputFileAsset(String inputFilePath, String outp } Future ioTestBuilder(String directoryName, String inputFileName, [String outputFileName = ""]) async { - return testBuilder( - generateRealmObjects(), - await getInputFileAsset('test/$directoryName/$inputFileName'), - outputs: outputFileName.isEmpty ? null : await getOutputFileAsset('test/$directoryName/$inputFileName', 'test/$directoryName/$outputFileName'), - reader: await PackageAssetReader.currentIsolate(), - writer: _OutputDartFileWriter(), - ); + return testBuilder(generateRealmObjects(), await getInputFileAsset('test/$directoryName/$inputFileName'), + outputs: outputFileName.isEmpty ? null : await getOutputFileAsset('test/$directoryName/$inputFileName', 'test/$directoryName/$outputFileName'), + reader: await PackageAssetReader.currentIsolate()); } Future ioTestErrorBuilder(String directoryName, String inputFileName) async { From c2028f88c3c989523d80cae1c5e100ef4ed92beb Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 17 Mar 2022 00:06:22 +0200 Subject: [PATCH 7/7] Symbols converted only for windows --- generator/test/test_util.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/generator/test/test_util.dart b/generator/test/test_util.dart index 0dc66b6f4c..e9c8b7233f 100644 --- a/generator/test/test_util.dart +++ b/generator/test/test_util.dart @@ -22,10 +22,12 @@ Future readFileAsDartFormattedString(String path) async { Future readFileAsErrorFormattedString(String directoryName, String logFileName) async { var file = File(_path.join(Directory.current.path, 'test/$directoryName/$logFileName')); String content = await file.readAsString(encoding: utf8); - var macToWinSymbols = {'╷': ',', '━': '=', '╵': '\'', '│': '|'}; - macToWinSymbols.forEach((key, value) { - content = Platform.isWindows ? content.replaceAll(key, value) : content.replaceAll(value, key); - }); + if (Platform.isWindows) { + var macToWinSymbols = {'╷': ',', '━': '=', '╵': '\'', '│': '|'}; + macToWinSymbols.forEach((key, value) { + content = content.replaceAll(key, value); + }); + } return LineSplitter.split(content).join('\n'); }