lox-d/test/all.d
2025-06-02 16:04:04 +00:00

49 lines
2 KiB
D
Executable file

#!/bin/env rdmd
import std.process;
import std.concurrency;
import std.conv;
import std.string, std.format;
import std.algorithm, std.range;
void main(){
auto scheduler = new ThreadScheduler();
scheduler.spawn((){ "./test/closure.lox".run.output.match("1\n2\n"); });
scheduler.spawn((){ "./test/scope.lox".run.output.match("global first first second first ".replace(' ', '\n').repeat(2).join("\n")); });
scheduler.spawn((){ "./test/fib_for.lox".run.output.match(fib(6765)); });
scheduler.spawn((){ "./test/fib_recursive.lox".run.output.match(fib(34)); });
scheduler.spawn((){ "./test/fib_closure.lox".run.output.match(fib(34)); });
scheduler.spawn((){ "./test/err/already_defined.lox".shouldFail(RetVal.other, "Already a variable with this name"); });
scheduler.spawn((){ "./test/err/undefined_var.lox".shouldFail(RetVal.runtime, "Undefined variable"); });
scheduler.spawn((){ "./test/err/self_ref_vardecl.lox".shouldFail(RetVal.runtime, "Undefined variable"); });
scheduler.spawn((){ "./test/err/invalid_syntax.lox".shouldFail(RetVal.other); });
scheduler.spawn((){ "./test/err/global_scope_return.lox".shouldFail(RetVal.other, "Can't return from top-level code"); });
}
enum RetVal{
success = 0, other = 1, runtime = 2
}
string fib(uint n){
string r = "";
double a = 0;
double temp;
for(double b = 1; a <= n; b = temp + b){
r ~= a.to!string ~ "\n";
temp = a;
a = b;
}
return r;
}
auto run(string file) => [ "./lox", file ].execute;
void match(string res, string correct){
assert(res == correct, "Match failed\n-- Got --\n%s\n-- Expected --\n%s".format(res, correct));
}
void shouldFail(string file, int code = 1, string msg = null){
auto c = file.run;
assert(c.status == code, "Expected %s to fail with code %d but got %d".format(file, code, c.status));
assert(!msg || c.output.toLower.indexOf(msg.toLower) >= 0, "ShouldFail %s failed\n-- Got --\n%s\n-- Expected --\n%s".format(file, c.output, msg));
assert(c.output.indexOf("_Dmain") == -1, "ShouldFail %s got D exception\n%s".format(file, c.output));
}