TestObjc.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. class TestObjc extends haxe.unit.TestCase
  2. {
  3. static function main()
  4. {
  5. var x:TestObjc = null;
  6. var c:TestClass = null;
  7. var runner = new haxe.unit.TestRunner();
  8. runner.add(new TestObjc());
  9. var code = runner.run() ? 0 : 1;
  10. Sys.exit(code);
  11. }
  12. var cls:TestClass;
  13. public function testCall()
  14. {
  15. assertEquals(TestClass.aStatic(), 42);
  16. cls = TestClass.alloc().init();
  17. assertEquals(cls.getOtherThing(), 0);
  18. cls.setOtherThing(42);
  19. assertEquals(cls.otherThing, 42);
  20. assertEquals(cls.getOtherThing(), 42);
  21. assertEquals(cls.getOtherThingChar(), 42);
  22. assertEquals(cls.isBiggerThan10(2), false);
  23. assertEquals(cls.isBiggerThan10(12), true);
  24. assertEquals(cls.isBiggerThan10Int(3), false);
  25. assertEquals(cls.isBiggerThan10Int(14), true);
  26. assertEquals(cls.isBiggerThan10Num(3).boolValue(), false);
  27. assertEquals(cls.isBiggerThan10Num(14).boolValue(), true);
  28. assertEquals(cls.addHello("World"), "Hello, World");
  29. cls.something = " test";
  30. assertEquals(cls.something, " test");
  31. assertEquals(cls.addSomething("Hey,"), "Hey, test");
  32. assertEquals(cls.addHelloAndString("World"," it works"), "Hello, World it works");
  33. cls.release();
  34. }
  35. public function testVar()
  36. {
  37. cls = TestClass.alloc().init();
  38. cls.setOtherThing(142);
  39. assertEquals(cls.otherThing, 142);
  40. assertEquals(cls.getOtherThing(), 142);
  41. cls.release();
  42. }
  43. public function testBoxing()
  44. {
  45. cls = TestClass.alloc().init();
  46. cls.setOtherThing(255);
  47. var dyn:Dynamic = cls;
  48. this.assertTrue(dyn != null);
  49. this.assertTrue(cls != null);
  50. var someObjDecl = { a:10, b: cls };
  51. dyn = someObjDecl; // don't let Haxe inline that TObjectDecl
  52. assertEquals(someObjDecl.b.getOtherThing(), 255);
  53. assertEquals(getFieldB(someObjDecl).getOtherThing(), 255);
  54. cls = someObjDecl.b;
  55. assertTrue(someObjDecl.b == cls);
  56. dyn = cls;
  57. cls.release();
  58. cls = null;
  59. this.assertTrue(cls == null);
  60. cls = dyn;
  61. assertEquals(cls.getOtherThing(), 255);
  62. cls = null;
  63. dyn = null;
  64. dyn = cls;
  65. assertTrue(dyn == null);
  66. assertEquals(dyn,null);
  67. assertTrue(dyn == cls);
  68. assertEquals(dyn,cls);
  69. cls.release();
  70. }
  71. static function getFieldB<T>(d:{ a:Int, b: T }):T
  72. return d.b;
  73. public function testNull()
  74. {
  75. this.assertTrue(TestClass.isNull(null));
  76. this.assertFalse(TestClass.isNull(TestClass.alloc().init()));
  77. }
  78. public function testInterface()
  79. {
  80. cls = TestClass.alloc().init();
  81. cls.setOtherThing(21);
  82. this.assertTrue(cls.getSelf() == cls);
  83. this.assertEquals(cls.getSelf(), cls);
  84. var iface:TestInterface = cls;
  85. var obj:Dynamic = iface;
  86. this.assertTrue(iface == cls);
  87. this.assertEquals(iface, cls);
  88. this.assertTrue(obj == cls);
  89. this.assertEquals(obj, cls);
  90. this.assertEquals(iface.getSelf(), cls);
  91. this.assertEquals(iface.getSelf(), cls.getSelf());
  92. this.assertEquals(iface.getOtherThing(), 21);
  93. this.assertEquals(iface.getOtherThingChar(), 21);
  94. cls.setOtherThing(100);
  95. this.assertEquals(iface.getOtherThing(), 100);
  96. this.assertEquals(iface.getOtherThingChar(), 100);
  97. this.assertEquals("someOptionalMethod!",iface.someOptionalMethod());
  98. cls.release();
  99. }
  100. }
  101. @:include("./native/include/test.h")
  102. @:objc extern interface TestInterface
  103. {
  104. function getSelf():TestInterface;
  105. function getOtherThing():Int;
  106. function getOtherThingChar():cpp.Int8;
  107. @:optional function someOptionalMethod():NSString;
  108. @:optional function unimplementedOptional():NSString;
  109. }
  110. @:include("./native/include/test.h")
  111. @:sourceFile("./native/test.m")
  112. @:objc extern class TestClass implements TestInterface
  113. {
  114. static function aStatic():Int;
  115. static function isNull(t:TestClass):Bool;
  116. static function alloc():TestClass;
  117. function init():TestClass;
  118. var something(get,set):NSString;
  119. var otherThing:Int;
  120. @:native("something") private function get_something():NSString;
  121. @:native("setSomething") private function set_something(value:NSString):NSString;
  122. function setOtherThing(value:Int):Void;
  123. function getOtherThing():Int;
  124. function getOtherThingChar():cpp.Int8;
  125. function addHello(str:NSString):NSString;
  126. @:native("addHello:andString") function addHelloAndString(str:NSString, str2:NSString):NSString;
  127. function addSomething(str:NSString):NSString;
  128. function isBiggerThan10(value:NSNumber):Bool;
  129. function isBiggerThan10Num(value:NSNumber):NSNumber;
  130. function isBiggerThan10Int(integer:Int):Bool;
  131. function release():Void;
  132. function retainCount():Int;
  133. function getSelf():TestClass;
  134. function someOptionalMethod():NSString;
  135. @:deprecated('This method is not implemented on this class')
  136. @:noCompletion @:optional function unimplementedOptional():NSString;
  137. @:plain static function some_c_call(t:TestClass):Int;
  138. @:plain static function is_bigger_than_10(t:TestClass, val:Int):Bool;
  139. }
  140. @:forward abstract NSString(_NSString) from _NSString to _NSString
  141. {
  142. @:from @:extern inline public static function fromString(str:String):NSString
  143. return _NSString.stringWithUTF8String(str);
  144. @:to @:extern inline public function toString():String
  145. return this.UTF8String();
  146. }
  147. @:native("NSString") @:objc extern class _NSString
  148. {
  149. static function stringWithUTF8String(str:cpp.CastCharStar):NSString;
  150. function UTF8String():cpp.ConstCharStar;
  151. }
  152. @:forward abstract NSNumber(_NSNumber) from _NSNumber to _NSNumber
  153. {
  154. @:from @:extern inline public static function fromInt(i:Int):NSNumber
  155. return _NSNumber.numberWithInt(i);
  156. @:to @:extern inline public function toInt():Int
  157. return this.intValue();
  158. @:to @:extern inline public function toBool():Bool
  159. return this.boolValue();
  160. }
  161. @:native("NSNumber") @:objc extern class _NSNumber
  162. {
  163. static function numberWithInt(i:Int):NSNumber;
  164. function intValue():Int;
  165. function boolValue():Bool;
  166. }