assignment-operators.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. let x;
  2. test("basic functionality", () => {
  3. x = 1;
  4. expect((x = 2)).toBe(2);
  5. expect(x).toBe(2);
  6. x = 1;
  7. expect((x += 2)).toBe(3);
  8. expect(x).toBe(3);
  9. x = 3;
  10. expect((x -= 2)).toBe(1);
  11. expect(x).toBe(1);
  12. x = 3;
  13. expect((x *= 2)).toBe(6);
  14. expect(x).toBe(6);
  15. x = 6;
  16. expect((x /= 2)).toBe(3);
  17. expect(x).toBe(3);
  18. x = 6;
  19. expect((x %= 4)).toBe(2);
  20. expect(x).toBe(2);
  21. x = 2;
  22. expect((x **= 3)).toBe(8);
  23. expect(x).toBe(8);
  24. x = 3;
  25. expect((x &= 2)).toBe(2);
  26. expect(x).toBe(2);
  27. x = 3;
  28. expect((x |= 4)).toBe(7);
  29. expect(x).toBe(7);
  30. x = 6;
  31. expect((x ^= 2)).toBe(4);
  32. expect(x).toBe(4);
  33. x = 2;
  34. expect((x <<= 2)).toBe(8);
  35. expect(x).toBe(8);
  36. x = 8;
  37. expect((x >>= 2)).toBe(2);
  38. expect(x).toBe(2);
  39. x = -(2 ** 32 - 10);
  40. expect((x >>>= 2)).toBe(2);
  41. expect(x).toBe(2);
  42. });
  43. test("evaluation order", () => {
  44. for (const op of [
  45. "=",
  46. "+=",
  47. "-=",
  48. "*=",
  49. "/=",
  50. "%=",
  51. "**=",
  52. "&=",
  53. "|=",
  54. "^=",
  55. "<<=",
  56. ">>=",
  57. ">>>=",
  58. ]) {
  59. var a = [];
  60. function b() {
  61. b.hasBeenCalled = true;
  62. throw Error();
  63. }
  64. function c() {
  65. c.hasBeenCalled = true;
  66. throw Error();
  67. }
  68. b.hasBeenCalled = false;
  69. c.hasBeenCalled = false;
  70. expect(() => {
  71. new Function(`a[b()] ${op} c()`)();
  72. }).toThrow(Error);
  73. expect(b.hasBeenCalled).toBeTrue();
  74. expect(c.hasBeenCalled).toBeFalse();
  75. }
  76. });