313 lines
10 KiB
Plaintext
313 lines
10 KiB
Plaintext
#! @file testing.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the test runner: the `test` attribute marking what it runs, the
|
|
#! `fixture` trait for what a test is given ready, and the `tester` a test
|
|
#! checks with, whose `run` runs every test of the program, each in a copy of
|
|
#! the program of its own.
|
|
|
|
import std::io;
|
|
import system;
|
|
module std::testing;
|
|
|
|
#! Marks a function, or a constant holding one, as a test that `tester::run`
|
|
#! runs.
|
|
#!
|
|
#! A test is a `def(t: &mut tester): void`. It may also take a fixture after the
|
|
#! tester, as `def(t: &mut tester, dir: temp_dir): void`, which the runner sets
|
|
#! up for it and which closes where the test returns. A generic test is marked
|
|
#! through a constant holding an instance, as `@test const sorts_i64 :=
|
|
#! sorts::<i64>;`.
|
|
@usage{ on = target::functions | target::constants }
|
|
pub struct test : attribute {
|
|
#! What the report calls the test, which is its path when this is empty.
|
|
pub val name: strview = "";
|
|
#! Whether the test is left out, which the report counts.
|
|
pub val skip: bool = false;
|
|
}
|
|
|
|
#! What a test is given ready, as its parameter after the tester.
|
|
#!
|
|
#! The runner makes it with `setup` before the test runs, and a fixture that is
|
|
#! `owning` is closed where the test returns, however it returns.
|
|
#! @tparam T The type of the fixture, which is the type implementing it.
|
|
pub trait fixture<T> {
|
|
#! Makes the fixture, before the test taking it runs.
|
|
#! @param t The tester of the test, which a fixture that cannot be made
|
|
#! checks with.
|
|
#! @returns The fixture.
|
|
def setup(t: &mut tester): T;
|
|
}
|
|
|
|
#! What a test checks with, which counts what it checked and what failed.
|
|
pub struct tester {
|
|
pub val name: strview; #! What the report calls the test.
|
|
pub var checks: u64; #! How many checks it has made.
|
|
pub var failed: u64; #! How many of those failed.
|
|
}
|
|
|
|
#! Checks a condition, reporting it with where it is written and the values its
|
|
#! operands held when it does not hold, and going on either way.
|
|
#!
|
|
#! It gives whether the condition held, so a check that the rest of the test
|
|
#! needs is a guard: `if !t.expects(p != null) { return; }`.
|
|
#! @param this The tester of the test, which counts the check.
|
|
#! @param cond The condition to check.
|
|
#! @param text The condition as it is written, which the compiler fills in.
|
|
#! @param got What its operands held, which the compiler fills in.
|
|
#! @param at Where it is written, which the compiler fills in.
|
|
#! @returns Whether the condition held.
|
|
pub def tester::expects(
|
|
&mut this,
|
|
cond: bool,
|
|
text: strview = source_of(cond),
|
|
got: string = cond ? string::new(nothing {}) : values_of(cond),
|
|
at: site = call_site()
|
|
): bool {
|
|
this.checks++;
|
|
return true if cond;
|
|
|
|
this.failed++;
|
|
val held := got.span;
|
|
val why := held.length != 0 ? $" with {held}" : "";
|
|
println($" {at.path}({at.line}:{at.column}): \"{text}\"{why}");
|
|
return false;
|
|
}
|
|
|
|
#! Makes the fixture a test takes, which the runner does before the test runs.
|
|
#! @tparam F The type of the fixture.
|
|
#! @param t The tester of the test.
|
|
#! @returns The fixture, which `setup` made.
|
|
pub def tester::prepare<F>(t: &mut tester): F where F: fixture<F> {
|
|
return F::setup(t);
|
|
}
|
|
|
|
#! Runs every test of the program, one after another, each in a copy of the
|
|
#! program of its own, so that one that faults, as by indexing past the end of
|
|
#! an array, fails alone and the rest still run. Each is reported as it
|
|
#! finishes, then how many passed, failed, and were skipped.
|
|
#!
|
|
#! The arguments may say `--only TEXT`, which runs the tests whose name holds
|
|
#! the text, and `--here`, which runs them in this program rather than in copies
|
|
#! of it, as a debugger wants, so the first fault stops them all.
|
|
#!
|
|
#! Windows cannot copy a program as it runs, so there each copy is the program
|
|
#! started again, with the same command line, and with `SAKURA_TEST_ONE` naming
|
|
#! the test it runs, which it runs alone, reporting nothing, then stops. What the
|
|
#! program does before calling this, each copy does again.
|
|
#! @param args The arguments the program was given.
|
|
#! @returns The status to stop with: zero when no test failed.
|
|
pub def tester::run(args: span<strview>): i32 {
|
|
var tally := tester::tally { only = "", here = false };
|
|
tally.read(args);
|
|
when os == windows {
|
|
alone = alone_named();
|
|
}
|
|
|
|
for const d in declarations() {
|
|
if const attached::<test>(d).length == 0 { continue; }
|
|
if const d.generic {
|
|
compile_error(
|
|
"""
|
|
A test is not generic; mark a constant holding an instance, as
|
|
`@test const sorts_i64 := sorts::<i64>;`.
|
|
""",
|
|
d.at
|
|
);
|
|
}
|
|
|
|
val marked := attached::<test>(d)[0];
|
|
val name := marked.name.length != 0 ? marked.name : d.path;
|
|
continue if !tally.wants(name);
|
|
|
|
if marked.skip {
|
|
tally.skip(name);
|
|
continue;
|
|
}
|
|
|
|
tally.begin(name);
|
|
val child := tally.here ? 0 : copy_running(name);
|
|
if child < 0 {
|
|
tally.ended(name, false, "a copy of the program could not be started");
|
|
continue;
|
|
}
|
|
|
|
if child == 0 {
|
|
var t := tester { name = name };
|
|
if const value_of(d) is def(&mut tester): void {
|
|
value_of(d)(&mut t);
|
|
}
|
|
else if const d.kind == target::functions && params_of(d).length == 2 {
|
|
if const !(value_of(d) is def(&mut tester, type_of(params_of(d)[1])): void) {
|
|
compile_error("A test takes the tester first, as `def(t: &mut tester, f: F): void`.", d.at);
|
|
}
|
|
if const type_of(params_of(d)[1]) is not fixture<type_of(params_of(d)[1])> {
|
|
compile_error("What a test takes after the tester is a fixture, which implements `fixture<F>`.", d.at);
|
|
}
|
|
value_of(d)(&mut t, tester::prepare::<type_of(params_of(d)[1])>(&mut t));
|
|
}
|
|
else {
|
|
compile_error("A test is a `def(t: &mut tester): void`, or also takes a fixture after the tester.", d.at);
|
|
}
|
|
|
|
if !tally.here {
|
|
exit(t.failed == 0 ? 0 : 1);
|
|
}
|
|
tally.ended(name, t.failed == 0, "");
|
|
continue;
|
|
}
|
|
tally.waited(name, child);
|
|
}
|
|
return tally.finish();
|
|
}
|
|
|
|
# What the runner counts and was asked, and how it reports each test.
|
|
struct tester::tally {
|
|
val only: strview;
|
|
val here: bool;
|
|
var passed: u64;
|
|
var failed: u64;
|
|
var skipped: u64;
|
|
}
|
|
|
|
def tester::tally::read(&mut this, args: span<strview>): void {
|
|
for i in 0..args.length {
|
|
if args[i] == "--here" {
|
|
this = tester::tally { only = this.only, here = true };
|
|
}
|
|
else if args[i] == "--only" && i + 1 < args.length {
|
|
this = tester::tally { only = args[i + 1], here = this.here };
|
|
}
|
|
}
|
|
}
|
|
|
|
# Whether a test's name holds the text `--only` asked for, which every name holds
|
|
# when it asked for none. A copy of the program on Windows wants only the test
|
|
# it runs.
|
|
def tester::tally::wants(&this, name: strview): bool {
|
|
when os == windows {
|
|
if alone.length != 0 {
|
|
return name == alone;
|
|
}
|
|
}
|
|
if this.only.length == 0 {
|
|
return true;
|
|
}
|
|
if this.only.length > name.length {
|
|
return false;
|
|
}
|
|
for at in 0..name.length - this.only.length + 1 {
|
|
var same := true;
|
|
for i in 0..this.only.length {
|
|
if name[at + i] != this.only[i] {
|
|
same = false;
|
|
break;
|
|
}
|
|
}
|
|
if same {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
def tester::tally::begin(&this, name: strview): void {
|
|
when os == windows {
|
|
return if alone.length != 0;
|
|
}
|
|
println($"[ RUN ] {name}");
|
|
}
|
|
|
|
def tester::tally::skip(&mut this, name: strview): void {
|
|
this.skipped++;
|
|
println($"[ SKIP ] {name}");
|
|
}
|
|
|
|
def tester::tally::ended(&mut this, name: strview, passed: bool, why: strview): void {
|
|
if passed {
|
|
this.passed++;
|
|
println($"[ OK ] {name}");
|
|
return;
|
|
}
|
|
|
|
this.failed++;
|
|
if why.length == 0 {
|
|
println($"[ FAIL ] {name}");
|
|
return;
|
|
}
|
|
println($"[ FAIL ] {name}: {why}");
|
|
}
|
|
|
|
# Waits for the copy running a test, which passed when it exited with zero.
|
|
def tester::tally::waited(&mut this, name: strview, child: i64): void {
|
|
var status: process_status;
|
|
var result := wait(child, &mut status);
|
|
while result < 0 && error_of(result) == io_error::interrupted {
|
|
result = wait(child, &mut status);
|
|
}
|
|
if result < 0 {
|
|
this.ended(name, false, "the copy running it could not be waited for");
|
|
return;
|
|
}
|
|
if !status.exited {
|
|
this.ended(name, false, $"stopped by signal {status.signal}".span);
|
|
return;
|
|
}
|
|
if status.code == 0 {
|
|
this.ended(name, true, "");
|
|
return;
|
|
}
|
|
this.ended(name, false, status.code == 1 ? "" : $"exited with {status.code}".span);
|
|
}
|
|
|
|
def tester::tally::finish(&this): i32 {
|
|
# A copy of the program on Windows stops as its test ends, so one that gets
|
|
# here found no test of the name it was given.
|
|
when os == windows {
|
|
if alone.length != 0 {
|
|
fd::stderr.put($"no test is named `{alone}`\n");
|
|
return 2;
|
|
}
|
|
}
|
|
println($"{this.passed} passed, {this.failed} failed, {this.skipped} skipped");
|
|
return this.failed == 0 ? 0 : 1;
|
|
}
|
|
|
|
when os == windows {
|
|
# The variable naming the test a copy of the program runs alone.
|
|
val alone_variable: strview = "SAKURA_TEST_ONE";
|
|
|
|
# The test this program runs alone, where it is a copy of the program started
|
|
# for it, or nothing.
|
|
var alone: strview;
|
|
|
|
# The test the variable names, or nothing where the program has none.
|
|
def alone_named(): strview {
|
|
for each in environment() {
|
|
if each.length > alone_variable.length && each[alone_variable.length] == '='
|
|
&& each.starts_with(alone_variable) {
|
|
return each.substr(alone_variable.length + 1, each.length - alone_variable.length - 1);
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
# Starts the copy of the program a test runs in, giving its number, or zero
|
|
# in a copy, which runs its test itself.
|
|
def copy_running(name: strview): i64 {
|
|
if alone.length != 0 {
|
|
return 0;
|
|
}
|
|
return respawn($"{alone_variable}={name}".span, fd::stdin, fd::stdout, fd::stderr);
|
|
}
|
|
}
|
|
else {
|
|
# Starts the copy of the program a test runs in, giving its number, or zero
|
|
# in the copy.
|
|
def copy_running(name: strview): i64 {
|
|
return fork();
|
|
}
|
|
}
|