aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/core.js
diff options
context:
space:
mode:
authorAurelio De Rosa <aurelioderosa@gmail.com>2015-09-21 00:05:50 +0100
committerOleg Gaidarenko <markelog@gmail.com>2015-10-13 03:48:55 +0300
commit6e466af010c8265b3fb3dd73ff5fa7f66d8de8ad (patch)
tree6c867053b30568cce4f4ca126d77c24e4472525d /test/unit/core.js
parent8cac6da55d78de0f0901927fa20ad70759938a71 (diff)
downloadjquery-6e466af010c8265b3fb3dd73ff5fa7f66d8de8ad.tar.gz
jquery-6e466af010c8265b3fb3dd73ff5fa7f66d8de8ad.zip
Tests: Account for array-like objects in jQuery.grep
Cherry-picked from 67b76f5986a78d868553b9aec0d0192f5259a078 Closes gh-2605
Diffstat (limited to 'test/unit/core.js')
-rw-r--r--test/unit/core.js79
1 files changed, 73 insertions, 6 deletions
diff --git a/test/unit/core.js b/test/unit/core.js
index a9b1fc802..e8202accd 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -975,16 +975,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 );