Repository navigation
Expand file tree
/
Copy pathTestHScript.hx
More file actions
592 lines (561 loc) · 21.7 KB
/
Copy pathTestHScript.hx
File metadata and controls
592 lines (561 loc) · 21.7 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import haxe.ds.EnumValueMap;
import haxe.ds.Option;
import hscript.Macro;
import hscript.Tools;
import hscript.Async;
import hscript.Printer;
import hscript.Checker;
import hscript.Parser;
import hscript.Interp;
import haxe.unit.*;
// TODO: tests regex
class TestHScript extends TestCase {
/**
* @param x Code to run
* @param v Expected return value
* @param vars Variables to use
* @param allowTypes Allow types parsing on script (default: false)
* @param pos `trace()` call information
*/
function assertScript(x:String, v:Dynamic, ?vars:Dynamic, allowTypes = false, ?pos:haxe.PosInfos) {
pos ??= assertPos;
var p = new hscript.Parser();
p.allowTypes = allowTypes;
p.allowMetadata = true;
var program = p.parseString(x);
var bytes = hscript.Bytes.encode(program, allowTypes);
program = hscript.Bytes.decode(bytes, allowTypes);
var interp = new hscript.Interp();
// #if js
// if( optimize ) interp = new hscript.JsInterp();
// #end
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, pos);
assertPos.lineNumber += 1;
}
/**
* @param code Code to run
* @param vars Variables to use
* @param allowTypes Allow types parsing on script (default: true)
* @param pos `trace()` call information
*/
function assertScriptRun(code:String, ?vars:Dynamic, ?allowTypes:Bool = true, ?pos:haxe.PosInfos) {
pos ??= assertPos;
var p = new hscript.Parser();
p.allowTypes = allowTypes;
p.allowMetadata = true;
var program = p.parseString(code);
var bytes = hscript.Bytes.encode(program, allowTypes);
program = hscript.Bytes.decode(bytes, allowTypes);
var interp = new hscript.Interp();
// #if js
// if( optimize ) interp = new hscript.JsInterp();
// #end
if (vars != null)
for (v in Reflect.fields(vars))
interp.variables.set(v, Reflect.field(vars, v));
var success:Bool = false;
try {
interp.execute(program);
success = true;
}
catch(e:hscript.Expr.Error) {
trace('Error: ${Printer.errorToString(e)}');
}
assertTrue(success, pos);
assertPos.lineNumber += 1;
}
private final assertPos:haxe.PosInfos = { // for debugging
fileName: "TestHScript.hx",
lineNumber: 0,
className: "TestHScript",
methodName: "test"
};
function test():Void {
assertPos.lineNumber = 81;
assertScript("0",0);
assertScript("var o = { x : 1 }; o.x = 5; o.x", 5);
assertScript("var o = { x : 1 }; o.x += 2; o.x *= 3; o.x", 9);
assertScript("var a = 0; var b = 0; if( a == 0 ) a = 1 else b = 2; a + b", 1);
assertScript("var a = 3; if( a > 2 ) return 5; 3", 5);
assertScript("for( i in 0...10 ) if( i == 4 ) return i; -1", 4);
assertScript("var o = { a : 2, b : 3 }; o.a + o.b", 5);
assertScript("var o = { n : 1 }; var a = o.n++; o.n-- + a + o.n", 4);
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("1e5",100000);
assertScript("1.2e2",120);
assertScript("100e-2",1);
assertScript("1.2e-1",0.12);
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("[11,22,33,][2]", 33);
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 }); // this fails because of how locals are handled
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 a = false; do { a = true; } while (!a); a;",true);
assertScript("var t = 0; for( x in 1...10 ) t += x; t", 45);
assertScript("var t = 0; for( x in new IntIterator(1,10) ) t +=x; t", 45);
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() return { \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);
assertScript("var f = x -> x + 1; f(3)", 4);
assertScript("var f = () -> 55; f()", 55);
assertScript("var f = (x) -> x + 1; f(3)", 4);
assertScript("var f = (x:Int) -> x + 1; f(3)", 4);
assertScript("var f = (x,y) -> x + y; f(3,1)", 4);
assertScript("var f = (x,y:Int) -> x + y; f(3,1)", 4);
assertScript("var f = (x:Int,y:Int) -> x + y; f(3,1)", 4);
assertScript("var f:Int->Int->Int = (x:Int,y:Int) -> x + y; f(3,1)", 4, null, true);
assertScript("var f:(x:Int, y:Int)->Int = (x:Int,y:Int) -> x + y; f(3,1)", 4, null, true);
// skipping those until issues with locals are addressed
//assertScript("var f:(x:Int)->(y:Int, z:Int)->Int = (x:Int) -> (y:Int, z:Int) -> x + y + z; f(3)(1, 2)", 6, null, true);
//assertScript("var f:(x:Int)->(Int, Int)->Int = (x:Int) -> (y:Int, z:Int) -> x + y + z; f(3)(1, 2)", 6, null, true);
assertScript("var a = 10; var b = 5; a - -b", 15);
assertScript("var a = 10; var b = 5; a - b / 2", 7.5);
assertScript("var a; a", null);
assertScript("var a = 1, b = 5; a + b;", 6);
assertScript("var a, b = 5; if (a == null) a = 2; a + b;", 7);
assertScript("var a:Int; a", null, null, true);
assertScript("var a:Int = 1, b:Int = 5; a + b;", 6, null, true);
assertScript("var a:Int, b:Int = 5; if (a == null) a = 2; a + b;", 7, null, true);
assertScript("false && xxx", false);
assertScript("true || xxx", true);
assertScript("[for( x in arr ) switch( x ) { case 1: 55; case 3: 66; default: 0; }].join(':')",'55:0:66',{ arr : [1,2,3] });
assertScript("switch( x ) { case 1: 55; case 3: 66; default: 0; }",66 ,{ x : 3 });
assertScript("var a = 1; switch( b ) { default: a = 2; }; a", 2, { b : 2 });
assertScript("var a = 1; switch( b ) { case 2: a = 100; default: a = 2; }; a", 100, { b : 2 });
assertScript("var a = 3; switch( b ) { case 2: if (a == 1) { a = 100; } else { a = 99; }; default: a = 2; }; a", 99, { b : 2 });
}
function testNullFieldAccess():Void {
assertPos.methodName = "testNullFieldAccess";
assertPos.lineNumber = 208;
var pt = {
x : 10,
call : function() { return 11; }
};
var vars = {
ptnull : null,
pt: pt,
pt2null : {pt : null},
pt2: {pt : pt}
}
assertScript("ptnull?.x", null, vars);
assertScript("ptnull?.pt.x", null, vars);
assertScript("ptnull?.call()", null, vars);
assertScript("ptnull?.pt.call()", null, vars);
assertScript("pt?.x", 10, vars);
assertScript("pt?.call()", 11, vars);
assertScript("pt2null?.pt", null, vars);
assertScript("pt2null?.pt?.x", null, vars);
assertScript("pt2null?.pt?.call()", null, vars);
assertScript("pt2?.pt", pt, vars);
assertScript("pt2?.pt?.x", 10, vars);
assertScript("pt2?.pt?.call()", 11, vars);
assertScript("ptnull ?? 12", 12, vars);
assertScript("pt2null.pt ?? 12", 12, vars);
assertScript("ptnull?.x ?? 12", 12, vars);
assertScript("pt?.x ?? 12", 10, vars);
assertScript("pt.y ?? 12", 12, vars);
}
function testIsOperator():Void {
assertPos.methodName = "testIsOperator";
assertPos.lineNumber = 237;
var vars = {
String: String,
Bool: Bool,
Int: Int,
Float: Float,
Dynamic: Dynamic
}
assertScript("10 is Int", true, vars);
assertScript("10.0 is Int", true, vars);
assertScript("10.1 is Int", false, vars);
assertScript("10 is Float", true, vars);
assertScript("10.0 is Float", true, vars);
assertScript("10.1 is Float", true, vars);
assertScript("10 is String", false, vars);
assertScript('"hscript" is String', true, vars);
assertScript('"" is String', true, vars);
assertScript('true is Bool', true, vars);
assertScript('false is Bool', true, vars);
assertScript('0 is Bool', false, vars);
assertScript('1 is Bool', false, vars);
assertScript('1 is Bool', false, vars);
assertScript("10 is Dynamic", true, vars);
assertScript("10.1 is Dynamic", true, vars);
assertScript('"hscript" is Dynamic', true, vars);
assertScript('null is Int', false, vars);
assertScript('null is Float', false, vars);
assertScript('null is String', false, vars);
assertScript('null is Bool', false, vars);
assertScript('null is Dynamic', false, vars);
}
function testMap():Void {
assertPos.methodName = "testMap";
assertPos.lineNumber = 276;
var objKey = { ok:true };
var vars = {
stringMap: ["foo" => "Foo", "bar" => "Bar"],
intMap:[100 => "one hundred"],
objKey: objKey,
objMap:[objKey => "ok"],
enumKey:Option.Some("some"),
enumMap:new EnumValueMap<Option<String>, String>(),
stringIntMap: ["foo" => 100]
}
vars.enumMap.set(vars.enumKey, "ok");
assertScript('stringMap["foo"]', "Foo", vars);
assertScript('intMap[100]', "one hundred", vars);
assertScript('objMap[objKey]', "ok", vars);
assertScript('enumMap[enumKey]', "ok", vars);
assertScript('stringMap["a"] = "A"; stringMap["a"]', "A", vars);
assertScript('intMap[200] = objMap[{foo:false}] = enumMap[enumKey] = "A"', "A", vars);
assertEquals('A', vars.intMap[200]);
assertEquals('A', vars.enumMap.get(vars.enumKey));
for (key in vars.objMap.keys()) {
if (key != objKey) {
assertEquals(false, (key:Dynamic).foo);
assertEquals('A', vars.objMap[key]);
}
}
assertScript('
var keys = [];
for (key in stringMap.keys()) keys.push(key);
keys.join("_");
', {
var keys = [];
for (key in vars.stringMap.keys()) keys.push(key);
keys.join("_");
}, vars);
assertPos.lineNumber += 9;
assertScript('stringMap.remove("foo"); stringMap.exists("foo");', false, vars);
assertScript('stringMap["foo"] = "a"; stringMap["foo"] += "b"', 'ab', vars);
assertEquals('ab', vars.stringMap['foo']);
assertScript('stringIntMap["foo"]++', 100, vars);
assertEquals(101, vars.stringIntMap['foo']);
assertScript('++stringIntMap["foo"]', 102, vars);
assertScript('var newMap = ["foo"=>"foo"]; newMap["foo"];', 'foo', vars);
#if (!php || (haxe_ver >= 3.3))
assertScript('var newMap = [enumKey=>"foo"]; newMap[enumKey];', 'foo', vars);
#end
assertScript('var newMap = [{a:"a"}=>"foo", objKey=>"bar"]; newMap[objKey];', 'bar', vars);
}
function testPatternMatching():Void {
// Literal match
assertScript("switch(2) { case 1: 10; case 2: 20; }", 20);
// Wildcard
assertScript("switch('hello') { case _: 99; }", 99);
// Variable binding
assertScript("switch(42) { case x: x + 1; }", 43);
// Array destructuring
assertScript("switch([1,2,3]) { case [a,b,c]: a + b + c; }", 6);
// Array with wildcard
assertScript("switch([10,20,30]) { case [a,_,_]: a; }", 10);
// Nested array pattern
assertScript("switch([[1,2],[3,4]]) { case [[a,b],_]: a + b; }", 3);
// Object pattern
assertScript("switch({x:1,y:2}) { case {x:a,y:b}: a + b; }", 3);
// Guard with binding
assertScript("switch(10) { case x if x > 5: x * 2; }", 20);
// Guard no match
assertScript("switch(2) { case x if x > 5: 100; case _: 0; }", 0);
// Guard match on second
assertScript("switch(2) { case x if x > 5: 100; case x: x; }", 2);
// Or pattern
assertScript("switch(3) { case 1|3|5: true; case _: false; }", true);
// Or pattern no match
assertScript("switch(2) { case 1|3|5: true; case _: false; }", false);
// String literal match
assertScript("switch('foo') { case 'bar': 0; case 'foo': 1; case _: 2; }", 1);
// First-match semantics
assertScript("switch(1) { case 1: 10; case 1: 20; }", 10);
}
function testTypeParams():Void {
// Type parameter with constraint (consumed at parse time, no runtime effect)
assertScript("var f = function<T:Int>(x:T):T return x; f(5)", 5, null, true);
assertScript("var f = function<T:Float>(x:T):T return x; f(1.5)", 1.5, null, true);
// Multiple type params
assertScript("var f = function<T,U>(x:T,y:U):T return x; f(1,'a')", 1, null, true);
}
function testAbstract():Void {
// Basic abstract with constructor, verify __value__ is set
assertScript("
abstract MyInt(Int) {
public function new(v) __value__ = v;
}
var x = new MyInt(42);
x.__value__;
", 42);
}
function testPatternAdvanced():Void {
// Empty array match
assertScript("switch([]) { case []: 1; case _: 0; }", 1);
// Bool match
assertScript("switch(false) { case false: 1; case true: 2; }", 1);
// Null match
assertScript("switch(null) { case null: 1; case _: 0; }", 1);
// Float match
assertScript("switch(1.5) { case 1.5: 1; case _: 0; }", 1);
// Array in object pattern
assertScript("switch({a:[1,2]}) { case {a:[x,y]}: x + y; }", 3);
// Object in array pattern
assertScript("switch([{x:1},{x:2}]) { case [{x:a},{x:b}]: a + b; }", 3);
// Guard with or-pattern
assertScript("switch(5) { case x if x > 10: 100; case 1|3|5: 1; case _: 0; }", 1);
// Default case
assertScript("switch(99) { case 1: 0; case _: 1; }", 1);
// No default, no match
assertScript("switch(99) { case 1: 10; }", null);
// Multiple guards
assertScript("switch(15) { case x if x > 0 && x < 10: 1; case x if x >= 10 && x < 20: 2; case _: 0; }", 2);
// Variable binding preserved in body
assertScript("switch('world') { case x: 'hello ' + x; }", "hello world");
// Array with two elements only (not three)
assertScript("switch([1,2,3]) { case [a,b]: a + b; case _: 0; }", 0);
// Exact array length mismatch
assertScript("switch([1]) { case [a,b]: 0; case [a]: a; }", 1);
}
function testAbstractAdvanced():Void {
// Two different abstracts
assertScript("
abstract A(Int) {
public function new(v) __value__ = v;
}
abstract B(String) {
public function new(v) __value__ = v;
}
var a = new A(10);
var b = new B('hi');
a.__value__;
", 10);
// Two abstract instances
assertScript("
abstract Box(Int) {
public function new(v) __value__ = v;
}
var a = new Box(3);
var b = new Box(4);
a.__value__ + b.__value__;
", 7);
}
function testMetadataAdvanced():Void {
// Metadata with string arg
assertScript("@:deprecated('use newFunc') var x = 1; x", 1);
// Metadata chained
assertScript("@:a @:b @:c var x = 42; x", 42);
}
function testCrossFeature():Void {
// Abstract with pattern matching (direct __value__ access)
assertScript("
abstract Box(Int) {
public function new(v) __value__ = v;
}
var items = [new Box(1), new Box(2)];
switch(items) {
case [a,b]: a.__value__ + b.__value__;
case _: 0;
}
", 3);
// Type params with pattern matching
assertScript("
var pair = function<T,U>(a:T,b:U):Array<Dynamic> return [a,b];
switch(pair(1,'two')) {
case [x,y]: Std.string(x) + y;
}
", "1two", null, true);
}
function testAbstractMethods():Void {
// Method with single param
assertScript("
abstract Calc(Int) {
public function new(v) __value__ = v;
public function triple(v) v * 3;
}
var c = new Calc(5);
c.triple(4);
", 12);
// Method with multiple params
assertScript("
abstract Calc(Int) {
public function new(v) __value__ = v;
public function add(a,b) a + b;
}
var c = new Calc(0);
c.add(10, 20);
", 30);
// Two instances calling methods
assertScript("
abstract Box(Int) {
public function new(v) __value__ = v;
public function mul(a) a * 2;
}
var a = new Box(1);
var b = new Box(2);
a.mul(5) + b.mul(10);
", 30);
// Method on different abstracts
assertScript("
abstract A(Int) {
public function new(v) __value__ = v;
public function ident(v) v;
}
abstract B(String) {
public function new(v) __value__ = v;
public function greet(v) 'hello ' + v;
}
var a = new A(99);
var b = new B('world');
a.ident(42) == 42 && b.greet('x') == 'hello x';
", true);
}
function testMetadataUtilities():Void {
// Metadata stored and retrievable via getMetas/getMeta
var p = new Parser();
var interp = new Interp();
var ast = p.parseString("@:deprecated('v1') {x: 1}");
var obj = interp.execute(ast);
var metas = Interp.getMetas(obj);
assertEquals(1, metas.length);
assertEquals("deprecated", metas[0].name);
assertEquals("v1", metas[0].params[0]);
var params = Interp.getMeta(obj, "deprecated");
assertEquals("v1", params[0]);
assertEquals(null, Interp.getMeta(obj, "nonexistent"));
}
function testPackage():Void {
assertPos.methodName = "testPackage";
assertPos.lineNumber = 316;
assertScriptRun('package;');
assertScriptRun('package my.pack;');
assertScriptRun('package my.other.pack;');
}
function testImport() {
assertPos.methodName = "testImport";
assertPos.lineNumber = 324;
assertScript('import haxe.ds.StringMap; StringMap', haxe.ds.StringMap);
assertScript('import Math as HaxeMath; HaxeMath', Math);
assertScript('using StringTools; " hscript ".trim()', "hscript");
assertScript('using Lambda; [1, 2, 3, 4].find((i) -> return i == 2)', 2);
}
function testClass() {
assertPos.methodName = "testClass";
assertPos.lineNumber = 333;
assertScript("class M {public var a:Int; function new(a:Int){this.a = a;}} var m = new M(10); m.a", 10, null, true);
assertScript("class M {public static var N:Int = 10; public static function S(n:Int){return N += n;}} M.S(10)", 20, null, true);
assertScript("class M {public var a:Int; function new(a:Int){this.a = a;}} class MA extends M {function new() {super(20);}} var ma = new MA(); ma.a", 20, null, true);
assertScript("class M extends TestClass {function new() {super(16, 16);} override function getSum() {return super.getSum() * 2;}} var m = new M(); m.getSum()", 64, {"TestClass": TestClass}, true);
}
function testProperty() {
assertPos.methodName = "testProperty";
assertPos.lineNumber = 342;
assertScript("var n(default, set):Int = 16; function set_n(v):Int {return n = v;} n += 16; n", 32, null, true);
assertScript("var p(get, set):Int; var _p:Int = 16; function get_p():Int {return _p;} function set_p(v):Int {return _p = v;} p += 16; p", 32, null, true);
assertScript("@:isVar var m(get, set):String = 'hscript'; function get_m() return m; function set_m(v) return m = v; m = 'hscript-improved'; m", 'hscript-improved', null, true);
}
function testEnum() {
assertPos.methodName = "testEnum";
assertPos.lineNumber = 350;
assertScript("enum E {INTEGER(i:Int); DECIMAL(f:Float); NULL;} var r = switch(E.INTEGER(10)) {case E.INTEGER(i): i; default: null;}; r", 10, null, true);
assertScript("enum abstract HttpStatus(Int) { var NOTFOUND = 404; var OK = 200; } var s = HttpStatus.OK; s", 200, null, true);
}
function testCast() {
assertPos.methodName = "testCast";
assertPos.lineNumber = 357;
assertScript('var a = cast b; a', 10, {b: 10});
assertScript('cast(m, Map).get(1)', 64, {m: [1 => 64]});
}
static function main() {
#if ((haxe_ver < 4) && php)
// uncaught exception: The each() function is deprecated. This message will be suppressed on further calls (errno: 8192)
// in file: /Users/travis/build/andyli/hscript/bin/lib/Type.class.php line 178
untyped __php__("error_reporting(E_ALL ^ E_DEPRECATED);");
#end
var runner = new TestRunner();
runner.add(new TestHScript());
//runner.add(new TestHScript(true)); // TODO: JS Interp
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
}
}
@:build(hscript.macros.ClassExtendMacro.build())
class TestClass {
private var a:Int;
private var b:Int;
public function new(a:Int, b:Int) {
this.a = a;
this.b = b;
}
public function getSum() {
return a + b;
}
}