Add baseEach tests.

This commit is contained in:
John-David Dalton
2014-01-25 02:22:42 -08:00
parent 79afa16a7f
commit 3986dd4c32
2 changed files with 164 additions and 139 deletions

View File

@@ -240,6 +240,12 @@
}; };
} }
function loadTests() {
require(['test'], function() {
QUnit.start();
});
}
function loadModulesAndTests() { function loadModulesAndTests() {
require(getConfig(), ['lodash', 'shimmed', 'underscore'], function(lodash, shimmed, underscore) { require(getConfig(), ['lodash', 'shimmed', 'underscore'], function(lodash, shimmed, underscore) {
var oldDash = window._; var oldDash = window._;
@@ -259,9 +265,14 @@
if (oldDash || ui.isModularize) { if (oldDash || ui.isModularize) {
window._ = lodash; window._ = lodash;
} }
require(['test'], function() { if (ui.isModularize) {
QUnit.start(); require(['lodash/internals/baseEach'], function(baseEach) {
}); lodash._baseEach = baseEach;
loadTests();
});
} else {
loadTests();
}
}); });
} }
if (window.requirejs) { if (window.requirejs) {

View File

@@ -243,147 +243,159 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
// add values from other realms // setup values for Node.js
(function() { (function() {
if (!amd) { if (amd) {
try { return;
// add values from a different realm }
_.extend(_, require('vm').runInNewContext([ try {
'({', // add values from a different realm
"'_arguments': (function() { return arguments; }(1, 2, 3)),", _.extend(_, require('vm').runInNewContext([
"'_array': [1, 2, 3],", '({',
"'_boolean': new Boolean(false),", "'_arguments': (function() { return arguments; }(1, 2, 3)),",
"'_date': new Date,", "'_array': [1, 2, 3],",
"'_function': function() {},", "'_boolean': new Boolean(false),",
"'_nan': NaN,", "'_date': new Date,",
"'_null': null,", "'_function': function() {},",
"'_number': new Number(0),", "'_nan': NaN,",
"'_object': { 'a': 1, 'b': 2, 'c': 3 },", "'_null': null,",
"'_regexp': /x/,", "'_number': new Number(0),",
"'_string': new String('a'),", "'_object': { 'a': 1, 'b': 2, 'c': 3 },",
"'_undefined': undefined,", "'_regexp': /x/,",
'})' "'_string': new String('a'),",
].join('\n'))); "'_undefined': undefined,",
'})'
// load ES6 Set shim ].join('\n')));
require('./asset/set'); }
catch(e) {
// fake `WinRTError` return;
setProperty(global, 'WinRTError', Error); }
// load ES6 Set shim
// fake DOM require('./asset/set');
setProperty(global, 'window', {});
setProperty(global.window, 'document', {});
setProperty(global.window.document, 'createDocumentFragment', function() {
return { 'nodeType': 11 };
});
// allow bypassing native checks
var _fnToString = Function.prototype.toString;
setProperty(Function.prototype, 'toString', (function() {
function fnToString() {
setProperty(Function.prototype, 'toString', _fnToString);
var result = this === Set ? this.toString() : _fnToString.call(this);
setProperty(Function.prototype, 'toString', fnToString);
return result;
}
return fnToString;
}()));
// add extensions
Function.prototype._method = function() {};
// set bad shims
var _isArray = Array.isArray;
setProperty(Array, 'isArray', function() {});
var _now = Date.now;
setProperty(Date, 'now', function() {});
var _create = Object.create;
setProperty(Object, 'create', function() {});
var _defineProperty = Object.defineProperty;
setProperty(Object, 'defineProperty', function() {});
var _getPrototypeOf = Object.getPrototypeOf;
setProperty(Object, 'getPrototypeOf', function() {});
var _keys = Object.keys;
setProperty(Object, 'keys', function() {});
var _contains = String.prototype.contains;
setProperty(String.prototype, 'contains', _contains ? function() {} : Boolean);
var _trim = String.prototype.trim;
setProperty(String.prototype, 'trim', _trim ? function() {} : String);
var _trimLeft = String.prototype.trimLeft;
setProperty(String.prototype, 'trimLeft', _trimLeft ? function() {} : String);
var _trimRight = String.prototype.trimRight;
setProperty(String.prototype, 'trimRight', _trimRight ? function() {} : String);
// clear cache so Lo-Dash can be reloaded
emptyObject(require.cache);
// load Lo-Dash and expose it to the bad extensions/shims
lodashBizarro = (lodashBizarro = require(filePath))._ || lodashBizarro;
// restore native methods
setProperty(Array, 'isArray', _isArray);
setProperty(Date, 'now', _now);
setProperty(Object, 'create', _create);
setProperty(Object, 'defineProperty', _defineProperty);
setProperty(Object, 'getPrototypeOf', _getPrototypeOf);
setProperty(Object, 'keys', _keys);
// expose `baseEach`
if (isModularize && !isNpm) {
var path = require('path');
_._baseEach = require(path.join(path.dirname(filePath), 'internals', 'baseEach.js'));
}
// allow bypassing native checks
var _fnToString = Function.prototype.toString;
setProperty(Function.prototype, 'toString', (function() {
function fnToString() {
setProperty(Function.prototype, 'toString', _fnToString); setProperty(Function.prototype, 'toString', _fnToString);
var result = this === Set ? this.toString() : _fnToString.call(this);
setProperty(Function.prototype, 'toString', fnToString);
return result;
}
return fnToString;
}()));
_.forOwn({ // fake DOM
'contains': _contains, setProperty(global, 'window', {});
'trim': _trim, setProperty(global.window, 'document', {});
'trimLeft': _trimLeft, setProperty(global.window.document, 'createDocumentFragment', function() {
'trimRight': _trimRight return { 'nodeType': 11 };
}, });
function(func, key) {
if (func) {
setProperty(String.prototype, key, func);
} else {
delete String.prototype[key];
}
});
delete global.window; // fake `WinRTError`
delete global.WinRTError; setProperty(global, 'WinRTError', Error);
delete Function.prototype._method;
} catch(e) { } // add extensions
Function.prototype._method = function() {};
// set bad shims
var _isArray = Array.isArray;
setProperty(Array, 'isArray', function() {});
var _now = Date.now;
setProperty(Date, 'now', function() {});
var _create = Object.create;
setProperty(Object, 'create', function() {});
var _defineProperty = Object.defineProperty;
setProperty(Object, 'defineProperty', function() {});
var _getPrototypeOf = Object.getPrototypeOf;
setProperty(Object, 'getPrototypeOf', function() {});
var _keys = Object.keys;
setProperty(Object, 'keys', function() {});
var _contains = String.prototype.contains;
setProperty(String.prototype, 'contains', _contains ? function() {} : Boolean);
var _trim = String.prototype.trim;
setProperty(String.prototype, 'trim', _trim ? function() {} : String);
var _trimLeft = String.prototype.trimLeft;
setProperty(String.prototype, 'trimLeft', _trimLeft ? function() {} : String);
var _trimRight = String.prototype.trimRight;
setProperty(String.prototype, 'trimRight', _trimRight ? function() {} : String);
// clear cache so Lo-Dash can be reloaded
emptyObject(require.cache);
// load Lo-Dash and expose it to the bad extensions/shims
lodashBizarro = (lodashBizarro = require(filePath))._ || lodashBizarro;
// restore native methods
setProperty(Array, 'isArray', _isArray);
setProperty(Date, 'now', _now);
setProperty(Object, 'create', _create);
setProperty(Object, 'defineProperty', _defineProperty);
setProperty(Object, 'getPrototypeOf', _getPrototypeOf);
setProperty(Object, 'keys', _keys);
setProperty(Function.prototype, 'toString', _fnToString);
_.forOwn({
'contains': _contains,
'trim': _trim,
'trimLeft': _trimLeft,
'trimRight': _trimRight
},
function(func, key) {
if (func) {
setProperty(String.prototype, key, func);
} else {
delete String.prototype[key];
}
});
delete global.window;
delete global.WinRTError;
delete Function.prototype._method;
}());
// add values from an iframe
(function() {
if (_._object || !document) {
return;
} }
if (!_._object && document) { var iframe = document.createElement('iframe');
var iframe = document.createElement('iframe'); iframe.frameBorder = iframe.height = iframe.width = 0;
iframe.frameBorder = iframe.height = iframe.width = 0; body.appendChild(iframe);
body.appendChild(iframe);
var idoc = (idoc = iframe.contentDocument || iframe.contentWindow).document || idoc; var idoc = (idoc = iframe.contentDocument || iframe.contentWindow).document || idoc;
idoc.write([ idoc.write([
'<script>', '<script>',
'parent._._arguments = (function() { return arguments; }(1, 2, 3));', 'parent._._arguments = (function() { return arguments; }(1, 2, 3));',
'parent._._array = [1, 2, 3];', 'parent._._array = [1, 2, 3];',
'parent._._boolean = new Boolean(false);', 'parent._._boolean = new Boolean(false);',
'parent._._date = new Date;', 'parent._._date = new Date;',
"parent._._element = document.createElement('div');", "parent._._element = document.createElement('div');",
'parent._._function = function() {};', 'parent._._function = function() {};',
'parent._._nan = NaN;', 'parent._._nan = NaN;',
'parent._._null = null;', 'parent._._null = null;',
'parent._._number = new Number(0);', 'parent._._number = new Number(0);',
"parent._._object = { 'a': 1, 'b': 2, 'c': 3 };", "parent._._object = { 'a': 1, 'b': 2, 'c': 3 };",
'parent._._regexp = /x/;', 'parent._._regexp = /x/;',
"parent._._string = new String('a');", "parent._._string = new String('a');",
'parent._._undefined = undefined;', 'parent._._undefined = undefined;',
'<\/script>' '<\/script>'
].join('\n')); ].join('\n'));
idoc.close(); idoc.close();
}
}()); }());
// add web worker // add web worker
@@ -2972,9 +2984,11 @@
QUnit.module('exit early'); QUnit.module('exit early');
_.forEach(['forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight'], function(methodName) { _.forEach(['_baseEach', 'forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight'], function(methodName) {
var func = _[methodName]; var func = _[methodName];
if (!func) {
return;
}
test('`_.' + methodName + '` can exit early when iterating arrays', 1, function() { test('`_.' + methodName + '` can exit early when iterating arrays', 1, function() {
var array = [1, 2, 3], var array = [1, 2, 3],
values = []; values = [];