Calls and Functions 24
This commit is contained in:
parent
2cd7a44bc7
commit
a45f6a9e17
18 changed files with 550 additions and 285 deletions
29
test/all.d
29
test/all.d
|
|
@ -8,27 +8,36 @@ import std.string, std.format;
|
|||
import std.algorithm, std.range;
|
||||
|
||||
void main(){
|
||||
|
||||
"./test/func.lox".match("-1\n6\n1\n");
|
||||
"./test/bigsum.lox".match("125250\n");
|
||||
"./test/biglocals.lox".match("125250\n");
|
||||
"./test/simplescope.lox".match("abc\nabd\n");
|
||||
"./test/for.lox".match("1 2 1 2 3 ".replace(' ', '\n'));
|
||||
"./test/ifelse.lox".match("a1 b2 c3 ".replace(' ', '\n'));
|
||||
"./test/while.lox".match("1 2 3 2 1 0 1 1 2 3 2 1 0 1 ".replace(' ', '\n'));
|
||||
|
||||
"./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/closure.lox".match("1\n2\n"); */
|
||||
"./test/scope.lox".match("global first first second first ".replace(' ', '\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/super.lox".match("Fry until golden brown.\nPipe full of custard and coat with chocolate.\nA method\n");
|
||||
/* "./test/fib_closure.lox".match(fib(34)); */
|
||||
/* "./test/class.lox".match("The German chocolate cake is delicious!\n"); */
|
||||
/* "./test/super.lox".match("Fry until golden brown.\nPipe full of custard and coat with chocolate.\nA method\n"); */
|
||||
|
||||
"./test/err/invalid_syntax.lox".shouldFail(RetVal.other);
|
||||
"./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/global_scope_return.lox".shouldFail(RetVal.other, "Can't return from top-level code");
|
||||
"./test/err/super_outside_class.lox".shouldFail(RetVal.other, "Can't use 'super' outside of a class");
|
||||
"./test/err/super_without_superclass.lox".shouldFail(RetVal.other, "Can't use 'super' in a class with no superclass");
|
||||
/* "./test/err/super_outside_class.lox".shouldFail(RetVal.other, "Can't use 'super' outside of a class"); */
|
||||
/* "./test/err/super_without_superclass.lox".shouldFail(RetVal.other, "Can't use 'super' in a class with no superclass"); */
|
||||
}
|
||||
|
||||
enum RetVal{
|
||||
success = 0, other = 1, runtime = 2
|
||||
success = 0, other = 65, runtime = 70
|
||||
}
|
||||
|
||||
string fib(uint n){
|
||||
|
|
@ -42,13 +51,13 @@ string fib(uint n){
|
|||
}
|
||||
return r;
|
||||
}
|
||||
auto run(string file) => [ "./lox", file ].execute;
|
||||
auto run(string file, Config.Flags f = Config.Flags.stderrPassThrough) => [ "./lox", file ].execute(null, Config(f));
|
||||
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;
|
||||
auto c = file.run(Config.Flags.none);
|
||||
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));
|
||||
|
|
|
|||
13
test/func.lox
Normal file
13
test/func.lox
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
fun sub(a, b){
|
||||
print a - b;
|
||||
}
|
||||
|
||||
fun mul(a, b){
|
||||
sub(a, b);
|
||||
print a * b;
|
||||
sub(b, a);
|
||||
}
|
||||
|
||||
mul(2, 3);
|
||||
|
||||
|
|
@ -14,6 +14,4 @@ fun f(){
|
|||
}
|
||||
|
||||
f();
|
||||
print "";
|
||||
f();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue