Skip to content

Commit 1ca72c3

Browse files
committed
check that Lib.concat calls Array.concat when it should
1 parent a735fbf commit 1ca72c3

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

test/jasmine/tests/lib_test.js

+43-13
Original file line numberDiff line numberDiff line change
@@ -2462,19 +2462,33 @@ describe('Test lib.js:', function() {
24622462

24632463
describe('concat', function() {
24642464
var concat = Lib.concat;
2465+
2466+
beforeEach(function() {
2467+
spyOn(Array.prototype, 'concat').and.callThrough();
2468+
});
2469+
24652470
it('works with multiple Arrays', function() {
2466-
expect(concat([1], [[2], 3], [{a: 4}, 5, 6]))
2467-
.toEqual([1, [2], 3, {a: 4}, 5, 6]);
2471+
var res = concat([1], [[2], 3], [{a: 4}, 5, 6]);
2472+
expect(Array.prototype.concat.calls.count()).toBe(1);
2473+
2474+
// note: can't `concat` in the `expect` if we want to count native
2475+
// `Array.concat calls`, because `toEqual` calls `Array.concat`
2476+
// profusely itself.
2477+
expect(res).toEqual([1, [2], 3, {a: 4}, 5, 6]);
24682478
});
24692479

24702480
it('works with some empty arrays', function() {
24712481
var a1 = [1];
2472-
expect(concat(a1, [], [2, 3])).toEqual([1, 2, 3]);
2482+
var res = concat(a1, [], [2, 3]);
2483+
expect(Array.prototype.concat.calls.count()).toBe(1);
2484+
expect(res).toEqual([1, 2, 3]);
24732485
expect(a1).toEqual([1]); // did not mutate a1
24742486

2487+
Array.prototype.concat.calls.reset();
24752488
var a1b = concat(a1, []);
24762489
var a1c = concat([], a1b);
24772490
var a1d = concat([], a1c, []);
2491+
expect(Array.prototype.concat.calls.count()).toBe(0);
24782492

24792493
expect(a1d).toEqual([1]);
24802494
// does not mutate a1, but *will* return it unchanged if it's the
@@ -2486,28 +2500,44 @@ describe('Test lib.js:', function() {
24862500
// a single typedArray will keep its identity (and type)
24872501
// even if other empty arrays don't match type.
24882502
var f1 = new Float32Array([1, 2]);
2489-
expect(concat([], f1, new Float64Array([]))).toBe(f1);
2503+
Array.prototype.concat.calls.reset();
2504+
res = concat([], f1, new Float64Array([]));
2505+
expect(Array.prototype.concat.calls.count()).toBe(0);
2506+
expect(res).toBe(f1);
24902507
expect(f1).toEqual(new Float32Array([1, 2]));
24912508
});
24922509

24932510
it('works with all empty arrays', function() {
2494-
expect(concat()).toEqual([]);
2495-
expect(concat([])).toEqual([]);
2496-
expect(concat([], [])).toEqual([]);
2497-
expect(concat([], [], [], [])).toEqual([]);
2511+
[[], [[]], [[], []], [[], [], [], []]].forEach(function(empties) {
2512+
Array.prototype.concat.calls.reset();
2513+
var res = concat.apply(null, empties);
2514+
expect(Array.prototype.concat.calls.count()).toBe(0);
2515+
expect(res).toEqual([]);
2516+
});
24982517
});
24992518

25002519
it('converts mismatched types to Array', function() {
2501-
expect(concat([1, 2], new Float64Array([3, 4]))).toEqual([1, 2, 3, 4]);
2502-
expect(concat(new Float64Array([1, 2]), [3, 4])).toEqual([1, 2, 3, 4]);
2503-
expect(concat(new Float64Array([1, 2]), new Float32Array([3, 4]))).toEqual([1, 2, 3, 4]);
2520+
[
2521+
[[1, 2], new Float64Array([3, 4])],
2522+
[new Float64Array([1, 2]), [3, 4]],
2523+
[new Float64Array([1, 2]), new Float32Array([3, 4])]
2524+
].forEach(function(mismatch) {
2525+
Array.prototype.concat.calls.reset();
2526+
var res = concat.apply(null, mismatch);
2527+
// no concat - all entries moved over individually
2528+
expect(Array.prototype.concat.calls.count()).toBe(0);
2529+
expect(res).toEqual([1, 2, 3, 4]);
2530+
});
25042531
});
25052532

25062533
it('concatenates matching TypedArrays preserving type', function() {
25072534
[Float32Array, Float64Array, Int16Array, Int32Array].forEach(function(Type, i) {
25082535
var v = i * 10;
2509-
expect(concat([], new Type([v]), new Type([v + 1, v]), new Type([v + 2, v, v])))
2510-
.toEqual(new Type([v, v + 1, v, v + 2, v, v]));
2536+
Array.prototype.concat.calls.reset();
2537+
var res = concat([], new Type([v]), new Type([v + 1, v]), new Type([v + 2, v, v]));
2538+
// no concat - uses `TypedArray.set`
2539+
expect(Array.prototype.concat.calls.count()).toBe(0);
2540+
expect(res).toEqual(new Type([v, v + 1, v, v + 2, v, v]));
25112541
});
25122542
});
25132543
});

0 commit comments

Comments
 (0)