@@ -654,10 +654,79 @@ describe('angular', function() {
654654 var log = [ ] ;
655655 var collection = [ ] ;
656656 collection [ 5 ] = 'SPARSE' ;
657- forEach ( collection , function ( item , index ) { log . push ( item + index ) ; } ) ;
657+ forEach ( collection , function ( item , index ) {
658+ log . push ( item + index ) ;
659+ } ) ;
658660 expect ( log . length ) . toBe ( 1 ) ;
659661 expect ( log [ 0 ] ) . toBe ( 'SPARSE5' ) ;
660662 } ) ;
663+
664+
665+ describe ( 'ES spec api compliance' , function ( ) {
666+
667+ function testForEachSpec ( expectedSize , collection ) {
668+ var that = { } ;
669+
670+ forEach ( collection , function ( value , key , collectionArg ) {
671+ expect ( collectionArg ) . toBe ( collection ) ;
672+ expect ( collectionArg [ key ] ) . toBe ( value ) ;
673+
674+ expect ( this ) . toBe ( that ) ;
675+
676+ expectedSize -- ;
677+ } , that ) ;
678+
679+ expect ( expectedSize ) . toBe ( 0 ) ;
680+ }
681+
682+
683+ it ( 'should follow the ES spec when called with array' , function ( ) {
684+ testForEachSpec ( 2 , [ 1 , 2 ] ) ;
685+ } ) ;
686+
687+
688+ it ( 'should follow the ES spec when called with arguments' , function ( ) {
689+ testForEachSpec ( 2 , ( function ( ) { return arguments ; } ( 1 , 2 ) ) ) ;
690+ } ) ;
691+
692+
693+ it ( 'should follow the ES spec when called with string' , function ( ) {
694+ testForEachSpec ( 2 , '12' ) ;
695+ } ) ;
696+
697+
698+ it ( 'should follow the ES spec when called with jQuery/jqLite' , function ( ) {
699+ testForEachSpec ( 2 , jqLite ( "<span>a</span><span>b</span>" ) ) ;
700+ } ) ;
701+
702+
703+ it ( 'should follow the ES spec when called with childNodes NodeList' , function ( ) {
704+ testForEachSpec ( 2 , jqLite ( "<p><span>a</span><span>b</span></p>" ) [ 0 ] . childNodes ) ;
705+ } ) ;
706+
707+
708+ it ( 'should follow the ES spec when called with getElementsByTagName HTMLCollection' , function ( ) {
709+ testForEachSpec ( 2 , jqLite ( "<p><span>a</span><span>b</span></p>" ) [ 0 ] . getElementsByTagName ( "*" ) ) ;
710+ } ) ;
711+
712+
713+ it ( 'should follow the ES spec when called with querySelectorAll HTMLCollection' , function ( ) {
714+ testForEachSpec ( 2 , jqLite ( "<p><span>a</span><span>b</span></p>" ) [ 0 ] . querySelectorAll ( "*" ) ) ;
715+ } ) ;
716+
717+
718+ it ( 'should follow the ES spec when called with JSON' , function ( ) {
719+ testForEachSpec ( 2 , { a : 1 , b : 2 } ) ;
720+ } ) ;
721+
722+
723+ it ( 'should follow the ES spec when called with function' , function ( ) {
724+ function f ( ) { }
725+ f . a = 1 ;
726+ f . b = 2 ;
727+ testForEachSpec ( 2 , f ) ;
728+ } ) ;
729+ } ) ;
661730 } ) ;
662731
663732
0 commit comments