Skip to content
11 changes: 4 additions & 7 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ jobs:
image: google/dart:latest
steps:
- uses: actions/checkout@v1
- name: Install dependencies
working-directory: generator
run: pub get
# Disabled temporarily, pending rework in #37
# - name: Run tests
# working-directory: generator
# run: pub run test
- name: Install ObjectBox C-API
run: ./install.sh
- name: Run tests
run: ./generator/test.sh

lib:
needs: generator
Expand Down
6 changes: 5 additions & 1 deletion generator/lib/src/code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CodeBuilder extends Builder {
final entities = List<ModelEntity>();
for (final entitiesList in files.values) {
for (final entityMap in entitiesList) {
entities.add(ModelEntity.fromMap(entityMap));
entities.add(ModelEntity.fromMap(entityMap, check: false));
}
}
entities.sort((a, b) => a.name.compareTo(b.name));
Expand Down Expand Up @@ -68,6 +68,7 @@ class CodeBuilder extends Builder {

// merge existing model and annotated model that was just read, then write new final model to file
merge(model, entities);
model.validate();

// write model info
// Can't use output, it's removed before each build, though writing to FS is explicitly forbidden by package:build.
Expand Down Expand Up @@ -114,6 +115,7 @@ class CodeBuilder extends Builder {
log.info("Found new property ${entity.name}.${prop.name}");
entity.addProperty(prop);
} else {
propInModel.name = prop.name;
propInModel.type = prop.type;
propInModel.flags = prop.flags;
}
Expand All @@ -131,6 +133,8 @@ class CodeBuilder extends Builder {
return createdEntity.id;
}

entityInModel.name = entity.name;

// here, the entity was found already and entityInModel and readEntity might differ, i.e. conflicts need to be resolved, so merge all properties first
entity.properties.forEach((p) => mergeProperty(entityInModel, p));

Expand Down
2 changes: 1 addition & 1 deletion generator/lib/src/code_chunks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CodeChunks {
import '${imports.join("';\n import '")}';

ModelDefinition getObjectBoxModel() {
final model = ModelInfo.fromMap(${JsonEncoder().convert(model.toMap(forCodeGen: true))});
final model = ModelInfo.fromMap(${JsonEncoder().convert(model.toMap(forCodeGen: true))}, check: false);

final bindings = Map<Type, EntityDefinition>();
${model.entities.map((entity) => "bindings[${entity.name}] = ${entityBinding(entity)};").join("\n")}
Expand Down
5 changes: 0 additions & 5 deletions generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ dependencies:
build: ^1.0.0
source_gen: ^0.9.0
analyzer: ">=0.35.0 <0.100.0"

dev_dependencies:
build_runner: ^1.0.0
test: ^1.0.0
glob: ^1.1.0
build_resolvers: ^1.0.0

# NOTE: remove before publishing
dependency_overrides:
Expand Down
48 changes: 48 additions & 0 deletions generator/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
set -euo pipefail

myDir=$(dirname "$0")

function runTestFile() {
file="${1}.dart"
if [ -f "${file}" ]; then
# execute "N-pre.dart" file if it exists
if [[ "${1}" != "0" && -f "${1}-pre.dart" ]]; then
echo "Executing ${1}-pre.dart"
dart "${1}-pre.dart"
fi

# build before each step, except for "0.dart"
if [ "${1}" != "0" ]; then
echo "Running build_runner before ${file}"
pub run build_runner build
fi
echo "Running ${file}"
pub run test "${file}"
fi
}

function runTestCase() {
testCase=$1
echo "Testing ${testCase}"

# Clean up beforehand by removing all ignored files
git clean -fXd "${testCase}"

cd "${testCase}"

pub get
for i in {0..9}; do
runTestFile $i
done

cd -
}

if [ $# -eq 0 ]; then
for testCase in "${myDir}"/test/*/; do
runTestCase "${testCase}"
done
else
runTestCase "${myDir}/test/$1"
fi
9 changes: 9 additions & 0 deletions generator/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Each subdirectory contains a test case, a complete dart package (with some shared files: pubspec.yaml, test_env.dart, ...).
* before a test starts, it's content is cleaned by running `git clean -fXd directory-path`, i.e. removing all ignored files
* each directory may contain `[0-9].dart` test files which are executed in ascending order using `pub run test N.dart`
* `pub run build_runner build` is executed before each test file, except `0.dart`
* you can skip any number (including `0.dart`) - if the file is not there, the test.sh will just skip it
* tests are allowed to make changes to the file system in their directory and these are preserved between test files,
but not removed between test-case runs
* additionally, there may be `[0-9]-pre.dart` command-line apps, which are executed `dart N-pre.dart` - these may be
used to further prepare the environment **before** code generation for the step `N` is issued and `N.dart` test is run
2 changes: 2 additions & 0 deletions generator/test/basics/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# start with an empty project, without a objectbox-model.json
objectbox-model.json
12 changes: 12 additions & 0 deletions generator/test/basics/0.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'dart:io';
import 'package:test/test.dart';
import '../test_env.dart';

void main() {
// this is actually a meta test - that `git clean -fX` is run
test("project must be clean before generating the code", () {
expect(TestEnv.dir.existsSync(), false);
expect(File("lib/objectbox.g.dart").existsSync(), false);
expect(File("lib/objectbox-model.json").existsSync(), false);
});
}
27 changes: 27 additions & 0 deletions generator/test/basics/1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:io';
import 'lib/lib.dart';
import 'lib/objectbox.g.dart';
import 'package:test/test.dart';
import '../test_env.dart';
import '../common.dart';

void main() {
TestEnv<A> env;
ModelDefinition defs = getObjectBoxModel();

setUp(() {
env = TestEnv<A>(defs);
});

tearDown(() {
env.close();
});

commonModelTests(defs, readModelJson("lib"));

test("project must be generated properly", () {
expect(TestEnv.dir.existsSync(), true);
expect(File("lib/objectbox.g.dart").existsSync(), true);
expect(File("lib/objectbox-model.json").existsSync(), true);
});
}
20 changes: 20 additions & 0 deletions generator/test/basics/lib/lib.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:objectbox/objectbox.dart';
import 'objectbox.g.dart';
export 'other.dart';

@Entity()
class A {
@Id()
int id;
String text;

A();
}

@Entity()
class B {
@Id() // TODO support id without an annotation
int id;

B();
}
9 changes: 9 additions & 0 deletions generator/test/basics/lib/other.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:objectbox/objectbox.dart';

@Entity()
class C {
@Id()
int id;

C();
}
1 change: 1 addition & 0 deletions generator/test/basics/pubspec.yaml
8 changes: 0 additions & 8 deletions generator/test/cases/multiple_entities/a.dart_testcase

This file was deleted.

34 changes: 0 additions & 34 deletions generator/test/cases/multiple_entities/a.g.dart_expected

This file was deleted.

8 changes: 0 additions & 8 deletions generator/test/cases/multiple_entities/b.dart_testcase

This file was deleted.

34 changes: 0 additions & 34 deletions generator/test/cases/multiple_entities/b.g.dart_expected

This file was deleted.

This file was deleted.

36 changes: 0 additions & 36 deletions generator/test/cases/single_entity/objectbox-model.json_expected

This file was deleted.

Loading