Skip to content

Commit

Permalink
Fix multi-file generate
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Jun 12, 2023
1 parent 2865da0 commit 5fb4f9e
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 83 deletions.
1 change: 1 addition & 0 deletions generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dev_dependencies:
build_test: ^2.1.4
lints: ^2.0.0
logging: ^1.2.0
meta: ^1.7.0
path: ^1.0.0
term_glyph: ^1.2.0
test: ^1.14.3
89 changes: 12 additions & 77 deletions generator/test/good_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:io';

import 'package:path/path.dart' as path;
import 'test_util.dart';

void main() async {
Expand All @@ -9,81 +9,16 @@ void main() async {
final sourceFile = File(generatedFile.path.replaceFirst('.expected', '.dart'));
testCompile('compile $sourceFile', sourceFile, generatedFile);
}
/*
test('Links to mapped to class in another file', () async {
final inputs = await getInputFileAsset('$directory/mapto.dart');
inputs['pkg|$directory/another_mapto.dart'] = r'''
import 'package:realm_common/realm_common.dart';
import 'mapto.dart';
part 'another_mapto.realm.dart';
@RealmModel()
@MapTo('this is also mapped')
class _MappedToo {
late $Original? singleLink;
late List<$Original> listLink;
}
''';
final outputs = await getExpectedFileAsset('$directory/mapto.dart', '$directory/mapto.expected');
outputs['pkg|$directory/another_mapto.realm.dart'] = r'''
// **************************************************************************
// RealmObjectGenerator
// **************************************************************************
class MappedToo extends _MappedToo
with RealmEntity, RealmObjectBase, RealmObject {
MappedToo({
Original? singleLink,
Iterable<Original> listLink = const [],
}) {
RealmObjectBase.set(this, 'singleLink', singleLink);
RealmObjectBase.set<RealmList<Original>>(
this, 'listLink', RealmList<Original>(listLink));
}
MappedToo._();
@override
Original? get singleLink =>
RealmObjectBase.get<Original>(this, 'singleLink') as Original?;
@override
set singleLink(covariant Original? value) =>
RealmObjectBase.set(this, 'singleLink', value);
@override
RealmList<Original> get listLink =>
RealmObjectBase.get<Original>(this, 'listLink') as RealmList<Original>;
@override
set listLink(covariant RealmList<Original> value) =>
throw RealmUnsupportedSetError();
@override
Stream<RealmObjectChanges<MappedToo>> get changes =>
RealmObjectBase.getChanges<MappedToo>(this);
@override
MappedToo freeze() => RealmObjectBase.freezeObject<MappedToo>(this);
static SchemaObject get schema => _schema ??= _initSchema();
static SchemaObject? _schema;
static SchemaObject _initSchema() {
RealmObjectBase.registerFactory(MappedToo._);
return const SchemaObject(
ObjectType.realmObject, MappedToo, 'this is also mapped', [
SchemaProperty('singleLink', RealmPropertyType.object,
optional: true, linkTarget: 'another type'),
SchemaProperty('listLink', RealmPropertyType.object,
linkTarget: 'another type', collectionType: RealmCollectionType.list),
]);
}
}
''';

await testBuilder(generateRealmObjects(), inputs, outputs: outputs, reader: await PackageAssetReader.currentIsolate());
});
*/
testCompileMany(
'Link to mapped class',
[
'another_mapto.dart',
'mapto.dart',
].map<File>((n) => File(path.join(directory, n))),
[
'another_mapto.expected_multi',
'mapto.expected',
].map<File>((n) => File(path.join(directory, n))),
);
}
12 changes: 12 additions & 0 deletions generator/test/good_test_data/another_mapto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:realm_common/realm_common.dart';
import 'mapto.dart';

part 'another_mapto.realm.dart';

@RealmModel()
@MapTo('this is also mapped')
class _MappedToo {
late $Original? singleLink;

late List<$Original> listLink;
}
55 changes: 55 additions & 0 deletions generator/test/good_test_data/another_mapto.expected_multi
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'another_mapto.dart';

// **************************************************************************
// RealmObjectGenerator
// **************************************************************************

