objects.js (lodash-3.0.0) | : | objects.js (lodash-4.0.0) | ||
---|---|---|---|---|
(function() { | (function() { | |||
var _ = typeof require == 'function' ? require('..') : window._; | ||||
module('Objects'); | QUnit.module('Objects'); | |||
/* global iObject, iElement, iArguments, iFunction, iArray, iString, iNumber, | ||||
iBoolean, iDate, iRegExp, iNaN, iNull, iUndefined, ActiveXObject */ | ||||
test('keys', function() { | var testElement = typeof document === 'object' ? document.createElement('div') | |||
deepEqual(_.keys({one : 1, two : 2}), ['one', 'two'], 'can extract the keys | : void 0; | |||
from an object'); | ||||
QUnit.test('keys', function(assert) { | ||||
assert.deepEqual(_.keys({one: 1, two: 2}), ['one', 'two'], 'can extract the | ||||
keys from an object'); | ||||
// the test above is not safe because it relies on for-in enumeration order | ||||
var a = []; a[1] = 0; | ||||
assert.deepEqual(_.keys(a), ['1'], 'is not fooled by sparse arrays; see issu | ||||
e #95'); | ||||
assert.deepEqual(_.keys(null), []); | ||||
assert.deepEqual(_.keys(void 0), []); | ||||
assert.deepEqual(_.keys(1), []); | ||||
assert.deepEqual(_.keys('a'), []); | ||||
assert.deepEqual(_.keys(true), []); | ||||
// keys that may be missed if the implementation isn't careful | ||||
var trouble = { | ||||
constructor: Object, | ||||
valueOf: _.noop, | ||||
hasOwnProperty: null, | ||||
toString: 5, | ||||
toLocaleString: void 0, | ||||
propertyIsEnumerable: /a/, | ||||
isPrototypeOf: this, | ||||
__defineGetter__: Boolean, | ||||
__defineSetter__: {}, | ||||
__lookupSetter__: false, | ||||
__lookupGetter__: [] | ||||
}; | ||||
var troubleKeys = ['constructor', 'valueOf', 'hasOwnProperty', 'toString', ' | ||||
toLocaleString', 'propertyIsEnumerable', | ||||
'isPrototypeOf', '__defineGetter__', '__defineSetter__', '__lo | ||||
okupSetter__', '__lookupGetter__'].sort(); | ||||
assert.deepEqual(_.keys(trouble).sort(), troubleKeys, 'matches non-enumerabl | ||||
e properties'); | ||||
}); | ||||
QUnit.test('allKeys', function(assert) { | ||||
assert.deepEqual(_.allKeys({one: 1, two: 2}), ['one', 'two'], 'can extract t | ||||
he allKeys from an object'); | ||||
// the test above is not safe because it relies on for-in enumeration order | // the test above is not safe because it relies on for-in enumeration order | |||
var a = []; a[1] = 0; | var a = []; a[1] = 0; | |||
deepEqual(_.keys(a), ['1'], 'is not fooled by sparse arrays; see issue #95') | assert.deepEqual(_.allKeys(a), ['1'], 'is not fooled by sparse arrays; see i | |||
; | ssue #95'); | |||
deepEqual(_.keys(null), []); | ||||
deepEqual(_.keys(void 0), []); | a.a = a; | |||
deepEqual(_.keys(1), []); | assert.deepEqual(_.allKeys(a), ['1', 'a'], 'is not fooled by sparse arrays w | |||
deepEqual(_.keys('a'), []); | ith additional properties'); | |||
deepEqual(_.keys(true), []); | ||||
_.each([null, void 0, 1, 'a', true, NaN, {}, [], new Number(5), new Date(0)] | ||||
, function(val) { | ||||
assert.deepEqual(_.allKeys(val), []); | ||||
}); | ||||
// allKeys that may be missed if the implementation isn't careful | ||||
var trouble = { | ||||
constructor: Object, | ||||
valueOf: _.noop, | ||||
hasOwnProperty: null, | ||||
toString: 5, | ||||
toLocaleString: void 0, | ||||
propertyIsEnumerable: /a/, | ||||
isPrototypeOf: this | ||||
}; | ||||
var troubleKeys = ['constructor', 'valueOf', 'hasOwnProperty', 'toString', ' | ||||
toLocaleString', 'propertyIsEnumerable', | ||||
'isPrototypeOf'].sort(); | ||||
assert.deepEqual(_.allKeys(trouble).sort(), troubleKeys, 'matches non-enumer | ||||
able properties'); | ||||
function A() {} | ||||
A.prototype.foo = 'foo'; | ||||
var b = new A(); | ||||
b.bar = 'bar'; | ||||
assert.deepEqual(_.allKeys(b).sort(), ['bar', 'foo'], 'should include inheri | ||||
ted keys'); | ||||
function y() {} | ||||
y.x = 'z'; | ||||
assert.deepEqual(_.allKeys(y), ['x'], 'should get keys from constructor'); | ||||
}); | }); | |||
test('values', function() { | QUnit.test('values', function(assert) { | |||
deepEqual(_.values({one: 1, two: 2}), [1, 2], 'can extract the values from a | assert.deepEqual(_.values({one: 1, two: 2}), [1, 2], 'can extract the values | |||
n object'); | from an object'); | |||
deepEqual(_.values({one: 1, two: 2, length: 3}), [1, 2, 3], '... even when o | assert.deepEqual(_.values({one: 1, two: 2, length: 3}), [1, 2, 3], '... even | |||
ne of them is "length"'); | when one of them is "length"'); | |||
}); | }); | |||
test('pairs', function() { | QUnit.test('pairs', function(assert) { | |||
deepEqual(_.pairs({one: 1, two: 2}), [['one', 1], ['two', 2]], 'can convert | assert.deepEqual(_.pairs({one: 1, two: 2}), [['one', 1], ['two', 2]], 'can c | |||
an object into pairs'); | onvert an object into pairs'); | |||
deepEqual(_.pairs({one: 1, two: 2, length: 3}), [['one', 1], ['two', 2], ['l | assert.deepEqual(_.pairs({one: 1, two: 2, length: 3}), [['one', 1], ['two', | |||
ength', 3]], '... even when one of them is "length"'); | 2], ['length', 3]], '... even when one of them is "length"'); | |||
}); | }); | |||
test('invert', function() { | QUnit.test('invert', function(assert) { | |||
var obj = {first: 'Moe', second: 'Larry', third: 'Curly'}; | var obj = {first: 'Moe', second: 'Larry', third: 'Curly'}; | |||
deepEqual(_.keys(_.invert(obj)), ['Moe', 'Larry', 'Curly'], 'can invert an o | assert.deepEqual(_.keys(_.invert(obj)), ['Moe', 'Larry', 'Curly'], 'can inve | |||
bject'); | rt an object'); | |||
deepEqual(_.invert(_.invert(obj)), obj, 'two inverts gets you back where you | assert.deepEqual(_.invert(_.invert(obj)), obj, 'two inverts gets you back wh | |||
started'); | ere you started'); | |||
obj = {length: 3}; | obj = {length: 3}; | |||
equal(_.invert(obj)['3'], 'length', 'can invert an object with "length"'); | assert.equal(_.invert(obj)['3'], 'length', 'can invert an object with "lengt h"'); | |||
}); | }); | |||
test('functions', function() { | QUnit.test('functions', function(assert) { | |||
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 | assert.deepEqual(['b', 'd'], _.functions(obj), 'can grab the function names | |||
passed-in object'); | of any passed-in object'); | |||
var Animal = function(){}; | var Animal = function(){}; | |||
Animal.prototype.run = function(){}; | Animal.prototype.run = function(){}; | |||
deepEqual(_.functions(new Animal), ['run'], 'also looks up functions on the prototype'); | assert.deepEqual(_.functions(new Animal), ['run'], 'also looks up functions on the prototype'); | |||
}); | }); | |||
test('methods', function() { | QUnit.test('methods', function(assert) { | |||
strictEqual(_.functions, _.methods, 'alias for functions'); | assert.strictEqual(_.methods, _.functions, 'is an alias for functions'); | |||
}); | }); | |||
test('extend', function() { | QUnit.test('extend', function(assert) { | |||
var result; | var result; | |||
equal(_.extend({}, {a: 'b'}).a, 'b', 'can extend an object with the attribut | assert.equal(_.extend({}, {a: 'b'}).a, 'b', 'can extend an object with the a | |||
es of another'); | ttributes of another'); | |||
equal(_.extend({a: 'x'}, {a: 'b'}).a, 'b', 'properties in source override de | assert.equal(_.extend({a: 'x'}, {a: 'b'}).a, 'b', 'properties in source over | |||
stination'); | ride destination'); | |||
equal(_.extend({x: 'x'}, {a: 'b'}).x, 'x', "properties not in source don't g | assert.equal(_.extend({x: 'x'}, {a: 'b'}).x, 'x', "properties not in source | |||
et overriden"); | don't get overriden"); | |||
result = _.extend({x: 'x'}, {a: 'a'}, {b: 'b'}); | result = _.extend({x: 'x'}, {a: 'a'}, {b: 'b'}); | |||
deepEqual(result, {x: 'x', a: 'a', b: 'b'}, 'can extend from multiple source objects'); | assert.deepEqual(result, {x: 'x', a: 'a', b: 'b'}, 'can extend from multiple source objects'); | |||
result = _.extend({x: 'x'}, {a: 'a', x: 2}, {a: 'b'}); | result = _.extend({x: 'x'}, {a: 'a', x: 2}, {a: 'b'}); | |||
deepEqual(result, {x: 2, a: 'b'}, 'extending from multiple source objects la st property trumps'); | assert.deepEqual(result, {x: 2, a: 'b'}, 'extending from multiple source obj ects last property trumps'); | |||
result = _.extend({}, {a: void 0, b: null}); | result = _.extend({}, {a: void 0, b: null}); | |||
deepEqual(_.keys(result), ['a', 'b'], 'extend copies undefined values'); | assert.deepEqual(_.keys(result), ['a', 'b'], 'extend copies undefined values '); | |||
var F = function() {}; | var F = function() {}; | |||
F.prototype = {a: 'b'}; | F.prototype = {a: 'b'}; | |||
var subObj = new F(); | var subObj = new F(); | |||
subObj.c = 'd'; | subObj.c = 'd'; | |||
deepEqual(_.extend({}, subObj), {c: 'd'}, 'extend ignores any properties but | assert.deepEqual(_.extend({}, subObj), {a: 'b', c: 'd'}, 'extend copies all | |||
own from source'); | properties from source'); | |||
_.extend(subObj, {}); | ||||
assert.ok(!subObj.hasOwnProperty('a'), "extend does not convert destination | ||||
object's 'in' properties to 'own' properties"); | ||||
try { | try { | |||
result = {}; | result = {}; | |||
_.extend(result, null, undefined, {a: 1}); | _.extend(result, null, void 0, {a: 1}); | |||
} catch(ex) {} | } catch (e) { /* ignored */ } | |||
assert.equal(result.a, 1, 'should not error on `null` or `undefined` sources | ||||
'); | ||||
assert.strictEqual(_.extend(null, {a: 1}), null, 'extending null results in | ||||
null'); | ||||
assert.strictEqual(_.extend(void 0, {a: 1}), void 0, 'extending undefined re | ||||
sults in undefined'); | ||||
}); | ||||
QUnit.test('extendOwn', function(assert) { | ||||
var result; | ||||
assert.equal(_.extendOwn({}, {a: 'b'}).a, 'b', 'can extend an object with th | ||||
e attributes of another'); | ||||
assert.equal(_.extendOwn({a: 'x'}, {a: 'b'}).a, 'b', 'properties in source o | ||||
verride destination'); | ||||
assert.equal(_.extendOwn({x: 'x'}, {a: 'b'}).x, 'x', "properties not in sour | ||||
ce don't get overriden"); | ||||
result = _.extendOwn({x: 'x'}, {a: 'a'}, {b: 'b'}); | ||||
assert.deepEqual(result, {x: 'x', a: 'a', b: 'b'}, 'can extend from multiple | ||||
source objects'); | ||||
result = _.extendOwn({x: 'x'}, {a: 'a', x: 2}, {a: 'b'}); | ||||
assert.deepEqual(result, {x: 2, a: 'b'}, 'extending from multiple source obj | ||||
ects last property trumps'); | ||||
assert.deepEqual(_.extendOwn({}, {a: void 0, b: null}), {a: void 0, b: null} | ||||
, 'copies undefined values'); | ||||
var F = function() {}; | ||||
F.prototype = {a: 'b'}; | ||||
var subObj = new F(); | ||||
subObj.c = 'd'; | ||||
assert.deepEqual(_.extendOwn({}, subObj), {c: 'd'}, 'copies own properties f | ||||
rom source'); | ||||
result = {}; | ||||
assert.deepEqual(_.extendOwn(result, null, void 0, {a: 1}), {a: 1}, 'should | ||||
not error on `null` or `undefined` sources'); | ||||
equal(result.a, 1, 'should not error on `null` or `undefined` sources'); | _.each(['a', 5, null, false], function(val) { | |||
assert.strictEqual(_.extendOwn(val, {a: 1}), val, 'extending non-objects r | ||||
esults in returning the non-object value'); | ||||
}); | ||||
assert.strictEqual(_.extendOwn(void 0, {a: 1}), void 0, 'extending undefined | ||||
results in undefined'); | ||||
result = _.extendOwn({a: 1, 0: 2, 1: '5', length: 6}, {0: 1, 1: 2, length: 2 | ||||
}); | ||||
assert.deepEqual(result, {a: 1, 0: 1, 1: 2, length: 2}, 'should treat array- | ||||
like objects like normal objects'); | ||||
}); | ||||
strictEqual(_.extend(null, {a: 1}), null, 'extending null results in null'); | QUnit.test('assign', function(assert) { | |||
strictEqual(_.extend(undefined, {a: 1}), undefined, 'extending undefined res | assert.strictEqual(_.assign, _.extendOwn, 'is an alias for extendOwn'); | |||
ults in undefined'); | ||||
}); | }); | |||
test('pick', function() { | QUnit.test('pick', function(assert) { | |||
var result; | var result; | |||
result = _.pick({a: 1, b: 2, c: 3}, 'a', 'c'); | result = _.pick({a: 1, b: 2, c: 3}, 'a', 'c'); | |||
deepEqual(result, {a: 1, c: 3}, 'can restrict properties to those named'); | assert.deepEqual(result, {a: 1, c: 3}, 'can restrict properties to those nam ed'); | |||
result = _.pick({a: 1, b: 2, c: 3}, ['b', 'c']); | result = _.pick({a: 1, b: 2, c: 3}, ['b', 'c']); | |||
deepEqual(result, {b: 2, c: 3}, 'can restrict properties to those named in a n array'); | assert.deepEqual(result, {b: 2, c: 3}, 'can restrict properties to those nam ed in an array'); | |||
result = _.pick({a: 1, b: 2, c: 3}, ['a'], 'b'); | result = _.pick({a: 1, b: 2, c: 3}, ['a'], 'b'); | |||
deepEqual(result, {a: 1, b: 2}, 'can restrict properties to those named in m ixed args'); | assert.deepEqual(result, {a: 1, b: 2}, 'can restrict properties to those nam ed in mixed args'); | |||
result = _.pick(['a', 'b'], 1); | result = _.pick(['a', 'b'], 1); | |||
deepEqual(result, {1: 'b'}, 'can pick numeric properties'); | assert.deepEqual(result, {1: 'b'}, 'can pick numeric properties'); | |||
deepEqual(_.pick(null, 'a', 'b'), {}, 'non objects return empty object'); | _.each([null, void 0], function(val) { | |||
deepEqual(_.pick(undefined, 'toString'), {}, 'null/undefined return empty ob | assert.deepEqual(_.pick(val, 'hasOwnProperty'), {}, 'Called with null/unde | |||
ject'); | fined'); | |||
deepEqual(_.pick(5, 'toString', 'b'), {toString: Number.prototype.toString}, | assert.deepEqual(_.pick(val, _.constant(true)), {}); | |||
'can iterate primitives'); | }); | |||
assert.deepEqual(_.pick(5, 'toString', 'b'), {toString: Number.prototype.toS | ||||
tring}, 'can iterate primitives'); | ||||
var data = {a: 1, b: 2, c: 3}; | var data = {a: 1, b: 2, c: 3}; | |||
var callback = function(value, key, object) { | var callback = function(value, key, object) { | |||
strictEqual(key, {1: 'a', 2: 'b', 3: 'c'}[value]); | assert.strictEqual(key, {1: 'a', 2: 'b', 3: 'c'}[value]); | |||
strictEqual(object, data); | assert.strictEqual(object, data); | |||
return value !== this.value; | return value !== this.value; | |||
}; | }; | |||
result = _.pick(data, callback, {value: 2}); | result = _.pick(data, callback, {value: 2}); | |||
deepEqual(result, {a: 1, c: 3}, 'can accept a predicate and context'); | assert.deepEqual(result, {a: 1, c: 3}, 'can accept a predicate and context') ; | |||
var Obj = function(){}; | var Obj = function(){}; | |||
Obj.prototype = {a: 1, b: 2, c: 3}; | Obj.prototype = {a: 1, b: 2, c: 3}; | |||
var instance = new Obj(); | var instance = new Obj(); | |||
deepEqual(_.pick(instance, 'a', 'c'), {a: 1, c: 3}, 'include prototype props '); | assert.deepEqual(_.pick(instance, 'a', 'c'), {a: 1, c: 3}, 'include prototyp e props'); | |||
deepEqual(_.pick(data, function(val, key) { | assert.deepEqual(_.pick(data, function(val, key) { | |||
return this[key] === 3 && this === instance; | return this[key] === 3 && this === instance; | |||
}, instance), {c: 3}, 'function is given context'); | }, instance), {c: 3}, 'function is given context'); | |||
assert.ok(!_.has(_.pick({}, 'foo'), 'foo'), 'does not set own property if pr | ||||
operty not in object'); | ||||
_.pick(data, function(value, key, obj) { | ||||
assert.equal(obj, data, 'passes same object as third parameter of iteratee | ||||
'); | ||||
}); | ||||
}); | }); | |||
test('omit', function() { | QUnit.test('omit', function(assert) { | |||
var result; | var result; | |||
result = _.omit({a: 1, b: 2, c: 3}, 'b'); | result = _.omit({a: 1, b: 2, c: 3}, 'b'); | |||
deepEqual(result, {a: 1, c: 3}, 'can omit a single named property'); | assert.deepEqual(result, {a: 1, c: 3}, 'can omit a single named property'); | |||
result = _.omit({a: 1, b: 2, c: 3}, 'a', 'c'); | result = _.omit({a: 1, b: 2, c: 3}, 'a', 'c'); | |||
deepEqual(result, {b: 2}, 'can omit several named properties'); | assert.deepEqual(result, {b: 2}, 'can omit several named properties'); | |||
result = _.omit({a: 1, b: 2, c: 3}, ['b', 'c']); | result = _.omit({a: 1, b: 2, c: 3}, ['b', 'c']); | |||
deepEqual(result, {a: 1}, 'can omit properties named in an array'); | assert.deepEqual(result, {a: 1}, 'can omit properties named in an array'); | |||
result = _.omit(['a', 'b'], 0); | result = _.omit(['a', 'b'], 0); | |||
deepEqual(result, {1: 'b'}, 'can omit numeric properties'); | assert.deepEqual(result, {1: 'b'}, 'can omit numeric properties'); | |||
deepEqual(_.omit(null, 'a', 'b'), {}, 'non objects return empty object'); | assert.deepEqual(_.omit(null, 'a', 'b'), {}, 'non objects return empty objec | |||
deepEqual(_.omit(undefined, 'toString'), {}, 'null/undefined return empty ob | t'); | |||
ject'); | assert.deepEqual(_.omit(void 0, 'toString'), {}, 'null/undefined return empt | |||
deepEqual(_.omit(5, 'toString', 'b'), {}, 'returns empty object for primitiv | y object'); | |||
es'); | assert.deepEqual(_.omit(5, 'toString', 'b'), {}, 'returns empty object for p | |||
rimitives'); | ||||
var data = {a: 1, b: 2, c: 3}; | var data = {a: 1, b: 2, c: 3}; | |||
var callback = function(value, key, object) { | var callback = function(value, key, object) { | |||
strictEqual(key, {1: 'a', 2: 'b', 3: 'c'}[value]); | assert.strictEqual(key, {1: 'a', 2: 'b', 3: 'c'}[value]); | |||
strictEqual(object, data); | assert.strictEqual(object, data); | |||
return value !== this.value; | return value !== this.value; | |||
}; | }; | |||
result = _.omit(data, callback, {value: 2}); | result = _.omit(data, callback, {value: 2}); | |||
deepEqual(result, {b: 2}, 'can accept a predicate'); | assert.deepEqual(result, {b: 2}, 'can accept a predicate'); | |||
var Obj = function(){}; | var Obj = function(){}; | |||
Obj.prototype = {a: 1, b: 2, c: 3}; | Obj.prototype = {a: 1, b: 2, c: 3}; | |||
var instance = new Obj(); | var instance = new Obj(); | |||
deepEqual(_.omit(instance, 'b'), {a: 1, c: 3}, 'include prototype props'); | assert.deepEqual(_.omit(instance, 'b'), {a: 1, c: 3}, 'include prototype pro ps'); | |||
deepEqual(_.omit(data, function(val, key) { | assert.deepEqual(_.omit(data, function(val, key) { | |||
return this[key] === 3 && this === instance; | return this[key] === 3 && this === instance; | |||
}, instance), {a: 1, b: 2}, 'function is given context'); | }, instance), {a: 1, b: 2}, 'function is given context'); | |||
}); | }); | |||
test('defaults', function() { | QUnit.test('defaults', function(assert) { | |||
var options = {zero: 0, one: 1, empty: '', nan: NaN, nothing: null}; | var options = {zero: 0, one: 1, empty: '', nan: NaN, nothing: null}; | |||
_.defaults(options, {zero: 1, one: 10, twenty: 20, nothing: 'str'}); | _.defaults(options, {zero: 1, one: 10, twenty: 20, nothing: 'str'}); | |||
equal(options.zero, 0, 'value exists'); | assert.equal(options.zero, 0, 'value exists'); | |||
equal(options.one, 1, 'value exists'); | assert.equal(options.one, 1, 'value exists'); | |||
equal(options.twenty, 20, 'default applied'); | assert.equal(options.twenty, 20, 'default applied'); | |||
equal(options.nothing, null, "null isn't overridden"); | assert.equal(options.nothing, null, "null isn't overridden"); | |||
_.defaults(options, {empty: 'full'}, {nan: 'nan'}, {word: 'word'}, {word: 'd og'}); | _.defaults(options, {empty: 'full'}, {nan: 'nan'}, {word: 'word'}, {word: 'd og'}); | |||
equal(options.empty, '', 'value exists'); | assert.equal(options.empty, '', 'value exists'); | |||
ok(_.isNaN(options.nan), "NaN isn't overridden"); | assert.ok(_.isNaN(options.nan), "NaN isn't overridden"); | |||
equal(options.word, 'word', 'new value is added, first one wins'); | assert.equal(options.word, 'word', 'new value is added, first one wins'); | |||
try { | try { | |||
options = {}; | options = {}; | |||
_.defaults(options, null, undefined, {a: 1}); | _.defaults(options, null, void 0, {a: 1}); | |||
} catch(ex) {} | } catch (e) { /* ignored */ } | |||
equal(options.a, 1, 'should not error on `null` or `undefined` sources'); | assert.equal(options.a, 1, 'should not error on `null` or `undefined` source s'); | |||
strictEqual(_.defaults(null, {a: 1}), null, 'result is null if destination i | assert.deepEqual(_.defaults(null, {a: 1}), {a: 1}, 'defaults skips nulls'); | |||
s null'); | assert.deepEqual(_.defaults(void 0, {a: 1}), {a: 1}, 'defaults skips undefin | |||
strictEqual(_.defaults(undefined, {a: 1}), undefined, 'result is undefined i | ed'); | |||
f destination is undefined'); | ||||
}); | }); | |||
test('clone', function() { | QUnit.test('clone', function(assert) { | |||
var moe = {name : 'moe', lucky : [13, 27, 34]}; | var moe = {name: 'moe', lucky: [13, 27, 34]}; | |||
var clone = _.clone(moe); | var clone = _.clone(moe); | |||
equal(clone.name, 'moe', 'the clone as the attributes of the original'); | assert.equal(clone.name, 'moe', 'the clone as the attributes of the original '); | |||
clone.name = 'curly'; | clone.name = 'curly'; | |||
ok(clone.name === 'curly' && moe.name === 'moe', 'clones can change shallow attributes without affecting the original'); | assert.ok(clone.name === 'curly' && moe.name === 'moe', 'clones can change s hallow attributes without affecting the original'); | |||
clone.lucky.push(101); | clone.lucky.push(101); | |||
equal(_.last(moe.lucky), 101, 'changes to deep attributes are shared with th | assert.equal(_.last(moe.lucky), 101, 'changes to deep attributes are shared | |||
e original'); | with the original'); | |||
assert.equal(_.clone(void 0), void 0, 'non objects should not be changed by | ||||
clone'); | ||||
assert.equal(_.clone(1), 1, 'non objects should not be changed by clone'); | ||||
assert.equal(_.clone(null), null, 'non objects should not be changed by clon | ||||
e'); | ||||
}); | ||||
QUnit.test('create', function(assert) { | ||||
var Parent = function() {}; | ||||
Parent.prototype = {foo: function() {}, bar: 2}; | ||||
_.each(['foo', null, void 0, 1], function(val) { | ||||
assert.deepEqual(_.create(val), {}, 'should return empty object when a non | ||||
-object is provided'); | ||||
}); | ||||
equal(_.clone(undefined), void 0, 'non objects should not be changed by clon | assert.ok(_.create([]) instanceof Array, 'should return new instance of arra | |||
e'); | y when array is provided'); | |||
equal(_.clone(1), 1, 'non objects should not be changed by clone'); | ||||
equal(_.clone(null), null, 'non objects should not be changed by clone'); | var Child = function() {}; | |||
Child.prototype = _.create(Parent.prototype); | ||||
assert.ok(new Child instanceof Parent, 'object should inherit prototype'); | ||||
var func = function() {}; | ||||
Child.prototype = _.create(Parent.prototype, {func: func}); | ||||
assert.strictEqual(Child.prototype.func, func, 'properties should be added t | ||||
o object'); | ||||
Child.prototype = _.create(Parent.prototype, {constructor: Child}); | ||||
assert.strictEqual(Child.prototype.constructor, Child); | ||||
Child.prototype.foo = 'foo'; | ||||
var created = _.create(Child.prototype, new Child); | ||||
assert.ok(!created.hasOwnProperty('foo'), 'should only add own properties'); | ||||
}); | }); | |||
test('isEqual', function() { | QUnit.test('isEqual', function(assert) { | |||
function First() { | function First() { | |||
this.value = 1; | this.value = 1; | |||
} | } | |||
First.prototype.value = 1; | First.prototype.value = 1; | |||
function Second() { | function Second() { | |||
this.value = 1; | this.value = 1; | |||
} | } | |||
Second.prototype.value = 2; | Second.prototype.value = 2; | |||
// Basic equality and identity comparisons. | // Basic equality and identity comparisons. | |||
ok(_.isEqual(null, null), '`null` is equal to `null`'); | assert.ok(_.isEqual(null, null), '`null` is equal to `null`'); | |||
ok(_.isEqual(), '`undefined` is equal to `undefined`'); | assert.ok(_.isEqual(), '`undefined` is equal to `undefined`'); | |||
ok(!_.isEqual(0, -0), '`0` is not equal to `-0`'); | assert.ok(!_.isEqual(0, -0), '`0` is not equal to `-0`'); | |||
ok(!_.isEqual(-0, 0), 'Commutative equality is implemented for `0` and `-0`' | assert.ok(!_.isEqual(-0, 0), 'Commutative equality is implemented for `0` an | |||
); | d `-0`'); | |||
ok(!_.isEqual(null, undefined), '`null` is not equal to `undefined`'); | assert.ok(!_.isEqual(null, void 0), '`null` is not equal to `undefined`'); | |||
ok(!_.isEqual(undefined, null), 'Commutative equality is implemented for `nu | assert.ok(!_.isEqual(void 0, null), 'Commutative equality is implemented for | |||
ll` and `undefined`'); | `null` and `undefined`'); | |||
// String object and primitive comparisons. | // String object and primitive comparisons. | |||
ok(_.isEqual('Curly', 'Curly'), 'Identical string primitives are equal'); | assert.ok(_.isEqual('Curly', 'Curly'), 'Identical string primitives are equa | |||
ok(_.isEqual(new String('Curly'), new String('Curly')), 'String objects with | l'); | |||
identical primitive values are equal'); | assert.ok(_.isEqual(new String('Curly'), new String('Curly')), 'String objec | |||
ok(_.isEqual(new String('Curly'), 'Curly'), 'String primitives and their cor | ts with identical primitive values are equal'); | |||
responding object wrappers are equal'); | assert.ok(_.isEqual(new String('Curly'), 'Curly'), 'String primitives and th | |||
ok(_.isEqual('Curly', new String('Curly')), 'Commutative equality is impleme | eir corresponding object wrappers are equal'); | |||
nted for string objects and primitives'); | assert.ok(_.isEqual('Curly', new String('Curly')), 'Commutative equality is | |||
implemented for string objects and primitives'); | ||||
ok(!_.isEqual('Curly', 'Larry'), 'String primitives with different values ar | ||||
e not equal'); | assert.ok(!_.isEqual('Curly', 'Larry'), 'String primitives with different va | |||
ok(!_.isEqual(new String('Curly'), new String('Larry')), 'String objects wit | lues are not equal'); | |||
h different primitive values are not equal'); | assert.ok(!_.isEqual(new String('Curly'), new String('Larry')), 'String obje | |||
ok(!_.isEqual(new String('Curly'), {toString: function(){ return 'Curly'; }} | cts with different primitive values are not equal'); | |||
), 'String objects and objects with a custom `toString` method are not equal'); | assert.ok(!_.isEqual(new String('Curly'), {toString: function(){ return 'Cur | |||
ly'; }}), 'String objects and objects with a custom `toString` method are not eq | ||||
ual'); | ||||
// Number object and primitive comparisons. | // Number object and primitive comparisons. | |||
ok(_.isEqual(75, 75), 'Identical number primitives are equal'); | assert.ok(_.isEqual(75, 75), 'Identical number primitives are equal'); | |||
ok(_.isEqual(new Number(75), new Number(75)), 'Number objects with identical | assert.ok(_.isEqual(new Number(75), new Number(75)), 'Number objects with id | |||
primitive values are equal'); | entical primitive values are equal'); | |||
ok(_.isEqual(75, new Number(75)), 'Number primitives and their corresponding | assert.ok(_.isEqual(75, new Number(75)), 'Number primitives and their corres | |||
object wrappers are equal'); | ponding object wrappers are equal'); | |||
ok(_.isEqual(new Number(75), 75), 'Commutative equality is implemented for n | assert.ok(_.isEqual(new Number(75), 75), 'Commutative equality is implemente | |||
umber objects and primitives'); | d for number objects and primitives'); | |||
ok(!_.isEqual(new Number(0), -0), '`new Number(0)` and `-0` are not equal'); | assert.ok(!_.isEqual(new Number(0), -0), '`new Number(0)` and `-0` are not e | |||
ok(!_.isEqual(0, new Number(-0)), 'Commutative equality is implemented for ` | qual'); | |||
new Number(0)` and `-0`'); | assert.ok(!_.isEqual(0, new Number(-0)), 'Commutative equality is implemente | |||
d for `new Number(0)` and `-0`'); | ||||
ok(!_.isEqual(new Number(75), new Number(63)), 'Number objects with differen | assert.ok(!_.isEqual(new Number(75), new Number(63)), 'Number objects with d | |||
t primitive values are not equal'); | ifferent primitive values are not equal'); | |||
ok(!_.isEqual(new Number(63), {valueOf: function(){ return 63; }}), 'Number | assert.ok(!_.isEqual(new Number(63), {valueOf: function(){ return 63; }}), ' | |||
objects and objects with a `valueOf` method are not equal'); | Number objects and objects with a `valueOf` method are not equal'); | |||
// Comparisons involving `NaN`. | // Comparisons involving `NaN`. | |||
ok(_.isEqual(NaN, NaN), '`NaN` is equal to `NaN`'); | assert.ok(_.isEqual(NaN, NaN), '`NaN` is equal to `NaN`'); | |||
ok(_.isEqual(new Object(NaN), NaN), 'Object(`NaN`) is equal to `NaN`'); | assert.ok(_.isEqual(new Number(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`') | assert.ok(!_.isEqual(61, NaN), 'A number primitive is not equal to `NaN`'); | |||
; | assert.ok(!_.isEqual(new Number(79), NaN), 'A number object is not equal to | |||
ok(!_.isEqual(Infinity, NaN), '`Infinity` is not equal to `NaN`'); | `NaN`'); | |||
assert.ok(!_.isEqual(Infinity, NaN), '`Infinity` is not equal to `NaN`'); | ||||
// Boolean object and primitive comparisons. | // Boolean object and primitive comparisons. | |||
ok(_.isEqual(true, true), 'Identical boolean primitives are equal'); | assert.ok(_.isEqual(true, true), 'Identical boolean primitives are equal'); | |||
ok(_.isEqual(new Boolean, new Boolean), 'Boolean objects with identical prim | assert.ok(_.isEqual(new Boolean, new Boolean), 'Boolean objects with identic | |||
itive values are equal'); | al primitive values are equal'); | |||
ok(_.isEqual(true, new Boolean(true)), 'Boolean primitives and their corresp | assert.ok(_.isEqual(true, new Boolean(true)), 'Boolean primitives and their | |||
onding object wrappers are equal'); | corresponding object wrappers are equal'); | |||
ok(_.isEqual(new Boolean(true), true), 'Commutative equality is implemented | assert.ok(_.isEqual(new Boolean(true), true), 'Commutative equality is imple | |||
for booleans'); | mented for booleans'); | |||
ok(!_.isEqual(new Boolean(true), new Boolean), 'Boolean objects with differe | assert.ok(!_.isEqual(new Boolean(true), new Boolean), 'Boolean objects with | |||
nt primitive values are not equal'); | different primitive values are not equal'); | |||
// Common type coercions. | // Common type coercions. | |||
ok(!_.isEqual(new Boolean(false), true), '`new Boolean(false)` is not equal | assert.ok(!_.isEqual(new Boolean(false), true), '`new Boolean(false)` is not | |||
to `true`'); | equal to `true`'); | |||
ok(!_.isEqual('75', 75), 'String and number primitives with like values are | assert.ok(!_.isEqual('75', 75), 'String and number primitives with like valu | |||
not equal'); | es are not equal'); | |||
ok(!_.isEqual(new Number(63), new String(63)), 'String and number objects wi | assert.ok(!_.isEqual(new Number(63), new String(63)), 'String and number obj | |||
th like values are not equal'); | ects with like values are not equal'); | |||
ok(!_.isEqual(75, '75'), 'Commutative equality is implemented for like strin | assert.ok(!_.isEqual(75, '75'), 'Commutative equality is implemented for lik | |||
g and number values'); | e string and number values'); | |||
ok(!_.isEqual(0, ''), 'Number and string primitives with like values are not | assert.ok(!_.isEqual(0, ''), 'Number and string primitives with like values | |||
equal'); | are not equal'); | |||
ok(!_.isEqual(1, true), 'Number and boolean primitives with like values are | assert.ok(!_.isEqual(1, true), 'Number and boolean primitives with like valu | |||
not equal'); | es are not equal'); | |||
ok(!_.isEqual(new Boolean(false), new Number(0)), 'Boolean and number object | assert.ok(!_.isEqual(new Boolean(false), new Number(0)), 'Boolean and number | |||
s with like values are not equal'); | objects with like values are not equal'); | |||
ok(!_.isEqual(false, new String('')), 'Boolean primitives and string objects | assert.ok(!_.isEqual(false, new String('')), 'Boolean primitives and string | |||
with like values are not equal'); | objects with like values are not equal'); | |||
ok(!_.isEqual(12564504e5, new Date(2009, 9, 25)), 'Dates and their correspon | assert.ok(!_.isEqual(12564504e5, new Date(2009, 9, 25)), 'Dates and their co | |||
ding numeric primitive values are not equal'); | rresponding numeric primitive values are not equal'); | |||
// Dates. | // Dates. | |||
ok(_.isEqual(new Date(2009, 9, 25), new Date(2009, 9, 25)), 'Date objects re | assert.ok(_.isEqual(new Date(2009, 9, 25), new Date(2009, 9, 25)), 'Date obj | |||
ferencing identical times are equal'); | ects referencing identical times are equal'); | |||
ok(!_.isEqual(new Date(2009, 9, 25), new Date(2009, 11, 13)), 'Date objects | assert.ok(!_.isEqual(new Date(2009, 9, 25), new Date(2009, 11, 13)), 'Date o | |||
referencing different times are not equal'); | bjects referencing different times are not equal'); | |||
ok(!_.isEqual(new Date(2009, 11, 13), { | assert.ok(!_.isEqual(new Date(2009, 11, 13), { | |||
getTime: function(){ | getTime: function(){ | |||
return 12606876e5; | return 12606876e5; | |||
} | } | |||
}), 'Date objects and objects with a `getTime` method are not equal'); | }), 'Date objects and objects with a `getTime` method are not equal'); | |||
ok(!_.isEqual(new Date('Curly'), new Date('Curly')), 'Invalid dates are not equal'); | assert.ok(!_.isEqual(new Date('Curly'), new Date('Curly')), 'Invalid dates a re not equal'); | |||
// Functions. | // Functions. | |||
ok(!_.isEqual(First, Second), 'Different functions with identical bodies and source code representations are not equal'); | assert.ok(!_.isEqual(First, Second), 'Different functions with identical bod ies and source code representations are not equal'); | |||
// RegExps. | // RegExps. | |||
ok(_.isEqual(/(?:)/gim, /(?:)/gim), 'RegExps with equivalent patterns and fl | assert.ok(_.isEqual(/(?:)/gim, /(?:)/gim), 'RegExps with equivalent patterns | |||
ags are equal'); | and flags are equal'); | |||
ok(_.isEqual(/(?:)/gi, /(?:)/ig), 'Flag order is not significant'); | assert.ok(_.isEqual(/(?:)/gi, /(?:)/ig), 'Flag order is not significant'); | |||
ok(!_.isEqual(/(?:)/g, /(?:)/gi), 'RegExps with equivalent patterns and diff | assert.ok(!_.isEqual(/(?:)/g, /(?:)/gi), 'RegExps with equivalent patterns a | |||
erent flags are not equal'); | nd different flags are not equal'); | |||
ok(!_.isEqual(/Moe/gim, /Curly/gim), 'RegExps with different patterns and eq | assert.ok(!_.isEqual(/Moe/gim, /Curly/gim), 'RegExps with different patterns | |||
uivalent flags are not equal'); | and equivalent flags are not equal'); | |||
ok(!_.isEqual(/(?:)/gi, /(?:)/g), 'Commutative equality is implemented for R | assert.ok(!_.isEqual(/(?:)/gi, /(?:)/g), 'Commutative equality is implemente | |||
egExps'); | d for RegExps'); | |||
ok(!_.isEqual(/Curly/g, {source: 'Larry', global: true, ignoreCase: false, m | assert.ok(!_.isEqual(/Curly/g, {source: 'Larry', global: true, ignoreCase: f | |||
ultiline: false}), 'RegExps and RegExp-like objects are not equal'); | alse, multiline: false}), 'RegExps and RegExp-like objects are not equal'); | |||
// Empty arrays, array-like objects, and object literals. | // Empty arrays, array-like objects, and object literals. | |||
ok(_.isEqual({}, {}), 'Empty object literals are equal'); | assert.ok(_.isEqual({}, {}), 'Empty object literals are equal'); | |||
ok(_.isEqual([], []), 'Empty array literals are equal'); | assert.ok(_.isEqual([], []), 'Empty array literals are equal'); | |||
ok(_.isEqual([{}], [{}]), 'Empty nested arrays and objects are equal'); | assert.ok(_.isEqual([{}], [{}]), 'Empty nested arrays and objects are equal' | |||
ok(!_.isEqual({length: 0}, []), 'Array-like objects and arrays are not equal | ); | |||
.'); | assert.ok(!_.isEqual({length: 0}, []), 'Array-like objects and arrays are no | |||
ok(!_.isEqual([], {length: 0}), 'Commutative equality is implemented for arr | t equal.'); | |||
ay-like objects'); | assert.ok(!_.isEqual([], {length: 0}), 'Commutative equality is implemented | |||
for array-like objects'); | ||||
ok(!_.isEqual({}, []), 'Object literals and array literals are not equal'); | assert.ok(!_.isEqual({}, []), 'Object literals and array literals are not eq | |||
ok(!_.isEqual([], {}), 'Commutative equality is implemented for objects and | ual'); | |||
arrays'); | assert.ok(!_.isEqual([], {}), 'Commutative equality is implemented for objec | |||
ts and arrays'); | ||||
// Arrays with primitive and object values. | // Arrays with primitive and object values. | |||
ok(_.isEqual([1, 'Larry', true], [1, 'Larry', true]), 'Arrays containing ide | assert.ok(_.isEqual([1, 'Larry', true], [1, 'Larry', true]), 'Arrays contain | |||
ntical primitives are equal'); | ing identical primitives are equal'); | |||
ok(_.isEqual([/Moe/g, new Date(2009, 9, 25)], [/Moe/g, new Date(2009, 9, 25) | assert.ok(_.isEqual([/Moe/g, new Date(2009, 9, 25)], [/Moe/g, new Date(2009, | |||
]), 'Arrays containing equivalent elements are equal'); | 9, 25)]), 'Arrays containing equivalent elements are equal'); | |||
// Multi-dimensional arrays. | // Multi-dimensional arrays. | |||
var a = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['ru nning', 'biking', new String('programming')], {a: 47}]; | var a = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['ru nning', 'biking', new String('programming')], {a: 47}]; | |||
var b = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['ru nning', 'biking', new String('programming')], {a: 47}]; | var b = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['ru nning', 'biking', new String('programming')], {a: 47}]; | |||
ok(_.isEqual(a, b), 'Arrays containing nested arrays and objects are recursi vely compared'); | assert.ok(_.isEqual(a, b), 'Arrays containing nested arrays and objects are recursively compared'); | |||
// Overwrite the methods defined in ES 5.1 section 15.4.4. | // Overwrite the methods defined in ES 5.1 section 15.4.4. | |||
a.forEach = a.map = a.filter = a.every = a.indexOf = a.lastIndexOf = a.some = a.reduce = a.reduceRight = null; | a.forEach = a.map = a.filter = a.every = a.indexOf = a.lastIndexOf = a.some = a.reduce = a.reduceRight = null; | |||
b.join = b.pop = b.reverse = b.shift = b.slice = b.splice = b.concat = b.sor t = b.unshift = null; | b.join = b.pop = b.reverse = b.shift = b.slice = b.splice = b.concat = b.sor t = b.unshift = null; | |||
// Array elements and properties. | // Array elements and properties. | |||
ok(_.isEqual(a, b), 'Arrays containing equivalent elements and different non -numeric properties are equal'); | assert.ok(_.isEqual(a, b), 'Arrays containing equivalent elements and differ ent non-numeric properties are equal'); | |||
a.push('White Rocks'); | a.push('White Rocks'); | |||
ok(!_.isEqual(a, b), 'Arrays of different lengths are not equal'); | assert.ok(!_.isEqual(a, b), 'Arrays of different lengths are not equal'); | |||
a.push('East Boulder'); | a.push('East Boulder'); | |||
b.push('Gunbarrel Ranch', 'Teller Farm'); | b.push('Gunbarrel Ranch', 'Teller Farm'); | |||
ok(!_.isEqual(a, b), 'Arrays of identical lengths containing different eleme nts are not equal'); | assert.ok(!_.isEqual(a, b), 'Arrays of identical lengths containing differen t elements are not equal'); | |||
// Sparse arrays. | // Sparse arrays. | |||
ok(_.isEqual(Array(3), Array(3)), 'Sparse arrays of identical lengths are eq | assert.ok(_.isEqual(Array(3), Array(3)), 'Sparse arrays of identical lengths | |||
ual'); | are equal'); | |||
ok(!_.isEqual(Array(3), Array(6)), 'Sparse arrays of different lengths are n | assert.ok(!_.isEqual(Array(3), Array(6)), 'Sparse arrays of different length | |||
ot equal when both are empty'); | s are not equal when both are empty'); | |||
var sparse = []; | var sparse = []; | |||
sparse[1] = 5; | sparse[1] = 5; | |||
ok(_.isEqual(sparse, [undefined, 5]), 'Handles sparse arrays as dense'); | assert.ok(_.isEqual(sparse, [void 0, 5]), 'Handles sparse arrays as dense'); | |||
// Simple objects. | // Simple objects. | |||
ok(_.isEqual({a: 'Curly', b: 1, c: true}, {a: 'Curly', b: 1, c: true}), 'Obj | assert.ok(_.isEqual({a: 'Curly', b: 1, c: true}, {a: 'Curly', b: 1, c: true} | |||
ects containing identical primitives are equal'); | ), 'Objects containing identical primitives are equal'); | |||
ok(_.isEqual({a: /Curly/g, b: new Date(2009, 11, 13)}, {a: /Curly/g, b: new | assert.ok(_.isEqual({a: /Curly/g, b: new Date(2009, 11, 13)}, {a: /Curly/g, | |||
Date(2009, 11, 13)}), 'Objects containing equivalent members are equal'); | b: new Date(2009, 11, 13)}), 'Objects containing equivalent members are equal'); | |||
ok(!_.isEqual({a: 63, b: 75}, {a: 61, b: 55}), 'Objects of identical sizes w | assert.ok(!_.isEqual({a: 63, b: 75}, {a: 61, b: 55}), 'Objects of identical | |||
ith different values are not equal'); | sizes with different values are not equal'); | |||
ok(!_.isEqual({a: 63, b: 75}, {a: 61, c: 55}), 'Objects of identical sizes w | assert.ok(!_.isEqual({a: 63, b: 75}, {a: 61, c: 55}), 'Objects of identical | |||
ith different property names are not equal'); | sizes with different property names are not equal'); | |||
ok(!_.isEqual({a: 1, b: 2}, {a: 1}), 'Objects of different sizes are not equ | assert.ok(!_.isEqual({a: 1, b: 2}, {a: 1}), 'Objects of different sizes are | |||
al'); | not equal'); | |||
ok(!_.isEqual({a: 1}, {a: 1, b: 2}), 'Commutative equality is implemented fo | assert.ok(!_.isEqual({a: 1}, {a: 1, b: 2}), 'Commutative equality is impleme | |||
r objects'); | nted for objects'); | |||
ok(!_.isEqual({x: 1, y: undefined}, {x: 1, z: 2}), 'Objects with identical k | assert.ok(!_.isEqual({x: 1, y: void 0}, {x: 1, z: 2}), 'Objects with identic | |||
eys and different values are not equivalent'); | al keys and different values are not equivalent'); | |||
// `A` contains nested objects and arrays. | // `A` contains nested objects and arrays. | |||
a = { | a = { | |||
name: new String('Moe Howard'), | name: new String('Moe Howard'), | |||
age: new Number(77), | age: new Number(77), | |||
stooge: true, | stooge: true, | |||
hobbies: ['acting'], | hobbies: ['acting'], | |||
film: { | film: { | |||
name: 'Sing a Song of Six Pants', | name: 'Sing a Song of Six Pants', | |||
release: new Date(1947, 9, 30), | release: new Date(1947, 9, 30), | |||
skipping to change at line 351 | skipping to change at line 478 | |||
stooge: true, | stooge: true, | |||
hobbies: ['acting'], | hobbies: ['acting'], | |||
film: { | film: { | |||
name: 'Sing a Song of Six Pants', | name: 'Sing a Song of Six Pants', | |||
release: new Date(1947, 9, 30), | release: new Date(1947, 9, 30), | |||
stars: [new String('Larry Fine'), 'Shemp Howard'], | stars: [new String('Larry Fine'), 'Shemp Howard'], | |||
minutes: new Number(16), | minutes: new Number(16), | |||
seconds: 54 | seconds: 54 | |||
} | } | |||
}; | }; | |||
ok(_.isEqual(a, b), 'Objects with nested equivalent members are recursively compared'); | assert.ok(_.isEqual(a, b), 'Objects with nested equivalent members are recur sively compared'); | |||
// Instances. | // Instances. | |||
ok(_.isEqual(new First, new First), 'Object instances are equal'); | assert.ok(_.isEqual(new First, new First), 'Object instances are equal'); | |||
ok(!_.isEqual(new First, new Second), 'Objects with different constructors a | assert.ok(!_.isEqual(new First, new Second), 'Objects with different constru | |||
nd identical own properties are not equal'); | ctors and identical own properties are not equal'); | |||
ok(!_.isEqual({value: 1}, new First), 'Object instances and objects sharing | assert.ok(!_.isEqual({value: 1}, new First), 'Object instances and objects s | |||
equivalent properties are not equal'); | haring equivalent properties are not equal'); | |||
ok(!_.isEqual({value: 2}, new Second), 'The prototype chain of objects shoul | assert.ok(!_.isEqual({value: 2}, new Second), 'The prototype chain of object | |||
d not be examined'); | s should not be examined'); | |||
// Circular Arrays. | // Circular Arrays. | |||
(a = []).push(a); | (a = []).push(a); | |||
(b = []).push(b); | (b = []).push(b); | |||
ok(_.isEqual(a, b), 'Arrays containing circular references are equal'); | assert.ok(_.isEqual(a, b), 'Arrays containing circular references are equal' ); | |||
a.push(new String('Larry')); | a.push(new String('Larry')); | |||
b.push(new String('Larry')); | b.push(new String('Larry')); | |||
ok(_.isEqual(a, b), 'Arrays containing circular references and equivalent pr operties are equal'); | assert.ok(_.isEqual(a, b), 'Arrays containing circular references and equiva lent properties are equal'); | |||
a.push('Shemp'); | a.push('Shemp'); | |||
b.push('Curly'); | b.push('Curly'); | |||
ok(!_.isEqual(a, b), 'Arrays containing circular references and different pr operties are not equal'); | assert.ok(!_.isEqual(a, b), 'Arrays containing circular references and diffe rent properties are not equal'); | |||
// More circular arrays #767. | // More circular arrays #767. | |||
a = ['everything is checked but', 'this', 'is not']; | a = ['everything is checked but', 'this', 'is not']; | |||
a[1] = a; | a[1] = a; | |||
b = ['everything is checked but', ['this', 'array'], 'is not']; | b = ['everything is checked but', ['this', 'array'], 'is not']; | |||
ok(!_.isEqual(a, b), 'Comparison of circular references with non-circular re ferences are not equal'); | assert.ok(!_.isEqual(a, b), 'Comparison of circular references with non-circ ular references are not equal'); | |||
// Circular Objects. | // Circular Objects. | |||
a = {abc: null}; | a = {abc: null}; | |||
b = {abc: null}; | b = {abc: null}; | |||
a.abc = a; | a.abc = a; | |||
b.abc = b; | b.abc = b; | |||
ok(_.isEqual(a, b), 'Objects containing circular references are equal'); | assert.ok(_.isEqual(a, b), 'Objects containing circular references are equal '); | |||
a.def = 75; | a.def = 75; | |||
b.def = 75; | b.def = 75; | |||
ok(_.isEqual(a, b), 'Objects containing circular references and equivalent p roperties are equal'); | assert.ok(_.isEqual(a, b), 'Objects containing circular references and equiv alent properties are equal'); | |||
a.def = new Number(75); | a.def = new Number(75); | |||
b.def = new Number(63); | b.def = new Number(63); | |||
ok(!_.isEqual(a, b), 'Objects containing circular references and different p roperties are not equal'); | assert.ok(!_.isEqual(a, b), 'Objects containing circular references and diff erent properties are not equal'); | |||
// More circular objects #767. | // More circular objects #767. | |||
a = {everything: 'is checked', but: 'this', is: 'not'}; | a = {everything: 'is checked', but: 'this', is: 'not'}; | |||
a.but = a; | a.but = a; | |||
b = {everything: 'is checked', but: {that: 'object'}, is: 'not'}; | b = {everything: 'is checked', but: {that: 'object'}, is: 'not'}; | |||
ok(!_.isEqual(a, b), 'Comparison of circular references with non-circular ob ject references are not equal'); | assert.ok(!_.isEqual(a, b), 'Comparison of circular references with non-circ ular object references are not equal'); | |||
// Cyclic Structures. | // Cyclic Structures. | |||
a = [{abc: null}]; | a = [{abc: null}]; | |||
b = [{abc: null}]; | b = [{abc: null}]; | |||
(a[0].abc = a).push(a); | (a[0].abc = a).push(a); | |||
(b[0].abc = b).push(b); | (b[0].abc = b).push(b); | |||
ok(_.isEqual(a, b), 'Cyclic structures are equal'); | assert.ok(_.isEqual(a, b), 'Cyclic structures are equal'); | |||
a[0].def = 'Larry'; | a[0].def = 'Larry'; | |||
b[0].def = 'Larry'; | b[0].def = 'Larry'; | |||
ok(_.isEqual(a, b), 'Cyclic structures containing equivalent properties are equal'); | assert.ok(_.isEqual(a, b), 'Cyclic structures containing equivalent properti es are equal'); | |||
a[0].def = new String('Larry'); | a[0].def = new String('Larry'); | |||
b[0].def = new String('Curly'); | b[0].def = new String('Curly'); | |||
ok(!_.isEqual(a, b), 'Cyclic structures containing different properties are not equal'); | assert.ok(!_.isEqual(a, b), 'Cyclic structures containing different properti es are not equal'); | |||
// Complex Circular References. | // Complex Circular References. | |||
a = {foo: {b: {foo: {c: {foo: null}}}}}; | a = {foo: {b: {foo: {c: {foo: null}}}}}; | |||
b = {foo: {b: {foo: {c: {foo: null}}}}}; | b = {foo: {b: {foo: {c: {foo: null}}}}}; | |||
a.foo.b.foo.c.foo = a; | a.foo.b.foo.c.foo = a; | |||
b.foo.b.foo.c.foo = b; | b.foo.b.foo.c.foo = b; | |||
ok(_.isEqual(a, b), 'Cyclic structures with nested and identically-named pro perties are equal'); | assert.ok(_.isEqual(a, b), 'Cyclic structures with nested and identically-na med properties are equal'); | |||
// Chaining. | // Chaining. | |||
ok(!_.isEqual(_({x: 1, y: undefined}).chain(), _({x: 1, z: 2}).chain()), 'Ch ained objects containing different values are not equal'); | assert.ok(!_.isEqual(_({x: 1, y: void 0}).chain(), _({x: 1, z: 2}).chain()), 'Chained objects containing different values are not equal'); | |||
a = _({x: 1, y: 2}).chain(); | a = _({x: 1, y: 2}).chain(); | |||
b = _({x: 1, y: 2}).chain(); | b = _({x: 1, y: 2}).chain(); | |||
equal(_.isEqual(a.isEqual(b), _(true)), true, '`isEqual` can be chained'); | assert.equal(_.isEqual(a.isEqual(b), _(true)), true, '`isEqual` can be chain | |||
ed'); | ||||
// Objects from another frame. | ||||
ok(_.isEqual({}, iObject)); | ||||
// Objects without a `constructor` property | // Objects without a `constructor` property | |||
if (Object.create) { | if (Object.create) { | |||
a = Object.create(null, {x: {value: 1, enumerable: true}}); | a = Object.create(null, {x: {value: 1, enumerable: true}}); | |||
b = {x: 1}; | b = {x: 1}; | |||
ok(_.isEqual(a, b), 'Handles objects without a constructor (e.g. from Ob | assert.ok(_.isEqual(a, b), 'Handles objects without a constructor (e.g. fr | |||
ject.create'); | om Object.create'); | |||
} | } | |||
function Foo() { this.a = 1; } | function Foo() { this.a = 1; } | |||
Foo.prototype.constructor = null; | Foo.prototype.constructor = null; | |||
var other = {a: 1}; | var other = {a: 1}; | |||
strictEqual(_.isEqual(new Foo, other), false, 'Objects from different constr | assert.strictEqual(_.isEqual(new Foo, other), false, 'Objects from different | |||
uctors are not equal'); | constructors are not equal'); | |||
}); | ||||
test('isEmpty', function() { | // Tricky object cases val comparisions | |||
ok(!_([1]).isEmpty(), '[1] is not empty'); | assert.equal(_.isEqual([0], [-0]), false); | |||
ok(_.isEmpty([]), '[] is empty'); | assert.equal(_.isEqual({a: 0}, {a: -0}), false); | |||
ok(!_.isEmpty({one : 1}), '{one : 1} is not empty'); | assert.equal(_.isEqual([NaN], [NaN]), true); | |||
ok(_.isEmpty({}), '{} is empty'); | assert.equal(_.isEqual({a: NaN}, {a: NaN}), true); | |||
ok(_.isEmpty(new RegExp('')), 'objects with prototype properties are empty') | }); | |||
; | ||||
ok(_.isEmpty(null), 'null is empty'); | QUnit.test('isEmpty', function(assert) { | |||
ok(_.isEmpty(), 'undefined is empty'); | assert.ok(!_([1]).isEmpty(), '[1] is not empty'); | |||
ok(_.isEmpty(''), 'the empty string is empty'); | assert.ok(_.isEmpty([]), '[] is empty'); | |||
ok(!_.isEmpty('moe'), 'but other strings are not'); | assert.ok(!_.isEmpty({one: 1}), '{one: 1} is not empty'); | |||
assert.ok(_.isEmpty({}), '{} is empty'); | ||||
assert.ok(_.isEmpty(new RegExp('')), 'objects with prototype properties are | ||||
empty'); | ||||
assert.ok(_.isEmpty(null), 'null is empty'); | ||||
assert.ok(_.isEmpty(), 'undefined is empty'); | ||||
assert.ok(_.isEmpty(''), 'the empty string is empty'); | ||||
assert.ok(!_.isEmpty('moe'), 'but other strings are not'); | ||||
var obj = {one : 1}; | var obj = {one: 1}; | |||
delete obj.one; | delete obj.one; | |||
ok(_.isEmpty(obj), 'deleting all the keys from an object empties it'); | assert.ok(_.isEmpty(obj), 'deleting all the keys from an object empties it') ; | |||
var args = function(){ return arguments; }; | var args = function(){ return arguments; }; | |||
ok(_.isEmpty(args()), 'empty arguments object is empty'); | assert.ok(_.isEmpty(args()), 'empty arguments object is empty'); | |||
ok(!_.isEmpty(args('')), 'non-empty arguments object is not empty'); | assert.ok(!_.isEmpty(args('')), 'non-empty arguments object is not empty'); | |||
}); | ||||
// Setup remote variables for iFrame tests. | // covers collecting non-enumerable properties in IE < 9 | |||
var iframe = document.createElement('iframe'); | var nonEnumProp = {toString: 5}; | |||
iframe.frameBorder = iframe.height = iframe.width = 0; | assert.ok(!_.isEmpty(nonEnumProp), 'non-enumerable property is not empty'); | |||
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>' | ||||
); | ||||
iDoc.close(); | ||||
test('isElement', function() { | ||||
ok(!_.isElement('div'), 'strings are not dom elements'); | ||||
ok(_.isElement(document.body), 'the body tag is a DOM element'); | ||||
ok(_.isElement(iElement), 'even from another frame'); | ||||
}); | }); | |||
test('isArguments', function() { | if (typeof document === 'object') { | |||
QUnit.test('isElement', function(assert) { | ||||
assert.ok(!_.isElement('div'), 'strings are not dom elements'); | ||||
assert.ok(_.isElement(testElement), 'an element is a DOM element'); | ||||
}); | ||||
} | ||||
QUnit.test('isArguments', function(assert) { | ||||
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'); | assert.ok(!_.isArguments('string'), 'a string is not an arguments object'); | |||
ok(!_.isArguments(_.isArguments), 'a function is not an arguments object'); | assert.ok(!_.isArguments(_.isArguments), 'a function is not an arguments obj | |||
ok(_.isArguments(args), 'but the arguments object is an arguments object'); | ect'); | |||
ok(!_.isArguments(_.toArray(args)), 'but not when it\'s converted into an ar | assert.ok(_.isArguments(args), 'but the arguments object is an arguments obj | |||
ray'); | ect'); | |||
ok(!_.isArguments([1, 2, 3]), 'and not vanilla arrays.'); | assert.ok(!_.isArguments(_.toArray(args)), 'but not when it\'s converted int | |||
ok(_.isArguments(iArguments), 'even from another frame'); | o an array'); | |||
}); | assert.ok(!_.isArguments([1, 2, 3]), 'and not vanilla arrays.'); | |||
test('isObject', function() { | ||||
ok(_.isObject(arguments), 'the arguments object is object'); | ||||
ok(_.isObject([1, 2, 3]), 'and arrays'); | ||||
ok(_.isObject(document.body), 'and DOM element'); | ||||
ok(_.isObject(iElement), 'even from another frame'); | ||||
ok(_.isObject(function () {}), 'and functions'); | ||||
ok(_.isObject(iFunction), 'even from another frame'); | ||||
ok(!_.isObject(null), 'but not null'); | ||||
ok(!_.isObject(undefined), 'and not undefined'); | ||||
ok(!_.isObject('string'), 'and not string'); | ||||
ok(!_.isObject(12), 'and not number'); | ||||
ok(!_.isObject(true), 'and not boolean'); | ||||
ok(_.isObject(new String('string')), 'but new String()'); | ||||
}); | ||||
test('isArray', function() { | ||||
ok(!_.isArray(undefined), 'undefined vars are not arrays'); | ||||
ok(!_.isArray(arguments), 'the arguments object is not an array'); | ||||
ok(_.isArray([1, 2, 3]), 'but arrays are'); | ||||
ok(_.isArray(iArray), 'even from another frame'); | ||||
}); | }); | |||
test('isString', function() { | QUnit.test('isObject', function(assert) { | |||
assert.ok(_.isObject(arguments), 'the arguments object is object'); | ||||
assert.ok(_.isObject([1, 2, 3]), 'and arrays'); | ||||
if (testElement) { | ||||
assert.ok(_.isObject(testElement), 'and DOM element'); | ||||
} | ||||
assert.ok(_.isObject(function() {}), 'and functions'); | ||||
assert.ok(!_.isObject(null), 'but not null'); | ||||
assert.ok(!_.isObject(void 0), 'and not undefined'); | ||||
assert.ok(!_.isObject('string'), 'and not string'); | ||||
assert.ok(!_.isObject(12), 'and not number'); | ||||
assert.ok(!_.isObject(true), 'and not boolean'); | ||||
assert.ok(_.isObject(new String('string')), 'but new String()'); | ||||
}); | ||||
QUnit.test('isArray', function(assert) { | ||||
assert.ok(!_.isArray(void 0), 'undefined vars are not arrays'); | ||||
assert.ok(!_.isArray(arguments), 'the arguments object is not an array'); | ||||
assert.ok(_.isArray([1, 2, 3]), 'but arrays are'); | ||||
}); | ||||
QUnit.test('isString', function(assert) { | ||||
var obj = new String('I am a string object'); | var obj = new String('I am a string object'); | |||
ok(!_.isString(document.body), 'the document body is not a string'); | if (testElement) { | |||
ok(_.isString([1, 2, 3].join(', ')), 'but strings are'); | assert.ok(!_.isString(testElement), 'an element is not a string'); | |||
ok(_.isString(iString), 'even from another frame'); | } | |||
ok(_.isString('I am a string literal'), 'string literals are'); | assert.ok(_.isString([1, 2, 3].join(', ')), 'but strings are'); | |||
ok(_.isString(obj), 'so are String objects'); | assert.strictEqual(_.isString('I am a string literal'), true, 'string litera | |||
}); | ls are'); | |||
assert.ok(_.isString(obj), 'so are String objects'); | ||||
test('isNumber', function() { | assert.strictEqual(_.isString(1), false); | |||
ok(!_.isNumber('string'), 'a string is not a number'); | }); | |||
ok(!_.isNumber(arguments), 'the arguments object is not a number'); | ||||
ok(!_.isNumber(undefined), 'undefined is not a number'); | QUnit.test('isNumber', function(assert) { | |||
ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are'); | assert.ok(!_.isNumber('string'), 'a string is not a number'); | |||
ok(_.isNumber(NaN), 'NaN *is* a number'); | assert.ok(!_.isNumber(arguments), 'the arguments object is not a number'); | |||
ok(_.isNumber(Infinity), 'Infinity is a number'); | assert.ok(!_.isNumber(void 0), 'undefined is not a number'); | |||
ok(_.isNumber(iNumber), 'even from another frame'); | assert.ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are'); | |||
ok(!_.isNumber('1'), 'numeric strings are not numbers'); | assert.ok(_.isNumber(NaN), 'NaN *is* a number'); | |||
}); | assert.ok(_.isNumber(Infinity), 'Infinity is a number'); | |||
assert.ok(!_.isNumber('1'), 'numeric strings are not numbers'); | ||||
test('isBoolean', function() { | }); | |||
ok(!_.isBoolean(2), 'a number is not a boolean'); | ||||
ok(!_.isBoolean('string'), 'a string is not a boolean'); | QUnit.test('isBoolean', function(assert) { | |||
ok(!_.isBoolean('false'), 'the string "false" is not a boolean'); | assert.ok(!_.isBoolean(2), 'a number is not a boolean'); | |||
ok(!_.isBoolean('true'), 'the string "true" is not a boolean'); | assert.ok(!_.isBoolean('string'), 'a string is not a boolean'); | |||
ok(!_.isBoolean(arguments), 'the arguments object is not a boolean'); | assert.ok(!_.isBoolean('false'), 'the string "false" is not a boolean'); | |||
ok(!_.isBoolean(undefined), 'undefined is not a boolean'); | assert.ok(!_.isBoolean('true'), 'the string "true" is not a boolean'); | |||
ok(!_.isBoolean(NaN), 'NaN is not a boolean'); | assert.ok(!_.isBoolean(arguments), 'the arguments object is not a boolean'); | |||
ok(!_.isBoolean(null), 'null is not a boolean'); | assert.ok(!_.isBoolean(void 0), 'undefined is not a boolean'); | |||
ok(_.isBoolean(true), 'but true is'); | assert.ok(!_.isBoolean(NaN), 'NaN is not a boolean'); | |||
ok(_.isBoolean(false), 'and so is false'); | assert.ok(!_.isBoolean(null), 'null is not a boolean'); | |||
ok(_.isBoolean(iBoolean), 'even from another frame'); | assert.ok(_.isBoolean(true), 'but true is'); | |||
}); | assert.ok(_.isBoolean(false), 'and so is false'); | |||
}); | ||||
test('isFunction', function() { | ||||
ok(!_.isFunction(undefined), 'undefined vars are not functions'); | QUnit.test('isFunction', function(assert) { | |||
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions'); | assert.ok(!_.isFunction(void 0), 'undefined vars are not functions'); | |||
ok(!_.isFunction('moe'), 'strings are not functions'); | assert.ok(!_.isFunction([1, 2, 3]), 'arrays are not functions'); | |||
ok(_.isFunction(_.isFunction), 'but functions are'); | assert.ok(!_.isFunction('moe'), 'strings are not functions'); | |||
ok(_.isFunction(iFunction), 'even from another frame'); | assert.ok(_.isFunction(_.isFunction), 'but functions are'); | |||
ok(_.isFunction(function(){}), 'even anonymous ones'); | assert.ok(_.isFunction(function(){}), 'even anonymous ones'); | |||
}); | ||||
if (testElement) { | ||||
test('isDate', function() { | assert.ok(!_.isFunction(testElement), 'elements are not functions'); | |||
ok(!_.isDate(100), 'numbers are not dates'); | } | |||
ok(!_.isDate({}), 'objects are not dates'); | ||||
ok(_.isDate(new Date()), 'but dates are'); | var nodelist = typeof document != 'undefined' && document.childNodes; | |||
ok(_.isDate(iDate), 'even from another frame'); | if (nodelist) { | |||
}); | assert.ok(!_.isFunction(nodelist)); | |||
} | ||||
test('isRegExp', function() { | }); | |||
ok(!_.isRegExp(_.identity), 'functions are not RegExps'); | ||||
ok(_.isRegExp(/identity/), 'but RegExps are'); | if (typeof Int8Array !== 'undefined') { | |||
ok(_.isRegExp(iRegExp), 'even from another frame'); | QUnit.test('#1929 Typed Array constructors are functions', function(assert) | |||
}); | { | |||
_.chain(['Float32Array', 'Float64Array', 'Int8Array', 'Int16Array', 'Int32 | ||||
test('isFinite', function() { | Array', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array']) | |||
ok(!_.isFinite(undefined), 'undefined is not finite'); | .map(_.propertyOf(typeof GLOBAL != 'undefined' ? GLOBAL : window)) | |||
ok(!_.isFinite(null), 'null is not finite'); | .compact() | |||
ok(!_.isFinite(NaN), 'NaN is not finite'); | .each(function(TypedArray) { | |||
ok(!_.isFinite(Infinity), 'Infinity is not finite'); | // PhantomJS reports `typeof UInt8Array == 'object'` and doesn't report | |||
ok(!_.isFinite(-Infinity), '-Infinity is not finite'); | toString TypeArray | |||
ok(_.isFinite('12'), 'Numeric strings are numbers'); | // as a function | |||
ok(!_.isFinite('1a'), 'Non numeric strings are not numbers'); | assert.strictEqual(_.isFunction(TypedArray), Object.prototype.toString.c | |||
ok(!_.isFinite(''), 'Empty strings are not numbers'); | all(TypedArray) === '[object Function]'); | |||
var obj = new Number(5); | }); | |||
ok(_.isFinite(obj), 'Number instances can be finite'); | ||||
ok(_.isFinite(0), '0 is finite'); | ||||
ok(_.isFinite(123), 'Ints are finite'); | ||||
ok(_.isFinite(-12.44), 'Floats are finite'); | ||||
}); | ||||
test('isNaN', function() { | ||||
ok(!_.isNaN(undefined), 'undefined is not NaN'); | ||||
ok(!_.isNaN(null), 'null is not NaN'); | ||||
ok(!_.isNaN(0), '0 is not NaN'); | ||||
ok(_.isNaN(NaN), 'but NaN is'); | ||||
ok(_.isNaN(iNaN), 'even from another frame'); | ||||
ok(_.isNaN(new Number(NaN)), 'wrapped NaN is still NaN'); | ||||
}); | ||||
test('isNull', function() { | ||||
ok(!_.isNull(undefined), 'undefined is not null'); | ||||
ok(!_.isNull(NaN), 'NaN is not null'); | ||||
ok(_.isNull(null), 'but null is'); | ||||
ok(_.isNull(iNull), 'even from another frame'); | ||||
}); | ||||
test('isUndefined', function() { | ||||
ok(!_.isUndefined(1), 'numbers are defined'); | ||||
ok(!_.isUndefined(null), 'null is defined'); | ||||
ok(!_.isUndefined(false), 'false is defined'); | ||||
ok(!_.isUndefined(NaN), 'NaN is defined'); | ||||
ok(_.isUndefined(), 'nothing is undefined'); | ||||
ok(_.isUndefined(undefined), 'undefined is undefined'); | ||||
ok(_.isUndefined(iUndefined), 'even from another frame'); | ||||
}); | ||||
if (window.ActiveXObject) { | ||||
test('IE host objects', function() { | ||||
var xml = new ActiveXObject('Msxml2.DOMDocument.3.0'); | ||||
ok(!_.isNumber(xml)); | ||||
ok(!_.isBoolean(xml)); | ||||
ok(!_.isNaN(xml)); | ||||
ok(!_.isFunction(xml)); | ||||
ok(!_.isNull(xml)); | ||||
ok(!_.isUndefined(xml)); | ||||
}); | }); | |||
} | } | |||
test('tap', function() { | QUnit.test('isDate', function(assert) { | |||
assert.ok(!_.isDate(100), 'numbers are not dates'); | ||||
assert.ok(!_.isDate({}), 'objects are not dates'); | ||||
assert.ok(_.isDate(new Date()), 'but dates are'); | ||||
}); | ||||
QUnit.test('isRegExp', function(assert) { | ||||
assert.ok(!_.isRegExp(_.identity), 'functions are not RegExps'); | ||||
assert.ok(_.isRegExp(/identity/), 'but RegExps are'); | ||||
}); | ||||
QUnit.test('isFinite', function(assert) { | ||||
assert.ok(!_.isFinite(void 0), 'undefined is not finite'); | ||||
assert.ok(!_.isFinite(null), 'null is not finite'); | ||||
assert.ok(!_.isFinite(NaN), 'NaN is not finite'); | ||||
assert.ok(!_.isFinite(Infinity), 'Infinity is not finite'); | ||||
assert.ok(!_.isFinite(-Infinity), '-Infinity is not finite'); | ||||
assert.ok(_.isFinite('12'), 'Numeric strings are numbers'); | ||||
assert.ok(!_.isFinite('1a'), 'Non numeric strings are not numbers'); | ||||
assert.ok(!_.isFinite(''), 'Empty strings are not numbers'); | ||||
var obj = new Number(5); | ||||
assert.ok(_.isFinite(obj), 'Number instances can be finite'); | ||||
assert.ok(_.isFinite(0), '0 is finite'); | ||||
assert.ok(_.isFinite(123), 'Ints are finite'); | ||||
assert.ok(_.isFinite(-12.44), 'Floats are finite'); | ||||
}); | ||||
QUnit.test('isNaN', function(assert) { | ||||
assert.ok(!_.isNaN(void 0), 'undefined is not NaN'); | ||||
assert.ok(!_.isNaN(null), 'null is not NaN'); | ||||
assert.ok(!_.isNaN(0), '0 is not NaN'); | ||||
assert.ok(!_.isNaN(new Number(0)), 'wrapped 0 is not NaN'); | ||||
assert.ok(_.isNaN(NaN), 'but NaN is'); | ||||
assert.ok(_.isNaN(new Number(NaN)), 'wrapped NaN is still NaN'); | ||||
}); | ||||
QUnit.test('isNull', function(assert) { | ||||
assert.ok(!_.isNull(void 0), 'undefined is not null'); | ||||
assert.ok(!_.isNull(NaN), 'NaN is not null'); | ||||
assert.ok(_.isNull(null), 'but null is'); | ||||
}); | ||||
QUnit.test('isUndefined', function(assert) { | ||||
assert.ok(!_.isUndefined(1), 'numbers are defined'); | ||||
assert.ok(!_.isUndefined(null), 'null is defined'); | ||||
assert.ok(!_.isUndefined(false), 'false is defined'); | ||||
assert.ok(!_.isUndefined(NaN), 'NaN is defined'); | ||||
assert.ok(_.isUndefined(), 'nothing is undefined'); | ||||
assert.ok(_.isUndefined(void 0), 'undefined is undefined'); | ||||
}); | ||||
QUnit.test('isError', function(assert) { | ||||
assert.ok(!_.isError(1), 'numbers are not Errors'); | ||||
assert.ok(!_.isError(null), 'null is not an Error'); | ||||
assert.ok(!_.isError(Error), 'functions are not Errors'); | ||||
assert.ok(_.isError(new Error()), 'Errors are Errors'); | ||||
assert.ok(_.isError(new EvalError()), 'EvalErrors are Errors'); | ||||
assert.ok(_.isError(new RangeError()), 'RangeErrors are Errors'); | ||||
assert.ok(_.isError(new ReferenceError()), 'ReferenceErrors are Errors'); | ||||
assert.ok(_.isError(new SyntaxError()), 'SyntaxErrors are Errors'); | ||||
assert.ok(_.isError(new TypeError()), 'TypeErrors are Errors'); | ||||
assert.ok(_.isError(new URIError()), 'URIErrors are Errors'); | ||||
}); | ||||
QUnit.test('tap', function(assert) { | ||||
var intercepted = null; | var intercepted = null; | |||
var interceptor = function(obj) { intercepted = obj; }; | var interceptor = function(obj) { intercepted = obj; }; | |||
var returned = _.tap(1, interceptor); | var returned = _.tap(1, interceptor); | |||
equal(intercepted, 1, 'passes tapped object to interceptor'); | assert.equal(intercepted, 1, 'passes tapped object to interceptor'); | |||
equal(returned, 1, 'returns tapped object'); | assert.equal(returned, 1, 'returns tapped object'); | |||
returned = _([1, 2, 3]).chain(). | returned = _([1, 2, 3]).chain(). | |||
map(function(n){ return n * 2; }). | map(function(n){ return n * 2; }). | |||
max(). | max(). | |||
tap(interceptor). | tap(interceptor). | |||
value(); | value(); | |||
equal(returned, 6, 'can use tapped objects in a chain'); | assert.equal(returned, 6, 'can use tapped objects in a chain'); | |||
equal(intercepted, returned, 'can use tapped objects in a chain'); | assert.equal(intercepted, returned, 'can use tapped objects in a chain'); | |||
}); | }); | |||
test('has', function () { | QUnit.test('has', function(assert) { | |||
var obj = {foo: 'bar', func: function(){}}; | var obj = {foo: 'bar', func: function(){}}; | |||
ok(_.has(obj, 'foo'), 'has() checks that the object has a property.'); | assert.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 p | ; | |||
roperty."); | assert.ok(!_.has(obj, 'baz'), "has() returns false if the object doesn't hav | |||
ok(_.has(obj, 'func'), 'has() works for functions too.'); | e the property."); | |||
assert.ok(_.has(obj, 'func'), 'has() works for functions too.'); | ||||
obj.hasOwnProperty = null; | obj.hasOwnProperty = null; | |||
ok(_.has(obj, 'foo'), 'has() works even when the hasOwnProperty method is de leted.'); | assert.ok(_.has(obj, 'foo'), 'has() works even when the hasOwnProperty metho d is deleted.'); | |||
var child = {}; | var child = {}; | |||
child.prototype = obj; | child.prototype = obj; | |||
ok(!_.has(child, 'foo'), 'has() does not check the prototype chain for a pro | assert.ok(!_.has(child, 'foo'), 'has() does not check the prototype chain fo | |||
perty.'); | r a property.'); | |||
strictEqual(_.has(null, 'foo'), false, 'has() returns false for null'); | assert.strictEqual(_.has(null, 'foo'), false, 'has() returns false for null' | |||
strictEqual(_.has(undefined, 'foo'), false, 'has() returns false for undefin | ); | |||
ed'); | assert.strictEqual(_.has(void 0, 'foo'), false, 'has() returns false for und | |||
efined'); | ||||
}); | }); | |||
test('matches', function() { | QUnit.test('isMatch', function(assert) { | |||
var moe = {name: 'Moe Howard', hair: true}; | ||||
var curly = {name: 'Curly Howard', hair: false}; | ||||
assert.equal(_.isMatch(moe, {hair: true}), true, 'Returns a boolean'); | ||||
assert.equal(_.isMatch(curly, {hair: true}), false, 'Returns a boolean'); | ||||
assert.equal(_.isMatch(5, {__x__: void 0}), false, 'can match undefined prop | ||||
s on primitives'); | ||||
assert.equal(_.isMatch({__x__: void 0}, {__x__: void 0}), true, 'can match u | ||||
ndefined props'); | ||||
assert.equal(_.isMatch(null, {}), true, 'Empty spec called with null object | ||||
returns true'); | ||||
assert.equal(_.isMatch(null, {a: 1}), false, 'Non-empty spec called with nul | ||||
l object returns false'); | ||||
_.each([null, void 0], function(item) { assert.strictEqual(_.isMatch(item, n | ||||
ull), true, 'null matches null'); }); | ||||
_.each([null, void 0], function(item) { assert.strictEqual(_.isMatch(item, n | ||||
ull), true, 'null matches {}'); }); | ||||
assert.strictEqual(_.isMatch({b: 1}, {a: void 0}), false, 'handles undefined | ||||
values (1683)'); | ||||
_.each([true, 5, NaN, null, void 0], function(item) { | ||||
assert.strictEqual(_.isMatch({a: 1}, item), true, 'treats primitives as em | ||||
pty'); | ||||
}); | ||||
function Prototest() {} | ||||
Prototest.prototype.x = 1; | ||||
var specObj = new Prototest; | ||||
assert.equal(_.isMatch({x: 2}, specObj), true, 'spec is restricted to own pr | ||||
operties'); | ||||
specObj.y = 5; | ||||
assert.equal(_.isMatch({x: 1, y: 5}, specObj), true); | ||||
assert.equal(_.isMatch({x: 1, y: 4}, specObj), false); | ||||
assert.ok(_.isMatch(specObj, {x: 1, y: 5}), 'inherited and own properties ar | ||||
e checked on the test object'); | ||||
Prototest.x = 5; | ||||
assert.ok(_.isMatch({x: 5, y: 1}, Prototest), 'spec can be a function'); | ||||
//null edge cases | ||||
var oCon = {constructor: Object}; | ||||
assert.deepEqual(_.map([null, void 0, 5, {}], _.partial(_.isMatch, _, oCon)) | ||||
, [false, false, false, true], 'doesnt falsey match constructor on undefined/nul | ||||
l'); | ||||
}); | ||||
QUnit.test('matcher', function(assert) { | ||||
var moe = {name: 'Moe Howard', hair: true}; | var moe = {name: 'Moe Howard', hair: true}; | |||
var curly = {name: 'Curly Howard', hair: false}; | var curly = {name: 'Curly Howard', hair: false}; | |||
var stooges = [moe, curly]; | var stooges = [moe, curly]; | |||
equal(_.matches({hair: true})(moe), true, 'Returns a boolean'); | assert.equal(_.matcher({hair: true})(moe), true, 'Returns a boolean'); | |||
equal(_.matches({hair: true})(curly), false, 'Returns a boolean'); | assert.equal(_.matcher({hair: true})(curly), false, 'Returns a boolean'); | |||
equal(_.matches({__x__: undefined})(5), false, 'can match undefined props on | assert.equal(_.matcher({__x__: void 0})(5), false, 'can match undefined prop | |||
primitives'); | s on primitives'); | |||
equal(_.matches({__x__: undefined})({__x__: undefined}), true, 'can match un | assert.equal(_.matcher({__x__: void 0})({__x__: void 0}), true, 'can match u | |||
defined props'); | ndefined props'); | |||
equal(_.matches({})(null), true, 'Empty spec called with null object returns | assert.equal(_.matcher({})(null), true, 'Empty spec called with null object | |||
true'); | returns true'); | |||
equal(_.matches({a: 1})(null), false, 'Non-empty spec called with null objec | assert.equal(_.matcher({a: 1})(null), false, 'Non-empty spec called with nul | |||
t returns false'); | l object returns false'); | |||
ok(_.find(stooges, _.matches({hair: false})) === curly, 'returns a predicate | assert.ok(_.find(stooges, _.matcher({hair: false})) === curly, 'returns a pr | |||
that can be used by finding functions.'); | edicate that can be used by finding functions.'); | |||
ok(_.find(stooges, _.matches(moe)) === moe, 'can be used to locate an object | assert.ok(_.find(stooges, _.matcher(moe)) === moe, 'can be used to locate an | |||
exists in a collection.'); | object exists in a collection.'); | |||
deepEqual(_.where([null, undefined], {a: 1}), [], 'Do not throw on null valu | assert.deepEqual(_.filter([null, void 0], _.matcher({a: 1})), [], 'Do not th | |||
es.'); | row on null values.'); | |||
deepEqual(_.where([null, undefined], null), [null, undefined], 'null matches | assert.deepEqual(_.filter([null, void 0], _.matcher(null)), [null, void 0], | |||
null'); | 'null matches null'); | |||
deepEqual(_.where([null, undefined], {}), [null, undefined], 'null matches { | assert.deepEqual(_.filter([null, void 0], _.matcher({})), [null, void 0], 'n | |||
}'); | ull matches {}'); | |||
deepEqual(_.where([{b: 1}], {a: undefined}), [], 'handles undefined values ( | assert.deepEqual(_.filter([{b: 1}], _.matcher({a: void 0})), [], 'handles un | |||
1683)'); | defined values (1683)'); | |||
_.each([true, 5, NaN, null, undefined], function(item) { | _.each([true, 5, NaN, null, void 0], function(item) { | |||
deepEqual(_.where([{a: 1}], item), [{a: 1}], 'treats primitives as empty') | assert.equal(_.matcher(item)({a: 1}), true, 'treats primitives as empty'); | |||
; | ||||
}); | }); | |||
function Prototest() {} | function Prototest() {} | |||
Prototest.prototype.x = 1; | Prototest.prototype.x = 1; | |||
var specObj = new Prototest; | var specObj = new Prototest; | |||
var protospec = _.matches(specObj); | var protospec = _.matcher(specObj); | |||
equal(protospec({x: 2}), true, 'spec is restricted to own properties'); | assert.equal(protospec({x: 2}), true, 'spec is restricted to own properties' | |||
); | ||||
specObj.y = 5; | specObj.y = 5; | |||
protospec = _.matches(specObj); | protospec = _.matcher(specObj); | |||
equal(protospec({x: 1, y: 5}), true); | assert.equal(protospec({x: 1, y: 5}), true); | |||
equal(protospec({x: 1, y: 4}), false); | assert.equal(protospec({x: 1, y: 4}), false); | |||
ok(_.matches({x: 1, y: 5})(specObj), 'inherited and own properties are check ed on the test object'); | assert.ok(_.matcher({x: 1, y: 5})(specObj), 'inherited and own properties ar e checked on the test object'); | |||
Prototest.x = 5; | Prototest.x = 5; | |||
ok(_.matches(Prototest)({x: 5, y: 1}), 'spec can be a function'); | assert.ok(_.matcher(Prototest)({x: 5, y: 1}), 'spec can be a function'); | |||
// #1729 | // #1729 | |||
var o = {'b': 1}; | var o = {b: 1}; | |||
var m = _.matches(o); | var m = _.matcher(o); | |||
equal(m({'b': 1}), true); | assert.equal(m({b: 1}), true); | |||
o.b = 2; | o.b = 2; | |||
o.a = 1; | o.a = 1; | |||
equal(m({'b': 1}), true, 'changing spec object doesnt change matches result' ); | assert.equal(m({b: 1}), true, 'changing spec object doesnt change matches re sult'); | |||
//null edge cases | //null edge cases | |||
var oCon = _.matches({'constructor': Object}); | var oCon = _.matcher({constructor: Object}); | |||
deepEqual(_.map([null, undefined, 5, {}], oCon), [false, false, false, true] | assert.deepEqual(_.map([null, void 0, 5, {}], oCon), [false, false, false, t | |||
, 'doesnt fasley match constructor on undefined/null'); | rue], 'doesnt falsey match constructor on undefined/null'); | |||
}); | }); | |||
QUnit.test('matches', function(assert) { | ||||
assert.strictEqual(_.matches, _.matcher, 'is an alias for matcher'); | ||||
}); | ||||
QUnit.test('findKey', function(assert) { | ||||
var objects = { | ||||
a: {a: 0, b: 0}, | ||||
b: {a: 1, b: 1}, | ||||
c: {a: 2, b: 2} | ||||
}; | ||||
assert.equal(_.findKey(objects, function(obj) { | ||||
return obj.a === 0; | ||||
}), 'a'); | ||||
assert.equal(_.findKey(objects, function(obj) { | ||||
return obj.b * obj.a === 4; | ||||
}), 'c'); | ||||
assert.equal(_.findKey(objects, 'a'), 'b', 'Uses lookupIterator'); | ||||
assert.equal(_.findKey(objects, function(obj) { | ||||
return obj.b * obj.a === 5; | ||||
}), void 0); | ||||
assert.strictEqual(_.findKey([1, 2, 3, 4, 5, 6], function(obj) { | ||||
return obj === 3; | ||||
}), '2', 'Keys are strings'); | ||||
assert.strictEqual(_.findKey(objects, function(a) { | ||||
return a.foo === null; | ||||
}), void 0); | ||||
_.findKey({a: {a: 1}}, function(a, key, obj) { | ||||
assert.equal(key, 'a'); | ||||
assert.deepEqual(obj, {a: {a: 1}}); | ||||
assert.strictEqual(this, objects, 'called with context'); | ||||
}, objects); | ||||
var array = [1, 2, 3, 4]; | ||||
array.match = 55; | ||||
assert.strictEqual(_.findKey(array, function(x) { return x === 55; }), 'matc | ||||
h', 'matches array-likes keys'); | ||||
}); | ||||
QUnit.test('mapObject', function(assert) { | ||||
var obj = {a: 1, b: 2}; | ||||
var objects = { | ||||
a: {a: 0, b: 0}, | ||||
b: {a: 1, b: 1}, | ||||
c: {a: 2, b: 2} | ||||
}; | ||||
assert.deepEqual(_.mapObject(obj, function(val) { | ||||
return val * 2; | ||||
}), {a: 2, b: 4}, 'simple objects'); | ||||
assert.deepEqual(_.mapObject(objects, function(val) { | ||||
return _.reduce(val, function(memo, v){ | ||||
return memo + v; | ||||
}, 0); | ||||
}), {a: 0, b: 2, c: 4}, 'nested objects'); | ||||
assert.deepEqual(_.mapObject(obj, function(val, key, o) { | ||||
return o[key] * 2; | ||||
}), {a: 2, b: 4}, 'correct keys'); | ||||
assert.deepEqual(_.mapObject([1, 2], function(val) { | ||||
return val * 2; | ||||
}), {0: 2, 1: 4}, 'check behavior for arrays'); | ||||
assert.deepEqual(_.mapObject(obj, function(val) { | ||||
return val * this.multiplier; | ||||
}, {multiplier: 3}), {a: 3, b: 6}, 'keep context'); | ||||
assert.deepEqual(_.mapObject({a: 1}, function() { | ||||
return this.length; | ||||
}, [1, 2]), {a: 2}, 'called with context'); | ||||
var ids = _.mapObject({length: 2, 0: {id: '1'}, 1: {id: '2'}}, function(n){ | ||||
return n.id; | ||||
}); | ||||
assert.deepEqual(ids, {length: void 0, 0: '1', 1: '2'}, 'Check with array-li | ||||
ke objects'); | ||||
// Passing a property name like _.pluck. | ||||
var people = {a: {name: 'moe', age: 30}, b: {name: 'curly', age: 50}}; | ||||
assert.deepEqual(_.mapObject(people, 'name'), {a: 'moe', b: 'curly'}, 'predi | ||||
cate string map to object properties'); | ||||
_.each([null, void 0, 1, 'abc', [], {}, void 0], function(val){ | ||||
assert.deepEqual(_.mapObject(val, _.identity), {}, 'mapValue identity'); | ||||
}); | ||||
var Proto = function(){ this.a = 1; }; | ||||
Proto.prototype.b = 1; | ||||
var protoObj = new Proto(); | ||||
assert.deepEqual(_.mapObject(protoObj, _.identity), {a: 1}, 'ignore inherite | ||||
d values from prototypes'); | ||||
}); | ||||
}()); | }()); | |||
End of changes. 126 change blocks. | ||||
503 lines changed or deleted | 829 lines changed or added |