Add tests for _.transform and objects from other realms.

This commit is contained in:
John-David Dalton
2014-10-22 22:28:47 -07:00
parent b930bd7326
commit 5414d4a9e4

View File

@@ -367,21 +367,32 @@
try { try {
// add values from a different realm // add values from a different realm
_.extend(_, require('vm').runInNewContext([ _.extend(_, require('vm').runInNewContext([
'({', '(function() {',
"'_arguments': (function() { return arguments; }(1, 2, 3)),", ' var object = {',
"'_array': [1, 2, 3],", " '_arguments': (function() { return arguments; }(1, 2, 3)),",
"'_boolean': Object(false),", " '_array': [1, 2, 3],",
"'_date': new Date,", " '_boolean': Object(false),",
"'_errors': [new Error, new EvalError, new RangeError, new ReferenceError, new SyntaxError, new TypeError, new URIError],", " '_date': new Date,",
"'_function': function() {},", " '_errors': [new Error, new EvalError, new RangeError, new ReferenceError, new SyntaxError, new TypeError, new URIError],",
"'_nan': NaN,", " '_function': function() {},",
"'_null': null,", " '_nan': NaN,",
"'_number': Object(0),", " '_null': null,",
"'_object': { 'a': 1, 'b': 2, 'c': 3 },", " '_number': Object(0),",
"'_regexp': /x/,", " '_object': { 'a': 1, 'b': 2, 'c': 3 },",
"'_string': Object('a'),", " '_regexp': /x/,",
"'_undefined': undefined", " '_string': Object('a'),",
'})' " '_undefined': undefined",
' };',
'',
" ['" + typedArrays.join("', '") + "'].forEach(function(type) {",
" var Ctor = Function('return typeof ' + type + \" != 'undefined' && \" + type)()",
' if (Ctor) {',
" object['_' + type.toLowerCase()] = new Ctor(new ArrayBuffer(24));",
' }',
" });",
'',
' return object;',
'}())'
].join('\n'))); ].join('\n')));
} }
catch(e) { catch(e) {
@@ -583,6 +594,14 @@
'parent._._regexp = /x/;', 'parent._._regexp = /x/;',
"parent._._string = Object('a');", "parent._._string = Object('a');",
'parent._._undefined = undefined;', 'parent._._undefined = undefined;',
'',
'var root = this;',
"parent._.each(['" + typedArrays.join("', '") + "'], function(type) {",
' var Ctor = root[type];',
' if (Ctor) {',
" parent._['_' + type.toLowerCase()] = new Ctor(new ArrayBuffer(24));",
' }',
'});',
'<\/script>' '<\/script>'
].join('\n')); ].join('\n'));
idoc.close(); idoc.close();
@@ -11586,6 +11605,30 @@
deepEqual(actual, object); deepEqual(actual, object);
}); });
}); });
test('should produce an object from the same realm as `object`', 1, function() {
var objects = _.transform(_, function(result, value, key) {
if (_.startsWith(key, '_') && _.isObject(value)) {
result.push(value);
}
}, []);
var expected = _.times(objects.length, _.constant(true));
var actual = _.map(objects, function(object) {
var result = _.transform(object);
if (result === object) {
return false;
}
if (typeof object.length == 'number' &&
!_.isArray(object) && !_.isFunction(object) && !_.isString(object)) {
return result instanceof Array;
}
return result instanceof object.constructor;
});
deepEqual(actual, expected);
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/