class MappedToo extends _MappedToo
with RealmEntity, RealmObjectBase, RealmObject {
MappedToo({
Original? singleLink,
Iterable<Original> listLink = const [],
}) {
RealmObjectBase.set(this, 'singleLink', singleLink);
RealmObjectBase.set<RealmList<Original>>(
this, 'listLink', RealmList<Original>(listLink));
}

MappedToo._();

@override
Original? get singleLink =>
RealmObjectBase.get<Original>(this, 'singleLink') as Original?;
@override
set singleLink(covariant Original? value) =>
RealmObjectBase.set(this, 'singleLink', value);

@override
RealmList<Original> get listLink =>
RealmObjectBase.get<Original>(this, 'listLink') as RealmList<Original>;
@override
set listLink(covariant RealmList<Original> value) =>
throw RealmUnsupportedSetError();

@override
Stream<RealmObjectChanges<MappedToo>> get changes =>
RealmObjectBase.getChanges<MappedToo>(this);

@override
MappedToo freeze() => RealmObjectBase.freezeObject<MappedToo>(this);

static SchemaObject get schema => _schema ??= _initSchema();
static SchemaObject? _schema;
static SchemaObject _initSchema() {
RealmObjectBase.registerFactory(MappedToo._);
return const SchemaObject(
ObjectType.realmObject, MappedToo, 'this is also mapped', [
SchemaProperty('singleLink', RealmPropertyType.object,
optional: true, linkTarget: 'another type'),
SchemaProperty('listLink', RealmPropertyType.object,
linkTarget: 'another type', collectionType: RealmCollectionType.list),
]);
}
}
2 changes: 2 additions & 0 deletions generator/test/good_test_data/another_mapto.realm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// MOCK FILE!
part of 'another_mapto.dart';
3 changes: 2 additions & 1 deletion generator/test/good_test_data/mapto.expected
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class Original extends $Original
static SchemaObject? _schema;
static SchemaObject _initSchema() {
RealmObjectBase.registerFactory(Original._);
return const SchemaObject(ObjectType.realmObject, Original, 'another type', [
return const SchemaObject(
ObjectType.realmObject, Original, 'another type', [
SchemaProperty('primitiveProperty', RealmPropertyType.int,
mapTo: 'remapped primitive'),
SchemaProperty('objectProperty', RealmPropertyType.object,
Expand Down
56 changes: 51 additions & 5 deletions generator/test/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import 'dart:io';
import 'package:build_test/build_test.dart';
import 'package:dart_style/dart_style.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:realm_generator/realm_generator.dart';
import 'package:test/test.dart';

final _formatter = DartFormatter(lineEnding: '\n');

// TODO: add meta package to pubspec.yaml
//@isTest
/// Used to test both correct an erroneous compilation.
/// [source] can be a [File] or a [String].
/// [matcher] can be a [File], [String] or a [Matcher].
/// Both expected and actual output will be formatted with [DartFormatter].
@isTest
void testCompile(String description, dynamic source, dynamic matcher, {dynamic skip, void Function(LogRecord)? onLog}) {
if (source is Iterable) {
testCompileMany(description, source, matcher);
return;
}

final assetName = source is File ? source.path : 'source.dart';
source = source is File ? source.readAsStringSync() : source;
if (source is! String) throw ArgumentError.value(source, 'source');
Expand All @@ -20,8 +29,6 @@ void testCompile(String description, dynamic source, dynamic matcher, {dynamic s
final source = _formatter.format(matcher);
matcher = completion(equals(source));
}
matcher ??= completes; // fallback

if (matcher is! Matcher) throw ArgumentError.value(matcher, 'matcher');

test(description, () async {
Expand All @@ -37,10 +44,49 @@ void testCompile(String description, dynamic source, dynamic matcher, {dynamic s
return _formatter.format(String.fromCharCodes(writer.assets.entries.single.value));
}

await expectLater(generate(), matcher);
expect(generate(), matcher);
}, skip: skip);
}

@isTest
void testCompileMany(String description, Iterable<dynamic> sources, dynamic matcher) async {
final inputs = switch (sources) {
Iterable<File> files => files.map((file) {
return ('pkg|${file.path}', _formatter.format(file.readAsStringSync()));
}),
Iterable<String> strings => strings.indexed.map((x) {
final (index, text) = x;
return ('pkg|source_$index.dart', _formatter.format(text));
}),
_ => throw ArgumentError.value(sources, 'sources'),
};

matcher = switch (matcher) {
Matcher m => m,
Iterable<String> strings => completion(equals(strings.map((e) => _formatter.format(e)))),
Iterable<File> files => completion(files.map((x) => _formatter.format(x.readAsStringSync()))),
_ => throw ArgumentError.value(matcher, 'matcher'),
};

test(description, () {
generate() async {
final writer = InMemoryAssetWriter();
await testBuilder(
generateRealmObjects(),
Map<String, Object>.fromEntries(inputs.map((x) {
final (id, source) = x;
return MapEntry(id, source);
})),
writer: writer,
reader: await PackageAssetReader.currentIsolate(),
);
return writer.assets.values.map((charCodes) => String.fromCharCodes(charCodes));
}

expect(generate(), matcher);
});
}

final _endOfLine = RegExp(r'\r\n?|\n');

extension StringX on String {
Expand Down

0 comments on commit 5fb4f9e

Please sign in to comment.