@@ -2459,6 +2459,88 @@ describe('Test lib.js:', function() {
2459
2459
expect ( toContainer ) . toEqual ( expected ) ;
2460
2460
} ) ;
2461
2461
} ) ;
2462
+
2463
+ describe ( 'concat' , function ( ) {
2464
+ var concat = Lib . concat ;
2465
+
2466
+ beforeEach ( function ( ) {
2467
+ spyOn ( Array . prototype , 'concat' ) . and . callThrough ( ) ;
2468
+ } ) ;
2469
+
2470
+ it ( 'works with multiple Arrays' , function ( ) {
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 ] ) ;
2478
+ } ) ;
2479
+
2480
+ it ( 'works with some empty arrays' , function ( ) {
2481
+ var a1 = [ 1 ] ;
2482
+ var res = concat ( a1 , [ ] , [ 2 , 3 ] ) ;
2483
+ expect ( Array . prototype . concat . calls . count ( ) ) . toBe ( 1 ) ;
2484
+ expect ( res ) . toEqual ( [ 1 , 2 , 3 ] ) ;
2485
+ expect ( a1 ) . toEqual ( [ 1 ] ) ; // did not mutate a1
2486
+
2487
+ Array . prototype . concat . calls . reset ( ) ;
2488
+ var a1b = concat ( a1 , [ ] ) ;
2489
+ var a1c = concat ( [ ] , a1b ) ;
2490
+ var a1d = concat ( [ ] , a1c , [ ] ) ;
2491
+ expect ( Array . prototype . concat . calls . count ( ) ) . toBe ( 0 ) ;
2492
+
2493
+ expect ( a1d ) . toEqual ( [ 1 ] ) ;
2494
+ // does not mutate a1, but *will* return it unchanged if it's the
2495
+ // only one with data
2496
+ expect ( a1d ) . toBe ( a1 ) ;
2497
+
2498
+ expect ( concat ( [ ] , [ 0 ] , [ 1 , 0 ] , [ 2 , 0 , 0 ] ) ) . toEqual ( [ 0 , 1 , 0 , 2 , 0 , 0 ] ) ;
2499
+
2500
+ // a single typedArray will keep its identity (and type)
2501
+ // even if other empty arrays don't match type.
2502
+ var f1 = new Float32Array ( [ 1 , 2 ] ) ;
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 ) ;
2507
+ expect ( f1 ) . toEqual ( new Float32Array ( [ 1 , 2 ] ) ) ;
2508
+ } ) ;
2509
+
2510
+ it ( 'works with all empty arrays' , function ( ) {
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
+ } ) ;
2517
+ } ) ;
2518
+
2519
+ it ( 'converts mismatched types to Array' , function ( ) {
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
+ } ) ;
2531
+ } ) ;
2532
+
2533
+ it ( 'concatenates matching TypedArrays preserving type' , function ( ) {
2534
+ [ Float32Array , Float64Array , Int16Array , Int32Array ] . forEach ( function ( Type , i ) {
2535
+ var v = i * 10 ;
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 ] ) ) ;
2541
+ } ) ;
2542
+ } ) ;
2543
+ } ) ;
2462
2544
} ) ;
2463
2545
2464
2546
describe ( 'Queue' , function ( ) {
0 commit comments