michael@0: load(libdir + "asserts.js"); michael@0: load(libdir + "eqArrayHelper.js"); michael@0: load(libdir + "iteration.js"); michael@0: michael@0: function checkCommon(f) { michael@0: assertEqArray(f.apply(null, ...[[1, 2, 3]]), [1, 2, 3]); michael@0: assertEqArray(f.apply(...[null], [1, 2, 3]), [1, 2, 3]); michael@0: assertEqArray(f.apply(...[null], ...[[1, 2, 3]]), [1, 2, 3]); michael@0: assertEqArray(f.apply(...[null, [1, 2, 3]]), [1, 2, 3]); michael@0: michael@0: // other iterable objects michael@0: assertEqArray(f.apply(...Set([null, [1, 2, 3]])), [1, 2, 3]); michael@0: assertEqArray(f.apply(...[null, [1, 2, 3]][std_iterator]()), [1, 2, 3]); michael@0: let itr = {}; michael@0: itr[std_iterator] = function() { michael@0: return { michael@0: i: 0, michael@0: next: function() { michael@0: this.i++; michael@0: if (this.i == 1) michael@0: return { value: null, done: false }; michael@0: else if (this.i == 2) michael@0: return { value: [1, 2, 3], done: false }; michael@0: else michael@0: return { value: undefined, done: true }; michael@0: } michael@0: }; michael@0: }; michael@0: assertEqArray(f.apply(...itr), [1, 2, 3]); michael@0: function* gen() { michael@0: yield null; michael@0: yield [1, 2, 3]; michael@0: } michael@0: assertEqArray(f.apply(...gen()), [1, 2, 3]); michael@0: michael@0: let a; michael@0: assertEqArray(f.apply(null, ...a=[[1, 2, 3]]), [1, 2, 3]); michael@0: michael@0: // According to the draft spec, null and undefined are to be treated as empty michael@0: // arrays. However, they are not iterable. If the spec is not changed to be in michael@0: // terms of iterables, these tests should be fixed. michael@0: //assertEqArray(f.apply(null, ...null, [1, 2, 3]), [1, 2, 3]); michael@0: //assertEqArray(f.apply(null, ...undefined, [1, 2, 3]), [1, 2, 3]); michael@0: assertThrowsInstanceOf(() => f.apply(null, ...null, [1, 2, 3]), TypeError); michael@0: assertThrowsInstanceOf(() => f.apply(null, ...undefined, [1, 2, 3]), TypeError); michael@0: } michael@0: michael@0: function checkNormal(f) { michael@0: checkCommon(f); michael@0: michael@0: assertEqArray(f.apply(null, ...[[]]), [undefined, undefined, undefined]); michael@0: assertEqArray(f.apply(null, ...[[1]]), [1, undefined, undefined]); michael@0: assertEqArray(f.apply(null, ...[[1, 2]]), [1, 2, undefined]); michael@0: assertEqArray(f.apply(null, ...[[1, 2, 3, 4]]), [1, 2, 3]); michael@0: michael@0: assertEqArray(f.apply(null, ...[[undefined]]), [undefined, undefined, undefined]); michael@0: } michael@0: michael@0: checkNormal(function(a, b, c) [a, b, c]); michael@0: checkNormal((a, b, c) => [a, b, c]); michael@0: michael@0: function checkDefault(f) { michael@0: checkCommon(f); michael@0: michael@0: assertEqArray(f.apply(null, ...[[]]), [-1, -2, -3]); michael@0: assertEqArray(f.apply(null, ...[[1]]), [1, -2, -3]); michael@0: assertEqArray(f.apply(null, ...[[1, 2]]), [1, 2, -3]); michael@0: assertEqArray(f.apply(null, ...[[1, 2, 3, 4]]), [1, 2, 3]); michael@0: michael@0: assertEqArray(f.apply(null, ...[[undefined]]), [-1, -2, -3]); michael@0: } michael@0: michael@0: checkDefault(function(a = -1, b = -2, c = -3) [a, b, c]); michael@0: checkDefault((a = -1, b = -2, c = -3) => [a, b, c]); michael@0: michael@0: function checkRest(f) { michael@0: checkCommon(f); michael@0: michael@0: assertEqArray(f.apply(null, ...[[]]), []); michael@0: assertEqArray(f.apply(null, ...[[1]]), [1]); michael@0: assertEqArray(f.apply(null, ...[[1, 2]]), [1, 2]); michael@0: assertEqArray(f.apply(null, ...[[1, 2, 3, 4]]), [1, 2, 3, 4]); michael@0: michael@0: assertEqArray(f.apply(null, ...[[undefined]]), [undefined]); michael@0: michael@0: // other iterable objects michael@0: assertEqArray(f.apply(null, ...Map([[["a", "A"], ["b", "B"]]])).map(([k, v]) => k + v), ["aA", "bB"]); michael@0: } michael@0: michael@0: checkRest(function(...x) x); michael@0: checkRest((...x) => x);