classComputedPropertyName.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. var tests = [
  7. {
  8. name: "Assigning an undeclared variable in a class' computed property name",
  9. body: function () {
  10. assert.throws(
  11. function () {
  12. class C {
  13. [f = 5]() { }
  14. }
  15. },
  16. ReferenceError,
  17. "Computed property names inside classes are specified to execute in strict mode,\
  18. thus a variable assignment to an undeclared variable should throw a ReferenceError in strict mode",
  19. "Variable undefined in strict mode"
  20. );
  21. assert.throws(
  22. function () {
  23. class C {
  24. static [f = 5]() { }
  25. }
  26. },
  27. ReferenceError,
  28. "Computed property names inside classes are specified to execute in strict mode,\
  29. thus a variable assignment to an undeclared variable should throw a ReferenceError in strict mode",
  30. "Variable undefined in strict mode"
  31. );
  32. assert.throws(
  33. function () {
  34. "use strict";
  35. class C {
  36. [f = 5]() { }
  37. }
  38. },
  39. ReferenceError,
  40. "Computed property names inside classes are specified to execute in strict mode,\
  41. thus a variable assignment to an undeclared variable should throw a ReferenceError in strict mode",
  42. "Variable undefined in strict mode"
  43. );
  44. }
  45. },
  46. {
  47. name: "Writing to a non writable object property in a class' computed property name",
  48. body: function () {
  49. assert.throws(
  50. function () {
  51. var a = {};
  52. Object.defineProperty(a, 'b', { value: 5, writable: false });
  53. class C {
  54. [a.b = 6]() { }
  55. }
  56. },
  57. TypeError,
  58. "Computed property names inside classes are specified to execute in strict mode,\
  59. thus assigning a value to a non writable property should throw a TypeError in strict mode",
  60. "Assignment to read-only properties is not allowed in strict mode"
  61. );
  62. assert.throws(
  63. function () {
  64. var a = {};
  65. Object.defineProperty(a, 'b', { value: 5, writable: false });
  66. class C {
  67. static [a.b = 6]() { }
  68. }
  69. },
  70. TypeError,
  71. "Computed property names inside classes are specified to execute in strict mode,\
  72. thus assigning a value to a non writable property should throw a TypeError in strict mode",
  73. "Assignment to read-only properties is not allowed in strict mode"
  74. );
  75. }
  76. },
  77. {
  78. name: "Writing to a getter-only object property in a class' computed property name",
  79. body: function () {
  80. assert.throws(
  81. function () {
  82. var a = { get b() { return 5; } };
  83. class C {
  84. [a.b = 6]() { }
  85. }
  86. },
  87. TypeError,
  88. "Computed property names inside classes are specified to execute in strict mode,\
  89. thus assigning a value to a getter-only property should throw a TypeError in strict mode",
  90. "Assignment to read-only properties is not allowed in strict mode"
  91. );
  92. assert.throws(
  93. function () {
  94. var a = { get b() { return 5; } };
  95. class C {
  96. static [a.b = 6]() { }
  97. }
  98. },
  99. TypeError,
  100. "Computed property names inside classes are specified to execute in strict mode,\
  101. thus assigning a value to a getter-only property should throw a TypeError in strict mode",
  102. "Assignment to read-only properties is not allowed in strict mode"
  103. );
  104. }
  105. },
  106. {
  107. name: "Writing to a property of a non-extensible object in a class' computed property name",
  108. body: function () {
  109. assert.throws(
  110. function () {
  111. var a = {};
  112. Object.preventExtensions(a);
  113. class C {
  114. [a.b = 5]() { }
  115. }
  116. },
  117. TypeError,
  118. "Computed property names inside classes are specified to execute in strict mode,\
  119. thus assigning a value to a property of a non-extensible object should throw a TypeError in strict mode",
  120. "Cannot create property for a non-extensible object"
  121. );
  122. assert.throws(
  123. function () {
  124. var a = {};
  125. Object.preventExtensions(a);
  126. class C {
  127. static [a.b = 5]() { }
  128. }
  129. },
  130. TypeError,
  131. "Computed property names inside classes are specified to execute in strict mode,\
  132. thus assigning a value to a property of a non-extensible object should throw a TypeError in strict mode",
  133. "Cannot create property for a non-extensible object"
  134. );
  135. }
  136. },
  137. {
  138. name: "Calling delete on an undeletable property in a class' computed property name",
  139. body: function () {
  140. assert.throws(
  141. function () {
  142. class C {
  143. [delete Object.prototype]() { }
  144. }
  145. },
  146. TypeError,
  147. "Computed property names inside classes are specified to execute in strict mode,\
  148. thus calling delete on an undeletable property of object should throw a TypeError in strict mode",
  149. "Calling delete on 'prototype' is not allowed in strict mode"
  150. );
  151. assert.throws(
  152. function () {
  153. class C {
  154. static [delete Object.prototype]() { }
  155. }
  156. },
  157. TypeError,
  158. "Computed property names inside classes are specified to execute in strict mode,\
  159. thus calling delete on an undeletable property of object should throw a TypeError in strict mode",
  160. "Calling delete on 'prototype' is not allowed in strict mode"
  161. );
  162. assert.throws(
  163. function () {
  164. var a = 5;
  165. class C {
  166. [a < 6 ? delete Object.prototype : 5]() { }
  167. }
  168. },
  169. TypeError,
  170. "Computed property names inside classes are specified to execute in strict mode, \
  171. thus calling delete on an undeletable property of object should throw a TypeError in strict mode",
  172. "Calling delete on 'prototype' is not allowed in strict mode"
  173. );
  174. assert.throws(
  175. function () {
  176. var a = 5;
  177. class C {
  178. static [a < 6 ? delete Object.prototype : 5]() { }
  179. }
  180. },
  181. TypeError,
  182. "Computed property names inside classes are specified to execute in strict mode, \
  183. thus calling delete on an undeletable property of object should throw a TypeError in strict mode",
  184. "Calling delete on 'prototype' is not allowed in strict mode"
  185. );
  186. assert.throws(
  187. function () {
  188. var a = {};
  189. Object.preventExtensions(a);
  190. class C {
  191. [a && delete Object.prototype]() { }
  192. }
  193. },
  194. TypeError,
  195. "Computed property names inside classes are specified to execute in strict mode, \
  196. thus calling delete on an undeletable property of object should throw a TypeError in strict mode",
  197. "Calling delete on 'prototype' is not allowed in strict mode"
  198. );
  199. assert.throws(
  200. function () {
  201. var a = {};
  202. Object.preventExtensions(a);
  203. class C {
  204. static [a && delete Object.prototype]() { }
  205. }
  206. },
  207. TypeError,
  208. "Computed property names inside classes are specified to execute in strict mode, \
  209. thus calling delete on an undeletable property of object should throw a TypeError in strict mode",
  210. "Calling delete on 'prototype' is not allowed in strict mode"
  211. );
  212. assert.throws(
  213. function () {
  214. var a = {};
  215. Object.defineProperty(a, "x", { value: 5, configurable: false });
  216. class C {
  217. [delete a["x"]]() { }
  218. }
  219. },
  220. TypeError,
  221. "Computed property names inside classes are specified to execute in strict mode, \
  222. thus calling delete on an undeletable property of object should throw a TypeError in strict mode",
  223. "Calling delete on 'x' is not allowed in strict mode"
  224. );
  225. assert.throws(
  226. function () {
  227. var a = {};
  228. Object.defineProperty(a, "x", { value: 5, configurable: false });
  229. class C {
  230. static [delete a["x"]]() { }
  231. }
  232. },
  233. TypeError,
  234. "Computed property names inside classes are specified to execute in strict mode, \
  235. thus calling delete on an undeletable property of object should throw a TypeError in strict mode",
  236. "Calling delete on 'x' is not allowed in strict mode"
  237. );
  238. }
  239. },
  240. ]
  241. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });