-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageTests.hx
More file actions
134 lines (116 loc) · 2.46 KB
/
LanguageTests.hx
File metadata and controls
134 lines (116 loc) · 2.46 KB
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
132
133
134
package;
import Main.Colors;
import Main.ColorValues;
using Std;
class LanguageTests {
var t:Main;
static final STATIC_ARR:Array<Int> = [];
static var STATIC_COUNTER = 0;
final arr:Array<Int> = [];
var counter = 0;
public function new(main:Main) {
t = main;
}
public function run() {
t.section("ForIn");
checkForIn();
t.section("ForRange");
checkForRange();
t.section("ForShort");
checkForShort();
t.section("FloatToInt");
checkFloatToInt();
t.section("Vars");
checkVars();
t.section("Statics");
checkStatics();
t.section("Enum");
checkEnum();
t.section("AbstractEnum");
checkAbstractEnum();
t.section("Arrow");
checkArrow();
t.section("TryCatch");
checkTryCatch();
t.section("StringEscape");
checkStringEscape();
}
function checkForIn() {
var a = [1, 2, 3];
var sum = 0;
for (i in a) {
sum += i;
}
t.intEquals(6, sum, "for-in sum");
}
function checkForRange() {
var count = 0;
for (i in 0...200) {
count++;
}
t.intEquals(200, count, "for range 200");
}
function checkForShort() {
var count = 0;
for (i in 0...3) {
count++;
}
t.intEquals(3, count, "for range 3");
}
function checkFloatToInt() {
var f = 1.9;
var i = f.int();
t.intEquals(1, i, "float 1.9 to int");
}
function checkVars() {
counter++;
arr.push(1);
t.intEquals(1, counter, "instance counter");
t.intEquals(1, arr.length, "instance array length");
}
function checkStatics() {
STATIC_COUNTER++;
STATIC_ARR.push(100);
t.intEquals(1, STATIC_COUNTER, "static counter");
t.intEquals(1, STATIC_ARR.length, "static array length");
}
function checkEnum() {
var color:Colors = Red;
var name = "";
switch color {
case Red:
name = "red";
case Green:
name = "green";
case Blue:
name = "blue";
}
t.stringEquals("red", name, "enum switch Red");
}
function checkAbstractEnum() {
var colorValue:ColorValues = Blue;
var val:Int = colorValue;
t.intEquals(255, val, "abstract enum Blue = 0x0000ff");
}
function checkArrow() {
final add = (a:Int, b:Int) -> a + b;
var result = add(1, 2);
t.intEquals(3, result, "arrow function 1+2");
}
function checkTryCatch() {
var caught = false;
try {
throw 'Error';
} catch (e:Dynamic) {
caught = true;
}
var wasCaught = caught;
t.isTrue(wasCaught, "exception caught");
}
function checkStringEscape() {
var str = "Hello\nWorld";
var len = str.length;
var lenOk = len > 0;
t.isTrue(lenOk, "escaped string not empty");
}
}