mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-17 15:54:07 +10:00
Moved tests from scripts/ to test/scripts/
This commit is contained in:
77
test/scripts/functions.toy
Normal file
77
test/scripts/functions.toy
Normal file
@@ -0,0 +1,77 @@
|
||||
//test function return
|
||||
fn testFourtyTwo() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
assert testFourtyTwo() == 42, "function returns failed";
|
||||
|
||||
|
||||
//test function parameters
|
||||
fn identity(x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
assert identity("hello world") == "hello world", "identity function failed";
|
||||
|
||||
|
||||
//test closures
|
||||
fn make() {
|
||||
var counter = 0;
|
||||
|
||||
fn count() {
|
||||
return ++counter;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
var tally = make();
|
||||
|
||||
assert tally() == 1 && tally() == 2, "Closures failed";
|
||||
|
||||
|
||||
//test closures self-capture
|
||||
fn capture(count: int) {
|
||||
if (count > 5) {
|
||||
return count;
|
||||
}
|
||||
|
||||
return capture(count + 1);
|
||||
}
|
||||
|
||||
assert capture(0) == 6, "Self capture failed";
|
||||
|
||||
|
||||
//test expressions as arguments
|
||||
fn argFn() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
fn outerFn(val) {
|
||||
assert val == 42, "expression as argument failed";
|
||||
}
|
||||
|
||||
outerFn(argFn());
|
||||
|
||||
|
||||
//test extra parameters
|
||||
fn extra(one, two, ...rest) {
|
||||
assert rest == ["three", "four", "five", "six", "seven"], "rest parameters failed";
|
||||
}
|
||||
|
||||
extra("one", "two", "three", "four", "five", "six", "seven");
|
||||
|
||||
|
||||
//test underscore functions
|
||||
fn _example(self, a, b, c) {
|
||||
assert a == "a", "underscore failed (a)";
|
||||
assert b == "b", "underscore failed (b)";
|
||||
assert c == "c", "underscore failed (c)";
|
||||
return self;
|
||||
}
|
||||
|
||||
assert "hello world".example("a", "b", "c") == "hello world", "underscore call failed";
|
||||
|
||||
|
||||
|
||||
print "All good";
|
||||
Reference in New Issue
Block a user