TestJava.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package unit;
  2. import haxe.io.Bytes;
  3. import haxe.test.Base;
  4. import haxe.test.Base.Base_InnerClass;
  5. #if java
  6. class TestJava extends Test
  7. {
  8. function testException()
  9. {
  10. var native = new NativeClass();
  11. var hx:NativeClass = new HxClass();
  12. exc(function() try native.excTest() catch (e:Dynamic) throw e);
  13. var dyn:Dynamic = native;
  14. exc(dyn.excTest);
  15. try
  16. hx.excTest()
  17. catch(e:Dynamic) throw e; //shouldn't throw any exception
  18. }
  19. function testHaxeKeywords()
  20. {
  21. eq(Base._inline, 42);
  22. eq(Base._callback, 43);
  23. eq(Base._cast, 44);
  24. eq(Base._untyped, 45);
  25. eq(Base._in, 46);
  26. Base._in = 40;
  27. eq(Base._in, 40);
  28. }
  29. function testTypes()
  30. {
  31. eq(Base.charTest(cast 10), cast 10);
  32. eq(Base.byteTest(cast 10), cast 10);
  33. }
  34. function testInnerClass()
  35. {
  36. //-java-lib should be able to detect inner classes on import
  37. var i = new Base_InnerClass();
  38. eq(i.nameClash(), 10);
  39. var i2 = new Base_InnerClass_InnerInnerClass();
  40. t(true);
  41. }
  42. function testGenerics()
  43. {
  44. var jcl:java.lang.Class<Base_InnerClass_InnerInnerClass> = cast Base_InnerClass_InnerInnerClass;
  45. t(haxe.test.GenericHelper.staticTypedGeneric(jcl) != null);
  46. var helper = new haxe.test.GenericHelper();
  47. //TODO use typedAs
  48. eq(helper.getUntypedGeneric(), null);
  49. eq(helper.typedGeneric, null);
  50. var val = new Base_InnerClass();
  51. var g1 = new haxe.test.Generic1(val);
  52. g1.complexTypeParameterOfTypeParameter(new Base_InnerClass_InnerInnerClass());
  53. //if no compile-time error, we're fine!
  54. t(true);
  55. }
  56. function testNameClash()
  57. {
  58. eq(Base._nameClash(null), -1);
  59. eq(new Base().nameClash(), 1);
  60. eq(new Base().varNameClash(1), 1);
  61. eq(Base._varNameClash(10.4), 10.4);
  62. }
  63. function testOverloadOverride()
  64. {
  65. var c = new TestMyClass();
  66. c.normalOverload(true);
  67. t(c.boolCalled);
  68. c.normalOverload(10);
  69. t(c.intCalled);
  70. c.normalOverload(haxe.Int64.ofInt(0));
  71. t(c.int64Called);
  72. c.normalOverload("");
  73. t(c.stringCalled);
  74. c.normalOverload({});
  75. t(c.dynamicCalled);
  76. var c = new TestMyClass("");
  77. t(c.alternativeCtorCalled);
  78. var b:haxe.test.MyClass = c;
  79. b.normalOverload(true);
  80. t(c.boolCalled);
  81. b.normalOverload(10);
  82. t(c.intCalled);
  83. b.normalOverload(haxe.Int64.ofInt(0));
  84. t(c.int64Called);
  85. b.normalOverload("");
  86. t(c.stringCalled);
  87. b.normalOverload({});
  88. t(c.dynamicCalled);
  89. }
  90. function testMiscJavaLib()
  91. {
  92. //setting inline should be an error
  93. t(TestType.typeError(Base.inlineNumber = 4));
  94. }
  95. //TODO:
  96. //overload with functions + variable types
  97. }
  98. private class TestMyClass extends haxe.test.MyClass
  99. {
  100. @:overload public function new()
  101. {
  102. super();
  103. }
  104. @:overload public function new(str:String)
  105. {
  106. super();
  107. alternativeCtorCalled = true;
  108. }
  109. public var alternativeCtorCalled:Bool;
  110. public var boolCalled:Bool;
  111. public var intCalled:Bool;
  112. public var int64Called:Bool;
  113. public var stringCalled:Bool;
  114. public var dynamicCalled:Bool;
  115. @:overload override public function normalOverload(b:Bool):Void
  116. {
  117. this.boolCalled = true;
  118. }
  119. @:overload override public function normalOverload(i:Int):Void
  120. {
  121. this.intCalled = true;
  122. }
  123. @:overload override public function normalOverload(i64:haxe.Int64):Void
  124. {
  125. this.int64Called = true;
  126. }
  127. @:overload override public function normalOverload(str:String):Void
  128. {
  129. this.stringCalled = true;
  130. }
  131. @:overload override public function normalOverload(dyn:Dynamic):Void
  132. {
  133. this.dynamicCalled = true;
  134. }
  135. }
  136. @:nativeGen private class NativeClass
  137. {
  138. public function new()
  139. {
  140. }
  141. @:throws("java.lang.Throwable")
  142. public function excTest():Void
  143. {
  144. throw new java.lang.Throwable("test", null);
  145. }
  146. }
  147. private class HxClass extends NativeClass
  148. {
  149. @:throws("java.lang.Throwable")
  150. override public function excTest():Void
  151. {
  152. }
  153. }
  154. #end