This project is used to test code generation in ObjectBox's Sourcery-descended code generator. It will run the codegen over a given target in the project, then build and run the product. You can do whatever testing you need to do in the actual Swift code.
Important note: by passing the --debug-parsetree option to the generator in RunToolTests.sh
it generates non-random, stable UIDs. See runCLI() of objectbox-swift-generator/Sourcery/main.swift.
These are unlike (notably shorter) UIDs than are generated for a user project.
These tests require a copy of ObjectBox Swift Code Generator in a known location. To get that all set up, do the following:
- Locate the objectbox-swift-generator submodule in this repository's
externalfolder. - Run the
_build.commandscript to get aSourcery.app. - Build the
CodeGenTeststarget in the ObjectBox Xcode project (this step runs the tests using the built code generator)
Adding a test case requires the following steps:
-
Open
ToolTestProject.xcodeprojin Xcode. -
Duplicate one of the
ToolTestProjectNcommand line tool targets and increase to next highest numberN. -
In
RunToolTests.shat the bottom add a command to run the test (replace<N>with the chosen number):# If the code generator should succeed test_target_num "Test Name" <N> || ((FAIL++)) # If the code generator should fail fail_codegen_target_num "Test Name" <N> || ((FAIL++))
-
Add a
ToolTestProjectN.swiftsource file (replaceNagain) to the command line tool target. It should look like:import ObjectBox // TODO Add entity classes func main(_ args: [String]) throws -> Int32 { // TODO Add test code, may print on error or throw return 0 // on success return 1 // or any value > 0 on failure (make sure to print error details) }
-
If the code generator should succeed, add the generated
EntityInfo.generatedN.swiftto theToolTestProjectNcommand line tool target as well (so it's verified it compiles).
The RunToolTests.sh script will run the code generator and pass the "Test Name" as the first parameter. It will also
check the code generator output against files in the expected folder:
- (only if expected to fail) that generator output messages contain messages in a file named like
messages/messagesN.log, - the model file against a file named like
model/modelN.json, - the generated code against a file named like
entity-info/Entity.generatedN.swift, - a dump of the generator schema against a file named like
schema-dump/schemaDumpN.txt
Note: by default a test will copy the expected model file before running the generator (to avoid UIDs from changing).
To start with a different model file, add a file like model/modelN.before.json.
For reference, all of the command line targets have these settings:
- Add
-framework ObjectBoxto the "Other Linker Flags" - Add
@executable_path/../../../and$(TOOLCHAIN_DIR)/usr/lib/swift/macosxto the "Runpath Search Paths" - Add the
main.swiftfile to the Compile sources build phase, it calls the main() function of theToolTestProjectN.swiftfile.