Add more _.forIn iteration tests and prep support for Error object iteration.

Former-commit-id: 3676681717d0648c9f96570a4952f7c35e6a9bec
This commit is contained in:
John-David Dalton
2013-05-07 01:38:13 -07:00
parent aad55fc3db
commit e0cf4e644b
3 changed files with 102 additions and 56 deletions

View File

@@ -999,12 +999,25 @@
(function() {
test('iterates over inherited properties', function() {
function Dog(name) { this.name = name; }
Dog.prototype.bark = function() { /* Woof, woof! */ };
function Foo() { this.a = 1; }
Foo.prototype.b = 2;
var keys = [];
_.forIn(new Dog('Dagny'), function(value, key) { keys.push(key); });
deepEqual(keys.sort(), ['bark', 'name']);
_.forIn(new Foo, function(value, key) { keys.push(key); });
deepEqual(keys.sort(), ['a', 'b']);
});
test('fixes the JScript [[DontEnum]] bug with inherited properties (test in IE < 9)', function() {
function Foo() {}
Foo.prototype = shadowedObject;
function Bar() {}
Bar.prototype = new Foo;
Bar.prototype.constructor = Bar;
var keys = [];
_.forIn(shadowedObject, function(value, key) { keys.push(key); });
deepEqual(keys.sort(), shadowedProps);
});
}());
@@ -1035,6 +1048,26 @@
deepEqual(keys.sort(), shadowedProps);
});
test('lodash.' + methodName + ' does not iterate over non-enumerable properties (test in IE < 9)', function() {
_.forOwn({
'Array': Array.prototype,
'Boolean': Boolean.prototype,
'Date': Date.prototype,
'Error': Error.prototype,
'Function': Function.prototype,
'Object': Object.prototype,
'Number': Number.prototype,
'TypeError': TypeError.prototype,
'RegExp': RegExp.prototype,
'String': String.prototype
},
function(object, builtin) {
var keys = [];
func(object, function(value, key) { keys.push(key); });
deepEqual(keys, [], 'non-enumerable properties on ' + builtin + '.prototype');
});
});
test('lodash.' + methodName + ' skips the prototype property of functions (test in Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1)', function() {
function Foo() {}
Foo.prototype.a = 1;