object-basic.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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("errors", () => {
  129. test("syntax errors", () => {
  130. expect("({ foo: function() { super.bar; } })").not.toEval();
  131. expect("({ get ...foo })").not.toEval();
  132. expect("({ get... foo })").not.toEval();
  133. expect("({ get foo })").not.toEval();
  134. expect("({ get foo: bar })").not.toEval();
  135. expect("({ get [foo]: bar })").not.toEval();
  136. expect("({ get ...[foo] })").not.toEval();
  137. expect("({ get foo(bar) {} })").not.toEval();
  138. expect("({ set foo() {} })").not.toEval();
  139. expect("({ set foo(...bar) {} })").not.toEval();
  140. expect("({ set foo(bar, baz) {} })").not.toEval();
  141. expect("({ ...foo: bar })").not.toEval();
  142. });
  143. });