Revised tests

This commit is contained in:
2026-05-30 10:53:21 +10:00
parent 75cb1dfa86
commit d194bff5fe
36 changed files with 266 additions and 199 deletions
@@ -0,0 +1,165 @@
//declare a variable with an initial value
var answer = 42;
//declare a variable without an initial value
var empty;
//assign a previously existing variable
answer = 6 * 9;
//access a variable
answer = answer + 1;
//compound assignments
answer += 5;
answer -= 5;
answer *= 9;
answer /= 2;
answer %= 10;
//equality checks
print 1 == 1; //true
print 1 != 1; //false
//comparison checks
print 1 < 2; //true
print "foo" > "bar"; //true
print 1 < 2; //true
print 1 > 2; //false
print 2 <= 2; //true
print 2 >= 2; //true
print 1 <= 2; //true
print 1 >= 2; //false
//logical checks
print true && true; //true
print true && false; //false
print false && true; //false
print false && false; //false
print true || true; //true
print true || false; //true
print false || true; //true
print false || false; //false
print !true; //false
print !false; //true
//logical AND short-circuits and chained assignments
{
var a = 1;
var b = 2;
var c = a + 1 && b + 2;
assert a == 1, "short circuit 1.1";
assert b == 2, "short circuit 1.2";
assert c == 4, "short circuit 1.3";
}
{
var a = 1;
var b = 2;
var c = a = (a + 1) && b + 2;
assert a == 4, "short circuit 2.1";
assert b == 2, "short circuit 2.2";
assert c == 4, "short circuit 2.3";
}
{
var a = 1;
var b = 2;
var c = a = a + 1 && b + 2;
assert a == 4, "short circuit 3.1";
assert b == 2, "short circuit 3.2";
assert c == 4, "short circuit 3.3";
}
//logical OR short-circuits and chained assignments
{
var a = 1;
var b = 2;
var c = a + 1 || b + 2;
assert a == 1, "short circuit 4.1";
assert b == 2, "short circuit 4.2";
assert c == 2, "short circuit 4.3";
}
{
var a = 1;
var b = 2;
var c = a = (a + 1) || b + 2;
assert a == 2, "short circuit 5.1";
assert b == 2, "short circuit 5.2";
assert c == 2, "short circuit 5.3";
}
{
var a = 1;
var b = 2;
var c = a = a + 1 || b + 2;
assert a == 2, "short circuit 6.1";
assert b == 2, "short circuit 6.2";
assert c == 2, "short circuit 6.3";
}
//types
{
var a: Int;
var b: Int = 42;
a = 69;
b = 8891;
print a;
print b;
}
//constants
{
var c: Int const = 42;
print c;
}
//indexing
{
var s = "Hello" .. "world!";
print s[3, 3];
}
//increment & decrement (prefix)
{
var a = 42;
assert a == 42, "prefix increment & decrement 1.1";
assert ++a == 43, "prefix increment & decrement 1.2";
assert a == 43, "prefix increment & decrement 1.3";
assert --a == 42, "prefix increment & decrement 1.4";
assert a == 42, "prefix increment & decrement 1.5";
}
//increment & decrement (postfix)
{
var a = 42;
assert a == 42, "postfix increment & decrement 1.1";
assert a++ == 42, "postfix increment & decrement 1.2";
assert a == 43, "postfix increment & decrement 1.3";
assert a-- == 43, "postfix increment & decrement 1.4";
assert a == 42, "postfix increment & decrement 1.5";
print a;
}
{
//Yet Another Cosmic Joke
var a = null;
var i = 42;
print a;
}
@@ -0,0 +1,18 @@
//shadowing
var answer = 42;
assert answer == 42;
{
var answer = 7;
assert answer == 7;
}
assert answer == 42;
//rebinding
var question = 42;
assert question == 42;
{
var question = question;
assert question == 42;
}
assert question == 42;
@@ -0,0 +1 @@
//This file left intentionally blank
+90
View File
@@ -0,0 +1,90 @@
//NOTE: Not tested: null (neither true or false), Opaque, Any
{
//booleans
var value: Bool = true;
if (value) print "boolean";
else assert false, "boolean";
}
{
var value: Bool = false;
if (value) assert false, "boolean";
else print "boolean";
}
//integers
{
var value: Int = 42;
if (value) print "integer";
else assert false, "integer";
}
{
var value: Int = 0;
if (value) assert false, "integer";
else print "integer";
}
//floats
{
var value: Float = 42.8891;
if (value) print "float";
else assert false, "float";
}
{
var value: Float = 0;
if (value) assert false, "float";
else print "float";
}
//strings
{
var value: String = "foobar";
if (value) print "string";
else assert false, "string";
}
{
var value: String = ""; //empty string is truthy
if (value) print "string";
else assert false, "string";
}
//arrays
{
var value: Array = [1,2,3];
if (value) print "array";
else assert false, "array";
}
{
var value: Array = []; //empty array is truthy
if (value) print "array";
else assert false, "array";
}
//table
{
var value: Table = ["alpha":"bravo"];
if (value) print "table";
else assert false, "table";
}
{
var value: Table = [:]; //empty table is truthy
if (value) print "table";
else assert false, "table";
}
//function
{
fn identity(x) {
return x;
}
if (identity) print "function";
else assert false, "function";
}
//TODO: test library as API, would be cleaner than using asserts
+29
View File
@@ -0,0 +1,29 @@
//NOTE: typenames are now capitalized, to ensure they don't overlap with variable names
var bool: Bool = true;
print bool;
var int: Int = 42;
print int;
var float: Float = 3.1415;
print float;
var string: String = "Hello world";
print string;
var array: Array = [1,2,3];
print array;
var table: Table = ["alpha":1];
print table;
var function: Function;
print function;
var opaque: Opaque;
print opaque;
var any: Any;
print any;