mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Update deps.
This commit is contained in:
160
vendor/underscore/test/objects.js
vendored
160
vendor/underscore/test/objects.js
vendored
@@ -1,6 +1,7 @@
|
||||
(function() {
|
||||
|
||||
module('Objects');
|
||||
/* global iObject, iElement, iArguments, iFunction, iArray, iString, iNumber, iBoolean, iDate, iRegExp, iNaN, iNull, iUndefined, ActiveXObject */
|
||||
|
||||
test('keys', function() {
|
||||
deepEqual(_.keys({one : 1, two : 2}), ['one', 'two'], 'can extract the keys from an object');
|
||||
@@ -29,12 +30,12 @@
|
||||
deepEqual(_.keys(_.invert(obj)), ['Moe', 'Larry', 'Curly'], 'can invert an object');
|
||||
deepEqual(_.invert(_.invert(obj)), obj, 'two inverts gets you back where you started');
|
||||
|
||||
var obj = {length: 3};
|
||||
ok(_.invert(obj)['3'] == 'length', 'can invert an object with "length"')
|
||||
obj = {length: 3};
|
||||
equal(_.invert(obj)['3'], 'length', 'can invert an object with "length"');
|
||||
});
|
||||
|
||||
test('functions', function() {
|
||||
var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce};
|
||||
var obj = {a : 'dash', b : _.map, c : /yo/, d : _.reduce};
|
||||
deepEqual(['b', 'd'], _.functions(obj), 'can grab the function names of any passed-in object');
|
||||
|
||||
var Animal = function(){};
|
||||
@@ -42,6 +43,10 @@
|
||||
deepEqual(_.functions(new Animal), ['run'], 'also looks up functions on the prototype');
|
||||
});
|
||||
|
||||
test('methods', function() {
|
||||
strictEqual(_.functions, _.methods, 'alias for functions');
|
||||
});
|
||||
|
||||
test('extend', function() {
|
||||
var result;
|
||||
equal(_.extend({}, {a: 'b'}).a, 'b', 'can extend an object with the attributes of another');
|
||||
@@ -54,6 +59,12 @@
|
||||
result = _.extend({}, {a: void 0, b: null});
|
||||
deepEqual(_.keys(result), ['a', 'b'], 'extend copies undefined values');
|
||||
|
||||
var F = function() {};
|
||||
F.prototype = {a: 'b'};
|
||||
var subObj = new F();
|
||||
subObj.c = 'd';
|
||||
deepEqual(_.extend({}, subObj), {c: 'd'}, 'extend ignores any properties but own from source');
|
||||
|
||||
try {
|
||||
result = {};
|
||||
_.extend(result, null, undefined, {a: 1});
|
||||
@@ -76,6 +87,10 @@
|
||||
result = _.pick(['a', 'b'], 1);
|
||||
deepEqual(result, {1: 'b'}, 'can pick numeric properties');
|
||||
|
||||
deepEqual(_.pick(null, 'a', 'b'), {}, 'non objects return empty object');
|
||||
deepEqual(_.pick(undefined, 'toString'), {}, 'null/undefined return empty object');
|
||||
deepEqual(_.pick(5, 'toString', 'b'), {toString: Number.prototype.toString}, 'can iterate primitives');
|
||||
|
||||
var data = {a: 1, b: 2, c: 3};
|
||||
var callback = function(value, key, object) {
|
||||
strictEqual(key, {1: 'a', 2: 'b', 3: 'c'}[value]);
|
||||
@@ -87,7 +102,12 @@
|
||||
|
||||
var Obj = function(){};
|
||||
Obj.prototype = {a: 1, b: 2, c: 3};
|
||||
deepEqual(_.pick(new Obj, 'a', 'c'), {a: 1, c: 3}, 'include prototype props');
|
||||
var instance = new Obj();
|
||||
deepEqual(_.pick(instance, 'a', 'c'), {a: 1, c: 3}, 'include prototype props');
|
||||
|
||||
deepEqual(_.pick(data, function(val, key) {
|
||||
return this[key] === 3 && this === instance;
|
||||
}, instance), {c: 3}, 'function is given context');
|
||||
});
|
||||
|
||||
test('omit', function() {
|
||||
@@ -101,6 +121,10 @@
|
||||
result = _.omit(['a', 'b'], 0);
|
||||
deepEqual(result, {1: 'b'}, 'can omit numeric properties');
|
||||
|
||||
deepEqual(_.omit(null, 'a', 'b'), {}, 'non objects return empty object');
|
||||
deepEqual(_.omit(undefined, 'toString'), {}, 'null/undefined return empty object');
|
||||
deepEqual(_.omit(5, 'toString', 'b'), {}, 'returns empty object for primitives');
|
||||
|
||||
var data = {a: 1, b: 2, c: 3};
|
||||
var callback = function(value, key, object) {
|
||||
strictEqual(key, {1: 'a', 2: 'b', 3: 'c'}[value]);
|
||||
@@ -112,11 +136,15 @@
|
||||
|
||||
var Obj = function(){};
|
||||
Obj.prototype = {a: 1, b: 2, c: 3};
|
||||
deepEqual(_.omit(new Obj, 'b'), {a: 1, c: 3}, 'include prototype props');
|
||||
var instance = new Obj();
|
||||
deepEqual(_.omit(instance, 'b'), {a: 1, c: 3}, 'include prototype props');
|
||||
|
||||
deepEqual(_.omit(data, function(val, key) {
|
||||
return this[key] === 3 && this === instance;
|
||||
}, instance), {a: 1, b: 2}, 'function is given context');
|
||||
});
|
||||
|
||||
test('defaults', function() {
|
||||
var result;
|
||||
var options = {zero: 0, one: 1, empty: '', nan: NaN, nothing: null};
|
||||
|
||||
_.defaults(options, {zero: 1, one: 10, twenty: 20, nothing: 'str'});
|
||||
@@ -147,7 +175,7 @@
|
||||
equal(clone.name, 'moe', 'the clone as the attributes of the original');
|
||||
|
||||
clone.name = 'curly';
|
||||
ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original');
|
||||
ok(clone.name === 'curly' && moe.name === 'moe', 'clones can change shallow attributes without affecting the original');
|
||||
|
||||
clone.lucky.push(101);
|
||||
equal(_.last(moe.lucky), 101, 'changes to deep attributes are shared with the original');
|
||||
@@ -199,6 +227,7 @@
|
||||
|
||||
// Comparisons involving `NaN`.
|
||||
ok(_.isEqual(NaN, NaN), '`NaN` is equal to `NaN`');
|
||||
ok(_.isEqual(new Object(NaN), NaN), 'Object(`NaN`) is equal to `NaN`');
|
||||
ok(!_.isEqual(61, NaN), 'A number primitive is not equal to `NaN`');
|
||||
ok(!_.isEqual(new Number(79), NaN), 'A number object is not equal to `NaN`');
|
||||
ok(!_.isEqual(Infinity, NaN), '`Infinity` is not equal to `NaN`');
|
||||
@@ -254,7 +283,7 @@
|
||||
|
||||
// Arrays with primitive and object values.
|
||||
ok(_.isEqual([1, 'Larry', true], [1, 'Larry', true]), 'Arrays containing identical primitives are equal');
|
||||
ok(_.isEqual([(/Moe/g), new Date(2009, 9, 25)], [(/Moe/g), new Date(2009, 9, 25)]), 'Arrays containing equivalent elements are equal');
|
||||
ok(_.isEqual([/Moe/g, new Date(2009, 9, 25)], [/Moe/g, new Date(2009, 9, 25)]), 'Arrays containing equivalent elements are equal');
|
||||
|
||||
// Multi-dimensional arrays.
|
||||
var a = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
|
||||
@@ -277,6 +306,10 @@
|
||||
ok(_.isEqual(Array(3), Array(3)), 'Sparse arrays of identical lengths are equal');
|
||||
ok(!_.isEqual(Array(3), Array(6)), 'Sparse arrays of different lengths are not equal when both are empty');
|
||||
|
||||
var sparse = [];
|
||||
sparse[1] = 5;
|
||||
ok(_.isEqual(sparse, [undefined, 5]), 'Handles sparse arrays as dense');
|
||||
|
||||
// Simple objects.
|
||||
ok(_.isEqual({a: 'Curly', b: 1, c: true}, {a: 'Curly', b: 1, c: true}), 'Objects containing identical primitives are equal');
|
||||
ok(_.isEqual({a: /Curly/g, b: new Date(2009, 11, 13)}, {a: /Curly/g, b: new Date(2009, 11, 13)}), 'Objects containing equivalent members are equal');
|
||||
@@ -393,14 +426,14 @@
|
||||
if (Object.create) {
|
||||
a = Object.create(null, {x: {value: 1, enumerable: true}});
|
||||
b = {x: 1};
|
||||
ok(_.isEqual(a, b));
|
||||
ok(_.isEqual(a, b), 'Handles objects without a constructor (e.g. from Object.create');
|
||||
}
|
||||
|
||||
function Foo() { this.a = 1; }
|
||||
Foo.prototype.constructor = null;
|
||||
|
||||
var other = { 'a': 1 };
|
||||
strictEqual(_.isEqual(new Foo, other), false);
|
||||
var other = {a: 1};
|
||||
strictEqual(_.isEqual(new Foo, other), false, 'Objects from different constructors are not equal');
|
||||
});
|
||||
|
||||
test('isEmpty', function() {
|
||||
@@ -425,25 +458,25 @@
|
||||
|
||||
// Setup remote variables for iFrame tests.
|
||||
var iframe = document.createElement('iframe');
|
||||
iframe.frameBorder = iframe.height = iframe.width = 0
|
||||
iframe.frameBorder = iframe.height = iframe.width = 0;
|
||||
document.body.appendChild(iframe);
|
||||
var iDoc = (iDoc = iframe.contentDocument || iframe.contentWindow).document || iDoc;
|
||||
iDoc.write(
|
||||
'<script>\
|
||||
parent.iElement = document.createElement("div");\
|
||||
parent.iArguments = (function(){ return arguments; })(1, 2, 3);\
|
||||
parent.iArray = [1, 2, 3];\
|
||||
parent.iString = new String("hello");\
|
||||
parent.iNumber = new Number(100);\
|
||||
parent.iFunction = (function(){});\
|
||||
parent.iDate = new Date();\
|
||||
parent.iRegExp = /hi/;\
|
||||
parent.iNaN = NaN;\
|
||||
parent.iNull = null;\
|
||||
parent.iBoolean = new Boolean(false);\
|
||||
parent.iUndefined = undefined;\
|
||||
parent.iObject = {};\
|
||||
</script>'
|
||||
'<script>' +
|
||||
' parent.iElement = document.createElement("div");' +
|
||||
' parent.iArguments = (function(){ return arguments; })(1, 2, 3);' +
|
||||
' parent.iArray = [1, 2, 3];' +
|
||||
' parent.iString = new String("hello");' +
|
||||
' parent.iNumber = new Number(100);' +
|
||||
' parent.iFunction = (function(){});' +
|
||||
' parent.iDate = new Date();' +
|
||||
' parent.iRegExp = /hi/;' +
|
||||
' parent.iNaN = NaN;' +
|
||||
' parent.iNull = null;' +
|
||||
' parent.iBoolean = new Boolean(false);' +
|
||||
' parent.iUndefined = undefined;' +
|
||||
' parent.iObject = {};' +
|
||||
'</script>'
|
||||
);
|
||||
iDoc.close();
|
||||
|
||||
@@ -454,7 +487,7 @@
|
||||
});
|
||||
|
||||
test('isArguments', function() {
|
||||
var args = (function(){ return arguments; })(1, 2, 3);
|
||||
var args = (function(){ return arguments; }(1, 2, 3));
|
||||
ok(!_.isArguments('string'), 'a string is not an arguments object');
|
||||
ok(!_.isArguments(_.isArguments), 'a function is not an arguments object');
|
||||
ok(_.isArguments(args), 'but the arguments object is an arguments object');
|
||||
@@ -607,32 +640,79 @@
|
||||
max().
|
||||
tap(interceptor).
|
||||
value();
|
||||
ok(returned == 6 && intercepted == 6, 'can use tapped objects in a chain');
|
||||
equal(returned, 6, 'can use tapped objects in a chain');
|
||||
equal(intercepted, returned, 'can use tapped objects in a chain');
|
||||
});
|
||||
|
||||
test("has", function () {
|
||||
var obj = {foo: "bar", func: function () {} };
|
||||
ok(_.has(obj, "foo"), "has() checks that the object has a property.");
|
||||
ok(_.has(obj, "baz") == false, "has() returns false if the object doesn't have the property.");
|
||||
ok(_.has(obj, "func"), "has() works for functions too.");
|
||||
test('has', function () {
|
||||
var obj = {foo: 'bar', func: function(){}};
|
||||
ok(_.has(obj, 'foo'), 'has() checks that the object has a property.');
|
||||
ok(!_.has(obj, 'baz'), "has() returns false if the object doesn't have the property.");
|
||||
ok(_.has(obj, 'func'), 'has() works for functions too.');
|
||||
obj.hasOwnProperty = null;
|
||||
ok(_.has(obj, "foo"), "has() works even when the hasOwnProperty method is deleted.");
|
||||
ok(_.has(obj, 'foo'), 'has() works even when the hasOwnProperty method is deleted.');
|
||||
var child = {};
|
||||
child.prototype = obj;
|
||||
ok(_.has(child, "foo") == false, "has() does not check the prototype chain for a property.");
|
||||
ok(!_.has(child, 'foo'), 'has() does not check the prototype chain for a property.');
|
||||
strictEqual(_.has(null, 'foo'), false, 'has() returns false for null');
|
||||
strictEqual(_.has(undefined, 'foo'), false, 'has() returns false for undefined');
|
||||
});
|
||||
|
||||
test("matches", function() {
|
||||
test('matches', function() {
|
||||
var moe = {name: 'Moe Howard', hair: true};
|
||||
var curly = {name: 'Curly Howard', hair: false};
|
||||
var stooges = [moe, curly];
|
||||
ok(_.find(stooges, _.matches({hair: false})) === curly, "returns a predicate that can be used by finding functions.");
|
||||
ok(_.find(stooges, _.matches(moe)) === moe, "can be used to locate an object exists in a collection.");
|
||||
|
||||
equal(_.matches({hair: true})(moe), true, 'Returns a boolean');
|
||||
equal(_.matches({hair: true})(curly), false, 'Returns a boolean');
|
||||
|
||||
equal(_.matches({__x__: undefined})(5), false, 'can match undefined props on primitives');
|
||||
equal(_.matches({__x__: undefined})({__x__: undefined}), true, 'can match undefined props');
|
||||
|
||||
equal(_.matches({})(null), true, 'Empty spec called with null object returns true');
|
||||
equal(_.matches({a: 1})(null), false, 'Non-empty spec called with null object returns false');
|
||||
|
||||
ok(_.find(stooges, _.matches({hair: false})) === curly, 'returns a predicate that can be used by finding functions.');
|
||||
ok(_.find(stooges, _.matches(moe)) === moe, 'can be used to locate an object exists in a collection.');
|
||||
deepEqual(_.where([null, undefined], {a: 1}), [], 'Do not throw on null values.');
|
||||
|
||||
deepEqual(_.where([null, undefined], null), [null, undefined], 'null matches null');
|
||||
deepEqual(_.where([null, undefined], {}), [null, undefined], 'null matches {}');
|
||||
deepEqual(_.where([{b: 1}], {a: undefined}), [], 'handles undefined values (1683)');
|
||||
|
||||
_.each([true, 5, NaN, null, undefined], function(item) {
|
||||
deepEqual(_.where([{a: 1}], item), [{a: 1}], 'treats primitives as empty');
|
||||
});
|
||||
|
||||
function Prototest() {}
|
||||
Prototest.prototype.x = 1;
|
||||
var specObj = new Prototest;
|
||||
var protospec = _.matches(specObj);
|
||||
equal(protospec({x: 2}), true, 'spec is restricted to own properties');
|
||||
|
||||
specObj.y = 5;
|
||||
protospec = _.matches(specObj);
|
||||
equal(protospec({x: 1, y: 5}), true);
|
||||
equal(protospec({x: 1, y: 4}), false);
|
||||
|
||||
ok(_.matches({x: 1, y: 5})(specObj), 'inherited and own properties are checked on the test object');
|
||||
|
||||
Prototest.x = 5;
|
||||
ok(_.matches(Prototest)({x: 5, y: 1}), 'spec can be a function');
|
||||
|
||||
// #1729
|
||||
var o = {'b': 1};
|
||||
var m = _.matches(o);
|
||||
|
||||
equal(m({'b': 1}), true);
|
||||
o.b = 2;
|
||||
o.a = 1;
|
||||
equal(m({'b': 1}), true, 'changing spec object doesnt change matches result');
|
||||
|
||||
|
||||
//null edge cases
|
||||
var oCon = _.matches({'constructor': Object});
|
||||
deepEqual(_.map([null, undefined, 5, {}], oCon), [false, false, false, true], 'doesnt fasley match constructor on undefined/null');
|
||||
});
|
||||
|
||||
})();
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user