Testing Sprig projects
sprig test runs ordinary Sprig programs. It adds no test keyword, annotation, function discovery or reflection rule. A test succeeds when it checks, compiles with javac, and exits with status 0 in a child JVM. Use the existing assert function or throw Error to fail a test.
sprig resolve
sprig test [PATH] [--filter TEXT] [--classpath PATH] [--json] [--offline]Run the command inside a sprig.toml project or pass a project path. A current sprig.lock is required. With no PATH, the runner discovers tests/**/*.spr. You can pass the project directory, a subdirectory, or one .spr file. The runner sorts names lexically relative to tests/; --filter selects names containing the supplied text. It uses the project's normal source imports, Sprig dependencies, locked Maven classpath and offline cache rules. sprig test never resolves or updates a lockfile for you.
--classpath PATH adds host Java JARs or output directories to compilation and test execution; repeat the option or pass a platform-separated classpath. Relative paths use the command's working directory. The Gradle plugin supplies the selected source set's real compile classpath so Sprig tests can check Java bridge types without building a second classpath by hand.
Runtime tests
For example, tests/table_squares.spr can contain a table without extra test syntax:
class Case:
let input: Int
let expected: Int
let cases: List[Case] = [Case(input=2, expected=4), Case(input=7, expected=49)]
for item in cases:
assert(item.input * item.input == item.expected)Each file gets a fresh JVM and a unique temporary directory. The child runs with the project root as its working directory and receives SPRIG_TEST_TMPDIR; the runner removes that directory after the test. A test that exits nonzero, raises an uncaught error, fails checking or javac, or runs longer than 30 seconds fails. Tests currently run sequentially.
@std/test.spr provides the test-specific helpers:
import "@std/test.spr" as testing
import "@std/files.spr" as files
let path = files.join(testing.temp_dir(), "note.txt")
files.write_utf8(path, "ready")
assert(files.read_utf8(path) == "ready")
let child = testing.run_process(["java", "--version"])
assert(child.exit_code == 0)
assert(child.stdout.length() + child.stderr.length() > 0)temp_dir() -> String throws Error fails clearly outside sprig test. run_process(command: List[String]) -> ProcessResult throws Error passes an argv vector directly to the operating system, with no shell interpretation. ProcessResult has exit_code: Int32, stdout: String, and stderr: String. It captures the two output streams separately as UTF-8. A nonzero child exit is a result value; an empty command, failure to start, invalid UTF-8 output, or a 30-second child-process timeout raises Error. Child processes inherit the test's environment and project working directory.
A failed assert reports only that an assertion failed. equal_int, equal_bool and equal_text compare two values and, when they differ, throw an Error that names both, so the test report shows what went wrong:
import "@std/test.spr" as testing
let quantity = 3
testing.equal_int(2 + 2, 4, "sum")
testing.equal_text("tea x" + quantity, "tea x2", "order line")The second check fails with order line: expected "tea x2", got "tea x3". The third argument says which value was checked. See the standard library for the exact text format.
A thrown Error ends the program, so the checks above stop at the first failure. To run several named checks and see every failure, give each one a name and a lambda, and finish with the tally:
import "@std/test.spr" as testing
testing.check("adds", fn() => testing.equal_int(2 + 2, 4, "sum"))
testing.check("joins", fn() => testing.equal_text("tea x" + 3, "tea x3", "order line"))
testing.check_error("rejects text", fn() => parse("x"))
testing.finish()check prints ok adds or FAIL adds: message and goes on to the next check; check_error[T] passes when the body throws Error (the body may return a value, and T is the type of the lambda's result); finish prints N passed, M failed and ends the program with status 1 when a check failed, which sprig test reports as a failed program. A lambda may call any function that throws Error, so a check body can be the code under test itself. testing.run_process is process.run from @std/process.spr under its earlier name; both run an argv vector with no shell.
Testing a program: keep main.spr thin
A test imports the module it checks with a relative path, such as import "../src/app.spr" as app. Importing a module runs its top-level statements once, so a test that imports ../src/main.spr first runs the whole program with the test's empty argument list: it prints, writes files and fails as it would on the command line. If the program calls process.exit, the test ends there; with status 0 it passes without running a single check.
Keep src/main.spr to argument handling, and put the work in another module that main.spr and the tests both import:
src/main.spr reads the arguments and passes them to app.run
src/app.spr the work, as functions; no top-level statement acts
tests/app_test.spr imports ../src/app.spr and calls its functionssrc/main.spr is one statement:
import "@std/process.spr" as process
import "./app.spr" as app
process.exit(app.run(process.arguments()))src/app.spr takes the arguments as a parameter and returns the exit status, so a test can try any command line without starting a process:
func greeting(name: String) -> String:
return "Hello, " + name + "!"
# Runs one command line and returns the exit status.
func run(arguments: List[String]) -> Int:
if arguments.size() != 1:
print("usage: greet NAME")
return 2
print(greeting(arguments[0]))
return 0tests/app_test.spr:
import "@std/test.spr" as testing
import "../src/app.spr" as app
testing.check("greets", fn() => testing.equal_text(app.greeting("Ada"), "Hello, Ada!", "greeting"))
testing.check("needs a name", fn() => testing.equal_int(app.run([]), 2, "status"))
testing.finish()examples/tasks is a complete program with this layout; examples/blog, examples/crawler and examples/todo keep their work out of main.spr the same way.
Expected compiler failures
Place negative fixtures under tests/compile_fail/. Each .spr file needs a sibling .expect.toml file:
tests/compile_fail/nullable.spr
tests/compile_fail/nullable.expect.tomlcodes = ["SPR-TYPE-NULLABLE"]
exact = trueThe runner calls the Sprig checker, without invoking javac or executing the fixture. Every listed code must occur among error diagnostics. By default, additional error codes are allowed. exact = true rejects additional error codes. Warnings are retained in the structured result but do not change the code comparison. Codes are stable; complete English messages are not matched. The expectation schema accepts only root codes (a nonempty array of unique SPR-* strings) and optional bare exact = true or false. Missing or malformed expectations are runner errors.
Output and exit status
Human output prints one PASS or FAIL line per file and a summary. Failures include the relevant compiler/runtime diagnostic, except a program's deliberate nonzero process exit: its output comes first and the exit status appears once. --json prints one object with schemaVersion, toolVersion, command, exitCode, summary, tests, and top-level diagnostics. Each test row has name, absolute path, mode (run or compile_fail), status, structured diagnostics, programOutput, and programErrorOutput; a failed row also has failure. Rows and diagnostics retain deterministic discovery/checking order. Program output and filesystem paths are the program's actual values and may vary if the program makes them vary. A test's standard input is empty, so process.read_line() returns null at once.
Exit 0 means every discovered test passed. Exit 1 means at least one program or expected-diagnostic comparison failed. Exit 2 means an invalid command, project/lock error, or malformed test expectation. The existing Python compiler regression harness remains useful for compiler implementation tests; this command is the product-facing package and application test runner.
See the runnable five-case project in examples/test_runner/.
