mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 04:12:43 +00:00 
			
		
		
		
	LibJS: Convert Array tests to new testing framework
This commit is contained in:
		
							parent
							
								
									8ebdf685a6
								
							
						
					
					
						commit
						461d90d042
					
				
					 33 changed files with 1315 additions and 1382 deletions
				
			
		|  | @ -1,28 +1,26 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.isArray).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | test("arguments that evaluate to false", () => { | ||||||
|     assert(Array.isArray.length === 1); |     expect(Array.isArray()).toBeFalse(); | ||||||
|  |     expect(Array.isArray("1")).toBeFalse(); | ||||||
|  |     expect(Array.isArray("foo")).toBeFalse(); | ||||||
|  |     expect(Array.isArray(1)).toBeFalse(); | ||||||
|  |     expect(Array.isArray(1, 2, 3)).toBeFalse(); | ||||||
|  |     expect(Array.isArray(undefined)).toBeFalse(); | ||||||
|  |     expect(Array.isArray(null)).toBeFalse(); | ||||||
|  |     expect(Array.isArray(Infinity)).toBeFalse(); | ||||||
|  |     expect(Array.isArray({})).toBeFalse(); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
|     assert(Array.isArray() === false); | test("arguments that evaluate to true", () => { | ||||||
|     assert(Array.isArray("1") === false); |     expect(Array.isArray([])).toBeTrue(); | ||||||
|     assert(Array.isArray("foo") === false); |     expect(Array.isArray([1])).toBeTrue(); | ||||||
|     assert(Array.isArray(1) === false); |     expect(Array.isArray([1, 2, 3])).toBeTrue(); | ||||||
|     assert(Array.isArray(1, 2, 3) === false); |     expect(Array.isArray(new Array())).toBeTrue(); | ||||||
|     assert(Array.isArray(undefined) === false); |     expect(Array.isArray(new Array(10))).toBeTrue(); | ||||||
|     assert(Array.isArray(null) === false); |     expect(Array.isArray(new Array("a", "b", "c"))).toBeTrue(); | ||||||
|     assert(Array.isArray(Infinity) === false); |  | ||||||
|     assert(Array.isArray({}) === false); |  | ||||||
| 
 |  | ||||||
|     assert(Array.isArray([]) === true); |  | ||||||
|     assert(Array.isArray([1]) === true); |  | ||||||
|     assert(Array.isArray([1, 2, 3]) === true); |  | ||||||
|     assert(Array.isArray(new Array()) === true); |  | ||||||
|     assert(Array.isArray(new Array(10)) === true); |  | ||||||
|     assert(Array.isArray(new Array("a", "b", "c")) === true); |  | ||||||
|     // FIXME: Array.prototype is supposed to be an array!
 |     // FIXME: Array.prototype is supposed to be an array!
 | ||||||
|     // assert(Array.isArray(Array.prototype) === true);
 |     // expect(Array.isArray(Array.prototype)).toBeTrue();
 | ||||||
| 
 | }); | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,55 +1,53 @@ | ||||||
| load("test-common.js"); | test("constructor properties", () => { | ||||||
|  |     expect(Array).toHaveLength(1); | ||||||
|  |     expect(Array.name).toBe("Array"); | ||||||
|  |     expect(Array.prototype.length).toBe(0); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.length === 1); |     test("invalid array length", () => { | ||||||
|     assert(Array.name === "Array"); |         [-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => { | ||||||
|     assert(Array.prototype.length === 0); |             expect(() => { | ||||||
| 
 |  | ||||||
|     assert(typeof Array() === "object"); |  | ||||||
|     assert(typeof new Array() === "object"); |  | ||||||
| 
 |  | ||||||
|     var a; |  | ||||||
| 
 |  | ||||||
|     a = new Array(5); |  | ||||||
|     assert(a.length === 5); |  | ||||||
| 
 |  | ||||||
|     a = new Array("5"); |  | ||||||
|     assert(a.length === 1); |  | ||||||
|     assert(a[0] === "5"); |  | ||||||
| 
 |  | ||||||
|     a = new Array(1, 2, 3); |  | ||||||
|     assert(a.length === 3); |  | ||||||
|     assert(a[0] === 1); |  | ||||||
|     assert(a[1] === 2); |  | ||||||
|     assert(a[2] === 3); |  | ||||||
| 
 |  | ||||||
|     a = new Array([1, 2, 3]); |  | ||||||
|     assert(a.length === 1); |  | ||||||
|     assert(a[0][0] === 1); |  | ||||||
|     assert(a[0][1] === 2); |  | ||||||
|     assert(a[0][2] === 3); |  | ||||||
| 
 |  | ||||||
|     a = new Array(1, 2, 3); |  | ||||||
|     Object.defineProperty(a, 3, { |  | ||||||
|         get() { |  | ||||||
|             return 10; |  | ||||||
|         }, |  | ||||||
|     }); |  | ||||||
|     assert(a.toString() === "1,2,3,10"); |  | ||||||
| 
 |  | ||||||
|     [-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => { |  | ||||||
|         assertThrowsError( |  | ||||||
|             () => { |  | ||||||
|                 new Array(value); |                 new Array(value); | ||||||
|             }, |             }).toThrowWithMessage(TypeError, "Invalid array length"); | ||||||
|             { |         }); | ||||||
|                 error: TypeError, |     }); | ||||||
|                 message: "Invalid array length", | }); | ||||||
|             } | 
 | ||||||
|         ); | describe("normal behavior", () => { | ||||||
|  |     test("typeof", () => { | ||||||
|  |         expect(typeof Array()).toBe("object"); | ||||||
|  |         expect(typeof new Array()).toBe("object"); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("constructor with single numeric argument", () => { | ||||||
| } catch (e) { |         var a = new Array(5); | ||||||
|     console.log("FAIL: " + e); |         expect(a instanceof Array).toBeTrue(); | ||||||
| } |         expect(a).toHaveLength(5); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("constructor with single non-numeric argument", () => { | ||||||
|  |         var a = new Array("5"); | ||||||
|  |         expect(a instanceof Array).toBeTrue(); | ||||||
|  |         expect(a).toHaveLength(1); | ||||||
|  |         expect(a[0]).toBe("5"); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("constructor with multiple numeric arguments", () => { | ||||||
|  |         var a = new Array(1, 2, 3); | ||||||
|  |         expect(a instanceof Array).toBeTrue(); | ||||||
|  |         expect(a).toHaveLength(3); | ||||||
|  |         expect(a[0]).toBe(1); | ||||||
|  |         expect(a[1]).toBe(2); | ||||||
|  |         expect(a[2]).toBe(3); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("constructor with single array argument", () => { | ||||||
|  |         var a = new Array([1, 2, 3]); | ||||||
|  |         expect(a instanceof Array).toBeTrue(); | ||||||
|  |         expect(a).toHaveLength(1); | ||||||
|  |         expect(a[0][0]).toBe(1); | ||||||
|  |         expect(a[0][1]).toBe(2); | ||||||
|  |         expect(a[0][2]).toBe(3); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,50 +1,59 @@ | ||||||
| load("test-common.js"); | test("length is 0", () => { | ||||||
|  |     expect(Array.of).toHaveLength(0); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("normal behavior", () => { | ||||||
|     assert(Array.of.length === 0); |     test("single numeric argument", () => { | ||||||
| 
 |         var a = Array.of(5); | ||||||
|     assert(typeof Array.of() === "object"); |         expect(a instanceof Array).toBeTrue(); | ||||||
| 
 |         expect(a).toHaveLength(1); | ||||||
|     var a; |         expect(a[0]).toBe(5); | ||||||
| 
 |  | ||||||
|     a = Array.of(5); |  | ||||||
|     assert(a.length === 1); |  | ||||||
|     assert(a[0] === 5); |  | ||||||
| 
 |  | ||||||
|     a = Array.of("5"); |  | ||||||
|     assert(a.length === 1); |  | ||||||
|     assert(a[0] === "5"); |  | ||||||
| 
 |  | ||||||
|     a = Array.of(Infinity); |  | ||||||
|     assert(a.length === 1); |  | ||||||
|     assert(a[0] === Infinity); |  | ||||||
| 
 |  | ||||||
|     a = Array.of(1, 2, 3); |  | ||||||
|     assert(a.length === 3); |  | ||||||
|     assert(a[0] === 1); |  | ||||||
|     assert(a[1] === 2); |  | ||||||
|     assert(a[2] === 3); |  | ||||||
| 
 |  | ||||||
|     a = Array.of([1, 2, 3]); |  | ||||||
|     assert(a.length === 1); |  | ||||||
|     assert(a[0][0] === 1); |  | ||||||
|     assert(a[0][1] === 2); |  | ||||||
|     assert(a[0][2] === 3); |  | ||||||
| 
 |  | ||||||
|     let t = [1, 2, 3]; |  | ||||||
|     Object.defineProperty(t, 3, { |  | ||||||
|         get() { |  | ||||||
|             return 4; |  | ||||||
|         }, |  | ||||||
|     }); |     }); | ||||||
|     a = Array.of(...t); |  | ||||||
|     assert(a.length === 4); |  | ||||||
|     assert(a[0] === 1); |  | ||||||
|     assert(a[1] === 2); |  | ||||||
|     assert(a[2] === 3); |  | ||||||
|     assert(a[3] === 4); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("single non-numeric argument", () => { | ||||||
| } catch (e) { |         var a = Array.of("5"); | ||||||
|     console.log("FAIL: " + e); |         expect(a instanceof Array).toBeTrue(); | ||||||
| } |         expect(a).toHaveLength(1); | ||||||
|  |         expect(a[0]).toBe("5"); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("single infinite numeric argument", () => { | ||||||
|  |         var a = Array.of(Infinity); | ||||||
|  |         expect(a instanceof Array).toBeTrue(); | ||||||
|  |         expect(a).toHaveLength(1); | ||||||
|  |         expect(a[0]).toBe(Infinity); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("multiple numeric arguments", () => { | ||||||
|  |         var a = Array.of(1, 2, 3); | ||||||
|  |         expect(a instanceof Array).toBeTrue(); | ||||||
|  |         expect(a).toHaveLength(3); | ||||||
|  |         expect(a[0]).toBe(1); | ||||||
|  |         expect(a[1]).toBe(2); | ||||||
|  |         expect(a[2]).toBe(3); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("single array argument", () => { | ||||||
|  |         var a = Array.of([1, 2, 3]); | ||||||
|  |         expect(a instanceof Array).toBeTrue(); | ||||||
|  |         expect(a).toHaveLength(1); | ||||||
|  |         expect(a[0][0]).toBe(1); | ||||||
|  |         expect(a[0][1]).toBe(2); | ||||||
|  |         expect(a[0][2]).toBe(3); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("getter property is included in returned array", () => { | ||||||
|  |         var t = [1, 2, 3]; | ||||||
|  |         Object.defineProperty(t, 3, { | ||||||
|  |             get() { | ||||||
|  |                 return 4; | ||||||
|  |             }, | ||||||
|  |         }); | ||||||
|  |         var a = Array.of(...t); | ||||||
|  |         expect(a).toHaveLength(4); | ||||||
|  |         expect(a[0]).toBe(1); | ||||||
|  |         expect(a[1]).toBe(2); | ||||||
|  |         expect(a[2]).toBe(3); | ||||||
|  |         expect(a[3]).toBe(4); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,164 +1,150 @@ | ||||||
| load("test-common.js"); | describe("ability to work with generic non-array objects", () => { | ||||||
|  |     test("push, pop", () => { | ||||||
|  |         [undefined, "foo", -42, 0].forEach(length => { | ||||||
|  |             const o = { length }; | ||||||
| 
 | 
 | ||||||
| try { |             expect(Array.prototype.push.call(o, "foo")).toBe(1); | ||||||
|     [undefined, "foo", -42, 0].forEach(length => { |             expect(o).toHaveLength(1); | ||||||
|         const o = { length }; |             expect(o[0]).toBe("foo"); | ||||||
|  |             expect(Array.prototype.push.call(o, "bar", "baz")).toBe(3); | ||||||
|  |             expect(o).toHaveLength(3); | ||||||
|  |             expect(o[0]).toBe("foo"); | ||||||
|  |             expect(o[1]).toBe("bar"); | ||||||
|  |             expect(o[2]).toBe("baz"); | ||||||
| 
 | 
 | ||||||
|         assert(Array.prototype.push.call(o, "foo") === 1); |             expect(Array.prototype.pop.call(o)).toBe("baz"); | ||||||
|         assert(o.length === 1); |             expect(o).toHaveLength(2); | ||||||
|         assert(o[0] === "foo"); |             expect(Array.prototype.pop.call(o)).toBe("bar"); | ||||||
|         assert(Array.prototype.push.call(o, "bar", "baz") === 3); |             expect(o).toHaveLength(1); | ||||||
|         assert(o.length === 3); |             expect(Array.prototype.pop.call(o)).toBe("foo"); | ||||||
|         assert(o[0] === "foo"); |             expect(o).toHaveLength(0); | ||||||
|         assert(o[1] === "bar"); |             expect(Array.prototype.pop.call(o)).toBeUndefined(); | ||||||
|         assert(o[2] === "baz"); |             expect(o).toHaveLength(0); | ||||||
| 
 | 
 | ||||||
|         assert(Array.prototype.pop.call(o) === "baz"); |             o.length = length; | ||||||
|         assert(o.length === 2); |             expect(Array.prototype.pop.call(o)).toBeUndefined(); | ||||||
|         assert(Array.prototype.pop.call(o) === "bar"); |             expect(o).toHaveLength(0); | ||||||
|         assert(o.length === 1); |         }); | ||||||
|         assert(Array.prototype.pop.call(o) === "foo"); |  | ||||||
|         assert(o.length === 0); |  | ||||||
|         assert(Array.prototype.pop.call(o) === undefined); |  | ||||||
|         assert(o.length === 0); |  | ||||||
| 
 |  | ||||||
|         o.length = length; |  | ||||||
|         assert(Array.prototype.pop.call(o) === undefined); |  | ||||||
|         assert(o.length === 0); |  | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     { |     test("splice", () => { | ||||||
|         const o = { length: 3, 0: "hello", 2: "serenity" }; |         const o = { length: 3, 0: "hello", 2: "serenity" }; | ||||||
|         const removed = Array.prototype.splice.call(o, 0, 2, "hello", "friends"); |         const removed = Array.prototype.splice.call(o, 0, 2, "hello", "friends"); | ||||||
|         assert(o.length === 3); |         expect(o).toHaveLength(3); | ||||||
|         assert(o[0] === "hello"); |         expect(o[0]).toBe("hello"); | ||||||
|         assert(o[1] === "friends"); |         expect(o[1]).toBe("friends"); | ||||||
|         assert(o[2] === "serenity"); |         expect(o[2]).toBe("serenity"); | ||||||
|         assert(removed.length === 2); |         expect(removed).toHaveLength(2); | ||||||
|         assert(removed[0] === "hello"); |         expect(removed[0]).toBe("hello"); | ||||||
|         assert(removed[1] === undefined); |         expect(removed[1]).toBeUndefined(); | ||||||
|     } |     }); | ||||||
| 
 | 
 | ||||||
|     { |     test("join", () => { | ||||||
|         assert(Array.prototype.join.call({}) === ""); |         expect(Array.prototype.join.call({})).toBe(""); | ||||||
|         assert(Array.prototype.join.call({ length: "foo" }) === ""); |         expect(Array.prototype.join.call({ length: "foo" })).toBe(""); | ||||||
|         assert(Array.prototype.join.call({ length: 3 }) === ",,"); |         expect(Array.prototype.join.call({ length: 3 })).toBe(",,"); | ||||||
|         assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" }) === "foo,bar"); |         expect(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" })).toBe("foo,bar"); | ||||||
|         assert( |         expect(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" })).toBe( | ||||||
|             Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" }) === "foo,bar" |             "foo,bar" | ||||||
|         ); |         ); | ||||||
|         assert(Array.prototype.join.call({ length: 3, 1: "bar" }, "~") === "~bar~"); |         expect(Array.prototype.join.call({ length: 3, 1: "bar" }, "~")).toBe("~bar~"); | ||||||
|         assert( |         expect(Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~")).toBe( | ||||||
|             Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~") === |             "foo~bar~baz" | ||||||
|                 "foo~bar~baz" |  | ||||||
|         ); |         ); | ||||||
|     } |     }); | ||||||
| 
 | 
 | ||||||
|     { |     // FIXME: test-js asserts when this is just called "toString" ಠ_ಠ
 | ||||||
|         assert(Array.prototype.toString.call({}) === "[object Object]"); |     test("toString (FIXME)", () => { | ||||||
|         assert(Array.prototype.toString.call({ join: "foo" }) === "[object Object]"); |         expect(Array.prototype.toString.call({})).toBe("[object Object]"); | ||||||
|         assert(Array.prototype.toString.call({ join: () => "foo" }) === "foo"); |         expect(Array.prototype.toString.call({ join: "foo" })).toBe("[object Object]"); | ||||||
|     } |         expect(Array.prototype.toString.call({ join: () => "foo" })).toBe("foo"); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     { |     test("indexOf", () => { | ||||||
|         assert(Array.prototype.indexOf.call({}) === -1); |         expect(Array.prototype.indexOf.call({})).toBe(-1); | ||||||
|         assert(Array.prototype.indexOf.call({ 0: undefined }) === -1); |         expect(Array.prototype.indexOf.call({ 0: undefined })).toBe(-1); | ||||||
|         assert(Array.prototype.indexOf.call({ length: 1, 0: undefined }) === 0); |         expect(Array.prototype.indexOf.call({ length: 1, 0: undefined })).toBe(0); | ||||||
|         assert(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo") === -1); |         expect(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo")).toBe(-1); | ||||||
|         assert(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo") === 2); |         expect(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo")).toBe(2); | ||||||
|         assert(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3) === 4); |         expect(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3)).toBe(4); | ||||||
|     } |     }); | ||||||
| 
 | 
 | ||||||
|     { |     test("lastIndexOf", () => { | ||||||
|         assert(Array.prototype.lastIndexOf.call({}) === -1); |         expect(Array.prototype.lastIndexOf.call({})).toBe(-1); | ||||||
|         assert(Array.prototype.lastIndexOf.call({ 0: undefined }) === -1); |         expect(Array.prototype.lastIndexOf.call({ 0: undefined })).toBe(-1); | ||||||
|         assert(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined }) === 0); |         expect(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined })).toBe(0); | ||||||
|         assert(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo") === -1); |         expect(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo")).toBe(-1); | ||||||
|         assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo") === 2); |         expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo")).toBe(2); | ||||||
|         assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo") === 4); |         expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo")).toBe(4); | ||||||
|         assert( |         expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2)).toBe( | ||||||
|             Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2) === 2 |             2 | ||||||
|         ); |         ); | ||||||
|     } |     }); | ||||||
| 
 | 
 | ||||||
|     { |     test("includes", () => { | ||||||
|         assert(Array.prototype.includes.call({}) === false); |         expect(Array.prototype.includes.call({})).toBeFalse(); | ||||||
|         assert(Array.prototype.includes.call({ 0: undefined }) === false); |         expect(Array.prototype.includes.call({ 0: undefined })).toBeFalse(); | ||||||
|         assert(Array.prototype.includes.call({ length: 1, 0: undefined }) === true); |         expect(Array.prototype.includes.call({ length: 1, 0: undefined })).toBeTrue(); | ||||||
|         assert(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo") === false); |         expect(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo")).toBeFalse(); | ||||||
|         assert(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo") === true); |         expect(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo")).toBeTrue(); | ||||||
|     } |     }); | ||||||
| 
 | 
 | ||||||
|     const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" }; |     const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" }; | ||||||
| 
 | 
 | ||||||
|     { |     test("every", () => { | ||||||
|         assertVisitsAll( |         const visited = []; | ||||||
|             visit => { |         Array.prototype.every.call(o, value => { | ||||||
|                 Array.prototype.every.call(o, function (value) { |             visited.push(value); | ||||||
|                     visit(value); |             return true; | ||||||
|                     return true; |         }); | ||||||
|                 }); |         expect(visited).toEqual(["foo", "bar", "baz"]); | ||||||
|             }, |  | ||||||
|             ["foo", "bar", "baz"] |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     ["find", "findIndex"].forEach(name => { |  | ||||||
|         assertVisitsAll( |  | ||||||
|             visit => { |  | ||||||
|                 Array.prototype[name].call(o, function (value) { |  | ||||||
|                     visit(value); |  | ||||||
|                     return false; |  | ||||||
|                 }); |  | ||||||
|             }, |  | ||||||
|             ["foo", "bar", undefined, "baz", undefined] |  | ||||||
|         ); |  | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     ["filter", "forEach", "map", "some"].forEach(name => { |     test("find, findIndex", () => { | ||||||
|         assertVisitsAll( |         ["find", "findIndex"].forEach(name => { | ||||||
|             visit => { |             const visited = []; | ||||||
|                 Array.prototype[name].call(o, function (value) { |             Array.prototype[name].call(o, value => { | ||||||
|                     visit(value); |                 visited.push(value); | ||||||
|                     return false; |                 return false; | ||||||
|                 }); |             }); | ||||||
|             }, |             expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]); | ||||||
|             ["foo", "bar", "baz"] |         }); | ||||||
|         ); |  | ||||||
|     }); |     }); | ||||||
|     { |  | ||||||
|         assertVisitsAll( |  | ||||||
|             visit => { |  | ||||||
|                 Array.prototype.reduce.call( |  | ||||||
|                     o, |  | ||||||
|                     function (_, value) { |  | ||||||
|                         visit(value); |  | ||||||
|                         return false; |  | ||||||
|                     }, |  | ||||||
|                     "initial" |  | ||||||
|                 ); |  | ||||||
|             }, |  | ||||||
|             ["foo", "bar", "baz"] |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     { |     test("filter, forEach, map, some", () => { | ||||||
|         assertVisitsAll( |         ["filter", "forEach", "map", "some"].forEach(name => { | ||||||
|             visit => { |             const visited = []; | ||||||
|                 Array.prototype.reduceRight.call( |             Array.prototype[name].call(o, value => { | ||||||
|                     o, |                 visited.push(value); | ||||||
|                     function (_, value) { |                 return false; | ||||||
|                         visit(value); |             }); | ||||||
|                         return false; |             expect(visited).toEqual(["foo", "bar", "baz"]); | ||||||
|                     }, |         }); | ||||||
|                     "initial" |     }); | ||||||
|                 ); |  | ||||||
|             }, |  | ||||||
|             ["baz", "bar", "foo"] |  | ||||||
|         ); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("reduce", () => { | ||||||
| } catch (e) { |         const visited = []; | ||||||
|     console.log("FAIL: " + e); |         Array.prototype.reduce.call( | ||||||
| } |             o, | ||||||
|  |             (_, value) => { | ||||||
|  |                 visited.push(value); | ||||||
|  |                 return false; | ||||||
|  |             }, | ||||||
|  |             "initial" | ||||||
|  |         ); | ||||||
|  |         expect(visited).toEqual(["foo", "bar", "baz"]); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("reduceRight", () => { | ||||||
|  |         const visited = []; | ||||||
|  |         Array.prototype.reduceRight.call( | ||||||
|  |             o, | ||||||
|  |             (_, value) => { | ||||||
|  |                 visited.push(value); | ||||||
|  |                 return false; | ||||||
|  |             }, | ||||||
|  |             "initial" | ||||||
|  |         ); | ||||||
|  |         expect(visited).toEqual(["baz", "bar", "foo"]); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,36 +1,43 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.concat).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("normal behavior", () => { | ||||||
|     assert(Array.prototype.concat.length === 1); |     var array = ["hello"]; | ||||||
| 
 | 
 | ||||||
|     var array = ["hello", "friends"]; |     test("no arguments", () => { | ||||||
|  |         var concatenated = array.concat(); | ||||||
|  |         expect(array).toHaveLength(1); | ||||||
|  |         expect(concatenated).toHaveLength(1); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     var array_concat = array.concat(); |     test("single argument", () => { | ||||||
|     assert(array_concat.length === array.length); |         var concatenated = array.concat("friends"); | ||||||
|  |         expect(array).toHaveLength(1); | ||||||
|  |         expect(concatenated).toHaveLength(2); | ||||||
|  |         expect(concatenated[0]).toBe("hello"); | ||||||
|  |         expect(concatenated[1]).toBe("friends"); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     array_concat = array.concat(1); |     test("single array argument", () => { | ||||||
|     assert(array_concat.length === 3); |         var concatenated = array.concat([1, 2, 3]); | ||||||
|     assert(array_concat[2] === 1); |         expect(array).toHaveLength(1); | ||||||
|  |         expect(concatenated).toHaveLength(4); | ||||||
|  |         expect(concatenated[0]).toBe("hello"); | ||||||
|  |         expect(concatenated[1]).toBe(1); | ||||||
|  |         expect(concatenated[2]).toBe(2); | ||||||
|  |         expect(concatenated[3]).toBe(3); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     array_concat = array.concat([1, 2, 3]); |     test("multiple arguments", () => { | ||||||
|     assert(array_concat.length === 5); |         var concatenated = array.concat(false, "serenity", { name: "libjs" }, [1, [2, 3]]); | ||||||
|     assert(array_concat[2] === 1); |         expect(array).toHaveLength(1); | ||||||
|     assert(array_concat[3] === 2); |         expect(concatenated).toHaveLength(6); | ||||||
|     assert(array_concat[4] === 3); |         expect(concatenated[0]).toBe("hello"); | ||||||
| 
 |         expect(concatenated[1]).toBeFalse(); | ||||||
|     array_concat = array.concat(false, "serenity"); |         expect(concatenated[2]).toBe("serenity"); | ||||||
|     assert(array_concat.length === 4); |         expect(concatenated[3]).toEqual({ name: "libjs" }); | ||||||
|     assert(array_concat[2] === false); |         expect(concatenated[4]).toBe(1); | ||||||
|     assert(array_concat[3] === "serenity"); |         expect(concatenated[5]).toEqual([2, 3]); | ||||||
| 
 |     }); | ||||||
|     array_concat = array.concat({ name: "libjs" }, [1, [2, 3]]); | }); | ||||||
|     assert(array_concat.length === 5); |  | ||||||
|     assert(array_concat[2].name === "libjs"); |  | ||||||
|     assert(array_concat[3] === 1); |  | ||||||
|     assert(array_concat[4][0] === 2); |  | ||||||
|     assert(array_concat[4][1] === 3); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,49 +1,55 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.every).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.every.length === 1); |     test("requires at least one argument", () => { | ||||||
|  |         expect(() => { | ||||||
|  |             [].every(); | ||||||
|  |         }).toThrowWithMessage(TypeError, "Array.prototype.every() requires at least one argument"); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("callback must be a function", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [].every(undefined); |             [].every(undefined); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, | }); | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     var arrayOne = ["serenity", { test: "serenity" }]; | describe("normal behavior", () => { | ||||||
|     var arrayTwo = [true, false, 1, 2, 3, "3"]; |     test("basic functionality", () => { | ||||||
|  |         var arrayOne = ["serenity", { test: "serenity" }]; | ||||||
|  |         var arrayTwo = [true, false, 1, 2, 3, "3"]; | ||||||
| 
 | 
 | ||||||
|     assert(arrayOne.every(value => value === "hello") === false); |         expect(arrayOne.every(value => value === "hello")).toBeFalse(); | ||||||
|     assert(arrayOne.every(value => value === "serenity") === false); |         expect(arrayOne.every(value => value === "serenity")).toBeFalse(); | ||||||
|     assert(arrayOne.every((value, index, arr) => index < 2) === true); |         expect(arrayOne.every((value, index, arr) => index < 2)).toBeTrue(); | ||||||
|     assert(arrayOne.every(value => typeof value === "string") === false); |         expect(arrayOne.every(value => typeof value === "string")).toBeFalse(); | ||||||
|     assert(arrayOne.every(value => arrayOne.pop()) === true); |         expect(arrayOne.every(value => arrayOne.pop())).toBeTrue(); | ||||||
| 
 | 
 | ||||||
|     assert(arrayTwo.every((value, index, arr) => index > 0) === false); |         expect(arrayTwo.every((value, index, arr) => index > 0)).toBeFalse(); | ||||||
|     assert(arrayTwo.every((value, index, arr) => index >= 0) === true); |         expect(arrayTwo.every((value, index, arr) => index >= 0)).toBeTrue(); | ||||||
|     assert(arrayTwo.every(value => typeof value !== "string") === false); |         expect(arrayTwo.every(value => typeof value !== "string")).toBeFalse(); | ||||||
|     assert(arrayTwo.every(value => typeof value === "number") === false); |         expect(arrayTwo.every(value => typeof value === "number")).toBeFalse(); | ||||||
|     assert(arrayTwo.every(value => value > 0) === false); |         expect(arrayTwo.every(value => value > 0)).toBeFalse(); | ||||||
|     assert(arrayTwo.every(value => value >= 0 && value < 4) === true); |         expect(arrayTwo.every(value => value >= 0 && value < 4)).toBeTrue(); | ||||||
|     assert(arrayTwo.every(value => arrayTwo.pop()) === true); |         expect(arrayTwo.every(value => arrayTwo.pop())).toBeTrue(); | ||||||
| 
 | 
 | ||||||
|     assert(["", "hello", "friends", "serenity"].every(value => value.length >= 0) === true); |         expect(["", "hello", "friends", "serenity"].every(value => value.length >= 0)).toBeTrue(); | ||||||
|     assert([].every(value => value === 1) === true); |     }); | ||||||
| 
 | 
 | ||||||
|     arrayTwo = [true, false, 1, 2, 3, "3"]; |     test("empty array", () => { | ||||||
|  |         expect([].every(value => value === 1)).toBeTrue(); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     // Every only goes up to the original length.
 |     test("elements past the initial array size are ignored", () => { | ||||||
|     assert( |         var array = [1, 2, 3, 4, 5]; | ||||||
|         arrayTwo.every((value, index, arr) => { |  | ||||||
|             arr.push("serenity"); |  | ||||||
|             return value < 4; |  | ||||||
|         }) === true |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |         expect( | ||||||
| } catch (e) { |             arrayTwo.every((value, index, arr) => { | ||||||
|     console.log("FAIL: " + e); |                 arr.push(6); | ||||||
| } |                 return value <= 5; | ||||||
|  |             }) | ||||||
|  |         ).toBeTrue(); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,23 +1,20 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
| 
 |     expect(Array.prototype.fill).toHaveLength(1); | ||||||
| try { | }); | ||||||
|     assert(Array.prototype.fill.length === 1); |  | ||||||
| 
 | 
 | ||||||
|  | test("basic functionality", () => { | ||||||
|     var array = [1, 2, 3, 4]; |     var array = [1, 2, 3, 4]; | ||||||
|     assertArrayEquals(array.fill(0, 2, 4), [1, 2, 0, 0]); |  | ||||||
|     assertArrayEquals(array.fill(5, 1), [1, 5, 5, 5]); |  | ||||||
|     assertArrayEquals(array.fill(6), [6, 6, 6, 6]); |  | ||||||
| 
 | 
 | ||||||
|     assertArrayEquals([1, 2, 3].fill(4), [4, 4, 4]); |     expect(array.fill(0, 2, 4)).toEqual([1, 2, 0, 0]); | ||||||
|     assertArrayEquals([1, 2, 3].fill(4, 1), [1, 4, 4]); |     expect(array.fill(5, 1)).toEqual([1, 5, 5, 5]); | ||||||
|     assertArrayEquals([1, 2, 3].fill(4, 1, 2), [1, 4, 3]); |     expect(array.fill(6)).toEqual([6, 6, 6, 6]); | ||||||
|     assertArrayEquals([1, 2, 3].fill(4, 3, 3), [1, 2, 3]); |  | ||||||
|     assertArrayEquals([1, 2, 3].fill(4, -3, -2), [4, 2, 3]); |  | ||||||
|     assertArrayEquals([1, 2, 3].fill(4, NaN, NaN), [1, 2, 3]); |  | ||||||
|     assertArrayEquals([1, 2, 3].fill(4, 3, 5), [1, 2, 3]); |  | ||||||
|     assertArrayEquals(Array(3).fill(4), [4, 4, 4]); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     expect([1, 2, 3].fill(4)).toEqual([4, 4, 4]); | ||||||
| } catch (e) { |     expect([1, 2, 3].fill(4, 1)).toEqual([1, 4, 4]); | ||||||
|     console.log("FAIL: " + e); |     expect([1, 2, 3].fill(4, 1, 2)).toEqual([1, 4, 3]); | ||||||
| } |     expect([1, 2, 3].fill(4, 3, 3)).toEqual([1, 2, 3]); | ||||||
|  |     expect([1, 2, 3].fill(4, -3, -2)).toEqual([4, 2, 3]); | ||||||
|  |     expect([1, 2, 3].fill(4, NaN, NaN)).toEqual([1, 2, 3]); | ||||||
|  |     expect([1, 2, 3].fill(4, 3, 5)).toEqual([1, 2, 3]); | ||||||
|  |     expect(Array(3).fill(4)).toEqual([4, 4, 4]); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,78 +1,68 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.filter).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.filter.length === 1); |     test("requires at least one argument", () => { | ||||||
| 
 |         expect(() => { | ||||||
|     assertThrowsError( |  | ||||||
|         () => { |  | ||||||
|             [].filter(); |             [].filter(); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "Array.prototype.filter() requires at least one argument"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, |  | ||||||
|             message: "Array.prototype.filter() requires at least one argument", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("callback must be a function", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [].filter(undefined); |             [].filter(undefined); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, | }); | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     var callbackCalled = 0; | describe("normal behavior", () => { | ||||||
|     var callback = () => { |     test("never calls callback with empty array", () => { | ||||||
|         callbackCalled++; |         var callbackCalled = 0; | ||||||
|     }; |         expect( | ||||||
|  |             [].filter(() => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toEqual([]); | ||||||
|  |         expect(callbackCalled).toBe(0); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assert([].filter(callback).length === 0); |     test("calls callback once for every item", () => { | ||||||
|     assert(callbackCalled === 0); |         var callbackCalled = 0; | ||||||
|  |         expect( | ||||||
|  |             [1, 2, 3].filter(() => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toEqual([]); | ||||||
|  |         expect(callbackCalled).toBe(3); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assert([1, 2, 3].filter(callback).length === 0); |     test("can filter based on callback return value", () => { | ||||||
|     assert(callbackCalled === 3); |         var evenNumbers = [0, 1, 2, 3, 4, 5, 6, 7].filter(x => x % 2 === 0); | ||||||
|  |         expect(evenNumbers).toEqual([0, 2, 4, 6]); | ||||||
| 
 | 
 | ||||||
|     var evenNumbers = [0, 1, 2, 3, 4, 5, 6, 7].filter(x => x % 2 === 0); |         var fruits = [ | ||||||
|     assert(evenNumbers.length === 4); |             "Apple", | ||||||
|     assert(evenNumbers[0] === 0); |             "Banana", | ||||||
|     assert(evenNumbers[1] === 2); |             "Blueberry", | ||||||
|     assert(evenNumbers[2] === 4); |             "Grape", | ||||||
|     assert(evenNumbers[3] === 6); |             "Mango", | ||||||
| 
 |             "Orange", | ||||||
|     var fruits = [ |             "Peach", | ||||||
|         "Apple", |             "Pineapple", | ||||||
|         "Banana", |             "Raspberry", | ||||||
|         "Blueberry", |             "Watermelon", | ||||||
|         "Grape", |         ]; | ||||||
|         "Mango", |         const filterItems = (arr, query) => { | ||||||
|         "Orange", |             return arr.filter(el => el.toLowerCase().indexOf(query.toLowerCase()) !== -1); | ||||||
|         "Peach", |         }; | ||||||
|         "Pineapple", |         expect(filterItems(fruits, "Berry")).toEqual(["Blueberry", "Raspberry"]); | ||||||
|         "Raspberry", |         expect(filterItems(fruits, "P")).toEqual([ | ||||||
|         "Watermelon", |             "Apple", | ||||||
|     ]; |             "Grape", | ||||||
|     const filterItems = (arr, query) => { |             "Peach", | ||||||
|         return arr.filter(el => el.toLowerCase().indexOf(query.toLowerCase()) !== -1); |             "Pineapple", | ||||||
|     }; |             "Raspberry", | ||||||
| 
 |         ]); | ||||||
|     var results; |     }); | ||||||
| 
 | }); | ||||||
|     results = filterItems(fruits, "Berry"); |  | ||||||
|     assert(results.length === 2); |  | ||||||
|     assert(results[0] === "Blueberry"); |  | ||||||
|     assert(results[1] === "Raspberry"); |  | ||||||
| 
 |  | ||||||
|     results = filterItems(fruits, "P"); |  | ||||||
|     assert(results.length === 5); |  | ||||||
|     assert(results[0] === "Apple"); |  | ||||||
|     assert(results[1] === "Grape"); |  | ||||||
|     assert(results[2] === "Peach"); |  | ||||||
|     assert(results[3] === "Pineapple"); |  | ||||||
|     assert(results[4] === "Raspberry"); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,54 +1,65 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.find).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.find.length === 1); |     test("requires at least one argument", () => { | ||||||
| 
 |         expect(() => { | ||||||
|     assertThrowsError( |             [].find(); | ||||||
|         () => { |         }).toThrowWithMessage(TypeError, "Array.prototype.find() requires at least one argument"); | ||||||
|             [].find(undefined); |  | ||||||
|         }, |  | ||||||
|         { |  | ||||||
|             error: TypeError, |  | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 |  | ||||||
|     var array = ["hello", "friends", 1, 2, false]; |  | ||||||
| 
 |  | ||||||
|     assert(array.find(value => value === "hello") === "hello"); |  | ||||||
|     assert(array.find((value, index, arr) => index === 1) === "friends"); |  | ||||||
|     assert(array.find(value => value == "1") === 1); |  | ||||||
|     assert(array.find(value => value === 1) === 1); |  | ||||||
|     assert(array.find(value => typeof value !== "string") === 1); |  | ||||||
|     assert(array.find(value => typeof value === "boolean") === false); |  | ||||||
|     assert(array.find(value => value > 1) === 2); |  | ||||||
|     assert(array.find(value => value > 1 && value < 3) === 2); |  | ||||||
|     assert(array.find(value => value > 100) === undefined); |  | ||||||
|     assert([].find(value => value === 1) === undefined); |  | ||||||
| 
 |  | ||||||
|     var callbackCalled = 0; |  | ||||||
|     var callback = () => { |  | ||||||
|         callbackCalled++; |  | ||||||
|     }; |  | ||||||
| 
 |  | ||||||
|     [].find(callback); |  | ||||||
|     assert(callbackCalled === 0); |  | ||||||
| 
 |  | ||||||
|     [1, 2, 3].find(callback); |  | ||||||
|     assert(callbackCalled === 3); |  | ||||||
| 
 |  | ||||||
|     callbackCalled = 0; |  | ||||||
|     [1, , , "foo", , undefined, , ,].find(callback); |  | ||||||
|     assert(callbackCalled === 8); |  | ||||||
| 
 |  | ||||||
|     callbackCalled = 0; |  | ||||||
|     [1, , , "foo", , undefined, , ,].find(value => { |  | ||||||
|         callbackCalled++; |  | ||||||
|         return value === undefined; |  | ||||||
|     }); |     }); | ||||||
|     assert(callbackCalled === 2); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("callback must be a function", () => { | ||||||
| } catch (e) { |         expect(() => { | ||||||
|     console.log("FAIL: " + e); |             [].find(undefined); | ||||||
| } |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | describe("normal behavior", () => { | ||||||
|  |     test("basic functionality", () => { | ||||||
|  |         var array = ["hello", "friends", 1, 2, false]; | ||||||
|  | 
 | ||||||
|  |         expect(array.find(value => value === "hello")).toBe("hello"); | ||||||
|  |         expect(array.find((value, index, arr) => index === 1)).toBe("friends"); | ||||||
|  |         expect(array.find(value => value == "1")).toBe(1); | ||||||
|  |         expect(array.find(value => value === 1)).toBe(1); | ||||||
|  |         expect(array.find(value => typeof value !== "string")).toBe(1); | ||||||
|  |         expect(array.find(value => typeof value === "boolean")).toBeFalse(); | ||||||
|  |         expect(array.find(value => value > 1)).toBe(2); | ||||||
|  |         expect(array.find(value => value > 1 && value < 3)).toBe(2); | ||||||
|  |         expect(array.find(value => value > 100)).toBeUndefined(); | ||||||
|  |         expect([].find(value => value === 1)).toBeUndefined(); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("never calls callback with empty array", () => { | ||||||
|  |         var callbackCalled = 0; | ||||||
|  |         expect( | ||||||
|  |             [].find(() => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toBeUndefined(); | ||||||
|  |         expect(callbackCalled).toBe(0); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("calls callback once for every item", () => { | ||||||
|  |         var callbackCalled = 0; | ||||||
|  |         expect( | ||||||
|  |             [1, 2, 3].find(() => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toBeUndefined(); | ||||||
|  |         expect(callbackCalled).toBe(3); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("empty slots are treated as undefined", () => { | ||||||
|  |         var callbackCalled = 0; | ||||||
|  |         expect( | ||||||
|  |             [1, , , "foo", , undefined, , ,].find(value => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |                 return value === undefined; | ||||||
|  |             }) | ||||||
|  |         ).toBeUndefined(); | ||||||
|  |         expect(callbackCalled).toBe(2); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,54 +1,68 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.findIndex).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.findIndex.length === 1); |     test("requires at least one argument", () => { | ||||||
| 
 |         expect(() => { | ||||||
|     assertThrowsError( |             [].findIndex(); | ||||||
|         () => { |         }).toThrowWithMessage( | ||||||
|             [].findIndex(undefined); |             TypeError, | ||||||
|         }, |             "Array.prototype.findIndex() requires at least one argument" | ||||||
|         { |         ); | ||||||
|             error: TypeError, |  | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 |  | ||||||
|     var array = ["hello", "friends", 1, 2, false]; |  | ||||||
| 
 |  | ||||||
|     assert(array.findIndex(value => value === "hello") === 0); |  | ||||||
|     assert(array.findIndex((value, index, arr) => index === 1) === 1); |  | ||||||
|     assert(array.findIndex(value => value == "1") === 2); |  | ||||||
|     assert(array.findIndex(value => value === 1) === 2); |  | ||||||
|     assert(array.findIndex(value => typeof value !== "string") === 2); |  | ||||||
|     assert(array.findIndex(value => typeof value === "boolean") === 4); |  | ||||||
|     assert(array.findIndex(value => value > 1) === 3); |  | ||||||
|     assert(array.findIndex(value => value > 1 && value < 3) === 3); |  | ||||||
|     assert(array.findIndex(value => value > 100) === -1); |  | ||||||
|     assert([].findIndex(value => value === 1) === -1); |  | ||||||
| 
 |  | ||||||
|     var callbackCalled = 0; |  | ||||||
|     var callback = () => { |  | ||||||
|         callbackCalled++; |  | ||||||
|     }; |  | ||||||
| 
 |  | ||||||
|     [].findIndex(callback); |  | ||||||
|     assert(callbackCalled === 0); |  | ||||||
| 
 |  | ||||||
|     [1, 2, 3].findIndex(callback); |  | ||||||
|     assert(callbackCalled === 3); |  | ||||||
| 
 |  | ||||||
|     callbackCalled = 0; |  | ||||||
|     [1, , , "foo", , undefined, , ,].findIndex(callback); |  | ||||||
|     assert(callbackCalled === 8); |  | ||||||
| 
 |  | ||||||
|     callbackCalled = 0; |  | ||||||
|     [1, , , "foo", , undefined, , ,].findIndex(value => { |  | ||||||
|         callbackCalled++; |  | ||||||
|         return value === undefined; |  | ||||||
|     }); |     }); | ||||||
|     assert(callbackCalled === 2); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("callback must be a function", () => { | ||||||
| } catch (e) { |         expect(() => { | ||||||
|     console.log("FAIL: " + e); |             [].findIndex(undefined); | ||||||
| } |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | describe("normal behavior", () => { | ||||||
|  |     test("basic functionality", () => { | ||||||
|  |         var array = ["hello", "friends", 1, 2, false]; | ||||||
|  | 
 | ||||||
|  |         expect(array.findIndex(value => value === "hello")).toBe(0); | ||||||
|  |         expect(array.findIndex((value, index, arr) => index === 1)).toBe(1); | ||||||
|  |         expect(array.findIndex(value => value == "1")).toBe(2); | ||||||
|  |         expect(array.findIndex(value => value === 1)).toBe(2); | ||||||
|  |         expect(array.findIndex(value => typeof value !== "string")).toBe(2); | ||||||
|  |         expect(array.findIndex(value => typeof value === "boolean")).toBe(4); | ||||||
|  |         expect(array.findIndex(value => value > 1)).toBe(3); | ||||||
|  |         expect(array.findIndex(value => value > 1 && value < 3)).toBe(3); | ||||||
|  |         expect(array.findIndex(value => value > 100)).toBe(-1); | ||||||
|  |         expect([].findIndex(value => value === 1)).toBe(-1); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("never calls callback with empty array", () => { | ||||||
|  |         var callbackCalled = 0; | ||||||
|  |         expect( | ||||||
|  |             [].findIndex(() => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toBe(-1); | ||||||
|  |         expect(callbackCalled).toBe(0); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("calls callback once for every item", () => { | ||||||
|  |         var callbackCalled = 0; | ||||||
|  |         expect( | ||||||
|  |             [1, 2, 3].findIndex(() => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toBe(-1); | ||||||
|  |         expect(callbackCalled).toBe(3); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("empty slots are treated as undefined", () => { | ||||||
|  |         var callbackCalled = 0; | ||||||
|  |         expect( | ||||||
|  |             [1, , , "foo", , undefined, , ,].findIndex(value => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |                 return value === undefined; | ||||||
|  |             }) | ||||||
|  |         ).toBe(1); | ||||||
|  |         expect(callbackCalled).toBe(2); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,70 +1,70 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.forEach).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.forEach.length === 1); |     test("requires at least one argument", () => { | ||||||
| 
 |         expect(() => { | ||||||
|     assertThrowsError( |  | ||||||
|         () => { |  | ||||||
|             [].forEach(); |             [].forEach(); | ||||||
|         }, |         }).toThrowWithMessage( | ||||||
|         { |             TypeError, | ||||||
|             error: TypeError, |             "Array.prototype.forEach() requires at least one argument" | ||||||
|             message: "Array.prototype.forEach() requires at least one argument", |         ); | ||||||
|         } |     }); | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("callback must be a function", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [].forEach(undefined); |             [].forEach(undefined); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, | }); | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     var a = [1, 2, 3]; | describe("normal behavior", () => { | ||||||
|     var o = {}; |     test("never calls callback with empty array", () => { | ||||||
|     var callbackCalled = 0; |         var callbackCalled = 0; | ||||||
|     var callback = () => { |         expect( | ||||||
|         callbackCalled++; |             [].forEach(() => { | ||||||
|     }; |                 callbackCalled++; | ||||||
| 
 |             }) | ||||||
|     assert([].forEach(callback) === undefined); |         ).toBeUndefined(); | ||||||
|     assert(callbackCalled === 0); |         expect(callbackCalled).toBe(0); | ||||||
| 
 |  | ||||||
|     assert(a.forEach(callback) === undefined); |  | ||||||
|     assert(callbackCalled === 3); |  | ||||||
| 
 |  | ||||||
|     callbackCalled = 0; |  | ||||||
|     a.forEach(function (value, index) { |  | ||||||
|         assert(value === a[index]); |  | ||||||
|         assert(index === a[index] - 1); |  | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |     test("calls callback once for every item", () => { | ||||||
|     a.forEach(function (_, _, array) { |         var callbackCalled = 0; | ||||||
|         callbackCalled++; |         expect( | ||||||
|         assert(a.length === array.length); |             [1, 2, 3].forEach(() => { | ||||||
|         a.push("test"); |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toBeUndefined(); | ||||||
|  |         expect(callbackCalled).toBe(3); | ||||||
|     }); |     }); | ||||||
|     assert(callbackCalled === 3); |  | ||||||
|     assert(a.length === 6); |  | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |     test("callback receives value and index", () => { | ||||||
|     a.forEach(function (value, index) { |         var a = [1, 2, 3]; | ||||||
|         callbackCalled++; |         a.forEach((value, index) => { | ||||||
|         this[index] = value; |             expect(value).toBe(a[index]); | ||||||
|     }, o); |             expect(index).toBe(a[index] - 1); | ||||||
|     assert(callbackCalled === 6); |         }); | ||||||
|     assert(o[0] === 1); |     }); | ||||||
|     assert(o[1] === 2); |  | ||||||
|     assert(o[2] === 3); |  | ||||||
|     assert(o[3] === "test"); |  | ||||||
|     assert(o[4] === "test"); |  | ||||||
|     assert(o[5] === "test"); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("callback receives array", () => { | ||||||
| } catch (e) { |         var callbackCalled = 0; | ||||||
|     console.log("FAIL: " + e); |         var a = [1, 2, 3]; | ||||||
| } |         a.forEach((_, _, array) => { | ||||||
|  |             callbackCalled++; | ||||||
|  |             expect(a).toEqual(array); | ||||||
|  |             a.push("test"); | ||||||
|  |         }); | ||||||
|  |         expect(callbackCalled).toBe(3); | ||||||
|  |         expect(a).toEqual([1, 2, 3, "test", "test", "test"]); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("this value can be modified", () => { | ||||||
|  |         var t = []; | ||||||
|  |         [1, 2, 3].forEach(function (value) { | ||||||
|  |             this.push(value); | ||||||
|  |         }, t); | ||||||
|  |         expect(t).toEqual([1, 2, 3]); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,22 +1,18 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
| 
 |     expect(Array.prototype.includes).toHaveLength(1); | ||||||
| try { | }); | ||||||
|     assert(Array.prototype.includes.length === 1); |  | ||||||
| 
 | 
 | ||||||
|  | test("basic functionality", () => { | ||||||
|     var array = ["hello", "friends", 1, 2, false]; |     var array = ["hello", "friends", 1, 2, false]; | ||||||
| 
 | 
 | ||||||
|     assert([].includes() === false); |     expect([].includes()).toBeFalse(); | ||||||
|     assert([undefined].includes() === true); |     expect([undefined].includes()).toBeTrue(); | ||||||
|     assert(array.includes("hello") === true); |     expect(array.includes("hello")).toBeTrue(); | ||||||
|     assert(array.includes(1) === true); |     expect(array.includes(1)).toBeTrue(); | ||||||
|     assert(array.includes(1, -3) === true); |     expect(array.includes(1, -3)).toBeTrue(); | ||||||
|     assert(array.includes("serenity") === false); |     expect(array.includes("serenity")).toBeFalse(); | ||||||
|     assert(array.includes(false, -1) === true); |     expect(array.includes(false, -1)).toBeTrue(); | ||||||
|     assert(array.includes(2, -1) === false); |     expect(array.includes(2, -1)).toBeFalse(); | ||||||
|     assert(array.includes(2, -100) === true); |     expect(array.includes(2, -100)).toBeTrue(); | ||||||
|     assert(array.includes("friends", 100) === false); |     expect(array.includes("friends", 100)).toBeFalse(); | ||||||
| 
 | }); | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,29 +1,25 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
| 
 |     expect(Array.prototype.indexOf).toHaveLength(1); | ||||||
| try { | }); | ||||||
|     assert(Array.prototype.indexOf.length === 1); |  | ||||||
| 
 | 
 | ||||||
|  | test("basic functionality", () => { | ||||||
|     var array = ["hello", "friends", 1, 2, false]; |     var array = ["hello", "friends", 1, 2, false]; | ||||||
| 
 | 
 | ||||||
|     assert(array.indexOf("hello") === 0); |     expect(array.indexOf("hello")).toBe(0); | ||||||
|     assert(array.indexOf("friends") === 1); |     expect(array.indexOf("friends")).toBe(1); | ||||||
|     assert(array.indexOf(false) === 4); |     expect(array.indexOf(false)).toBe(4); | ||||||
|     assert(array.indexOf(false, 2) === 4); |     expect(array.indexOf(false, 2)).toBe(4); | ||||||
|     assert(array.indexOf(false, -2) === 4); |     expect(array.indexOf(false, -2)).toBe(4); | ||||||
|     assert(array.indexOf(1) === 2); |     expect(array.indexOf(1)).toBe(2); | ||||||
|     assert(array.indexOf(1, 1000) === -1); |     expect(array.indexOf(1, 1000)).toBe(-1); | ||||||
|     assert(array.indexOf(1, -1000) === 2); |     expect(array.indexOf(1, -1000)).toBe(2); | ||||||
|     assert(array.indexOf("serenity") === -1); |     expect(array.indexOf("serenity")).toBe(-1); | ||||||
|     assert(array.indexOf(false, -1) === 4); |     expect(array.indexOf(false, -1)).toBe(4); | ||||||
|     assert(array.indexOf(2, -1) === -1); |     expect(array.indexOf(2, -1)).toBe(-1); | ||||||
|     assert(array.indexOf(2, -2) === 3); |     expect(array.indexOf(2, -2)).toBe(3); | ||||||
|     assert([].indexOf("serenity") === -1); |     expect([].indexOf("serenity")).toBe(-1); | ||||||
|     assert([].indexOf("serenity", 10) === -1); |     expect([].indexOf("serenity", 10)).toBe(-1); | ||||||
|     assert([].indexOf("serenity", -10) === -1); |     expect([].indexOf("serenity", -10)).toBe(-1); | ||||||
|     assert([].indexOf() === -1); |     expect([].indexOf()).toBe(-1); | ||||||
|     assert([undefined].indexOf() === 0); |     expect([undefined].indexOf()).toBe(0); | ||||||
| 
 | }); | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,19 +1,15 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.join).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | test("basic functionality", () => { | ||||||
|     assert(Array.prototype.join.length === 1); |     expect(["hello", "friends"].join()).toBe("hello,friends"); | ||||||
| 
 |     expect(["hello", "friends"].join(" ")).toBe("hello friends"); | ||||||
|     assert(["hello", "friends"].join() === "hello,friends"); |     expect(["hello", "friends", "foo"].join("~", "#")).toBe("hello~friends~foo"); | ||||||
|     assert(["hello", "friends"].join(" ") === "hello friends"); |     expect([].join()).toBe(""); | ||||||
|     assert(["hello", "friends", "foo"].join("~", "#") === "hello~friends~foo"); |     expect([null].join()).toBe(""); | ||||||
|     assert([].join() === ""); |     expect([undefined].join()).toBe(""); | ||||||
|     assert([null].join() === ""); |     expect([undefined, null, ""].join()).toBe(",,"); | ||||||
|     assert([undefined].join() === ""); |     expect([1, null, 2, undefined, 3].join()).toBe("1,,2,,3"); | ||||||
|     assert([undefined, null, ""].join() === ",,"); |     expect(Array(3).join()).toBe(",,"); | ||||||
|     assert([1, null, 2, undefined, 3].join() === "1,,2,,3"); | }); | ||||||
|     assert(Array(3).join() === ",,"); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,26 +1,22 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
| 
 |     expect(Array.prototype.lastIndexOf).toHaveLength(1); | ||||||
| try { | }); | ||||||
|     assert(Array.prototype.lastIndexOf.length === 1); |  | ||||||
| 
 | 
 | ||||||
|  | test("basic functionality", () => { | ||||||
|     var array = [1, 2, 3, 1, "hello"]; |     var array = [1, 2, 3, 1, "hello"]; | ||||||
| 
 | 
 | ||||||
|     assert(array.lastIndexOf("hello") === 4); |     expect(array.lastIndexOf("hello")).toBe(4); | ||||||
|     assert(array.lastIndexOf("hello", 1000) === 4); |     expect(array.lastIndexOf("hello", 1000)).toBe(4); | ||||||
|     assert(array.lastIndexOf(1) === 3); |     expect(array.lastIndexOf(1)).toBe(3); | ||||||
|     assert(array.lastIndexOf(1, -1) === 3); |     expect(array.lastIndexOf(1, -1)).toBe(3); | ||||||
|     assert(array.lastIndexOf(1, -2) === 3); |     expect(array.lastIndexOf(1, -2)).toBe(3); | ||||||
|     assert(array.lastIndexOf(2) === 1); |     expect(array.lastIndexOf(2)).toBe(1); | ||||||
|     assert(array.lastIndexOf(2, -3) === 1); |     expect(array.lastIndexOf(2, -3)).toBe(1); | ||||||
|     assert(array.lastIndexOf(2, -4) === 1); |     expect(array.lastIndexOf(2, -4)).toBe(1); | ||||||
|     assert([].lastIndexOf("hello") === -1); |     expect([].lastIndexOf("hello")).toBe(-1); | ||||||
|     assert([].lastIndexOf("hello", 10) === -1); |     expect([].lastIndexOf("hello", 10)).toBe(-1); | ||||||
|     assert([].lastIndexOf("hello", -10) === -1); |     expect([].lastIndexOf("hello", -10)).toBe(-1); | ||||||
|     assert([].lastIndexOf() === -1); |     expect([].lastIndexOf()).toBe(-1); | ||||||
|     assert([undefined].lastIndexOf() === 0); |     expect([undefined].lastIndexOf()).toBe(0); | ||||||
|     assert([undefined, undefined, undefined].lastIndexOf() === 2); |     expect([undefined, undefined, undefined].lastIndexOf()).toBe(2); | ||||||
| 
 | }); | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,63 +1,57 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.map).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.map.length === 1); |     test("requires at least one argument", () => { | ||||||
| 
 |         expect(() => { | ||||||
|     assertThrowsError( |  | ||||||
|         () => { |  | ||||||
|             [].map(); |             [].map(); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "Array.prototype.map() requires at least one argument"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, |  | ||||||
|             message: "Array.prototype.map() requires at least one argument", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("callback must be a function", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [].map(undefined); |             [].map(undefined); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, | }); | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     var callbackCalled = 0; | describe("normal behavior", () => { | ||||||
|     var callback = () => { |     test("never calls callback with empty array", () => { | ||||||
|         callbackCalled++; |         var callbackCalled = 0; | ||||||
|     }; |         expect( | ||||||
|  |             [].map(() => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toEqual([]); | ||||||
|  |         expect(callbackCalled).toBe(0); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assert([].map(callback).length === 0); |     test("calls callback once for every item", () => { | ||||||
|     assert(callbackCalled === 0); |         var callbackCalled = 0; | ||||||
|  |         expect( | ||||||
|  |             [1, 2, 3].map(() => { | ||||||
|  |                 callbackCalled++; | ||||||
|  |             }) | ||||||
|  |         ).toEqual([undefined, undefined, undefined]); | ||||||
|  |         expect(callbackCalled).toBe(3); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assert([1, 2, 3].map(callback).length === 3); |     test("can map based on callback return value", () => { | ||||||
|     assert(callbackCalled === 3); |         expect( | ||||||
|  |             [undefined, null, true, "foo", 42, {}].map( | ||||||
|  |                 (value, index) => "" + index + " -> " + value | ||||||
|  |             ) | ||||||
|  |         ).toEqual([ | ||||||
|  |             "0 -> undefined", | ||||||
|  |             "1 -> null", | ||||||
|  |             "2 -> true", | ||||||
|  |             "3 -> foo", | ||||||
|  |             "4 -> 42", | ||||||
|  |             "5 -> [object Object]", | ||||||
|  |         ]); | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |         var squaredNumbers = [0, 1, 2, 3, 4].map(x => x ** 2); | ||||||
|     assert([1, , , "foo", , undefined, , ,].map(callback).length === 8); |         expect(squaredNumbers).toEqual([0, 1, 4, 9, 16]); | ||||||
|     assert(callbackCalled === 3); |     }); | ||||||
| 
 | }); | ||||||
|     var results = [undefined, null, true, "foo", 42, {}].map( |  | ||||||
|         (value, index) => "" + index + " -> " + value |  | ||||||
|     ); |  | ||||||
|     assert(results.length === 6); |  | ||||||
|     assert(results[0] === "0 -> undefined"); |  | ||||||
|     assert(results[1] === "1 -> null"); |  | ||||||
|     assert(results[2] === "2 -> true"); |  | ||||||
|     assert(results[3] === "3 -> foo"); |  | ||||||
|     assert(results[4] === "4 -> 42"); |  | ||||||
|     assert(results[5] === "5 -> [object Object]"); |  | ||||||
| 
 |  | ||||||
|     var squaredNumbers = [0, 1, 2, 3, 4].map(x => x ** 2); |  | ||||||
|     assert(squaredNumbers.length === 5); |  | ||||||
|     assert(squaredNumbers[0] === 0); |  | ||||||
|     assert(squaredNumbers[1] === 1); |  | ||||||
|     assert(squaredNumbers[2] === 4); |  | ||||||
|     assert(squaredNumbers[3] === 9); |  | ||||||
|     assert(squaredNumbers[4] === 16); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,21 +1,23 @@ | ||||||
| load("test-common.js"); | test("length is 0", () => { | ||||||
|  |     expect(Array.prototype.pop).toHaveLength(0); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("normal behavior", () => { | ||||||
|     var a = [1, 2, 3]; |     test("array with elements", () => { | ||||||
|     var value = a.pop(); |         var a = [1, 2, 3]; | ||||||
|     assert(value === 3); |         expect(a.pop()).toBe(3); | ||||||
|     assert(a.length === 2); |         expect(a).toEqual([1, 2]); | ||||||
|     assert(a[0] === 1); |     }); | ||||||
|     assert(a[1] === 2); |  | ||||||
| 
 | 
 | ||||||
|     var a = []; |     test("empty array", () => { | ||||||
|     var value = a.pop(); |         var a = []; | ||||||
|     assert(value === undefined); |         expect(a.pop()).toBeUndefined(); | ||||||
|     assert(a.length === 0); |         expect(a).toEqual([]); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assert([,].pop() === undefined); |     test("array with empty slot", () => { | ||||||
| 
 |         var a = [,]; | ||||||
|     console.log("PASS"); |         expect(a.pop()).toBeUndefined(); | ||||||
| } catch (e) { |         expect(a).toEqual([]); | ||||||
|     console.log("FAIL: " + e); |     }); | ||||||
| } | }); | ||||||
|  |  | ||||||
|  | @ -1,30 +1,23 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.push).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("normal behavior", () => { | ||||||
|     assert(Array.prototype.push.length === 1); |     test("no argument", () => { | ||||||
|  |         var a = ["hello"]; | ||||||
|  |         expect(a.push()).toBe(1); | ||||||
|  |         expect(a).toEqual(["hello"]); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     var a = ["hello"]; |     test("single argument", () => { | ||||||
|     var length = a.push(); |         var a = ["hello"]; | ||||||
|     assert(length === 1); |         expect(a.push("friends")).toBe(2); | ||||||
|     assert(a.length === 1); |         expect(a).toEqual(["hello", "friends"]); | ||||||
|     assert(a[0] === "hello"); |     }); | ||||||
| 
 | 
 | ||||||
|     length = a.push("friends"); |     test("multiple arguments", () => { | ||||||
|     assert(length === 2); |         var a = ["hello", "friends"]; | ||||||
|     assert(a.length === 2); |         expect(a.push(1, 2, 3)).toBe(5); | ||||||
|     assert(a[0] === "hello"); |         expect(a).toEqual(["hello", "friends", 1, 2, 3]); | ||||||
|     assert(a[1] === "friends"); |     }); | ||||||
| 
 | }); | ||||||
|     length = a.push(1, 2, 3); |  | ||||||
|     assert(length === 5); |  | ||||||
|     assert(a.length === 5); |  | ||||||
|     assert(a[0] === "hello"); |  | ||||||
|     assert(a[1] === "friends"); |  | ||||||
|     assert(a[2] === 1); |  | ||||||
|     assert(a[3] === 2); |  | ||||||
|     assert(a[4] === 3); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,132 +1,113 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.reduce).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.reduce.length === 1); |     test("requires at least one argument", () => { | ||||||
|  |         expect(() => { | ||||||
|  |             [].reduce(); | ||||||
|  |         }).toThrowWithMessage(TypeError, "Array.prototype.reduce() requires at least one argument"); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("callback must be a function", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [1].reduce(); |             [].reduce(undefined); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, |  | ||||||
|             message: "Array.prototype.reduce() requires at least one argument", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("reduce of empty array with no initial value", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [1].reduce(undefined); |  | ||||||
|         }, |  | ||||||
|         { |  | ||||||
|             error: TypeError, |  | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 |  | ||||||
|     assertThrowsError( |  | ||||||
|         () => { |  | ||||||
|             [].reduce((a, x) => x); |             [].reduce((a, x) => x); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, |  | ||||||
|             message: "Reduce of empty array with no initial value", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("reduce of array with only empty slots and no initial value", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [, ,].reduce((a, x) => x); |             [, ,].reduce((a, x) => x); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value"); | ||||||
|         { |  | ||||||
|             error: TypeError, |  | ||||||
|             message: "Reduce of empty array with no initial value", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 |  | ||||||
|     [1, 2].reduce(function () { |  | ||||||
|         assert(this === undefined); |  | ||||||
|     }); |     }); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
|     var callbackCalled = 0; | describe("normal behavior", () => { | ||||||
|     var callback = () => { |     test("basic functionality", () => { | ||||||
|         callbackCalled++; |         [1, 2].reduce(function () { | ||||||
|         return true; |             expect(this).toBeUndefined(); | ||||||
|     }; |         }); | ||||||
| 
 | 
 | ||||||
|     assert([1].reduce(callback) === 1); |         var callbackCalled = 0; | ||||||
|     assert(callbackCalled === 0); |         var callback = () => { | ||||||
|  |             callbackCalled++; | ||||||
|  |             return true; | ||||||
|  |         }; | ||||||
| 
 | 
 | ||||||
|     assert([, 1].reduce(callback) === 1); |         expect([1].reduce(callback)).toBe(1); | ||||||
|     assert(callbackCalled === 0); |         expect(callbackCalled).toBe(0); | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |         expect([, 1].reduce(callback)).toBe(1); | ||||||
|     assert([1, 2, 3].reduce(callback) === true); |         expect(callbackCalled).toBe(0); | ||||||
|     assert(callbackCalled === 2); |  | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |         callbackCalled = 0; | ||||||
|     assert([, , 1, 2, 3].reduce(callback) === true); |         expect([1, 2, 3].reduce(callback)).toBeTrue(); | ||||||
|     assert(callbackCalled === 2); |         expect(callbackCalled).toBe(2); | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |         callbackCalled = 0; | ||||||
|     assert([1, , , 10, , 100, , ,].reduce(callback) === true); |         expect([, , 1, 2, 3].reduce(callback)).toBeTrue(); | ||||||
|     assert(callbackCalled === 2); |         expect(callbackCalled).toBe(2); | ||||||
| 
 | 
 | ||||||
|     var constantlySad = () => ":^("; |         callbackCalled = 0; | ||||||
|     var result = [].reduce(constantlySad, ":^)"); |         expect([1, , , 10, , 100, , ,].reduce(callback)).toBeTrue(); | ||||||
|     assert(result === ":^)"); |         expect(callbackCalled).toBe(2); | ||||||
| 
 | 
 | ||||||
|     result = [":^0"].reduce(constantlySad, ":^)"); |         var constantlySad = () => ":^("; | ||||||
|     assert(result === ":^("); |         var result = [].reduce(constantlySad, ":^)"); | ||||||
|  |         expect(result).toBe(":^)"); | ||||||
| 
 | 
 | ||||||
|     result = [":^0"].reduce(constantlySad); |         result = [":^0"].reduce(constantlySad, ":^)"); | ||||||
|     assert(result === ":^0"); |         expect(result).toBe(":^("); | ||||||
| 
 | 
 | ||||||
|     result = [5, 4, 3, 2, 1].reduce((accum, elem) => accum + elem); |         result = [":^0"].reduce(constantlySad); | ||||||
|     assert(result === 15); |         expect(result).toBe(":^0"); | ||||||
| 
 | 
 | ||||||
|     result = [1, 2, 3, 4, 5, 6].reduce((accum, elem) => accum + elem, 100); |         result = [5, 4, 3, 2, 1].reduce((accum, elem) => accum + elem); | ||||||
|     assert(result === 121); |         expect(result).toBe(15); | ||||||
| 
 | 
 | ||||||
|     result = [6, 5, 4, 3, 2, 1].reduce((accum, elem) => { |         result = [1, 2, 3, 4, 5, 6].reduce((accum, elem) => accum + elem, 100); | ||||||
|         return accum + elem; |         expect(result).toBe(121); | ||||||
|     }, 100); |  | ||||||
|     assert(result === 121); |  | ||||||
| 
 | 
 | ||||||
|     var indexes = []; |         result = [6, 5, 4, 3, 2, 1].reduce((accum, elem) => { | ||||||
|     result = ["foo", 1, true].reduce((a, v, i) => { |             return accum + elem; | ||||||
|         indexes.push(i); |         }, 100); | ||||||
|  |         expect(result).toBe(121); | ||||||
|  | 
 | ||||||
|  |         var indexes = []; | ||||||
|  |         result = ["foo", 1, true].reduce((a, v, i) => { | ||||||
|  |             indexes.push(i); | ||||||
|  |         }); | ||||||
|  |         expect(result).toBeUndefined(); | ||||||
|  |         expect(indexes.length).toBe(2); | ||||||
|  |         expect(indexes[0]).toBe(1); | ||||||
|  |         expect(indexes[1]).toBe(2); | ||||||
|  | 
 | ||||||
|  |         indexes = []; | ||||||
|  |         result = ["foo", 1, true].reduce((a, v, i) => { | ||||||
|  |             indexes.push(i); | ||||||
|  |         }, "foo"); | ||||||
|  |         expect(result).toBeUndefined(); | ||||||
|  |         expect(indexes).toEqual([0, 1, 2]); | ||||||
|  | 
 | ||||||
|  |         var mutable = { prop: 0 }; | ||||||
|  |         result = ["foo", 1, true].reduce((a, v) => { | ||||||
|  |             a.prop = v; | ||||||
|  |             return a; | ||||||
|  |         }, mutable); | ||||||
|  |         expect(result).toBe(mutable); | ||||||
|  |         expect(result.prop).toBeTrue(); | ||||||
|  | 
 | ||||||
|  |         var a1 = [1, 2]; | ||||||
|  |         var a2 = null; | ||||||
|  |         a1.reduce((a, v, i, t) => { | ||||||
|  |             a2 = t; | ||||||
|  |         }); | ||||||
|  |         expect(a1).toBe(a2); | ||||||
|     }); |     }); | ||||||
|     assert(result === undefined); | }); | ||||||
|     assert(indexes.length === 2); |  | ||||||
|     assert(indexes[0] === 1); |  | ||||||
|     assert(indexes[1] === 2); |  | ||||||
| 
 |  | ||||||
|     indexes = []; |  | ||||||
|     result = ["foo", 1, true].reduce((a, v, i) => { |  | ||||||
|         indexes.push(i); |  | ||||||
|     }, "foo"); |  | ||||||
|     assert(result === undefined); |  | ||||||
|     assert(indexes.length === 3); |  | ||||||
|     assert(indexes[0] === 0); |  | ||||||
|     assert(indexes[1] === 1); |  | ||||||
|     assert(indexes[2] === 2); |  | ||||||
| 
 |  | ||||||
|     var mutable = { prop: 0 }; |  | ||||||
|     result = ["foo", 1, true].reduce((a, v) => { |  | ||||||
|         a.prop = v; |  | ||||||
|         return a; |  | ||||||
|     }, mutable); |  | ||||||
|     assert(result === mutable); |  | ||||||
|     assert(result.prop === true); |  | ||||||
| 
 |  | ||||||
|     var a1 = [1, 2]; |  | ||||||
|     var a2 = null; |  | ||||||
|     a1.reduce((a, v, i, t) => { |  | ||||||
|         a2 = t; |  | ||||||
|     }); |  | ||||||
|     assert(a1 === a2); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,134 +1,118 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.reduceRight).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.reduceRight.length === 1); |     test("requires at least one argument", () => { | ||||||
|  |         expect(() => { | ||||||
|  |             [].reduceRight(); | ||||||
|  |         }).toThrowWithMessage( | ||||||
|  |             TypeError, | ||||||
|  |             "Array.prototype.reduceRight() requires at least one argument" | ||||||
|  |         ); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("callback must be a function", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [1].reduceRight(); |             [].reduceRight(undefined); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, |  | ||||||
|             message: "Array.prototype.reduceRight() requires at least one argument", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("reduce of empty array with no initial value", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [1].reduceRight(undefined); |  | ||||||
|         }, |  | ||||||
|         { |  | ||||||
|             error: TypeError, |  | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 |  | ||||||
|     assertThrowsError( |  | ||||||
|         () => { |  | ||||||
|             [].reduceRight((a, x) => x); |             [].reduceRight((a, x) => x); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, |  | ||||||
|             message: "Reduce of empty array with no initial value", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("reduce of array with only empty slots and no initial value", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [, ,].reduceRight((a, x) => x); |             [, ,].reduceRight((a, x) => x); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value"); | ||||||
|         { |  | ||||||
|             error: TypeError, |  | ||||||
|             message: "Reduce of empty array with no initial value", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 |  | ||||||
|     [1, 2].reduceRight(function () { |  | ||||||
|         assert(this === undefined); |  | ||||||
|     }); |     }); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
|     var callbackCalled = 0; | describe("normal behavior", () => { | ||||||
|     var callback = () => { |     test("basic functionality", () => { | ||||||
|         callbackCalled++; |         [1, 2].reduceRight(function () { | ||||||
|         return true; |             expect(this).toBeUndefined(); | ||||||
|     }; |         }); | ||||||
| 
 | 
 | ||||||
|     assert([1].reduceRight(callback) === 1); |         var callbackCalled = 0; | ||||||
|     assert(callbackCalled === 0); |         var callback = () => { | ||||||
|  |             callbackCalled++; | ||||||
|  |             return true; | ||||||
|  |         }; | ||||||
| 
 | 
 | ||||||
|     assert([1].reduceRight(callback) === 1); |         expect([1].reduceRight(callback)).toBe(1); | ||||||
|     assert(callbackCalled === 0); |         expect(callbackCalled).toBe(0); | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |         expect([1].reduceRight(callback)).toBe(1); | ||||||
|     assert([1, 2, 3].reduceRight(callback) === true); |         expect(callbackCalled).toBe(0); | ||||||
|     assert(callbackCalled === 2); |  | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |         callbackCalled = 0; | ||||||
|     assert([1, 2, 3, ,].reduceRight(callback) === true); |         expect([1, 2, 3].reduceRight(callback)).toBe(true); | ||||||
|     assert(callbackCalled === 2); |         expect(callbackCalled).toBe(2); | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |         callbackCalled = 0; | ||||||
|     assert([, , , 1, , , 10, , 100, , ,].reduceRight(callback) === true); |         expect([1, 2, 3, ,].reduceRight(callback)).toBe(true); | ||||||
|     assert(callbackCalled === 2); |         expect(callbackCalled).toBe(2); | ||||||
| 
 | 
 | ||||||
|     var constantlySad = () => ":^("; |         callbackCalled = 0; | ||||||
|     var result = [].reduceRight(constantlySad, ":^)"); |         expect([, , , 1, , , 10, , 100, , ,].reduceRight(callback)).toBe(true); | ||||||
|     assert(result === ":^)"); |         expect(callbackCalled).toBe(2); | ||||||
| 
 | 
 | ||||||
|     result = [":^0"].reduceRight(constantlySad, ":^)"); |         var constantlySad = () => ":^("; | ||||||
|     assert(result === ":^("); |         var result = [].reduceRight(constantlySad, ":^)"); | ||||||
|  |         expect(result).toBe(":^)"); | ||||||
| 
 | 
 | ||||||
|     result = [":^0"].reduceRight(constantlySad); |         result = [":^0"].reduceRight(constantlySad, ":^)"); | ||||||
|     assert(result === ":^0"); |         expect(result).toBe(":^("); | ||||||
| 
 | 
 | ||||||
|     result = [5, 4, 3, 2, 1].reduceRight((accum, elem) => "" + accum + elem); |         result = [":^0"].reduceRight(constantlySad); | ||||||
|     assert(result === "12345"); |         expect(result).toBe(":^0"); | ||||||
| 
 | 
 | ||||||
|     result = [1, 2, 3, 4, 5, 6].reduceRight((accum, elem) => { |         result = [5, 4, 3, 2, 1].reduceRight((accum, elem) => "" + accum + elem); | ||||||
|         return "" + accum + elem; |         expect(result).toBe("12345"); | ||||||
|     }, 100); |  | ||||||
|     assert(result === "100654321"); |  | ||||||
| 
 | 
 | ||||||
|     result = [6, 5, 4, 3, 2, 1].reduceRight((accum, elem) => { |         result = [1, 2, 3, 4, 5, 6].reduceRight((accum, elem) => { | ||||||
|         return "" + accum + elem; |             return "" + accum + elem; | ||||||
|     }, 100); |         }, 100); | ||||||
|     assert(result === "100123456"); |         expect(result).toBe("100654321"); | ||||||
| 
 | 
 | ||||||
|     var indexes = []; |         result = [6, 5, 4, 3, 2, 1].reduceRight((accum, elem) => { | ||||||
|     result = ["foo", 1, true].reduceRight((a, v, i) => { |             return "" + accum + elem; | ||||||
|         indexes.push(i); |         }, 100); | ||||||
|  |         expect(result).toBe("100123456"); | ||||||
|  | 
 | ||||||
|  |         var indexes = []; | ||||||
|  |         result = ["foo", 1, true].reduceRight((a, v, i) => { | ||||||
|  |             indexes.push(i); | ||||||
|  |         }); | ||||||
|  |         expect(result).toBeUndefined(); | ||||||
|  |         expect(indexes.length).toBe(2); | ||||||
|  |         expect(indexes[0]).toBe(1); | ||||||
|  |         expect(indexes[1]).toBe(0); | ||||||
|  | 
 | ||||||
|  |         indexes = []; | ||||||
|  |         result = ["foo", 1, true].reduceRight((a, v, i) => { | ||||||
|  |             indexes.push(i); | ||||||
|  |         }, "foo"); | ||||||
|  |         expect(result).toBeUndefined(); | ||||||
|  |         expect(indexes).toEqual([2, 1, 0]); | ||||||
|  | 
 | ||||||
|  |         var mutable = { prop: 0 }; | ||||||
|  |         result = ["foo", 1, true].reduceRight((a, v) => { | ||||||
|  |             a.prop = v; | ||||||
|  |             return a; | ||||||
|  |         }, mutable); | ||||||
|  |         expect(result).toBe(mutable); | ||||||
|  |         expect(result.prop).toBe("foo"); | ||||||
|  | 
 | ||||||
|  |         var a1 = [1, 2]; | ||||||
|  |         var a2 = null; | ||||||
|  |         a1.reduceRight((a, v, i, t) => { | ||||||
|  |             a2 = t; | ||||||
|  |         }); | ||||||
|  |         expect(a1).toBe(a2); | ||||||
|     }); |     }); | ||||||
|     assert(result === undefined); | }); | ||||||
|     assert(indexes.length === 2); |  | ||||||
|     assert(indexes[0] === 1); |  | ||||||
|     assert(indexes[1] === 0); |  | ||||||
| 
 |  | ||||||
|     indexes = []; |  | ||||||
|     result = ["foo", 1, true].reduceRight((a, v, i) => { |  | ||||||
|         indexes.push(i); |  | ||||||
|     }, "foo"); |  | ||||||
|     assert(result === undefined); |  | ||||||
|     assert(indexes.length === 3); |  | ||||||
|     assert(indexes[0] === 2); |  | ||||||
|     assert(indexes[1] === 1); |  | ||||||
|     assert(indexes[2] === 0); |  | ||||||
| 
 |  | ||||||
|     var mutable = { prop: 0 }; |  | ||||||
|     result = ["foo", 1, true].reduceRight((a, v) => { |  | ||||||
|         a.prop = v; |  | ||||||
|         return a; |  | ||||||
|     }, mutable); |  | ||||||
|     assert(result === mutable); |  | ||||||
|     assert(result.prop === "foo"); |  | ||||||
| 
 |  | ||||||
|     var a1 = [1, 2]; |  | ||||||
|     var a2 = null; |  | ||||||
|     a1.reduceRight((a, v, i, t) => { |  | ||||||
|         a2 = t; |  | ||||||
|     }); |  | ||||||
|     assert(a1 === a2); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,31 +1,9 @@ | ||||||
| load("test-common.js"); | test("length is 0", () => { | ||||||
| 
 |     expect(Array.prototype.reverse).toHaveLength(0); | ||||||
| try { | }); | ||||||
|     assert(Array.prototype.reverse.length === 0); |  | ||||||
| 
 | 
 | ||||||
|  | test("basic functionality", () => { | ||||||
|     var array = [1, 2, 3]; |     var array = [1, 2, 3]; | ||||||
| 
 |     expect(array.reverse()).toEqual([3, 2, 1]); | ||||||
|     assert(array[0] === 1); |     expect(array).toEqual([3, 2, 1]); | ||||||
|     assert(array[1] === 2); | }); | ||||||
|     assert(array[2] === 3); |  | ||||||
| 
 |  | ||||||
|     array.reverse(); |  | ||||||
| 
 |  | ||||||
|     assert(array[0] === 3); |  | ||||||
|     assert(array[1] === 2); |  | ||||||
|     assert(array[2] === 1); |  | ||||||
| 
 |  | ||||||
|     var array_ref = array.reverse(); |  | ||||||
| 
 |  | ||||||
|     assert(array_ref[0] === 1); |  | ||||||
|     assert(array_ref[1] === 2); |  | ||||||
|     assert(array_ref[2] === 3); |  | ||||||
| 
 |  | ||||||
|     assert(array[0] === 1); |  | ||||||
|     assert(array[1] === 2); |  | ||||||
|     assert(array[2] === 3); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,21 +1,23 @@ | ||||||
| load("test-common.js"); | test("length is 0", () => { | ||||||
|  |     expect(Array.prototype.shift).toHaveLength(0); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("normal behavior", () => { | ||||||
|     var a = [1, 2, 3]; |     test("array with elements", () => { | ||||||
|     var value = a.shift(); |         var a = [1, 2, 3]; | ||||||
|     assert(value === 1); |         expect(a.shift()).toBe(1); | ||||||
|     assert(a.length === 2); |         expect(a).toEqual([2, 3]); | ||||||
|     assert(a[0] === 2); |     }); | ||||||
|     assert(a[1] === 3); |  | ||||||
| 
 | 
 | ||||||
|     var a = []; |     test("empty array", () => { | ||||||
|     var value = a.shift(); |         var a = []; | ||||||
|     assert(value === undefined); |         expect(a.shift()).toBeUndefined(); | ||||||
|     assert(a.length === 0); |         expect(a).toEqual([]); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assert([,].shift() === undefined); |     test("array with empty slot", () => { | ||||||
| 
 |         var a = [,]; | ||||||
|     console.log("PASS"); |         expect(a.shift()).toBeUndefined(); | ||||||
| } catch (e) { |         expect(a).toEqual([]); | ||||||
|     console.log("FAIL: " + e); |     }); | ||||||
| } | }); | ||||||
|  |  | ||||||
|  | @ -1,53 +1,39 @@ | ||||||
| load("test-common.js"); | test("length is 0", () => { | ||||||
| 
 |     expect(Array.prototype.slice).toHaveLength(2); | ||||||
| try { | }); | ||||||
|     assert(Array.prototype.slice.length === 2); |  | ||||||
| 
 | 
 | ||||||
|  | test("basic functionality", () => { | ||||||
|     var array = ["hello", "friends", "serenity", 1]; |     var array = ["hello", "friends", "serenity", 1]; | ||||||
| 
 | 
 | ||||||
|     var array_slice = array.slice(); |     var slice = array.slice(); | ||||||
|     assert(array_slice.length === array.length); |     expect(array).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice.length === 4); |     expect(slice).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice[0] === "hello"); |  | ||||||
|     assert(array_slice[1] === "friends"); |  | ||||||
|     assert(array_slice[2] === "serenity"); |  | ||||||
|     assert(array_slice[3] === 1); |  | ||||||
| 
 | 
 | ||||||
|     array_slice = array.slice(1); |     slice = array.slice(1); | ||||||
|     assert(array_slice.length === 3); |     expect(array).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice[0] === "friends"); |     expect(slice).toEqual(["friends", "serenity", 1]); | ||||||
|     assert(array_slice[1] === "serenity"); |  | ||||||
|     assert(array_slice[2] === 1); |  | ||||||
| 
 | 
 | ||||||
|     array_slice = array.slice(0, 2); |     slice = array.slice(0, 2); | ||||||
|     assert(array_slice.length === 2); |     expect(array).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice[0] === "hello"); |     expect(slice).toEqual(["hello", "friends"]); | ||||||
|     assert(array_slice[1] === "friends"); |  | ||||||
| 
 | 
 | ||||||
|     array_slice = array.slice(-1); |     slice = array.slice(-1); | ||||||
|     assert(array_slice.length === 1); |     expect(array).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice[0] === 1); |     expect(slice).toEqual([1]); | ||||||
| 
 | 
 | ||||||
|     array_slice = array.slice(1, 1); |     slice = array.slice(1, 1); | ||||||
|     assert(array_slice.length === 0); |     expect(array).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|  |     expect(slice).toEqual([]); | ||||||
| 
 | 
 | ||||||
|     array_slice = array.slice(1, -1); |     slice = array.slice(1, -1); | ||||||
|     assert(array_slice.length === 2); |     expect(array).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice[0] === "friends"); |     expect(slice).toEqual(["friends", "serenity"]); | ||||||
|     assert(array_slice[1] === "serenity"); |  | ||||||
| 
 | 
 | ||||||
|     array_slice = array.slice(2, -1); |     slice = array.slice(2, -1); | ||||||
|     assert(array_slice.length === 1); |     expect(array).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice[0] === "serenity"); |     expect(slice).toEqual(["serenity"]); | ||||||
| 
 | 
 | ||||||
|     array_slice = array.slice(0, 100); |     slice = array.slice(0, 100); | ||||||
|     assert(array_slice.length === 4); |     expect(array).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice[0] === "hello"); |     expect(slice).toEqual(["hello", "friends", "serenity", 1]); | ||||||
|     assert(array_slice[1] === "friends"); | }); | ||||||
|     assert(array_slice[2] === "serenity"); |  | ||||||
|     assert(array_slice[3] === 1); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,37 +1,37 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.some).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("errors", () => { | ||||||
|     assert(Array.prototype.some.length === 1); |     test("requires at least one argument", () => { | ||||||
|  |         expect(() => { | ||||||
|  |             [].some(); | ||||||
|  |         }).toThrowWithMessage(TypeError, "Array.prototype.some() requires at least one argument"); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("callback must be a function", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [].some(undefined); |             [].some(undefined); | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "undefined is not a function"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, | }); | ||||||
|             message: "undefined is not a function", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|  | test("basic functionality", () => { | ||||||
|     var array = ["hello", "friends", 1, 2, false, -42, { name: "serenityos" }]; |     var array = ["hello", "friends", 1, 2, false, -42, { name: "serenityos" }]; | ||||||
| 
 | 
 | ||||||
|     assert(array.some(value => value === "hello") === true); |     expect(array.some(value => value === "hello")).toBeTrue(); | ||||||
|     assert(array.some(value => value === "serenity") === false); |     expect(array.some(value => value === "serenity")).toBeFalse(); | ||||||
|     assert(array.some((value, index, arr) => index === 1) === true); |     expect(array.some((value, index, arr) => index === 1)).toBeTrue(); | ||||||
|     assert(array.some(value => value == "1") === true); |     expect(array.some(value => value == "1")).toBeTrue(); | ||||||
|     assert(array.some(value => value === 1) === true); |     expect(array.some(value => value === 1)).toBeTrue(); | ||||||
|     assert(array.some(value => value === 13) === false); |     expect(array.some(value => value === 13)).toBeFalse(); | ||||||
|     assert(array.some(value => typeof value !== "string") === true); |     expect(array.some(value => typeof value !== "string")).toBeTrue(); | ||||||
|     assert(array.some(value => typeof value === "boolean") === true); |     expect(array.some(value => typeof value === "boolean")).toBeTrue(); | ||||||
|     assert(array.some(value => value > 1) === true); |     expect(array.some(value => value > 1)).toBeTrue(); | ||||||
|     assert(array.some(value => value > 1 && value < 3) === true); |     expect(array.some(value => value > 1 && value < 3)).toBeTrue(); | ||||||
|     assert(array.some(value => value > 100) === false); |     expect(array.some(value => value > 100)).toBeFalse(); | ||||||
|     assert(array.some(value => value < 0) === true); |     expect(array.some(value => value < 0)).toBeTrue(); | ||||||
|     assert(array.some(value => array.pop()) === true); |     expect(array.some(value => array.pop())).toBeTrue(); | ||||||
|     assert(["", "hello", "friends", "serenity"].some(value => value.length === 0) === true); |     expect(["", "hello", "friends", "serenity"].some(value => value.length === 0)).toBeTrue(); | ||||||
|     assert([].some(value => value === 1) === false); |     expect([].some(value => value === 1)).toBeFalse(); | ||||||
| 
 | }); | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,87 +1,49 @@ | ||||||
| load("test-common.js"); | test("length is 2", () => { | ||||||
| 
 |     expect(Array.prototype.splice).toHaveLength(2); | ||||||
| try { | }); | ||||||
|     assert(Array.prototype.splice.length === 2); |  | ||||||
| 
 | 
 | ||||||
|  | test("basic functionality", () => { | ||||||
|     var array = ["hello", "friends", "serenity", 1, 2]; |     var array = ["hello", "friends", "serenity", 1, 2]; | ||||||
|     var removed = array.splice(3); |     var removed = array.splice(3); | ||||||
|     assert(array.length === 3); |     expect(array).toEqual(["hello", "friends", "serenity"]); | ||||||
|     assert(array[0] === "hello"); |     expect(removed).toEqual([1, 2]); | ||||||
|     assert(array[1] === "friends"); |  | ||||||
|     assert(array[2] === "serenity"); |  | ||||||
|     assert(removed.length === 2); |  | ||||||
|     assert(removed[0] === 1); |  | ||||||
|     assert(removed[1] === 2); |  | ||||||
| 
 | 
 | ||||||
|     array = ["hello", "friends", "serenity", 1, 2]; |     array = ["hello", "friends", "serenity", 1, 2]; | ||||||
|     removed = array.splice(-2); |     removed = array.splice(-2); | ||||||
|     assert(array.length === 3); |     expect(array).toEqual(["hello", "friends", "serenity"]); | ||||||
|     assert(array[0] === "hello"); |     expect(removed).toEqual([1, 2]); | ||||||
|     assert(array[1] === "friends"); |  | ||||||
|     assert(array[2] === "serenity"); |  | ||||||
|     assert(removed.length === 2); |  | ||||||
|     assert(removed[0] === 1); |  | ||||||
|     assert(removed[1] === 2); |  | ||||||
| 
 | 
 | ||||||
|     array = ["hello", "friends", "serenity", 1, 2]; |     array = ["hello", "friends", "serenity", 1, 2]; | ||||||
|     removed = array.splice(-2, 1); |     removed = array.splice(-2, 1); | ||||||
|     assert(array.length === 4); |     expect(array).toEqual(["hello", "friends", "serenity", 2]); | ||||||
|     assert(array[0] === "hello"); |     expect(removed).toEqual([1]); | ||||||
|     assert(array[1] === "friends"); |  | ||||||
|     assert(array[2] === "serenity"); |  | ||||||
|     assert(array[3] === 2); |  | ||||||
|     assert(removed.length === 1); |  | ||||||
|     assert(removed[0] === 1); |  | ||||||
| 
 | 
 | ||||||
|     array = ["serenity"]; |     array = ["serenity"]; | ||||||
|     removed = array.splice(0, 0, "hello", "friends"); |     removed = array.splice(0, 0, "hello", "friends"); | ||||||
|     assert(array.length === 3); |     expect(array).toEqual(["hello", "friends", "serenity"]); | ||||||
|     assert(array[0] === "hello"); |     expect(removed).toEqual([]); | ||||||
|     assert(array[1] === "friends"); |  | ||||||
|     assert(array[2] === "serenity"); |  | ||||||
|     assert(removed.length === 0); |  | ||||||
| 
 | 
 | ||||||
|     array = ["goodbye", "friends", "serenity"]; |     array = ["goodbye", "friends", "serenity"]; | ||||||
|     removed = array.splice(0, 1, "hello"); |     removed = array.splice(0, 1, "hello"); | ||||||
|     assert(array.length === 3); |     expect(array).toEqual(["hello", "friends", "serenity"]); | ||||||
|     assert(array[0] === "hello"); |     expect(removed).toEqual(["goodbye"]); | ||||||
|     assert(array[1] === "friends"); |  | ||||||
|     assert(array[2] === "serenity"); |  | ||||||
|     assert(removed.length === 1); |  | ||||||
|     assert(removed[0] === "goodbye"); |  | ||||||
| 
 | 
 | ||||||
|     array = ["foo", "bar", "baz"]; |     array = ["foo", "bar", "baz"]; | ||||||
|     removed = array.splice(); |     removed = array.splice(); | ||||||
|     assert(array.length === 3); |     expect(array).toEqual(["foo", "bar", "baz"]); | ||||||
|     assert(array[0] === "foo"); |     expect(removed).toEqual([]); | ||||||
|     assert(array[1] === "bar"); |  | ||||||
|     assert(array[2] === "baz"); |  | ||||||
|     assert(removed.length === 0); |  | ||||||
| 
 | 
 | ||||||
|     removed = array.splice(0, 123); |     removed = array.splice(0, 123); | ||||||
|     assert(array.length === 0); |     expect(array).toEqual([]); | ||||||
|     assert(removed.length === 3); |     expect(removed).toEqual(["foo", "bar", "baz"]); | ||||||
|     assert(removed[0] === "foo"); |  | ||||||
|     assert(removed[1] === "bar"); |  | ||||||
|     assert(removed[2] === "baz"); |  | ||||||
| 
 | 
 | ||||||
|     array = ["foo", "bar", "baz"]; |     array = ["foo", "bar", "baz"]; | ||||||
|     removed = array.splice(123, 123); |     removed = array.splice(123, 123); | ||||||
|     assert(array.length === 3); |     expect(array).toEqual(["foo", "bar", "baz"]); | ||||||
|     assert(array[0] === "foo"); |     expect(removed).toEqual([]); | ||||||
|     assert(array[1] === "bar"); |  | ||||||
|     assert(array[2] === "baz"); |  | ||||||
|     assert(removed.length === 0); |  | ||||||
| 
 | 
 | ||||||
|     array = ["foo", "bar", "baz"]; |     array = ["foo", "bar", "baz"]; | ||||||
|     removed = array.splice(-123, 123); |     removed = array.splice(-123, 123); | ||||||
|     assert(array.length === 0); |     expect(array).toEqual([]); | ||||||
|     assert(removed.length === 3); |     expect(removed).toEqual(["foo", "bar", "baz"]); | ||||||
|     assert(removed[0] === "foo"); | }); | ||||||
|     assert(removed[1] === "bar"); |  | ||||||
|     assert(removed[2] === "baz"); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,24 +1,60 @@ | ||||||
| load("test-common.js"); | test("length is 0", () => { | ||||||
|  |     expect(Array.prototype.toLocaleString).toHaveLength(0); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("normal behavior", () => { | ||||||
|     assert(Array.prototype.toLocaleString.length === 0); |     test("array with no elements", () => { | ||||||
|  |         expect([].toLocaleString()).toBe(""); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assert([].toLocaleString() === ""); |     test("array with one element", () => { | ||||||
|     assert(["foo"].toLocaleString() === "foo"); |         expect(["foo"].toLocaleString()).toBe("foo"); | ||||||
|     assert(["foo", "bar"].toLocaleString() === "foo,bar"); |     }); | ||||||
|     assert(["foo", undefined, "bar", null, "baz"].toLocaleString() === "foo,,bar,,baz"); |  | ||||||
| 
 | 
 | ||||||
|     var toStringCalled = 0; |     test("array with multiple elements", () => { | ||||||
|     var o = { |         expect(["foo", "bar", "baz"].toLocaleString()).toBe("foo,bar,baz"); | ||||||
|         toString: () => { |     }); | ||||||
|             toStringCalled++; |  | ||||||
|             return "o"; |  | ||||||
|         }, |  | ||||||
|     }; |  | ||||||
|     assert([o, undefined, o, null, o].toLocaleString() === "o,,o,,o"); |  | ||||||
|     assert(toStringCalled === 3); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("number stringification differs from regular toString, for now", () => { | ||||||
| } catch (e) { |         expect([1, 2, 3].toLocaleString()).toBe( | ||||||
|     console.log("FAIL: " + e); |             "[object NumberObject],[object NumberObject],[object NumberObject]" | ||||||
| } |         ); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("null and undefined result in empty strings", () => { | ||||||
|  |         expect([null].toLocaleString()).toBe(""); | ||||||
|  |         expect([undefined].toLocaleString()).toBe(""); | ||||||
|  |         expect([undefined, null].toLocaleString()).toBe(","); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("empty values result in empty strings", () => { | ||||||
|  |         expect(new Array(1).toLocaleString()).toBe(""); | ||||||
|  |         expect(new Array(3).toLocaleString()).toBe(",,"); | ||||||
|  |         var a = new Array(5); | ||||||
|  |         a[2] = "foo"; | ||||||
|  |         a[4] = "bar"; | ||||||
|  |         expect(a.toLocaleString()).toBe(",,foo,,bar"); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("getter property is included in returned string", () => { | ||||||
|  |         var a = ["foo"]; | ||||||
|  |         Object.defineProperty(a, 1, { | ||||||
|  |             get() { | ||||||
|  |                 return "bar"; | ||||||
|  |             }, | ||||||
|  |         }); | ||||||
|  |         expect(a.toLocaleString()).toBe("foo,bar"); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("array with elements that have a custom toString() function", () => { | ||||||
|  |         var toStringCalled = 0; | ||||||
|  |         var o = { | ||||||
|  |             toString() { | ||||||
|  |                 toStringCalled++; | ||||||
|  |                 return "o"; | ||||||
|  |             }, | ||||||
|  |         }; | ||||||
|  |         expect([o, undefined, o, null, o].toLocaleString()).toBe("o,,o,,o"); | ||||||
|  |         expect(toStringCalled).toBe(3); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,22 +1,58 @@ | ||||||
| load("test-common.js"); | test("length is 0", () => { | ||||||
|  |     expect(Array.prototype.toString).toHaveLength(0); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("normal behavior", () => { | ||||||
|     var a = [1, 2, 3]; |     test("array with no elements", () => { | ||||||
|     assert(a.toString() === "1,2,3"); |         expect([].toString()).toBe(""); | ||||||
|     assert([].toString() === ""); |     }); | ||||||
|     assert([5].toString() === "5"); |  | ||||||
| 
 | 
 | ||||||
|     assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)"); |     test("array with one element", () => { | ||||||
|  |         expect([1].toString()).toBe("1"); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     assert([undefined, null].toString() === ","); |     test("array with multiple elements", () => { | ||||||
|  |         expect([1, 2, 3].toString()).toBe("1,2,3"); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     a = new Array(5); |     test("string and array concatenation", () => { | ||||||
|     assert(a.toString() === ",,,,"); |         expect("rgb(" + [10, 11, 12] + ")").toBe("rgb(10,11,12)"); | ||||||
|     a[2] = "foo"; |     }); | ||||||
|     a[4] = "bar"; |  | ||||||
|     assert(a.toString() === ",,foo,,bar"); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("null and undefined result in empty strings", () => { | ||||||
| } catch (e) { |         expect([null].toString()).toBe(""); | ||||||
|     console.log("FAIL: " + e); |         expect([undefined].toString()).toBe(""); | ||||||
| } |         expect([undefined, null].toString()).toBe(","); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("empty values result in empty strings", () => { | ||||||
|  |         expect(new Array(1).toString()).toBe(""); | ||||||
|  |         expect(new Array(3).toString()).toBe(",,"); | ||||||
|  |         var a = new Array(5); | ||||||
|  |         a[2] = "foo"; | ||||||
|  |         a[4] = "bar"; | ||||||
|  |         expect(a.toString()).toBe(",,foo,,bar"); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("getter property is included in returned string", () => { | ||||||
|  |         var a = [1, 2, 3]; | ||||||
|  |         Object.defineProperty(a, 3, { | ||||||
|  |             get() { | ||||||
|  |                 return 10; | ||||||
|  |             }, | ||||||
|  |         }); | ||||||
|  |         expect(a.toString()).toBe("1,2,3,10"); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("array with elements that have a custom toString() function", () => { | ||||||
|  |         var toStringCalled = 0; | ||||||
|  |         var o = { | ||||||
|  |             toString() { | ||||||
|  |                 toStringCalled++; | ||||||
|  |                 return "o"; | ||||||
|  |             }, | ||||||
|  |         }; | ||||||
|  |         expect([o, undefined, o, null, o].toString()).toBe("o,,o,,o"); | ||||||
|  |         expect(toStringCalled).toBe(3); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,30 +1,23 @@ | ||||||
| load("test-common.js"); | test("length is 1", () => { | ||||||
|  |     expect(Array.prototype.unshift).toHaveLength(1); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| try { | describe("normal behavior", () => { | ||||||
|     assert(Array.prototype.unshift.length === 1); |     test("no argument", () => { | ||||||
|  |         var a = ["hello"]; | ||||||
|  |         expect(a.unshift()).toBe(1); | ||||||
|  |         expect(a).toEqual(["hello"]); | ||||||
|  |     }); | ||||||
| 
 | 
 | ||||||
|     var a = ["hello"]; |     test("single argument", () => { | ||||||
|     var length = a.unshift(); |         var a = ["hello"]; | ||||||
|     assert(length === 1); |         expect(a.unshift("friends")).toBe(2); | ||||||
|     assert(a.length === 1); |         expect(a).toEqual(["friends", "hello"]); | ||||||
|     assert(a[0] === "hello"); |     }); | ||||||
| 
 | 
 | ||||||
|     length = a.unshift("friends"); |     test("multiple arguments", () => { | ||||||
|     assert(length === 2); |         var a = ["friends", "hello"]; | ||||||
|     assert(a.length === 2); |         expect(a.unshift(1, 2, 3)).toBe(5); | ||||||
|     assert(a[0] === "friends"); |         expect(a).toEqual([1, 2, 3, "friends", "hello"]); | ||||||
|     assert(a[1] === "hello"); |     }); | ||||||
| 
 | }); | ||||||
|     length = a.unshift(1, 2, 3); |  | ||||||
|     assert(length === 5); |  | ||||||
|     assert(a.length === 5); |  | ||||||
|     assert(a[0] === 1); |  | ||||||
|     assert(a[1] === 2); |  | ||||||
|     assert(a[2] === 3); |  | ||||||
|     assert(a[3] === "friends"); |  | ||||||
|     assert(a[4] === "hello"); |  | ||||||
| 
 |  | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,63 +1,57 @@ | ||||||
| load("test-common.js"); | test("basic functionality", () => { | ||||||
| 
 |  | ||||||
| try { |  | ||||||
|     var a = [1, 2, 3]; |     var a = [1, 2, 3]; | ||||||
| 
 | 
 | ||||||
|     assert(typeof a === "object"); |     expect(typeof a).toBe("object"); | ||||||
|     assert(a.length === 3); |     expect(a).toHaveLength(3); | ||||||
|     assert(a[0] === 1); |     expect(a[0]).toBe(1); | ||||||
|     assert(a[1] === 2); |     expect(a[1]).toBe(2); | ||||||
|     assert(a[2] === 3); |     expect(a[2]).toBe(3); | ||||||
| 
 | 
 | ||||||
|     a[1] = 5; |     a[1] = 5; | ||||||
|     assert(a[1] === 5); |     expect(a[1]).toBe(5); | ||||||
|     assert(a.length === 3); |     expect(a).toHaveLength(3); | ||||||
| 
 | 
 | ||||||
|     a.push(7); |     a.push(7); | ||||||
|     assert(a[3] === 7); |     expect(a[3]).toBe(7); | ||||||
|     assert(a.length === 4); |     expect(a).toHaveLength(4); | ||||||
| 
 | 
 | ||||||
|     a = [,]; |     a = [,]; | ||||||
|     assert(a.length === 1); |     expect(a).toHaveLength(1); | ||||||
|     assert(a.toString() === ""); |     expect(a.toString()).toBe(""); | ||||||
|     assert(a[0] === undefined); |     expect(a[0]).toBeUndefined(); | ||||||
| 
 | 
 | ||||||
|     a = [, , , ,]; |     a = [, , , ,]; | ||||||
|     assert(a.length === 4); |     expect(a).toHaveLength(4); | ||||||
|     assert(a.toString() === ",,,"); |     expect(a.toString()).toBe(",,,"); | ||||||
|     assert(a[0] === undefined); |     expect(a[0]).toBeUndefined(); | ||||||
|     assert(a[1] === undefined); |     expect(a[1]).toBeUndefined(); | ||||||
|     assert(a[2] === undefined); |     expect(a[2]).toBeUndefined(); | ||||||
|     assert(a[3] === undefined); |     expect(a[3]).toBeUndefined(); | ||||||
| 
 | 
 | ||||||
|     a = [1, , 2, , , 3]; |     a = [1, , 2, , , 3]; | ||||||
|     assert(a.length === 6); |     expect(a).toHaveLength(6); | ||||||
|     assert(a.toString() === "1,,2,,,3"); |     expect(a.toString()).toBe("1,,2,,,3"); | ||||||
|     assert(a[0] === 1); |     expect(a[0]).toBe(1); | ||||||
|     assert(a[1] === undefined); |     expect(a[1]).toBeUndefined(); | ||||||
|     assert(a[2] === 2); |     expect(a[2]).toBe(2); | ||||||
|     assert(a[3] === undefined); |     expect(a[3]).toBeUndefined(); | ||||||
|     assert(a[4] === undefined); |     expect(a[4]).toBeUndefined(); | ||||||
|     assert(a[5] === 3); |     expect(a[5]).toBe(3); | ||||||
| 
 | 
 | ||||||
|     a = [1, , 2, , , 3]; |     a = [1, , 2, , , 3]; | ||||||
|     Object.defineProperty(a, 1, { |     Object.defineProperty(a, 1, { | ||||||
|         get() { |         get() { | ||||||
|             return this.secret_prop; |             return this.getterSetterValue; | ||||||
|         }, |         }, | ||||||
|         set(value) { |         set(value) { | ||||||
|             this.secret_prop = value; |             this.getterSetterValue = value; | ||||||
|         }, |         }, | ||||||
|     }); |     }); | ||||||
|     assert(a.length === 6); |     expect(a).toHaveLength(6); | ||||||
|     assert(a.toString() === "1,,2,,,3"); |     expect(a.toString()).toBe("1,,2,,,3"); | ||||||
|     assert(a.secret_prop === undefined); |     expect(a.getterSetterValue).toBeUndefined(); | ||||||
|     a[1] = 20; |     a[1] = 20; | ||||||
|     assert(a.length === 6); |     expect(a).toHaveLength(6); | ||||||
|     assert(a.toString() === "1,20,2,,,3"); |     expect(a.toString()).toBe("1,20,2,,,3"); | ||||||
|     assert(a.secret_prop === 20); |     expect(a.getterSetterValue).toBe(20); | ||||||
| 
 | }); | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,51 +1,37 @@ | ||||||
| load("test-common.js"); | describe("errors", () => { | ||||||
| 
 |     test("invalid array length value", () => { | ||||||
| try { |         var a = [1, 2, 3]; | ||||||
|     var a = [1, 2, 3]; |         [undefined, "foo", -1, Infinity, -Infinity, NaN].forEach(value => { | ||||||
| 
 |             expect(() => { | ||||||
|     assert(a.length === 3); |  | ||||||
|     assert(a[0] === 1); |  | ||||||
|     assert(a[1] === 2); |  | ||||||
|     assert(a[2] === 3); |  | ||||||
| 
 |  | ||||||
|     a.length = 5; |  | ||||||
|     assert(a.length === 5); |  | ||||||
|     assert(a[0] === 1); |  | ||||||
|     assert(a[1] === 2); |  | ||||||
|     assert(a[2] === 3); |  | ||||||
|     assert(a[3] === undefined); |  | ||||||
|     assert(a[4] === undefined); |  | ||||||
| 
 |  | ||||||
|     a.length = 1; |  | ||||||
|     assert(a.length === 1); |  | ||||||
|     assert(a[0] === 1); |  | ||||||
| 
 |  | ||||||
|     a.length = 0; |  | ||||||
|     assert(a.length === 0); |  | ||||||
| 
 |  | ||||||
|     a.length = "42"; |  | ||||||
|     assert(a.length === 42); |  | ||||||
| 
 |  | ||||||
|     a.length = []; |  | ||||||
|     assert(a.length === 0); |  | ||||||
| 
 |  | ||||||
|     a.length = true; |  | ||||||
|     assert(a.length === 1); |  | ||||||
| 
 |  | ||||||
|     [undefined, "foo", -1, Infinity, -Infinity, NaN].forEach(value => { |  | ||||||
|         assertThrowsError( |  | ||||||
|             () => { |  | ||||||
|                 a.length = value; |                 a.length = value; | ||||||
|             }, |             }).toThrowWithMessage(RangeError, "Invalid array length"); | ||||||
|             { |             expect(a).toHaveLength(3); | ||||||
|                 error: RangeError, |         }); | ||||||
|                 message: "Invalid array length", |     }); | ||||||
|             } | }); | ||||||
|         ); | 
 | ||||||
|         assert(a.length === 1); | describe("normal behavior", () => { | ||||||
|  |     test("extend array by setting length", () => { | ||||||
|  |         var a = [1, 2, 3]; | ||||||
|  |         a.length = 5; | ||||||
|  |         expect(a).toEqual([1, 2, 3, undefined, undefined]); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); |     test("truncate array by setting length", () => { | ||||||
| } catch (e) { |         var a = [1, 2, 3]; | ||||||
|     console.log("FAIL: " + e); |         a.length = 2; | ||||||
| } |         expect(a).toEqual([1, 2]); | ||||||
|  |         a.length = 0; | ||||||
|  |         expect(a).toEqual([]); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("length value is coerced to number if possible", () => { | ||||||
|  |         var a = [1, 2, 3]; | ||||||
|  |         a.length = "42"; | ||||||
|  |         expect(a).toHaveLength(42); | ||||||
|  |         a.length = []; | ||||||
|  |         expect(a).toHaveLength(0); | ||||||
|  |         a.length = true; | ||||||
|  |         expect(a).toHaveLength(1); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -1,6 +1,4 @@ | ||||||
| load("test-common.js"); | test("Issue #1992, shrinking array during find() iteration", () => { | ||||||
| 
 |  | ||||||
| try { |  | ||||||
|     var a, callbackCalled; |     var a, callbackCalled; | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |     callbackCalled = 0; | ||||||
|  | @ -9,7 +7,7 @@ try { | ||||||
|         callbackCalled++; |         callbackCalled++; | ||||||
|         a.pop(); |         a.pop(); | ||||||
|     }); |     }); | ||||||
|     assert(callbackCalled === 5); |     expect(callbackCalled).toBe(5); | ||||||
| 
 | 
 | ||||||
|     callbackCalled = 0; |     callbackCalled = 0; | ||||||
|     a = [1, 2, 3, 4, 5]; |     a = [1, 2, 3, 4, 5]; | ||||||
|  | @ -17,9 +15,5 @@ try { | ||||||
|         callbackCalled++; |         callbackCalled++; | ||||||
|         a.pop(); |         a.pop(); | ||||||
|     }); |     }); | ||||||
|     assert(callbackCalled === 5); |     expect(callbackCalled).toBe(5); | ||||||
| 
 | }); | ||||||
|     console.log("PASS"); |  | ||||||
| } catch (e) { |  | ||||||
|     console.log("FAIL: " + e); |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,45 +1,25 @@ | ||||||
| load("test-common.js"); | describe("errors", () => { | ||||||
| 
 |     test("cannot spread number in array", () => { | ||||||
| function testArray(arr) { |         expect(() => { | ||||||
|     return arr.length === 4 && arr[0] === 0 && arr[1] === 1 && arr[2] === 2 && arr[3] === 3; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| try { |  | ||||||
|     let arr = [0, ...[1, 2], 3]; |  | ||||||
|     assert(testArray(arr)); |  | ||||||
| 
 |  | ||||||
|     let a = [1, 2]; |  | ||||||
|     arr = [0, ...a, 3]; |  | ||||||
|     assert(testArray(arr)); |  | ||||||
| 
 |  | ||||||
|     let obj = { a: [1, 2] }; |  | ||||||
|     arr = [0, ...obj.a, 3]; |  | ||||||
|     assert(testArray(arr)); |  | ||||||
| 
 |  | ||||||
|     arr = [...[], ...[...[0, 1, 2]], 3]; |  | ||||||
|     assert(testArray(arr)); |  | ||||||
| 
 |  | ||||||
|     assertThrowsError( |  | ||||||
|         () => { |  | ||||||
|             [...1]; |             [...1]; | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "1 is not iterable"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, |  | ||||||
|             message: "1 is not iterable", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     assertThrowsError( |     test("cannot spread object in array", () => { | ||||||
|         () => { |         expect(() => { | ||||||
|             [...{}]; |             [...{}]; | ||||||
|         }, |         }).toThrowWithMessage(TypeError, "[object Object] is not iterable"); | ||||||
|         { |     }); | ||||||
|             error: TypeError, | }); | ||||||
|             message: "[object Object] is not iterable", |  | ||||||
|         } |  | ||||||
|     ); |  | ||||||
| 
 | 
 | ||||||
|     console.log("PASS"); | test("basic functionality", () => { | ||||||
| } catch (e) { |     expect([1, ...[2, 3], 4]).toEqual([1, 2, 3, 4]); | ||||||
|     console.log("FAIL: " + e); | 
 | ||||||
| } |     let a = [2, 3]; | ||||||
|  |     expect([1, ...a, 4]).toEqual([1, 2, 3, 4]); | ||||||
|  | 
 | ||||||
|  |     let obj = { a: [2, 3] }; | ||||||
|  |     expect([1, ...obj.a, 4]).toEqual([1, 2, 3, 4]); | ||||||
|  | 
 | ||||||
|  |     expect([...[], ...[...[1, 2, 3]], 4]).toEqual([1, 2, 3, 4]); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | @ -43,6 +43,38 @@ | ||||||
| 
 | 
 | ||||||
| // FIXME: Will eventually not be necessary when all tests are converted
 | // FIXME: Will eventually not be necessary when all tests are converted
 | ||||||
| Vector<String> tests_to_run = { | Vector<String> tests_to_run = { | ||||||
|  |     "builtins/Array/Array.js", | ||||||
|  |     "builtins/Array/array-basic.js", | ||||||
|  |     "builtins/Array/array-length-setter.js", | ||||||
|  |     "builtins/Array/array-shrink-during-find-crash.js", | ||||||
|  |     "builtins/Array/array-spread.js", | ||||||
|  |     "builtins/Array/Array.isArray.js", | ||||||
|  |     "builtins/Array/Array.of.js", | ||||||
|  |     "builtins/Array/Array.prototype-generic-functions.js", | ||||||
|  |     "builtins/Array/Array.prototype.concat.js", | ||||||
|  |     "builtins/Array/Array.prototype.every.js", | ||||||
|  |     "builtins/Array/Array.prototype.fill.js", | ||||||
|  |     "builtins/Array/Array.prototype.filter.js", | ||||||
|  |     "builtins/Array/Array.prototype.find.js", | ||||||
|  |     "builtins/Array/Array.prototype.findIndex.js", | ||||||
|  |     "builtins/Array/Array.prototype.forEach.js", | ||||||
|  |     "builtins/Array/Array.prototype.includes.js", | ||||||
|  |     "builtins/Array/Array.prototype.indexOf.js", | ||||||
|  |     "builtins/Array/Array.prototype.join.js", | ||||||
|  |     "builtins/Array/Array.prototype.lastIndexOf.js", | ||||||
|  |     "builtins/Array/Array.prototype.map.js", | ||||||
|  |     "builtins/Array/Array.prototype.pop.js", | ||||||
|  |     "builtins/Array/Array.prototype.push.js", | ||||||
|  |     "builtins/Array/Array.prototype.reduce.js", | ||||||
|  |     "builtins/Array/Array.prototype.reduceRight.js", | ||||||
|  |     "builtins/Array/Array.prototype.reverse.js", | ||||||
|  |     "builtins/Array/Array.prototype.shift.js", | ||||||
|  |     "builtins/Array/Array.prototype.slice.js", | ||||||
|  |     "builtins/Array/Array.prototype.some.js", | ||||||
|  |     "builtins/Array/Array.prototype.splice.js", | ||||||
|  |     "builtins/Array/Array.prototype.toLocaleString.js", | ||||||
|  |     "builtins/Array/Array.prototype.toString.js", | ||||||
|  |     "builtins/Array/Array.prototype.unshift.js", | ||||||
|     "builtins/BigInt/BigInt.js", |     "builtins/BigInt/BigInt.js", | ||||||
|     "builtins/BigInt/bigint-basic.js", |     "builtins/BigInt/bigint-basic.js", | ||||||
|     "builtins/BigInt/bigint-number-mix-errors.js", |     "builtins/BigInt/bigint-number-mix-errors.js", | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Linus Groh
						Linus Groh