TestDCE.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package unit;
  2. class DCEClass {
  3. // used statics
  4. static function staticUsed() { }
  5. @:keep static function staticKeep() { }
  6. static var staticVarUsed = "foo";
  7. @:isVar static var staticPropUsed(get, set):Int = 1;
  8. static function get_staticPropUsed() return staticPropUsed;
  9. static function set_staticPropUsed(i:Int) return 0;
  10. // used members
  11. function memberUsed() { }
  12. @:keep function memberKeep() { }
  13. var memberVarUsed = 0;
  14. @:isVar var memberPropUsed(get, set):Int = 1;
  15. function get_memberPropUsed() return memberPropUsed;
  16. function set_memberPropUsed(i:Int) return 0;
  17. // unused statics
  18. static function staticUnused() { }
  19. static var staticVarUnused = "bar";
  20. static var staticPropUnused(get, set):Int;
  21. static function get_staticPropUnused() return 0;
  22. static function set_staticPropUnused(i:Int) return 0;
  23. // unused members
  24. function memberUnused() { }
  25. var memberVarUnused = 1;
  26. var memberPropUnused(get, set):Int;
  27. function get_memberPropUnused() return 0;
  28. function set_memberPropUnused(i:Int) return 0;
  29. static var c :Array<Dynamic> = [null, unit.UsedReferenced2];
  30. public function new() {
  31. staticUsed();
  32. staticVarUsed;
  33. staticPropUsed = 1;
  34. staticPropUsed;
  35. memberUsed();
  36. memberVarUsed;
  37. memberPropUsed = 2;
  38. memberPropUsed;
  39. new UsedConstructed();
  40. try cast (null, UsedReferenced) catch(e:Dynamic) { }
  41. new UsedAsBaseChild();
  42. c.length;
  43. }
  44. }
  45. class TestDCE extends Test {
  46. public function testFields() {
  47. var dce = new DCEClass();
  48. var c = Type.getClass(dce);
  49. hf(c, "memberKeep");
  50. hf(c, "memberUsed");
  51. hf(c, "memberVarUsed");
  52. hf(c, "memberPropUsed");
  53. hf(c, "get_memberPropUsed");
  54. hf(c, "set_memberPropUsed");
  55. hsf(c, "staticKeep");
  56. hsf(c, "staticUsed");
  57. hsf(c, "staticVarUsed");
  58. hsf(c, "staticPropUsed");
  59. hsf(c, "get_staticPropUsed");
  60. hsf(c, "set_staticPropUsed");
  61. nhf(c, "memberUnused");
  62. nhf(c, "memberVarUnused");
  63. nhf(c, "memberPropUnused");
  64. nhf(c, "get_memberPropUnused");
  65. nhf(c, "set_memberPropUnused");
  66. nhsf(c, "staticUnused");
  67. nhsf(c, "staticVarUnused");
  68. nhsf(c, "staticPropUnused");
  69. nhsf(c, "get_staticPropUnused");
  70. nhsf(c, "set_staticPropUnused");
  71. }
  72. public function testInterface() {
  73. var l:UsedInterface = new UsedThroughInterface();
  74. var l2:UsedInterface = new InterfaceMethodFromBaseClassChild();
  75. var ic = Type.resolveClass("unit.UsedInterface");
  76. var c = Type.getClass(l);
  77. var bc = Type.resolveClass("unit.InterfaceMethodFromBaseClass");
  78. l.usedInterfaceFunc();
  79. hf(ic, "usedInterfaceFunc");
  80. hf(c, "usedInterfaceFunc");
  81. hf(bc, "usedInterfaceFunc");
  82. nhf(ic, "unusedInterfaceFunc");
  83. nhf(c, "unusedInterfaceFunc");
  84. nhf(bc, "unusedInterfaceFunc");
  85. }
  86. public function testProperty() {
  87. var l:PropertyInterface = new PropertyAccessorsFromBaseClassChild();
  88. var ic = Type.resolveClass("unit.PropertyInterface");
  89. var c = Type.getClass(l);
  90. var bc = Type.resolveClass("unit.PropertyAccessorsFromBaseClass");
  91. l.x = "bar";
  92. hf(c, "set_x");
  93. hf(bc, "set_x");
  94. nhf(ic, "set_x");
  95. nhf(ic, "get_x");
  96. nhf(c, "get_x");
  97. nhf(bc, "get_x");
  98. }
  99. #if (!cpp && !java && !cs)
  100. public function testProperty2() {
  101. var a = new RemovePropertyKeepAccessors();
  102. a.test = 3;
  103. eq(a.test, 3);
  104. Reflect.setProperty(a, "test", 2);
  105. eq(a.test, 2);
  106. var c = Type.resolveClass("unit.RemovePropertyKeepAccessors");
  107. hf(c, "get_test");
  108. hf(c, "set_test");
  109. hf(c, "_test");
  110. nhf(c, "test");
  111. }
  112. #end
  113. public function testClasses() {
  114. t(Type.resolveClass("unit.UsedConstructed") != null);
  115. t(Type.resolveClass("unit.UsedReferenced") != null);
  116. t(Type.resolveClass("unit.UsedReferenced2") != null);
  117. t(Type.resolveClass("unit.UsedInterface") != null);
  118. t(Type.resolveClass("unit.UsedThroughInterface") != null);
  119. t(Type.resolveClass("unit.UsedAsBase") != null);
  120. t(Type.resolveClass("unit.UsedAsBaseChild") != null);
  121. t(Type.resolveClass("unit.Unused") == null);
  122. t(Type.resolveClass("unit.UnusedChild") == null);
  123. t(Type.resolveClass("unit.UnusedImplements") == null);
  124. t(Type.resolveClass("unit.UsedConstructedChild") == null);
  125. t(Type.resolveClass("unit.UsedReferencedChild") == null);
  126. }
  127. public function testThrow() {
  128. // class has to be known for this to work
  129. var c = new ThrownWithToString();
  130. try {
  131. throw c;
  132. } catch (_:Dynamic) { }
  133. #if js
  134. if (!js.Browser.supported || js.Browser.navigator.userAgent.indexOf('MSIE 8') == -1)
  135. #end
  136. hf(ThrownWithToString, "toString");
  137. }
  138. }
  139. class UsedConstructed {
  140. public function new() { }
  141. }
  142. class UsedReferenced { }
  143. class UsedReferenced2 { }
  144. class UsedConstructedChild extends UsedConstructed {
  145. }
  146. class UsedReferencedChild extends UsedReferenced {
  147. }
  148. interface UsedInterface {
  149. public function usedInterfaceFunc():Void;
  150. public function unusedInterfaceFunc():Void;
  151. }
  152. class UsedThroughInterface implements UsedInterface {
  153. public function new() { }
  154. public function usedInterfaceFunc():Void { }
  155. public function unusedInterfaceFunc():Void { }
  156. public function otherFunc() { }
  157. }
  158. class UsedAsBase { }
  159. class UsedAsBaseChild extends UsedAsBase {
  160. public function new() { }
  161. }
  162. class Unused {
  163. }
  164. class UnusedChild extends Unused { }
  165. class UnusedImplements implements UsedInterface {
  166. public function usedInterfaceFunc():Void { }
  167. public function unusedInterfaceFunc():Void { }
  168. }
  169. interface PropertyInterface {
  170. public var x(get_x, set_x):String;
  171. }
  172. class PropertyAccessorsFromBaseClass {
  173. public function get_x() return throw "must not set";
  174. public function set_x(x:String) return "ok";
  175. }
  176. class PropertyAccessorsFromBaseClassChild extends PropertyAccessorsFromBaseClass implements PropertyInterface {
  177. public var x(get_x, set_x):String;
  178. public function new() { }
  179. }
  180. class InterfaceMethodFromBaseClass {
  181. public function usedInterfaceFunc():Void { }
  182. public function unusedInterfaceFunc():Void { }
  183. }
  184. class InterfaceMethodFromBaseClassChild extends InterfaceMethodFromBaseClass implements UsedInterface {
  185. public function new() { }
  186. }
  187. class ThrownWithToString {
  188. public function new() { }
  189. public function toString() { return "I was thrown today"; }
  190. }
  191. class RemovePropertyKeepAccessors
  192. {
  193. public function new() {}
  194. var _test:Float;
  195. public var test(get, set):Float;
  196. public function get_test():Float return _test;
  197. public function set_test(a:Float):Float { _test = a; return _test; }
  198. }