53 lines
1.9 KiB
D
Executable file
53 lines
1.9 KiB
D
Executable file
#!/bin/env rdmd
|
|
|
|
import std.stdio;
|
|
import std.process;
|
|
import std.concurrency;
|
|
import std.conv;
|
|
import std.string, std.format;
|
|
import std.algorithm, std.range;
|
|
|
|
void main(){
|
|
"./test/ops.lox".match("1\n2\n3\n4\n5\n6\n7\ntrue\nfalse\ntrue\ntrue\nhello, world\n");
|
|
"./test/shortcircuit.lox".match("true\nAAAA!\nAAAA!\nAAAA?\n");
|
|
"./test/closure.lox".match("1\n2\n");
|
|
"./test/scope.lox".match("global first first second first ".replace(' ', '\n').repeat(2).join("\n"));
|
|
"./test/fib_for.lox".match(fib(6765));
|
|
"./test/fib_recursive.lox".match(fib(34));
|
|
"./test/fib_closure.lox".match(fib(34));
|
|
"./test/class.lox".match("The German chocolate cake is delicious!\n");
|
|
|
|
"./test/err/already_defined.lox".shouldFail(RetVal.other, "Already a variable with this name");
|
|
"./test/err/undefined_var.lox".shouldFail(RetVal.runtime, "Undefined variable");
|
|
"./test/err/self_ref_vardecl.lox".shouldFail(RetVal.runtime, "Undefined variable");
|
|
"./test/err/invalid_syntax.lox".shouldFail(RetVal.other);
|
|
"./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 file, string correct){
|
|
auto res = file.run.output;
|
|
assert(res == correct, "Match %s failed\n-- Got --\n%s\n-- Expected --\n%s".format(file, 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));
|
|
}
|
|
|