Add unit tests for _.where.

Former-commit-id: 5247bba20d7aa772867e29def99221faea376db1
This commit is contained in:
John-David Dalton
2012-07-29 14:17:34 -07:00
parent 8079fb5bc5
commit c3cd9007d2
2 changed files with 51 additions and 1 deletions

View File

@@ -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)'
});

View File

@@ -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));
@@ -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() {