Skip to content

Commit

Permalink
Add unit tests for _.where.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Jul 29, 2012
1 parent d956cc9 commit 6801bbc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lodash.js
Expand Up @@ -2198,7 +2198,7 @@
'inLoop':
'for (pass = true, propIndex = 0; propIndex < propsLength; propIndex++) {\n' +
' prop = props[propIndex];\n' +
' if (pass = value[prop] === properties[prop]) break\n' +
' if (!(pass = value[prop] === properties[prop])) break\n' +
'}\n' +
'if (pass) result.push(value)'
});
Expand Down
50 changes: 50 additions & 0 deletions test/test.js
Expand Up @@ -253,7 +253,9 @@

test('should clone problem JScript properties (test in IE < 9)', function() {
deepEqual(_.clone(shadowed), shadowed);
ok(_.clone(shadowed) != shadowed);
deepEqual(_.clone(shadowed, true), shadowed);
ok(_.clone(shadowed, true) != shadowed);
});
}(1, 2, 3));

Expand Down Expand Up @@ -1223,6 +1225,54 @@

/*--------------------------------------------------------------------------*/

QUnit.module('lodash.where');

(function() {
var array = [
{ 'a': 1 },
{ 'a': 1 },
{ 'a': 1, 'b': 2 },
{ 'a': 2, 'b': 2 },
{ 'a': 3 }
];

test('should filter by properties', function() {
deepEqual(_.where(array, { 'a': 1 }), [{ 'a': 1 }, { 'a': 1 }, { 'a': 1, 'b': 2 }]);
deepEqual(_.where(array, { 'a': 2 }), [{ 'a': 2, 'b': 2 }]);
deepEqual(_.where(array, { 'a': 3 }), [{ 'a': 3 }]);
deepEqual(_.where(array, { 'b': 1 }), []);
deepEqual(_.where(array, { 'b': 2 }), [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }]);
deepEqual(_.where(array, { 'a': 1, 'b': 2 }), [{ 'a': 1, 'b': 2 }]);
});

test('should filter by inherited properties', function() {
function Foo() {}
Foo.prototype = { 'b': 2 };

var properties = new Foo;
properties.a = 1;

deepEqual(_.where(array, properties), [{ 'a': 1, 'b': 2 }]);
});

test('should filter by problem JScript properties (test in IE < 9)', function() {
var collection = [shadowed];
deepEqual(_.where(collection, shadowed), [shadowed]);
});

test('should work with an object for `collection`', function() {
var collection = {
'x': { 'a': 1 },
'y': { 'a': 3 },
'z': { 'a': 1, 'b': 2 }
};

deepEqual(_.where(collection, { 'a': 1 }), [{ 'a': 1 }, { 'a': 1, 'b': 2 }]);
});
}());

/*--------------------------------------------------------------------------*/

QUnit.module('lodash.zipObject');

(function() {
Expand Down

0 comments on commit 6801bbc

Please sign in to comment.