-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lx
131 lines (111 loc) · 2.89 KB
/
test.lx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
fun Test(name, assertion) {
print name + " => " + assertion();
}
fun assertEq(a, b) {
fun inner() {
return a == b;
}
return inner;
}
fun bool(a) {
if(a) {
return true;
}
return false;
}
fun assertTrue(a) {
return assertEq(bool(a), true);
}
var arithmetic = 1 + 2 - 3 * 4;
Test("Basic arithmetic", assertEq(arithmetic, -9));
var conditionExpected;
if ("string is true") {
conditionExpected = true;
}
Test("Basic condition", assertEq(conditionExpected, true));
var global = "global";
{
var local = "local";
Test("Access global from scope", assertEq(global, "global"));
Test("Access local from scope", assertEq(local, "local"));
}
var whileCounter = 1;
while (whileCounter < 5) {whileCounter = whileCounter + 1;}
Test("While loop", assertEq(whileCounter, 5));
var forCounter;
for (forCounter = 0; forCounter < 3; forCounter = forCounter + 1 ) { true;}
Test("For loop", assertEq(forCounter, 3));
Test("Native function", assertTrue(clock()));
var closureSetter;
var closureFirst;
var closureSecond;
fun PoorClass() {
var first_ = "first";
var second_ = "second";
fun set(param) { second_ = param; }
fun first() {return first_;}
fun second() {return second_;}
fun get() {
print first;
print second;
print third;
}
closureSetter = set;
closureFirst = first;
closureSecond = second;
}
PoorClass();
Test("PoorClass: before update", assertEq(closureSecond(), "second"));
closureSetter("third");
Test("PoorClass: after update", assertEq(closureSecond(), "third"));
Test("PoorClass: static getter ", assertEq(closureFirst(), "first"));
class TestClass {
testMethod() { return "testMethod"; }
setField(value) { this.field = value; }
getField() { return this.field; }
}
var TestInstance = TestClass();
TestInstance.testField = "testValue";
Test("Set class property", assertEq(TestInstance.testField, "testValue"));
Test("Call class method", assertEq(TestInstance.testMethod(), "testMethod"));
TestInstance.setField("setField");
Test("Class this", assertEq(TestInstance.getField(), "setField"));
class Initialized {
init(value) {
this.field = value;
}
}
Test("Class initializer", assertEq(Initialized("value").field, "value"));
class FieldInvoke {
init() {
fun f() {
return "invoke";
}
this.field = f;
}
}
var fi = FieldInvoke();
Test("Class Field handling method", assertEq(fi.field(), "invoke"));
class ChainCall {
init() {
class Inner {
init() {
this.inner = "inner";
}
}
this.outer = Inner();
}
}
var chain = ChainCall();
Test("Class chain call", assertEq(chain.outer.inner, "inner"));
class Parent {
parent() {
return "parent";
}
}
class Child < Parent {
child() {
return super.parent();
}
}
Test("Class inheritance", assertEq(Child().child(), "parent"));