TestObjc.hx 5.7 KB

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