mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-15 21:27:50 +00:00
Add tests for _.transform and objects from other realms.
This commit is contained in:
47
test/test.js
47
test/test.js
@@ -367,7 +367,8 @@
|
||||
try {
|
||||
// add values from a different realm
|
||||
_.extend(_, require('vm').runInNewContext([
|
||||
'({',
|
||||
'(function() {',
|
||||
' var object = {',
|
||||
" '_arguments': (function() { return arguments; }(1, 2, 3)),",
|
||||
" '_array': [1, 2, 3],",
|
||||
" '_boolean': Object(false),",
|
||||
@@ -381,7 +382,17 @@
|
||||
" '_regexp': /x/,",
|
||||
" '_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')));
|
||||
}
|
||||
catch(e) {
|
||||
@@ -583,6 +594,14 @@
|
||||
'parent._._regexp = /x/;',
|
||||
"parent._._string = Object('a');",
|
||||
'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>'
|
||||
].join('\n'));
|
||||
idoc.close();
|
||||
@@ -11586,6 +11605,30 @@
|
||||
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);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user