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
external
folder. - Run the
_build.command
script to get aSourcery.app
. - Build the
CodeGenTests
target 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.xcodeproj
in Xcode. - Duplicate one of the
ToolTestProjectN
command line tool targets and increase to next highest numberN
. - In
RunToolTests.sh
at 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.swift
source file (replaceN
again) 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.swift
to theToolTestProjectN
command 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 a file named Entity.generatedN.swift
, the model file against a modelN.json
and such.
For reference, all of the command line targets have these settings:
- Add
-framework ObjectBox
to the "Other Linker Flags" - Add
@executable_path/../../../
and$(TOOLCHAIN_DIR)/usr/lib/swift/macosx
to the "Runpath Search Paths" - Add the
main.swift
file to the Compile sources build phase, it calls the main() function of theToolTestProjectN.swift
file.