object-basic.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. expect({0x10:true}).toBe({16:true});
  35. expect({0b10:true}).toBe({2:true});
  36. expect({0o10:true}).toBe({8:true});
  37. expect({.5:true}).toBe({"0.5":true});
  38. });
  39. test("computed properties", () => {
  40. const foo = "bar";
  41. const computed = "computed";
  42. const o = {
  43. [1 + 2]: 42,
  44. [`I am a ${computed} key`]: foo,
  45. };
  46. expect(o[3]).toBe(42);
  47. expect(o["I am a computed key"]).toBe("bar");
  48. });
  49. test("duplicate keys", () => {
  50. const o = {
  51. duplicate: "hello",
  52. duplicate: "world",
  53. };
  54. expect(o.duplicate).toBe("world");
  55. });
  56. test("assigning after creation", () => {
  57. const o = {};
  58. o.baz = "test";
  59. expect(o.baz).toBe("test");
  60. expect(o["baz"]).toBe("test");
  61. expect(o[-1]).toBeUndefined();
  62. o[-1] = "hello friends";
  63. expect(o[-1]).toBe("hello friends");
  64. expect(o["-1"]).toBe("hello friends");
  65. });
  66. test("floating point keys", () => {
  67. const math = { 3.14: "pi" };
  68. expect(math["3.14"]).toBe("pi");
  69. // FIXME: Floating point literals are coerced to i32
  70. // expect(math[3.14]).toBe("pi");
  71. });
  72. test("keywords as property keys", () => {
  73. const o2 = {
  74. return: 1,
  75. yield: 1,
  76. for: 1,
  77. catch: 1,
  78. break: 1,
  79. };
  80. expect(o2.return).toBe(1);
  81. expect(o2.yield).toBe(1);
  82. expect(o2.for).toBe(1);
  83. expect(o2.catch).toBe(1);
  84. expect(o2.break).toBe(1);
  85. });
  86. test("prototypical inheritance", () => {
  87. var base = {
  88. getNumber() {
  89. return 10;
  90. },
  91. };
  92. var derived = {
  93. getNumber() {
  94. return 20 + super.getNumber();
  95. },
  96. };
  97. Object.setPrototypeOf(derived, base);
  98. expect(derived.getNumber()).toBe(30);
  99. });
  100. });
  101. describe("side effects", () => {
  102. let a;
  103. const append = x => {
  104. a.push(x);
  105. };
  106. test("computed key side effects", () => {
  107. a = [];
  108. const o3 = { [append(1)]: 1, [append(2)]: 2, [append(3)]: 3 };
  109. expect(a).toHaveLength(3);
  110. expect(a[0]).toBe(1);
  111. expect(a[1]).toBe(2);
  112. expect(a[2]).toBe(3);
  113. expect(o3.undefined).toBe(3);
  114. });
  115. test("value side effects", () => {
  116. a = [];
  117. const o4 = { test: append(1), test: append(2), test: append(3) };
  118. expect(a).toHaveLength(3);
  119. expect(a[0]).toBe(1);
  120. expect(a[1]).toBe(2);
  121. expect(a[2]).toBe(3);
  122. expect(o4.test).toBeUndefined();
  123. });
  124. });
  125. describe("errors", () => {
  126. test("syntax errors", () => {
  127. expect("({ foo: function() { super.bar; } })").not.toEval();
  128. expect("({ get ...foo })").not.toEval();
  129. expect("({ get... foo })").not.toEval();
  130. expect("({ get foo })").not.toEval();
  131. expect("({ get foo: bar })").not.toEval();
  132. expect("({ get [foo]: bar })").not.toEval();
  133. expect("({ get ...[foo] })").not.toEval();
  134. expect("({ get foo(bar) {} })").not.toEval();
  135. expect("({ set foo() {} })").not.toEval();
  136. expect("({ set foo(...bar) {} })").not.toEval();
  137. expect("({ set foo(bar, baz) {} })").not.toEval();
  138. expect("({ ...foo: bar })").not.toEval();
  139. });
  140. });