@@ -2462,19 +2462,33 @@ describe('Test lib.js:', function() {
2462
2462
2463
2463
describe ( 'concat' , function ( ) {
2464
2464
var concat = Lib . concat ;
2465
+
2466
+ beforeEach ( function ( ) {
2467
+ spyOn ( Array . prototype , 'concat' ) . and . callThrough ( ) ;
2468
+ } ) ;
2469
+
2465
2470
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 ] ) ;
2468
2478
} ) ;
2469
2479
2470
2480
it ( 'works with some empty arrays' , function ( ) {
2471
2481
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 ] ) ;
2473
2485
expect ( a1 ) . toEqual ( [ 1 ] ) ; // did not mutate a1
2474
2486
2487
+ Array . prototype . concat . calls . reset ( ) ;
2475
2488
var a1b = concat ( a1 , [ ] ) ;
2476
2489
var a1c = concat ( [ ] , a1b ) ;
2477
2490
var a1d = concat ( [ ] , a1c , [ ] ) ;
2491
+ expect ( Array . prototype . concat . calls . count ( ) ) . toBe ( 0 ) ;
2478
2492
2479
2493
expect ( a1d ) . toEqual ( [ 1 ] ) ;
2480
2494
// does not mutate a1, but *will* return it unchanged if it's the
@@ -2486,28 +2500,44 @@ describe('Test lib.js:', function() {
2486
2500
// a single typedArray will keep its identity (and type)
2487
2501
// even if other empty arrays don't match type.
2488
2502
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 ) ;
2490
2507
expect ( f1 ) . toEqual ( new Float32Array ( [ 1 , 2 ] ) ) ;
2491
2508
} ) ;
2492
2509
2493
2510
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
+ } ) ;
2498
2517
} ) ;
2499
2518
2500
2519
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
+ } ) ;
2504
2531
} ) ;
2505
2532
2506
2533
it ( 'concatenates matching TypedArrays preserving type' , function ( ) {
2507
2534
[ Float32Array , Float64Array , Int16Array , Int32Array ] . forEach ( function ( Type , i ) {
2508
2535
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 ] ) ) ;
2511
2541
} ) ;
2512
2542
} ) ;
2513
2543
} ) ;
0 commit comments