forked from HaxeFoundation/hscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.hx
112 lines (108 loc) · 4.03 KB
/
Test.hx
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
import hscript.Macro;
import haxe.unit.*;
class Test extends TestCase {
function assertScript(x,v:Dynamic,?vars : Dynamic,allowTypes=false) {
var p = new hscript.Parser();
p.allowTypes = allowTypes;
var program = p.parseString(x);
var bytes = hscript.Bytes.encode(program);
program = hscript.Bytes.decode(bytes);
var interp = new hscript.Interp();
if( vars != null )
for( v in Reflect.fields(vars) )
interp.variables.set(v,Reflect.field(vars,v));
var ret : Dynamic = interp.execute(program);
assertEquals(v, ret);
}
function test():Void {
assertScript("0",0);
assertScript("0xFF", 255);
#if !(php || python)
#if haxe3
assertScript("0xBFFFFFFF", 0xBFFFFFFF);
assertScript("0x7FFFFFFF", 0x7FFFFFFF);
#elseif !neko
assertScript("n(0xBFFFFFFF)", 0xBFFFFFFF, { n : haxe.Int32.toNativeInt });
assertScript("n(0x7FFFFFFF)", 0x7FFFFFFF, { n : haxe.Int32.toNativeInt } );
#end
#end
assertScript("-123",-123);
assertScript("- 123",-123);
assertScript("1.546",1.546);
assertScript(".545",.545);
assertScript("'bla'","bla");
assertScript("null",null);
assertScript("true",true);
assertScript("false",false);
assertScript("1 == 2",false);
assertScript("1.3 == 1.3",true);
assertScript("5 > 3",true);
assertScript("0 < 0",false);
assertScript("-1 <= -1",true);
assertScript("1 + 2",3);
assertScript("~545",-546);
assertScript("'abc' + 55","abc55");
assertScript("'abc' + 'de'","abcde");
assertScript("-1 + 2",1);
assertScript("1 / 5",0.2);
assertScript("3 * 2 + 5",11);
assertScript("3 * (2 + 5)",21);
assertScript("3 * 2 // + 5 \n + 6",12);
assertScript("3 /* 2\n */ + 5",8);
assertScript("[55,66,77][1]",66);
assertScript("var a = [55]; a[0] *= 2; a[0]",110);
assertScript("x",55,{ x : 55 });
assertScript("var y = 33; y",33);
assertScript("{ 1; 2; 3; }",3);
assertScript("{ var x = 0; } x",55,{ x : 55 });
assertScript("o.val",55,{ o : { val : 55 } });
assertScript("o.val",null,{ o : {} });
assertScript("var a = 1; a++",1);
assertScript("var a = 1; a++; a",2);
assertScript("var a = 1; ++a",2);
assertScript("var a = 1; a *= 3",3);
assertScript("a = b = 3; a + b",6);
assertScript("add(1,2)",3,{ add : function(x,y) return x + y });
assertScript("a.push(5); a.pop() + a.pop()",8,{ a : [3] });
assertScript("if( true ) 1 else 2",1);
assertScript("if( false ) 1 else 2",2);
assertScript("var t = 0; for( x in [1,2,3] ) t += x; t",6);
assertScript("var a = new Array(); for( x in 0...5 ) a[x] = x; a.join('-')","0-1-2-3-4");
assertScript("(function(a,b) return a + b)(4,5)",9);
assertScript("var y = 0; var add = function(a) y += a; add(5); add(3); y", 8);
assertScript("var a = [1,[2,[3,[4,null]]]]; var t = 0; while( a != null ) { t += a[0]; a = a[1]; }; t",10);
assertScript("var t = 0; for( x in 1...10 ) t += x; t", 45);
#if haxe3
assertScript("var t = 0; for( x in new IntIterator(1,10) ) t +=x; t", 45);
#else
assertScript("var t = 0; for( x in new IntIter(1,10) ) t +=x; t", 45);
#end
assertScript("var x = 1; try { var x = 66; throw 789; } catch( e : Dynamic ) e + x",790);
assertScript("var x = 1; var f = function(x) throw x; try f(55) catch( e : Dynamic ) e + x",56);
assertScript("var i=2; if( true ) --i; i",1);
assertScript("var i=0; if( i++ > 0 ) i=3; i",1);
assertScript("var a = 5/2; a",2.5);
assertScript("{ x = 3; x; }", 3);
assertScript("{ x : 3, y : {} }.x", 3);
assertScript("function bug(){ \n }\nbug().x", null);
assertScript("1 + 2 == 3", true);
assertScript("-2 == 3 - 5", true);
assertScript("var x=-3; x", -3);
assertScript("var a:Array<Dynamic>=[1,2,4]; a[2]", 4, null, true);
assertScript("/**/0", 0);
assertScript("x=1;x*=-2", -2);
}
static function main() {
var runner = new TestRunner();
runner.add(new Test());
var succeed = runner.run();
#if sys
Sys.exit(succeed ? 0 : 1);
#elseif flash
flash.system.System.exit(succeed ? 0 : 1);
#else
if (!succeed)
throw "failed";
#end
}
}