object-basic.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. describe("correct behavior", () => {
  2. test("numeric indexing", () => {
  3. const o = { 1: 23 };
  4. expect(o[1]).toBe(23);
  5. expect(o[1n]).toBe(23);
  6. expect(o["1"]).toBe(23);
  7. o[10] = "123";
  8. expect(o[10]).toBe("123");
  9. expect(o["10"]).toBe("123");
  10. o[10n] = "1234";
  11. expect(o[10]).toBe("1234");
  12. expect(o["10"]).toBe("1234");
  13. });
  14. test("string indexing", () => {
  15. let foo = "bar";
  16. const o = {
  17. foo,
  18. bar: "baz",
  19. qux: true ? 10 : 20,
  20. hello: "friends",
  21. };
  22. expect(o.foo).toBe("bar");
  23. expect(o["foo"]).toBe("bar");
  24. expect(o.qux).toBe(10), expect(o.hello).toBe("friends");
  25. expect(o["hello"]).toBe("friends");
  26. });
  27. test("symbol keys", () => {
  28. let object = {};
  29. let symbol = Symbol("foo");
  30. object[symbol] = 2;
  31. expect(object[symbol]).toBe(2);
  32. });
  33. test("numeric keys", () => {
  34. const hex = { 0x10: "16" };
  35. const oct = { 0o10: "8" };
  36. const bin = { 0b10: "2" };
  37. const float = { 0.5: "0.5" };
  38. expect(hex["16"]).toBe("16");
  39. expect(oct["8"]).toBe("8");
  40. expect(bin["2"]).toBe("2");
  41. expect(float["0.5"]).toBe("0.5");
  42. });
  43. test("computed properties", () => {
  44. const foo = "bar";
  45. const computed = "computed";
  46. const o = {
  47. [1 + 2]: 42,
  48. [`I am a ${computed} key`]: foo,
  49. };
  50. expect(o[3]).toBe(42);
  51. expect(o["I am a computed key"]).toBe("bar");
  52. });
  53. test("duplicate keys", () => {
  54. const o = {
  55. duplicate: "hello",
  56. duplicate: "world",
  57. };
  58. expect(o.duplicate).toBe("world");
  59. });
  60. test("assigning after creation", () => {
  61. const o = {};
  62. o.baz = "test";
  63. expect(o.baz).toBe("test");
  64. expect(o["baz"]).toBe("test");
  65. expect(o[-1]).toBeUndefined();
  66. o[-1] = "hello friends";
  67. expect(o[-1]).toBe("hello friends");
  68. expect(o["-1"]).toBe("hello friends");
  69. });
  70. test("floating point keys", () => {
  71. const math = { 3.14: "pi" };
  72. expect(math["3.14"]).toBe("pi");
  73. expect(math[3.14]).toBe("pi");
  74. });
  75. test("keywords as property keys", () => {
  76. const o2 = {
  77. return: 1,
  78. yield: 1,
  79. for: 1,
  80. catch: 1,
  81. break: 1,
  82. };
  83. expect(o2.return).toBe(1);
  84. expect(o2.yield).toBe(1);
  85. expect(o2.for).toBe(1);
  86. expect(o2.catch).toBe(1);
  87. expect(o2.break).toBe(1);
  88. });
  89. test("prototypical inheritance", () => {
  90. var base = {
  91. getNumber() {
  92. return 10;
  93. },
  94. };
  95. var derived = {
  96. getNumber() {
  97. return 20 + super.getNumber();
  98. },
  99. };
  100. Object.setPrototypeOf(derived, base);
  101. expect(derived.getNumber()).toBe(30);
  102. });
  103. });
  104. describe("side effects", () => {
  105. let a;
  106. const append = x => {
  107. a.push(x);
  108. };
  109. test("computed key side effects", () => {
  110. a = [];
  111. const o3 = { [append(1)]: 1, [append(2)]: 2, [append(3)]: 3 };
  112. expect(a).toHaveLength(3);
  113. expect(a[0]).toBe(1);
  114. expect(a[1]).toBe(2);
  115. expect(a[2]).toBe(3);
  116. expect(o3.undefined).toBe(3);
  117. });
  118. test("value side effects", () => {
  119. a = [];
  120. const o4 = { test: append(1), test: append(2), test: append(3) };
  121. expect(a).toHaveLength(3);
  122. expect(a[0]).toBe(1);
  123. expect(a[1]).toBe(2);
  124. expect(a[2]).toBe(3);
  125. expect(o4.test).toBeUndefined();
  126. });
  127. });
  128. describe("shorthanded properties with special names", () => {
  129. test("keywords cannot be used", () => {
  130. expect("({ function, })").not.toEval();
  131. expect("({ var, })").not.toEval();
  132. expect("({ const, })").not.toEval();
  133. expect("({ class, })").not.toEval();
  134. });
  135. test("reserved words are allowed in non-strict mode", () => {
  136. {
  137. var implements = 3;
  138. expect({ implements }).toEqual({ implements: 3 });
  139. }
  140. {
  141. var public = "a";
  142. expect({ public }).toEqual({ public: "a" });
  143. }
  144. {
  145. var let = 9;
  146. expect({ let }).toEqual({ let: 9 });
  147. }
  148. {
  149. var await = 8;
  150. expect({ await }).toEqual({ await: 8 });
  151. }
  152. {
  153. var async = 7;
  154. expect({ async }).toEqual({ async: 7 });
  155. }
  156. {
  157. var yield = 6;
  158. expect({ yield }).toEqual({ yield: 6 });
  159. }
  160. });
  161. test("reserved words are not allowed in strict mode", () => {
  162. expect('"use strict"; var implements = 3; ({ implements })').not.toEval();
  163. expect("\"use strict\"; var public = 'a'; ({ public })").not.toEval();
  164. expect('"use strict"; var let = 9; ({ let, })').not.toEval();
  165. expect('"use strict"; var yield = 6; ({ yield, })').not.toEval();
  166. });
  167. test("special non reserved words are allowed even in strict mode", () => {
  168. expect('"use strict"; var await = 8; ({ await, })').toEval();
  169. expect('"use strict"; var async = 7; ({ async, })').toEval();
  170. });
  171. });
  172. describe("errors", () => {
  173. test("syntax errors", () => {
  174. expect("({ foo: function() { super.bar; } })").not.toEval();
  175. expect("({ get ...foo })").not.toEval();
  176. expect("({ get... foo })").not.toEval();
  177. expect("({ get foo })").not.toEval();
  178. expect("({ get foo: bar })").not.toEval();
  179. expect("({ get [foo]: bar })").not.toEval();
  180. expect("({ get ...[foo] })").not.toEval();
  181. expect("({ get foo(bar) {} })").not.toEval();
  182. expect("({ set foo() {} })").not.toEval();
  183. expect("({ set foo(...bar) {} })").not.toEval();
  184. expect("({ set foo(bar, baz) {} })").not.toEval();
  185. expect("({ ...foo: bar })").not.toEval();
  186. });
  187. });
  188. describe("naming of anon functions", () => {
  189. test("method has name", () => {
  190. expect({ func() {} }.func.name).toBe("func");
  191. });
  192. test("getter has name", () => {
  193. expect(Object.getOwnPropertyDescriptor({ get func() {} }, "func").get.name).toBe(
  194. "get func"
  195. );
  196. });
  197. test("setter has name", () => {
  198. expect(Object.getOwnPropertyDescriptor({ set func(v) {} }, "func").set.name).toBe(
  199. "set func"
  200. );
  201. });
  202. test("anon function property", () => {
  203. expect({ func: function () {} }.func.name).toBe("func");
  204. });
  205. test("anon function from within parenthesis", () => {
  206. expect({ func: function () {} }.func.name).toBe("func");
  207. });
  208. test("anon function from indirect expression", () => {
  209. expect({ func: (0, function () {}) }.func.name).toBe("");
  210. });
  211. test("function from function call does not get named", () => {
  212. function f() {
  213. return function () {};
  214. }
  215. expect(f().name).toBe("");
  216. expect({ func: f() }.func.name).toBe("");
  217. });
  218. });