TestType.hx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. package unit;
  2. import unit.MyEnum;
  3. import unit.MyClass;
  4. class TestType extends Test {
  5. static inline function u( s : String ) : String {
  6. #if flash
  7. return untyped __unprotect__(s);
  8. #else
  9. return s;
  10. #end
  11. }
  12. static public macro function getCompilationDate() {
  13. return macro $v { Std.string(Date.now()) };
  14. }
  15. static public macro function typedAs(actual:haxe.macro.Expr, expected:haxe.macro.Expr) {
  16. var tExpected = haxe.macro.Context.typeof(expected);
  17. var tActual = haxe.macro.Context.typeof(actual);
  18. return haxe.macro.Context.parse("{Test.count++; eq('" +Std.string(tActual) + "', '" +Std.string(tExpected) + "');}", haxe.macro.Context.currentPos());
  19. }
  20. static public macro function typeError(e:haxe.macro.Expr) {
  21. var result = try {
  22. haxe.macro.Context.typeof(e);
  23. "false";
  24. } catch (e:Dynamic) "true";
  25. return { pos: haxe.macro.Context.currentPos(), expr: haxe.macro.Expr.ExprDef.EConst(haxe.macro.Expr.Constant.CIdent(result)) };
  26. }
  27. static public macro function complete(e:String) : haxe.macro.Expr.ExprOf<String> {
  28. var str = new String(untyped haxe.macro.Context.load("display", 1)(e.__s));
  29. return { expr : EConst(CString(str)), pos : haxe.macro.Context.currentPos() };
  30. }
  31. public function testType() {
  32. var name = u("unit")+"."+u("MyClass");
  33. eq( Type.resolveClass(name), unit.MyClass );
  34. eq( Type.getClassName(unit.MyClass), name );
  35. eq( Type.getClassFields(unit.MyClass).length , 0 );
  36. }
  37. public function testFields() {
  38. var sfields = Type.getClassFields(unit.MySubClass);
  39. eq( sfields.length , 1 );
  40. eq( sfields[0], u("XXX") );
  41. var fields = [u("add"),u("get"),u("intValue"),u("ref"),u("set"),u("stringValue"),u("val")];
  42. var fl = Type.getInstanceFields(unit.MyClass);
  43. fl.sort(Reflect.compare);
  44. eq( fl.join("|"), fields.join("|") );
  45. var fl = Type.getInstanceFields(unit.MySubClass);
  46. fl.sort(Reflect.compare);
  47. eq( fl.join("|"), fields.join("|") );
  48. // AS3 generator will create native properties
  49. #if !as3
  50. // x should not be listed since it's not a variable
  51. var fl = Type.getInstanceFields(VarProps);
  52. var fields = ["get_x","get_y","set_x","set_y","set_z","y","z"];
  53. fl.sort(Reflect.compare);
  54. eq( fl.join("|"), fields.join("|"));
  55. // same for statics
  56. var fl = Type.getClassFields(VarProps);
  57. var fields = ["SY", "get_SX", "get_SY", "set_SX", "set_SY"];
  58. fl.sort(Reflect.compare);
  59. eq( fl.join("|"), fields.join("|"));
  60. #end
  61. }
  62. public function testEnumEq() {
  63. t( Type.enumEq(null,null) );
  64. f( Type.enumEq(A,null) );
  65. f( Type.enumEq(null,D(A)) );
  66. t( Type.enumEq(A,A) );
  67. t( Type.enumEq(B,B) );
  68. f( Type.enumEq(A,B) );
  69. t( Type.enumEq(C(1,"hello"),C(1,"hello")) );
  70. f( Type.enumEq(C(1,"hello"),C(1,"hellox")) );
  71. t( Type.enumEq(D(A),D(A)) );
  72. f( Type.enumEq(D(A),D(B)) );
  73. }
  74. function testPossibleBug() {
  75. var c = Type.getEnumConstructs(MyEnum);
  76. var old = c[0];
  77. c[0] = "modified";
  78. eq( Type.getEnumConstructs(MyEnum)[0], old );
  79. var i = Type.getInstanceFields(TestType);
  80. var old = i[0];
  81. i[0] = "modified";
  82. eq( Type.getInstanceFields(TestType)[0], old );
  83. var i = Type.getClassFields(TestType);
  84. var old = i[0];
  85. i[0] = "modified";
  86. eq( Type.getClassFields(TestType)[0], old );
  87. // we don't check for Type.enumParameters modifications :
  88. // we want it to be as fast as possible even if it references
  89. // the current enum - since it's not cachable
  90. }
  91. function testAllField() {
  92. eq( Type.allEnums(MyEnum).join("#"), "A#B" );
  93. }
  94. function testWiderVisibility() {
  95. var c = new MyClass.MyChild1();
  96. eq(12, c.a());
  97. // TODO: this is also a problem
  98. #if !as3
  99. var mc2 = new MyChild2();
  100. eq(21, mc2.test1(new MyChild1()));
  101. #end
  102. }
  103. function testUnifyMin() {
  104. // array
  105. var ti1:Array<I1>;
  106. var tbase:Array<Base>;
  107. var tpbase:Array<PClassBase<Float>>;
  108. #if (flash9 || cpp || java || cs)
  109. var tnullbool:Array<Null<Bool>>;
  110. var tnullbase:Array<Null<Base>>;
  111. #else
  112. var tnullbool:Array<Bool>;
  113. var tnullbase:Array<Base>;
  114. #end
  115. var tchild1:Array<Child1>;
  116. var ts:Array<{s:String}>;
  117. typedAs([new Child1(), new Child2()], tbase);
  118. typedAs([new Child1(), new Child2(), new Base()], tbase);
  119. typedAs([new Child1(), new Child2_1(), new Base()], tbase);
  120. typedAs([new Child2(), new Unrelated()], ti1);
  121. typedAs([new Child2_1(), new Unrelated()], ti1);
  122. typedAs([new ClassI2(), new Child2()], ti1);
  123. typedAs([new CI1(), new CI2()], tbase);
  124. typedAs([new CII1(), new CII2()], tbase);
  125. typedAs([new PClass1(), new PClass2(2.0)], tpbase);
  126. typedAs([null, false], tnullbool);
  127. typedAs([false, null], tnullbool);
  128. typedAs([null, new Base()], tnullbase);
  129. //typedAs([new Base(), null], tnullbase); // TODO: this fails on flash9 and cpp
  130. typedAs([new Base()], tbase);
  131. typedAs([new Base(), new Child1()], tbase);
  132. typedAs([new Child1(), new Base()], tbase);
  133. typedAs([new Child1(), new Child1()], tchild1);
  134. typedAs([ { s:"foo" }, new Unrelated()], ts);
  135. typedAs([new Unrelated(), { s:"foo" } ], ts);
  136. // if
  137. var tbase:Base;
  138. var ti1:I1;
  139. #if (flash9 || cpp || java || cs)
  140. var tnullbool:Null<Bool>;
  141. #else
  142. var tnullbool:Bool;
  143. #end
  144. var ts: { s:String };
  145. typedAs(if (false) new Child1(); else new Child2(), tbase);
  146. typedAs(
  147. if (false) new Child1();
  148. else if (true) new Child2();
  149. else new Base(), tbase);
  150. typedAs(
  151. if (false) new Child1();
  152. else if (true) new Child2_1();
  153. else new Base(), tbase);
  154. typedAs(if (false) new Child2(); else new Unrelated(), ti1);
  155. typedAs(if (false) new Child2_1(); else new Unrelated(), ti1);
  156. typedAs(if (false) null; else false, tnullbool);
  157. typedAs(if (false) true; else null, tnullbool);
  158. typedAs(if (false) new Unrelated(); else {s:"foo"}, ts);
  159. typedAs(if (false) { s:"foo" }; else new Unrelated(), ts);
  160. //switch
  161. typedAs(switch(false) { case true: new Child1(); case false: new Child2(); }, tbase);
  162. typedAs(switch(1) { case 0: new Child1(); case 1: new Child2_1(); default: new Base(); }, tbase);
  163. typedAs(switch(false) { case true: new Child2(); case false: new Unrelated(); }, ti1);
  164. typedAs(switch(false) { case true: new Child2_1(); case false: new Unrelated(); }, ti1);
  165. typedAs(switch(false) { case true: null; default: false; }, tnullbool);
  166. typedAs(switch(false) { case true: true; default: null; }, tnullbool);
  167. typedAs(switch(false) { case true: new Unrelated(); default: {s:"foo"}; }, ts);
  168. typedAs(switch(false) { case true: { s:"foo" }; default: new Unrelated(); }, ts);
  169. typedAs([ { x : new Child1() }, { x : new Child2() } ], [{ x: new Base() }]);
  170. #if flash9
  171. typedAs(function() { return 0; var v:UInt = 0; return v; } (), 1);
  172. #end
  173. }
  174. function testCallback()
  175. {
  176. var func = function(a:Int, b:String, c:Float) return a;
  177. var tstringfloat = function(b:String, c:Float) return 0;
  178. var tfloat = function(c:Float) return 0;
  179. var tvoid = function() return 0;
  180. var tintstring = function(a:Int, b:String) return 0;
  181. var tintfloat = function(a:Int, c:Float) return 0;
  182. var tint = function(a:Int) return 0;
  183. var tstring = function(b:String) return 0;
  184. // all missing
  185. typedAs(func.bind(), func);
  186. typedAs(func.bind(_), func);
  187. typedAs(func.bind(_, _), func);
  188. typedAs(func.bind(_, _, _), func);
  189. // all given
  190. typedAs(func.bind(22, "2", 13), tvoid);
  191. // last missing
  192. typedAs(func.bind(22, "2"), tfloat);
  193. typedAs(func.bind(22, "2", _), tfloat);
  194. // first given
  195. typedAs(func.bind(22), tstringfloat);
  196. typedAs(func.bind(22, _), tstringfloat);
  197. typedAs(func.bind(22, _, _), tstringfloat);
  198. // mixed
  199. typedAs(func.bind(_, _, 12), tintstring);
  200. typedAs(func.bind(_, "22", _), tintfloat);
  201. typedAs(func.bind(_, "22", 12), tint);
  202. typedAs(func.bind(12, _, 12), tstring);
  203. // values
  204. eq(1, func.bind()(1, "2", 3));
  205. eq(2, func.bind(2)("2", 3));
  206. eq(2, func.bind(2, "3")(3));
  207. eq(2, func.bind(2, "3", 4)());
  208. eq(1, func.bind(_, "2", 3)(1));
  209. eq(1, func.bind(_, "2")(1, 3));
  210. eq(1, func.bind(_)(1, "2", 3));
  211. eq(1, func.bind(_, "2", _)(1, 2));
  212. eq(1, func.bind().bind(_, "2", 3)(1));
  213. eq(1, func.bind(1).bind("2", 3)());
  214. eq(1, func.bind(1, _).bind("2")(3));
  215. eq(1, func.bind(_, "2").bind(1)(3));
  216. var a = 5;
  217. var b = "foo";
  218. var cb = func.bind(a);
  219. a = 6;
  220. func = function(a,b,c):Int return throw "error";
  221. eq(5, cb(b, 0));
  222. var optfunc = function(a:Int, b:Int, ?c:Int = 2) return a + b + c;
  223. eq(6, optfunc.bind(1)(3));
  224. eq(6, optfunc.bind(1, 3)());
  225. eq(7, optfunc.bind(_, _, _)(1, 2, 4));
  226. eq(7, optfunc.bind(_, 2, _)(1, 4));
  227. var foo = function ( x : Int, ?p : haxe.PosInfos ) { return "foo" + x; }
  228. var f : Void -> String = foo.bind(0);
  229. eq("foo0", f());
  230. // TODO: this fails on flash 9
  231. var foo = function(bar = 2) { return bar; };
  232. #if (flash9)
  233. t(typeError(foo.bind(_)));
  234. #else
  235. var l = foo.bind(_);
  236. eq(2, l());
  237. #end
  238. // note that this does not
  239. var foo = function(bar:Null<Int> = 2) { return bar; };
  240. var l = foo.bind(_);
  241. eq(2, l());
  242. }
  243. function testConstantAnonCovariance()
  244. {
  245. var func = function (str:String, ?str1: { x:Float, y:Int }, ?str2: { w:Float, h:Int } ) { };
  246. var a: { v:Float };
  247. var b:Dynamic = "bar";
  248. f(typeError(a = { v:0.2 } ));
  249. f(typeError(a = { v:0 } ));
  250. typedAs(a = { v: 0 }, a);
  251. typedAs(a = { v: 0.2 }, a);
  252. t(typeError(a = { v: "foo" } ));
  253. f(typeError(a = { v: untyped "foo" } ));
  254. f(typeError(a = { v: b } ));
  255. f(typeError( { var b: { v:Dynamic } = { v: "foo" };} ));
  256. t(typeError( { var b: { v:Int } = { v: 1.2 }; } ));
  257. t(typeError( { var b: { v:Int } = { v:0, w:"foo" }; }));
  258. t(typeError( { var b: { v:Int } = { v:0, v:2 }; } ));
  259. t(typeError( { var b: { v:Int, w:String } = { v:0 }; } ));
  260. typedAs({ v: 0.2, " foo":2 }, a);
  261. t(typeError(a = { v:0, " foo":2 } ));
  262. f(typeError(func("foo", { x:1.2, y:2 } )));
  263. f(typeError(func("foo", { w:1.2, h:2 } )));
  264. }
  265. function testCovariantReturn()
  266. {
  267. var b:Base = null;
  268. var c1:Child1 = null;
  269. var c2_1:Child2_1 = null;
  270. var c = new Cov2();
  271. typedAs(c.covariant(), c1);
  272. t(Std.is(c.covariant(), Child1));
  273. t(Std.is(cast(c, Cov1).covariant(), Child1));
  274. // base class reference
  275. var br:Cov1 = c;
  276. typedAs(br.covariant(), b);
  277. t(Std.is(br.covariant(), Child1));
  278. // interface reference
  279. var ir:CovI = c;
  280. typedAs(ir.covariant(), b);
  281. t(Std.is(ir.covariant(), Child1));
  282. // dynamic
  283. var dr:Dynamic = c;
  284. t(Std.is(dr.covariant(), Child1));
  285. // interface covariance
  286. var c3 = new Cov3();
  287. typedAs(c3.covariant(), c2_1);
  288. t(Std.is(c3.covariant(), Child2_1));
  289. }
  290. function testContravariantArgs()
  291. {
  292. var b = function(arg:Base):Void { };
  293. var c1 = function(arg:Child1):Void { };
  294. var c = new Ctrv2();
  295. typedAs(c.contravariant, b);
  296. typedAs(cast (c, Ctrv1).contravariant, c1);
  297. }
  298. function testInlineCast() {
  299. var s = new InlineCastB().test().quote();
  300. eq(s, "I am the greatest.");
  301. }
  302. function testInitFields()
  303. {
  304. var c = new InitBase();
  305. eq(c.i, 2);
  306. eq(c.s, "foo");
  307. eq(c.b, true);
  308. eq(c.t, String);
  309. var c = new InitChild();
  310. eq(c.i, 2);
  311. eq(c.s, "foo");
  312. eq(c.b, true);
  313. eq(c.t, String);
  314. var c = new InitChildWithCtor(null);
  315. eq(c.i, 2);
  316. eq(c.s, "foo");
  317. eq(c.b, true);
  318. eq(c.t, String);
  319. var c = Type.createInstance(InitWithoutCtor, []);
  320. eq(c.i, 2);
  321. var c = new InitProperties();
  322. eq(c.accNull, 3);
  323. eq(c.accDefault, 3);
  324. eq(c.accFunc, 3);
  325. eq(c.accNever, 3);
  326. eq(c.accDynamic, 3);
  327. exc(function() c.accFunc = 4);
  328. }
  329. function testReturnFlow()
  330. {
  331. var l = function():String
  332. {
  333. while (true)
  334. {
  335. return "foo";
  336. }
  337. // some platforms may have to add an implicit return null here
  338. }
  339. eq(l(), "foo");
  340. }
  341. function testOptionalParamsSkip() {
  342. function foo( a : MyEnum, ?b : Bool, ?c : MyEnum ) {
  343. return "";
  344. }
  345. typedAs(foo(A), "");
  346. typedAs(foo(A, true), "");
  347. typedAs(foo(A, A), "");
  348. typeError(foo(A, A, false));
  349. }
  350. function testParamConstraints()
  351. {
  352. var pcc = new ParamConstraintsClass();
  353. var b = new Base();
  354. var c1 = new Child1();
  355. var u = new Unrelated();
  356. var ci1 = new CI1();
  357. eq(ParamConstraintsClass.staticSingle(b), b);
  358. eq(ParamConstraintsClass.staticSingle(c1), c1);
  359. // TODO: these should fail (param is constrained to Base)
  360. //ParamConstraintsClass.staticSingle(u);
  361. //ParamConstraintsClass.staticSingle(1);
  362. //ParamConstraintsClass.staticSingle("foo");
  363. eq(pcc.memberSingle(b), b);
  364. eq(pcc.memberSingle(c1), c1);
  365. //typeError(pcc.memberSingle(u));
  366. eq(pcc.memberMultiple(ci1), ci1);
  367. //typeError(pcc.memberMultiple(b));
  368. //typeError(pcc.memberMultiple(c1));
  369. var l = new List();
  370. l.push(ci1);
  371. var lmono = new List();
  372. eq(pcc.memberComplex(ci1, l), l);
  373. eq(pcc.memberComplex(ci1, lmono), lmono);
  374. //typeError(pcc.memberComplex(ci1, [ci1]));
  375. eq(pcc.memberBasic("foo", ["bar"]), "bar");
  376. eq(pcc.memberAnon( { x : 1, y : 3. } ), 4);
  377. //typeError(pcc.memberAnon( { x : 1 } ));
  378. //typeError(pcc.memberAnon( { y : 3. } ));
  379. #if !(java || cs)
  380. // pcc.memberOverload("foo", "bar");
  381. #end
  382. // TODO: this should not fail (overload accepts)
  383. //pcc.memberOverload(1, [2]);
  384. //t(typeError(pcc.memberOverload(1, ["foo"])));
  385. var pcc2 = new ParamConstraintsClass2();
  386. pcc2.check([1]);
  387. //typeError(pcc2.check(["foo"]));
  388. var pcc2 = new ParamConstraintsClass2();
  389. pcc2.bind("foo");
  390. //typeError(pcc2.check([1]));
  391. pcc2.check(["foo"]);
  392. var pcc2 = new ParamConstraintsClass2<String>();
  393. //t(typeError(pcc2.check([1])));
  394. pcc2.check(["foo"]);
  395. }
  396. function testUsing()
  397. {
  398. eq(UsingChild1.test(), "FOOFOOFOO");
  399. eq(UsingChild2.test(), "FOO");
  400. eq(UsingUnrelated.test(), "FOOFOO");
  401. }
  402. function testInlineInit()
  403. {
  404. eq(InitBase.si, 2);
  405. eq(InitBase.sop, 27);
  406. eq(InitBase.sp, 6);
  407. eq(InitBase.st, String);
  408. eq(InitBase.sinline, 60000.);
  409. }
  410. function testInline()
  411. {
  412. typedAs(inlineTest1([1]), var void:Void);
  413. typedAs(inlineTest2([1]), var void:Void);
  414. }
  415. inline function inlineTest1<T>(map:Array<T>) {
  416. map[0];
  417. }
  418. inline function inlineTest2(map:Array<Dynamic>) {
  419. map[0];
  420. }
  421. public function testMacroRest() {
  422. #if !macro
  423. var r = MyMacro.MyRestMacro.testRest1(1, 2, 3);
  424. eq(r.length, 3);
  425. eq(r[0], 1);
  426. eq(r[1], 2);
  427. eq(r[2], 3);
  428. var r : Array<Dynamic> = MyMacro.MyRestMacro.testRest1(1, [2, 3]);
  429. eq(r.length, 2);
  430. eq(r[0], 1);
  431. eq(r[1][0], 2);
  432. eq(r[1][1], 3);
  433. var r = MyMacro.MyRestMacro.testRest1(1);
  434. eq(r.length, 1);
  435. eq(r[0], 1);
  436. var r = MyMacro.MyRestMacro.testRest2(1, 2, 3, 4);
  437. eq(r.length, 4);
  438. eq(r[0], 1);
  439. eq(r[1], 2);
  440. eq(r[2], 3);
  441. eq(r[3], 4);
  442. var r = MyMacro.MyRestMacro.testRest2(1, 2);
  443. eq(r.length, 2);
  444. eq(r[0], 1);
  445. eq(r[1], 2);
  446. var r : Array<Dynamic> = MyMacro.MyRestMacro.testRest2(1, 2, [3]);
  447. eq(r.length, 3);
  448. eq(r[0], 1);
  449. eq(r[1], 2);
  450. eq(r[2][0], 3);
  451. #end
  452. }
  453. public function testGenericFunction() {
  454. gf1(2);
  455. gf1("foo");
  456. gf1(true);
  457. #if !flash8
  458. // no support for flash8
  459. gf1(new haxe.Template("foo"));
  460. #end
  461. gf1(new haxe.ds.GenericStack<Int>());
  462. hsf(TestType, "gf1_Int");
  463. hsf(TestType, "gf1_String");
  464. hsf(TestType, "gf1_Bool");
  465. #if !flash8
  466. hsf(TestType, "gf1_haxe_Template");
  467. #end
  468. hsf(TestType, #if (flash9 || cpp) "gf1_haxe_ds_GenericStack_Int" #else "gf1_haxe_ds_GenericStack" #end);
  469. t(typeError(gf1(null))); // monos don't work
  470. t(typeError(gf1( { foo:1 } ))); // structures don't work
  471. eq("foo[1,2]", gf2("foo", [1, 2]));
  472. eq("foo[[1,2]]", gf2("foo", [[1, 2]]));
  473. hsf(TestType, "gf2_String_Int");
  474. hsf(TestType, "gf2_String_Array");
  475. var a = gf3("foo", ["bar", "baz"]);
  476. eq(a[0], "bar");
  477. eq(a[1], "baz");
  478. eq(a[2], "foo");
  479. hsf(TestType, "gf3_String_Array");
  480. #if !flash8
  481. var t = new haxe.Template("foo");
  482. var ta = gf3(t, [])[0];
  483. f(t == ta);
  484. hsf(TestType, "gf3_haxe_Template_Array");
  485. #end
  486. eq(overloadFake(1), 1);
  487. eq(overloadFake("bar"), "barfoo");
  488. }
  489. @:generic static function gf1<T>(a:T) {
  490. return a;
  491. }
  492. @:generic static function gf2<A,B>(a:A, b:Array<B>) {
  493. return Std.string(a) + Std.string(b);
  494. }
  495. @:generic static function gf3 < A:{function new(s:String):Void;}, B:Array<A> > (a:A, b:B) {
  496. var clone = new A("foo");
  497. b.push(clone);
  498. return b;
  499. }
  500. @:generic static function overloadFake<A>(a:A) {
  501. return a;
  502. }
  503. static function overloadFake_String(a:String) {
  504. return a + "foo";
  505. }
  506. function testCompletion() {
  507. #if !macro
  508. var s = { foo: 1 };
  509. eq(complete("s.|"), "foo:Int");
  510. eq(complete("var x : haxe.|"), "path(haxe)");
  511. eq(complete("var x : haxe.macro.Expr.|"), "path(haxe.macro:Expr)");
  512. // could be improved by listing sub types
  513. eq(complete("haxe.macro.Expr.|"), "error(haxe.macro.Expr is not a value)");
  514. // know issue : the expr optimization will prevent inferring the array content
  515. //eq(complete('{
  516. //var a = [];
  517. //a.push("");
  518. //a[0].|
  519. //}'),"Unknown<0>");
  520. // could be improved : expr optimization assume that variable not in scope is a member
  521. // so it will eliminate the assignement that would have forced it into the local context
  522. // that would be useful when you want to write some code and add the member variable afterwards
  523. eq(complete('{
  524. unknownVar = "";
  525. unknownVar.|
  526. }'),"path(unknownVar)");
  527. for (k in [s].iterator()) {
  528. eq(complete("k.|"), "foo:Int");
  529. }
  530. var f = function():Iterator<{foo:Int}> {
  531. return [s].iterator();
  532. };
  533. eq(complete("for (k in f()) k.|"), "foo:Int");
  534. #end
  535. }
  536. function testSuperPropAccess() {
  537. var c = new ChildSuperProp();
  538. eq(c.prop, 2);
  539. eq(c.prop = 4, 5);
  540. eq(c.test(), "test2");
  541. eq(c.fProp(9), "test09");
  542. }
  543. function testVoidFunc() {
  544. exc(function() { throw null; return 1; } );
  545. exc(function() { throw null; return "foo"; } );
  546. exc(function() { throw null; return MyEnum.A; } );
  547. #if !flash8
  548. exc(function() { throw null; return new haxe.Template("foo"); } );
  549. #end
  550. exc(function() { throw null; return null; } );
  551. exc(function() { throw null; return { foo: 1}; } );
  552. }
  553. function testAbstractCastConstraints() {
  554. var z:unit.MyAbstract.AbstractZ<String> = new unit.MyAbstract.AbstractBase("foo");
  555. var s:String = z;
  556. t(typeError( {
  557. var i:Int = z;
  558. }));
  559. eq("foo", s);
  560. var z:unit.MyAbstract.AbstractZ<Int> = new unit.MyAbstract.AbstractBase(12);
  561. var i:Int = z;
  562. eq(12, i);
  563. t(typeError( {
  564. var s:String = z;
  565. }));
  566. }
  567. function testOpArrow() {
  568. var m = new Map<Int,Int>();
  569. var map = [1 => 2, 3 => 4];
  570. typedAs(map, m);
  571. t(Std.is(map, haxe.ds.IntMap));
  572. eq(map.get(1), 2);
  573. eq(map.get(3), 4);
  574. var m = new Map<String,Int>();
  575. var map = ["1" => 2, "3" => 4];
  576. typedAs(map, m);
  577. t(Std.is(map, haxe.ds.StringMap));
  578. eq(map.get("1"), 2);
  579. eq(map.get("3"), 4);
  580. var a = new unit.MyAbstract.ClassWithHashCode(1);
  581. var b = new unit.MyAbstract.ClassWithHashCode(2);
  582. var m = new Map<unit.MyAbstract.ClassWithHashCode,Int>();
  583. var map = [a => 2, b => 4];
  584. typedAs(map, m);
  585. //t(Std.is(map, haxe.ds.IntMap));
  586. eq(map.get(a), 2);
  587. eq(map.get(b), 4);
  588. // duplicate key
  589. t(typeError([1 => 2, 1 => 3]));
  590. // key unification
  591. t(typeError([1 => 2, "1" => 2]));
  592. // value unification
  593. t(typeError([1 => 2, 1 => "2"]));
  594. }
  595. function testAbstractGeneric() {
  596. var map = new Map();
  597. map.set("foo", 1);
  598. t(Std.is(map, haxe.ds.StringMap));
  599. var map = new Map();
  600. _mapMe(map); // infer from function call
  601. t(Std.is(map, haxe.ds.IntMap));
  602. var map = new Map();
  603. var a = new unit.MyAbstract.ClassWithHashCode(1);
  604. var b = new unit.MyAbstract.ClassWithHashCode(2);
  605. map.set(a, "foo");
  606. map.set(b, "bar");
  607. eq(map.get(a), "foo");
  608. eq(map.get(b), "bar");
  609. //t(Std.is(map, haxe.ds.IntMap));
  610. var map = new Map();
  611. var a = new unit.MyAbstract.ClassWithoutHashCode(1);
  612. var b = new unit.MyAbstract.ClassWithoutHashCode(2);
  613. map.set(a, "foo");
  614. map.set(b, "bar");
  615. eq(map.get(a), "foo");
  616. eq(map.get(b), "bar");
  617. // this may be specialized
  618. //t(Std.is(map, haxe.ds.ObjectMap));
  619. //var map = new unit.MyAbstract.MyMap();
  620. //map.set(new haxe.Template("foo"), 99);
  621. //t(Std.is(map, unit.MyAbstract.PseudoObjectHash));
  622. // all these cause a compilation error, but we cannot typeError test that because it happens
  623. // during a post-process check
  624. //var map = new Map(); // Could not determine type for IMap<Float, Int>
  625. //map.set(1.1, 1);
  626. //var map = new Map(); // Could not determine type for IMap<x : String -> String, Int>
  627. //map.set(function(x:String) return x, 1);
  628. //var map = new Map(); // Could not determine type for IMap<Unknown<0>, Unknown<1>>
  629. }
  630. static function _mapMe(map:Map < Int, String > ) { }
  631. function testAbstractOverload() {
  632. var ms1:unit.MyAbstract.MyString = "foo";
  633. var ms2:unit.MyAbstract.MyString = "bar";
  634. var msum = ms1 + ms2;
  635. eq(msum, "foobar");
  636. typedAs(msum, ms1);
  637. t(Std.is(msum, String));
  638. var msum2 = ms1 + 1;
  639. eq(msum2, "foo1");
  640. typedAs(msum2, ms1);
  641. t(Std.is(msum2, String));
  642. // operation is defined, but return type is not compatible
  643. t(typeError(ms1 + true));
  644. // operation is not defined
  645. t(typeError(ms1 - ms2));
  646. }
  647. function testAbstractUnop() {
  648. var vec:unit.MyAbstract.MyVector = new unit.MyAbstract.MyPoint3(1, 2, 3);
  649. var vec2 = -vec;
  650. t(vec2 != vec);
  651. eq(vec.toString(), "(1,2,3)");
  652. eq(vec2.toString(), "(-1,-2,-3)");
  653. var my = new unit.MyAbstract.MyInt2(12);
  654. eq( (-my).get(), -12);
  655. typedAs( -my, my);
  656. ++my;
  657. eq(my.get(), 13);
  658. // not defined op
  659. t(typeError(!my));
  660. // wrong flag
  661. t(typeError(my++));
  662. }
  663. function testMapComprehension() {
  664. var map = [for (x in ["a", "b"]) x => x.toUpperCase()];
  665. t(map.exists("a"));
  666. t(map.exists("b"));
  667. eq(map.get("a"), "A");
  668. eq(map.get("b"), "B");
  669. }
  670. function testCustomArrayAccess() {
  671. var obj = {
  672. foo: 12,
  673. bar: "test"
  674. };
  675. var mr:unit.MyAbstract.MyReflect = obj;
  676. eq(mr["foo"], 12);
  677. eq(mr["bar"], "test");
  678. mr["foo"] = 11;
  679. eq(mr["foo"], 11);
  680. mr["foo"] += 99;
  681. eq(mr["foo"], 110);
  682. mr["baz"] = mr["bar"] += mr["foo"];
  683. eq(mr["baz"], "test110");
  684. eq(mr["bar"], "test110");
  685. var v = "hh";
  686. mr[v] = 1;
  687. mr[v += "h"] = 2;
  688. eq(mr["hhh"], 2);
  689. eq(v, "hhh");
  690. mr["hhhh"] = 0;
  691. mr[v += "h"] += 4;
  692. eq(mr["hhhh"], 4);
  693. eq(mr["hhh"], 2);
  694. eq(v, "hhhh");
  695. // note for later: As3 compilation fails if the function name is removed
  696. mr["101"] = function n(x) return 9 + x;
  697. eq(mr["101"](1), 10);
  698. }
  699. function testAbstractClosure() {
  700. var s = new unit.MyAbstract.MyAbstractClosure("foo");
  701. var func1 = s.test();
  702. eq(func1(), "foo");
  703. s.setVal("bar");
  704. eq(func1(), "foo");
  705. eq(s.test()(), "bar");
  706. }
  707. function testAbstractTypeParameterVariance() {
  708. var a:Array<unit.MyAbstract.MyInt> = [1, 2, 3];
  709. var b:Array<unit.MyAbstract.MyInt2> = a;
  710. }
  711. }