mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 00:27:50 +00:00
Merge pull request #390 from jdalton/unittests
Add unit tests and cleanup code for for-in issues.
This commit is contained in:
@@ -127,9 +127,12 @@ $(document).ready(function() {
|
||||
});
|
||||
|
||||
test("arrays: indexOf", function() {
|
||||
var numbers = [1, 2, 3];
|
||||
var numbers = [1];
|
||||
numbers[2] = 3;
|
||||
numbers.indexOf = null;
|
||||
equals(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
|
||||
equals(_.indexOf(numbers, 3), 2, 'can compute indexOf, even without the native function');
|
||||
equals(_.indexOf(numbers, void 0), -1, 'handles sparse arrays correctly');
|
||||
|
||||
var result = (function(){ return _.indexOf(arguments, 2); })(1, 2, 3);
|
||||
equals(result, 1, 'works on an arguments object');
|
||||
equals(_.indexOf(null, 2), -1, 'handles nulls properly');
|
||||
@@ -148,10 +151,13 @@ $(document).ready(function() {
|
||||
});
|
||||
|
||||
test("arrays: lastIndexOf", function() {
|
||||
var numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0];
|
||||
var numbers = [1, 0, 1, 0, 0, 1, 0];
|
||||
numbers[8] = 0;
|
||||
numbers.lastIndexOf = null;
|
||||
equals(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');
|
||||
equals(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
|
||||
equals(_.lastIndexOf(numbers, void 0), -1, 'handles sparse arrays correctly');
|
||||
|
||||
var result = (function(){ return _.lastIndexOf(arguments, 1); })(1, 0, 1, 0, 0, 1, 0, 0, 0);
|
||||
equals(result, 5, 'works on an arguments object');
|
||||
equals(_.indexOf(null, 2), -1, 'handles nulls properly');
|
||||
|
||||
@@ -16,11 +16,11 @@ $(document).ready(function($, undefined) {
|
||||
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
|
||||
|
||||
answers = [];
|
||||
var obj = {one : 1, two : 2, three : 3};
|
||||
var obj = {one: 1, two: 2, three: 3, toString: 1};
|
||||
obj.constructor.prototype.four = 4;
|
||||
_.each(obj, function(value, key){ answers.push(key); });
|
||||
equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
|
||||
delete obj.constructor.prototype.four;
|
||||
equals(answers.sort().join(', '), 'one, three, toString, two', 'iterating over objects works, and ignores the object prototype.');
|
||||
|
||||
answer = null;
|
||||
_.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; });
|
||||
@@ -238,7 +238,7 @@ $(document).ready(function($, undefined) {
|
||||
equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');
|
||||
|
||||
var numbers = _.toArray({one : 1, two : 2, three : 3});
|
||||
equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
|
||||
equals(numbers.sort().join(', '), '1, 2, 3', 'object flattened into array');
|
||||
});
|
||||
|
||||
test('collections: size', function() {
|
||||
|
||||
@@ -3,8 +3,30 @@ $(document).ready(function($, undefined) {
|
||||
module("Objects");
|
||||
|
||||
test("objects: keys", function() {
|
||||
equals(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
|
||||
// the test above is not safe because it relies on for-in enumeration order
|
||||
equals(
|
||||
_.keys({one: 1, two: 2}).sort().join(', '),
|
||||
'one, two',
|
||||
'can extract the keys from an object'
|
||||
);
|
||||
|
||||
equals(
|
||||
_.keys({
|
||||
constructor: 'a',
|
||||
hasOwnProperty: 'b',
|
||||
isPrototypeOf: 'c',
|
||||
propertyIsEnumerable: 'd',
|
||||
toLocaleString: 'e',
|
||||
toString: 'f',
|
||||
valueOf: 'g'
|
||||
}).sort().join(', '),
|
||||
'constructor, hasOwnProperty, isPrototypeOf, propertyIsEnumerable, toLocaleString, toString, valueOf',
|
||||
'keys of shadowing properties'
|
||||
);
|
||||
|
||||
var fn = function(){};
|
||||
fn.x = fn.y = fn.z = fn.prototype.a = 1;
|
||||
equals(_.keys(fn).sort().join(', '), 'x, y, z', 'keys of functions works, and ignores the prototype property');
|
||||
|
||||
var a = []; a[1] = 0;
|
||||
equals(_.keys(a).join(', '), '1', 'is not fooled by sparse arrays; see issue #95');
|
||||
raises(function() { _.keys(null); }, TypeError, 'throws an error for `null` values');
|
||||
@@ -15,7 +37,7 @@ $(document).ready(function($, undefined) {
|
||||
});
|
||||
|
||||
test("objects: values", function() {
|
||||
equals(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object');
|
||||
equals(_.values({one : 1, two : 2}).sort().join(', '), '1, 2', 'can extract the values from an object');
|
||||
});
|
||||
|
||||
test("objects: functions", function() {
|
||||
@@ -29,20 +51,38 @@ $(document).ready(function($, undefined) {
|
||||
|
||||
test("objects: extend", function() {
|
||||
var result;
|
||||
var expected = {
|
||||
constructor: 'a',
|
||||
hasOwnProperty: 'b',
|
||||
isPrototypeOf: 'c',
|
||||
propertyIsEnumerable: 'd',
|
||||
toLocaleString: 'e',
|
||||
toString: 'f',
|
||||
valueOf: 'g'
|
||||
};
|
||||
|
||||
equals(_.extend({}, {a:'b'}).a, 'b', 'can extend an object with the attributes of another');
|
||||
equals(_.extend({a:'x'}, {a:'b'}).a, 'b', 'properties in source override destination');
|
||||
equals(_.extend({x:'x'}, {a:'b'}).x, 'x', 'properties not in source dont get overriden');
|
||||
|
||||
result = _.extend({}, expected);
|
||||
ok(_.isEqual(result, expected), 'extend with shadow properties');
|
||||
|
||||
result = _.extend({x:'x'}, {a:'a'}, {b:'b'});
|
||||
ok(_.isEqual(result, {x:'x', a:'a', b:'b'}), 'can extend from multiple source objects');
|
||||
|
||||
result = _.extend({x:'x'}, {a:'a', x:2}, {a:'b'});
|
||||
ok(_.isEqual(result, {x:2, a:'b'}), 'extending from multiple source objects last property trumps');
|
||||
|
||||
result = _.extend({}, {a: void 0, b: null});
|
||||
equals(_.keys(result).join(''), 'b', 'extend does not copy undefined values');
|
||||
});
|
||||
|
||||
test("objects: defaults", function() {
|
||||
var result;
|
||||
var options = {zero: 0, one: 1, empty: "", nan: NaN, string: "string"};
|
||||
var Options = function() { this.empty = ''; this.string = 'string'; this.zero = 0; };
|
||||
_.extend(Options.prototype, {nan: NaN, one:1});
|
||||
var options = new Options;
|
||||
|
||||
_.defaults(options, {zero: 1, one: 10, twenty: 20});
|
||||
equals(options.zero, 0, 'value exists');
|
||||
@@ -56,9 +96,14 @@ $(document).ready(function($, undefined) {
|
||||
});
|
||||
|
||||
test("objects: clone", function() {
|
||||
var moe = {name : 'moe', lucky : [13, 27, 34]};
|
||||
var toString = function() { return this.name; };
|
||||
var valueOf = function() { return this.name; };
|
||||
var moe = {name : 'moe', lucky : [13, 27, 34], toString: toString, valueOf: valueOf};
|
||||
|
||||
var clone = _.clone(moe);
|
||||
equals(clone.name, 'moe', 'the clone as the attributes of the original');
|
||||
equals(clone.toString, toString, 'cloned own toString method');
|
||||
equals(clone.valueOf, valueOf, 'cloned own valueOf method');
|
||||
|
||||
clone.name = 'curly';
|
||||
ok(clone.name == 'curly' && moe.name == 'moe', 'clones can change shallow attributes without affecting the original');
|
||||
@@ -81,6 +126,16 @@ $(document).ready(function($, undefined) {
|
||||
}
|
||||
Second.prototype.value = 2;
|
||||
|
||||
var obj = {
|
||||
constructor: 'a',
|
||||
hasOwnProperty: 'b',
|
||||
isPrototypeOf: 'c',
|
||||
propertyIsEnumerable: 'd',
|
||||
toLocaleString: 'e',
|
||||
toString: 'f',
|
||||
valueOf: 'g'
|
||||
};
|
||||
|
||||
// Basic equality and identity comparisons.
|
||||
ok(_.isEqual(null, null), "`null` is equal to `null`");
|
||||
ok(_.isEqual(), "`undefined` is equal to `undefined`");
|
||||
@@ -207,6 +262,10 @@ $(document).ready(function($, undefined) {
|
||||
ok(!_.isEqual({a: 1}, {a: 1, b: 2}), "Commutative equality is implemented for objects");
|
||||
ok(!_.isEqual({x: 1, y: undefined}, {x: 1, z: 2}), "Objects with identical keys and different values are not equivalent");
|
||||
|
||||
// Objects with properties that shadow non-enumerable ones.
|
||||
ok(!_.isEqual({}, {toString: 1}), "Object with custom toString is not equal to {}");
|
||||
ok(_.isEqual({toString: 1, valueOf: 2}, {toString: 1, valueOf: 2}), "Objects with equivalent shadow properties");
|
||||
|
||||
// `A` contains nested objects and arrays.
|
||||
a = {
|
||||
name: new String("Moe Howard"),
|
||||
@@ -357,7 +416,7 @@ $(document).ready(function($, undefined) {
|
||||
test("objects: isEmpty", function() {
|
||||
ok(!_([1]).isEmpty(), '[1] is not empty');
|
||||
ok(_.isEmpty([]), '[] is empty');
|
||||
ok(!_.isEmpty({one : 1}), '{one : 1} is not empty');
|
||||
ok(!_.isEmpty({valueOf: 1}), '{valueOf: 1} is not empty');
|
||||
ok(_.isEmpty({}), '{} is empty');
|
||||
ok(_.isEmpty(new RegExp('')), 'objects with prototype properties are empty');
|
||||
ok(_.isEmpty(null), 'null is empty');
|
||||
@@ -387,7 +446,7 @@ $(document).ready(function($, undefined) {
|
||||
parent.iNaN = NaN;\
|
||||
parent.iNull = null;\
|
||||
parent.iBoolean = new Boolean(false);\
|
||||
parent.iUndefined = undefined;\
|
||||
parent.iUndefined = void 0;\
|
||||
</script>"
|
||||
);
|
||||
iDoc.close();
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
|
||||
// Create quick reference variables for speed access to core prototypes.
|
||||
var concat = ArrayProto.concat,
|
||||
push = ArrayProto.push,
|
||||
slice = ArrayProto.slice,
|
||||
unshift = ArrayProto.unshift,
|
||||
toString = ObjProto.toString,
|
||||
hasOwnProperty = ObjProto.hasOwnProperty;
|
||||
|
||||
@@ -84,6 +84,8 @@
|
||||
var fn = iterator;
|
||||
var i = -1;
|
||||
var l = obj.length;
|
||||
|
||||
// We optimize for common use by only binding a context when it's passed.
|
||||
if (context) {
|
||||
iterator = function() { return fn.call(context, obj[i], i, obj); };
|
||||
}
|
||||
@@ -98,7 +100,7 @@
|
||||
}
|
||||
};
|
||||
|
||||
// A simple each, for when we know we're dealing with an array.
|
||||
// A simple each, for dealing with non-sparse arrays and arguments objects.
|
||||
var simpleEach = function(obj, iterator, index) {
|
||||
index || (index = 0);
|
||||
for (var l = obj.length; index < l; index++) {
|
||||
@@ -112,15 +114,15 @@
|
||||
'toLocaleString', 'toString', 'valueOf'
|
||||
];
|
||||
|
||||
// IE < 9 skips enumerable properties shadowing non-enumerable ones.
|
||||
// IE < 9 makes properties, shadowing non-enumerable ones, non-enumerable too.
|
||||
var forShadowed = !{valueOf:0}.propertyIsEnumerable('valueOf') &&
|
||||
function(obj, iterator) {
|
||||
// because IE < 9 can't set the `[[Enumerable]]` attribute of an existing
|
||||
// Because IE < 9 can't set the `[[Enumerable]]` attribute of an existing
|
||||
// property and the `constructor` property of a prototype defaults to
|
||||
// non-enumerable, we manually skip the `constructor` property when we
|
||||
// think we are iterating over a `prototype` object.
|
||||
var ctor = obj.constructor;
|
||||
var skipCtor = ctor && ctor.prototype && ctor.prototype.constructor == ctor;
|
||||
var skipCtor = ctor && ctor.prototype && ctor.prototype.constructor === ctor;
|
||||
for (var key, i = 0; key = shadowed[i]; i++) {
|
||||
if (!(skipCtor && key == 'constructor') &&
|
||||
hasOwnProperty.call(obj, key) &&
|
||||
@@ -131,24 +133,25 @@
|
||||
};
|
||||
|
||||
// Iterates over an object's properties, executing the `callback` for each.
|
||||
var forProps = function(obj, iterator, ownOnly, context) {
|
||||
var forProps = function(obj, iterator, ownOnly) {
|
||||
var done = !obj;
|
||||
var skipProto = typeof obj == 'function';
|
||||
|
||||
for (var key in obj) {
|
||||
// Opera < 12 and Safari < 5.1 (if the prototype or a property on the prototype has been set)
|
||||
// Firefox < 3.6, Opera > 9.50 - Opera < 12, and Safari < 5.1
|
||||
// (if the prototype or a property on the prototype has been set)
|
||||
// incorrectly set a function's `prototype` property [[Enumerable]] value
|
||||
// to true. Because of this we standardize on skipping the the `prototype`
|
||||
// property of functions regardless of their [[Enumerable]] value.
|
||||
if (done =
|
||||
!(skipProto && key == 'prototype') &&
|
||||
(!ownOnly || ownOnly && hasOwnProperty.call(obj, key)) &&
|
||||
iterator.call(context, obj[key], key, obj) === breaker) {
|
||||
iterator(obj[key], key, obj) === breaker) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!done && forShadowed) {
|
||||
forShadowed(obj, iterator, context);
|
||||
forShadowed(obj, iterator);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1018,8 +1021,8 @@
|
||||
// A method to easily add functions to the OOP wrapper.
|
||||
var addToWrapper = function(name, func) {
|
||||
wrapper.prototype[name] = function() {
|
||||
var args = slice.call(arguments);
|
||||
unshift.call(args, this._wrapped);
|
||||
var args = [this._wrapped];
|
||||
push.apply(args, arguments);
|
||||
return result(func.apply(_, args), this._chain);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user