From 67b76f5986a78d868553b9aec0d0192f5259a078 Mon Sep 17 00:00:00 2001 From: Aurelio De Rosa Date: Mon, 21 Sep 2015 00:05:50 +0100 Subject: [PATCH] Tests: Account for array-like objects in jQuery.grep Closes gh-2605 --- test/unit/core.js | 79 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/test/unit/core.js b/test/unit/core.js index 1ed266318..33d162d68 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -970,16 +970,83 @@ QUnit.test( "jQuery.grep()", function( assert ) { assert.deepEqual( jQuery.grep( [], searchCriterion ), [], "Empty array" ); assert.deepEqual( jQuery.grep( new Array( 4 ), searchCriterion ), [], "Sparse array" ); - assert.deepEqual( jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion ), [ 2, 4, 6 ], "Satisfying elements present" ); - assert.deepEqual( jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion ), [], "Satisfying elements absent" ); + assert.deepEqual( + jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion ), + [ 2, 4, 6 ], + "Satisfying elements present" + ); + assert.deepEqual( + jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion ), + [], + "Satisfying elements absent" + ); - assert.deepEqual( jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, true ), [ 1, 3, 5 ], "Satisfying elements present and grep inverted" ); - assert.deepEqual( jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, true ), [ 1, 3, 5, 7 ], "Satisfying elements absent and grep inverted" ); + assert.deepEqual( + jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, true ), + [ 1, 3, 5 ], + "Satisfying elements present and grep inverted" + ); + assert.deepEqual( + jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, true ), + [ 1, 3, 5, 7 ], + "Satisfying elements absent and grep inverted" + ); - assert.deepEqual( jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, false ), [ 2, 4, 6 ], "Satisfying elements present but grep explicitly uninverted" ); - assert.deepEqual( jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, false ), [], "Satisfying elements absent and grep explicitly uninverted" ); + assert.deepEqual( + jQuery.grep( [ 1, 2, 3, 4, 5, 6 ], searchCriterion, false ), + [ 2, 4, 6 ], + "Satisfying elements present but grep explicitly uninverted" + ); + assert.deepEqual( + jQuery.grep( [ 1, 3, 5, 7 ], searchCriterion, false ), + [], + "Satisfying elements absent and grep explicitly uninverted" + ); } ); +QUnit.test( "jQuery.grep(Array-like)", function( assert ) { + assert.expect( 7 ); + + var searchCriterion = function( value ) { + return value % 2 === 0; + }; + + assert.deepEqual( jQuery.grep( { length: 0 }, searchCriterion ), [], "Empty array-like" ); + + assert.deepEqual( + jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion ), + [ 2, 4, 6 ], + "Satisfying elements present and array-like object used" + ); + assert.deepEqual( + jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion ), + [], + "Satisfying elements absent and Array-like object used" + ); + + assert.deepEqual( + jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion, true ), + [ 1, 3, 5 ], + "Satisfying elements present, array-like object used, and grep inverted" + ); + assert.deepEqual( + jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion, true ), + [ 1, 3, 5, 7 ], + "Satisfying elements absent, array-like object used, and grep inverted" + ); + + assert.deepEqual( + jQuery.grep( { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, length: 6 }, searchCriterion, false ), + [ 2, 4, 6 ], + "Satisfying elements present, Array-like object used, but grep explicitly uninverted" + ); + assert.deepEqual( + jQuery.grep( { 0: 1, 1: 3, 2: 5, 3: 7, length: 4 }, searchCriterion, false ), + [], + "Satisfying elements absent, Array-like object used, and grep explicitly uninverted" + ); +}); + QUnit.test( "jQuery.extend(Object, Object)", function( assert ) { assert.expect( 28 ); -- 2.39.5