exec.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. class Point {
  2. #x;
  3. #y;
  4. constructor(x = 0, y = 0) {
  5. this.#x = +x;
  6. this.#y = +y;
  7. }
  8. get x() { return this.#x }
  9. set x(value) { this.#x = +value }
  10. get y() { return this.#y }
  11. set y(value) { this.#y = +value }
  12. equals(p) { return this.#x === p.#x && this.#y === p.#y }
  13. toString() { return `Point<${ this.#x },${ this.#y }>` }
  14. }
  15. const p1 = new Point(1, 2);
  16. const p2 = new Point(2, 3);
  17. const p3 = new Point(1, 2);
  18. expect(p1.x).toBe(1);
  19. expect(p1.y).toBe(2);
  20. expect(p2.x).toBe(2);
  21. expect(p2.y).toBe(3);
  22. expect(p3.x).toBe(1);
  23. expect(p3.y).toBe(2);
  24. expect(p1.equals(p1)).toBe(true)
  25. expect(p1.equals(p2)).toBe(false)
  26. expect(p1.equals(p3)).toBe(true)
  27. expect(p2.equals(p1)).toBe(false)
  28. expect(p2.equals(p2)).toBe(true)
  29. expect(p2.equals(p3)).toBe(false)
  30. expect(p3.equals(p1)).toBe(true)
  31. expect(p3.equals(p2)).toBe(false)
  32. expect(p3.equals(p3)).toBe(true)
  33. expect(p1.toString()).toBe("Point<1,2>")
  34. expect(p2.toString()).toBe("Point<2,3>")
  35. expect(p3.toString()).toBe("Point<1,2>")
  36. p1.x += 1;
  37. p1.y = 3;
  38. p2.x -= 1;
  39. p2.y = 3;
  40. p3.x = 0;
  41. p3.y = 0;
  42. expect(p1.x).toBe(2);
  43. expect(p1.y).toBe(3);
  44. expect(p2.x).toBe(1);
  45. expect(p2.y).toBe(3);
  46. expect(p3.x).toBe(0);
  47. expect(p3.y).toBe(0);
  48. expect(p1.equals(p1)).toBe(true)
  49. expect(p1.equals(p2)).toBe(false)
  50. expect(p1.equals(p3)).toBe(false)
  51. expect(p2.equals(p1)).toBe(false)
  52. expect(p2.equals(p2)).toBe(true)
  53. expect(p2.equals(p3)).toBe(false)
  54. expect(p3.equals(p1)).toBe(false)
  55. expect(p3.equals(p2)).toBe(false)
  56. expect(p3.equals(p3)).toBe(true)
  57. expect(p1.toString()).toBe("Point<2,3>")
  58. expect(p2.toString()).toBe("Point<1,3>")
  59. expect(p3.toString()).toBe("Point<0,0>")