array-length-setter.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. describe("errors", () => {
  2. test("invalid array length value", () => {
  3. var a = [1, 2, 3];
  4. [undefined, "foo", -1, Infinity, -Infinity, NaN].forEach(value => {
  5. expect(() => {
  6. a.length = value;
  7. }).toThrowWithMessage(RangeError, "Invalid array length");
  8. expect(a).toHaveLength(3);
  9. });
  10. });
  11. });
  12. describe("normal behavior", () => {
  13. test("extend array by setting length", () => {
  14. var a = [1, 2, 3];
  15. a.length = 5;
  16. expect(a).toEqual([1, 2, 3, undefined, undefined]);
  17. });
  18. test("truncate array by setting length", () => {
  19. var a = [1, 2, 3];
  20. a.length = 2;
  21. expect(a).toEqual([1, 2]);
  22. a.length = 0;
  23. expect(a).toEqual([]);
  24. });
  25. test("length value is coerced to number if possible", () => {
  26. var a = [1, 2, 3];
  27. a.length = "42";
  28. expect(a).toHaveLength(42);
  29. a.length = [];
  30. expect(a).toHaveLength(0);
  31. a.length = true;
  32. expect(a).toHaveLength(1);
  33. });
  34. test("setting a huge array length", () => {
  35. var a = [];
  36. a.length = 0x80000000;
  37. expect(a.length).toEqual(0x80000000);
  38. var b = [];
  39. b.length = 0x80000001;
  40. expect(b.length).toEqual(0x80000001);
  41. });
  42. test("should not remove non-configurable values", () => {
  43. var a = [1, undefined, 3];
  44. Object.defineProperty(a, 1, { configurable: false, value: 2 });
  45. expect(a.length).toEqual(3);
  46. expect((a.length = 1)).toEqual(1);
  47. expect(a.length).toEqual(2);
  48. expect(a[1]).toEqual(2);
  49. });
  50. });
  51. describe("behavior when obj has Array prototype", () => {
  52. function ArrExtend() {}
  53. ArrExtend.prototype = [10, 11, 12];
  54. test("Has the properties from prototype", () => {
  55. var arr = new ArrExtend();
  56. expect(arr.length).toEqual(3);
  57. expect(arr[0]).toEqual(10);
  58. expect(arr[1]).toEqual(11);
  59. expect(arr[2]).toEqual(12);
  60. });
  61. test("Can override length to any value", () => {
  62. [null, "Hello friends :^)", -6, 0].forEach(value => {
  63. var arr = new ArrExtend();
  64. arr.length = value;
  65. expect(arr.length).toEqual(value);
  66. // should not wipe high values
  67. expect(arr[0]).toEqual(10);
  68. expect(arr[1]).toEqual(11);
  69. expect(arr[2]).toEqual(12);
  70. });
  71. });
  72. test("Can call array methods", () => {
  73. var arr = new ArrExtend();
  74. arr.push(1);
  75. expect(arr.length).toEqual(4);
  76. expect(arr[3]).toEqual(1);
  77. });
  78. test("If length overwritten uses that value", () => {
  79. [null, "Hello friends :^)", -6, 0].forEach(value => {
  80. var arr = new ArrExtend();
  81. arr.length = value;
  82. expect(arr.length).toEqual(value);
  83. arr.push(99);
  84. expect(arr.length).toEqual(1);
  85. expect(arr[0]).toEqual(99);
  86. // should not wipe higher value
  87. expect(arr[1]).toEqual(11);
  88. expect(arr[2]).toEqual(12);
  89. arr.push(100);
  90. expect(arr.length).toEqual(2);
  91. expect(arr[1]).toEqual(100);
  92. arr.length = 0;
  93. // should not wipe values since we are not an array
  94. expect(arr[0]).toEqual(99);
  95. expect(arr[1]).toEqual(100);
  96. expect(arr[2]).toEqual(12);
  97. });
  98. });
  99. });