deferparseclass.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. const tests = [
  7. {
  8. name: "class toString uses the full class body text extents",
  9. body() {
  10. class a { };
  11. assert.areEqual('class a { }', a.toString());
  12. class b extends a { };
  13. assert.areEqual('class b extends a { }', b.toString());
  14. class c { constructor() { return "a"; } };
  15. assert.areEqual('class c { constructor() { return "a"; } }', c.toString());
  16. class d extends c { constructor() { super(); return "a"; } };
  17. assert.areEqual('class d extends c { constructor() { super(); return "a"; } }', d.toString());
  18. class e extends a { constructor() { super(); } };
  19. assert.areEqual('class e extends a { constructor() { super(); } }', e.toString());
  20. }
  21. },
  22. {
  23. name: "defer parse common class member types",
  24. body: function () {
  25. var sym = Symbol();
  26. let out = 'nothing';
  27. class o {
  28. constructor() { }
  29. get a() { return 'get a'; }
  30. set a(v) { out = 'set a'; }
  31. b() { return 'b'; }
  32. ['c']() { return 'c'; }
  33. [sym]() { return 'sym'; }
  34. async d() { return 'd'; }
  35. *e() { yield 'e'; }
  36. get ['f']() { return 'get f'; }
  37. set ['f'](v) { out = 'set f'; }
  38. async ['g']() { return 'g'; }
  39. *['h']() { yield 'h'; }
  40. async async() { return 'async async'; }
  41. }
  42. class o2 {
  43. async() { return 'async'; }
  44. }
  45. class o3 {
  46. get async() { return 'get async'; }
  47. set async(v) { out = 'set async'; }
  48. }
  49. class o4 {
  50. *async() { yield 'generator async'; }
  51. }
  52. var obj = new o();
  53. var obj2 = new o2();
  54. var obj3 = new o3();
  55. var obj4 = new o4();
  56. assert.areEqual('get a', obj.a, "Simple named getter");
  57. obj.a = 123;
  58. assert.areEqual('set a', out, "Simple named setter");
  59. assert.areEqual('b', obj.b(), "Simple method");
  60. assert.areEqual('c', obj.c(), "Method with computed property name");
  61. assert.areEqual('sym', obj[sym](), "Method with computed property name (key is not string)");
  62. assert.isTrue(obj.d() instanceof Promise, "Async method");
  63. assert.areEqual('e', obj.e().next().value, "Generator method");
  64. assert.areEqual('get f', obj.f, "Getter method with computed name");
  65. obj.f = 123;
  66. assert.areEqual('set f', out, "Setter method with computed name");
  67. assert.isTrue(obj.g() instanceof Promise, "Async method with computed name");
  68. assert.areEqual('h', obj.h().next().value, "Generator method with computed name");
  69. assert.isTrue(obj.async() instanceof Promise, "Async method named async");
  70. assert.areEqual('async', obj2.async(), "Method named async");
  71. assert.areEqual('get async', obj3.async, "Getter named async");
  72. obj3.async = 123;
  73. assert.areEqual('set async', out, "Setter named async");
  74. assert.areEqual('generator async', obj4.async().next().value, "Generator method named async");
  75. }
  76. },
  77. {
  78. name: "defer parse common static class member types",
  79. body: function () {
  80. var sym = Symbol();
  81. let out = 'nothing';
  82. class obj {
  83. static get a() { return 'get a'; }
  84. static set a(v) { out = 'set a'; }
  85. static b() { return 'b'; }
  86. static ['c']() { return 'c'; }
  87. static [sym]() { return 'sym'; }
  88. static async d() { return 'd'; }
  89. static *e() { yield 'e'; }
  90. static get ['f']() { return 'get f'; }
  91. static set ['f'](v) { out = 'set f'; }
  92. static async ['g']() { return 'g'; }
  93. static *['h']() { yield 'h'; }
  94. static async async() { return 'async async'; }
  95. }
  96. class obj2 {
  97. static async() { return 'async'; }
  98. }
  99. class obj3 {
  100. static get async() { return 'get async'; }
  101. static set async(v) { out = 'set async'; }
  102. }
  103. class obj4 {
  104. static *async() { yield 'generator async'; }
  105. }
  106. assert.areEqual('get a', obj.a, "Simple named getter");
  107. obj.a = 123;
  108. assert.areEqual('set a', out, "Simple named setter");
  109. assert.areEqual('b', obj.b(), "Simple method");
  110. assert.areEqual('c', obj.c(), "Method with computed property name");
  111. assert.areEqual('sym', obj[sym](), "Method with computed property name (key is not string)");
  112. assert.isTrue(obj.d() instanceof Promise, "Async method");
  113. assert.areEqual('e', obj.e().next().value, "Generator method");
  114. assert.areEqual('get f', obj.f, "Getter method with computed name");
  115. obj.f = 123;
  116. assert.areEqual('set f', out, "Setter method with computed name");
  117. assert.isTrue(obj.g() instanceof Promise, "Async method with computed name");
  118. assert.areEqual('h', obj.h().next().value, "Generator method with computed name");
  119. assert.isTrue(obj.async() instanceof Promise, "Async method named async");
  120. assert.areEqual('async', obj2.async(), "Method named async");
  121. assert.areEqual('get async', obj3.async, "Getter named async");
  122. obj3.async = 123;
  123. assert.areEqual('set async', out, "Setter named async");
  124. assert.areEqual('generator async', obj4.async().next().value, "Generator method named async");
  125. }
  126. },
  127. {
  128. name: "defer parse less-common class member types",
  129. body: function() {
  130. var out = 'nothing';
  131. class o {
  132. "s1"() { return "s1"; }
  133. async "s2"() { return "s2"; }
  134. * "s3"() { return "s3"; }
  135. get "s4"() { return "s4"; }
  136. set "s4"(v) { out = "s4"; }
  137. 0.1() { return 0.1; }
  138. async 0.2() { return 0.2; }
  139. * 0.3() { return 0.3; }
  140. get 0.4() { return 0.4; }
  141. set 0.4(v) { out = 0.4; }
  142. 123() { return 123; }
  143. async 456() { return 456; }
  144. * 789() { yield 789; }
  145. get 123456() { return 123456; }
  146. set 123456(v) { out = 123456; }
  147. while() { return "while"; }
  148. async else() { return "else"; }
  149. * if() { return "if"; }
  150. get catch() { return "catch"; }
  151. set catch(v) { out = "catch"; }
  152. }
  153. var obj = new o();
  154. assert.areEqual('s1', obj.s1(), "Method with string name");
  155. assert.areEqual(0.1, obj[0.1](), "Method with float name");
  156. assert.areEqual(123, obj[123](), "Method with numeric name");
  157. assert.areEqual('while', obj.while(), "Method with keyword name");
  158. assert.isTrue(obj.s2() instanceof Promise, "Async method with string name");
  159. assert.isTrue(obj[0.2]() instanceof Promise, "Async method with float name");
  160. assert.isTrue(obj[456]() instanceof Promise, "Async method with numeric name");
  161. assert.isTrue(obj.else() instanceof Promise, "Async method with keyword name");
  162. assert.areEqual('s3', obj.s3().next().value, "Generator method with string name");
  163. assert.areEqual(0.3, obj[0.3]().next().value, "Generator method with float name");
  164. assert.areEqual(789, obj[789]().next().value, "Generator method with numeric name");
  165. assert.areEqual('if', obj.if().next().value, "Generator method with keyword name");
  166. assert.areEqual('s4', obj.s4, "Getter method with string name");
  167. assert.areEqual(0.4, obj[0.4], "Getter method with float name");
  168. assert.areEqual(123456, obj[123456], "Getter method with numeric name");
  169. assert.areEqual('catch', obj.catch, "Getter method with keyword name");
  170. obj.s4 = 123
  171. assert.areEqual('s4', out, "Setter method with string name");
  172. obj[0.4] = 123
  173. assert.areEqual(0.4, out, "Setter method with float name");
  174. obj[123456] = 123
  175. assert.areEqual(123456, out, "Setter method with numeric name");
  176. obj.catch = 123
  177. assert.areEqual('catch', out, "Setter method with keyword name");
  178. }
  179. },
  180. {
  181. name: "defer parse less-common static class member types",
  182. body: function() {
  183. var out = 'nothing';
  184. class obj {
  185. static "s1"() { return "s1"; }
  186. static async "s2"() { return "s2"; }
  187. static * "s3"() { return "s3"; }
  188. static get "s4"() { return "s4"; }
  189. static set "s4"(v) { out = "s4"; }
  190. static 0.1() { return 0.1; }
  191. static async 0.2() { return 0.2; }
  192. static * 0.3() { return 0.3; }
  193. static get 0.4() { return 0.4; }
  194. static set 0.4(v) { out = 0.4; }
  195. static 123() { return 123; }
  196. static async 456() { return 456; }
  197. static * 789() { yield 789; }
  198. static get 123456() { return 123456; }
  199. static set 123456(v) { out = 123456; }
  200. static while() { return "while"; }
  201. static async else() { return "else"; }
  202. static * if() { return "if"; }
  203. static get catch() { return "catch"; }
  204. static set catch(v) { out = "catch"; }
  205. }
  206. assert.areEqual('s1', obj.s1(), "Method with string name");
  207. assert.areEqual(0.1, obj[0.1](), "Method with float name");
  208. assert.areEqual(123, obj[123](), "Method with numeric name");
  209. assert.areEqual('while', obj.while(), "Method with keyword name");
  210. assert.isTrue(obj.s2() instanceof Promise, "Async method with string name");
  211. assert.isTrue(obj[0.2]() instanceof Promise, "Async method with float name");
  212. assert.isTrue(obj[456]() instanceof Promise, "Async method with numeric name");
  213. assert.isTrue(obj.else() instanceof Promise, "Async method with keyword name");
  214. assert.areEqual('s3', obj.s3().next().value, "Generator method with string name");
  215. assert.areEqual(0.3, obj[0.3]().next().value, "Generator method with float name");
  216. assert.areEqual(789, obj[789]().next().value, "Generator method with numeric name");
  217. assert.areEqual('if', obj.if().next().value, "Generator method with keyword name");
  218. assert.areEqual('s4', obj.s4, "Getter method with string name");
  219. assert.areEqual(0.4, obj[0.4], "Getter method with float name");
  220. assert.areEqual(123456, obj[123456], "Getter method with numeric name");
  221. assert.areEqual('catch', obj.catch, "Getter method with keyword name");
  222. obj.s4 = 123
  223. assert.areEqual('s4', out, "Setter method with string name");
  224. obj[0.4] = 123
  225. assert.areEqual(0.4, out, "Setter method with float name");
  226. obj[123456] = 123
  227. assert.areEqual(123456, out, "Setter method with numeric name");
  228. obj.catch = 123
  229. assert.areEqual('catch', out, "Setter method with keyword name");
  230. }
  231. },
  232. ];
  233. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });