Cleanup lodashBizarro code to get coverage up in AMD modules.

This commit is contained in:
John-David Dalton
2014-01-22 23:09:42 -08:00
parent cb44479420
commit 7b8a23453f
2 changed files with 47 additions and 25 deletions

View File

@@ -14,6 +14,7 @@
<script src="../vendor/qunit/qunit/qunit.js"></script> <script src="../vendor/qunit/qunit/qunit.js"></script>
<script src="../vendor/qunit-extras/qunit-extras.js"></script> <script src="../vendor/qunit-extras/qunit-extras.js"></script>
<script src="../vendor/platform.js/platform.js"></script> <script src="../vendor/platform.js/platform.js"></script>
<script src="./asset/set.js"></script>
<script src="./asset/test-ui.js"></script> <script src="./asset/test-ui.js"></script>
<div id="qunit"></div> <div id="qunit"></div>
<div id="exports"></div> <div id="exports"></div>
@@ -51,14 +52,11 @@
Function.prototype._method = function() {}; Function.prototype._method = function() {};
// allow bypassing native checks // allow bypassing native checks
setProperty(Function.prototype, 'toString', (function() { var _toString = Function.prototype.toString;
function toString() { setProperty(Function.prototype, '_toString', _toString);
return this.toString !== toString ? this.toString() : func.call(this); setProperty(Function.prototype, 'toString', function() {
} return this === Set ? this.toString() : _toString.call(this);
var func = Function.prototype.toString; });
setProperty(Function.prototype, '_toString', func);
return toString;
}()));
// set bad shims // set bad shims
setProperty(Array, '_isArray', Array.isArray); setProperty(Array, '_isArray', Array.isArray);
@@ -91,7 +89,6 @@
setProperty(String.prototype, '_trimRight', String.prototype.trimRight); setProperty(String.prototype, '_trimRight', String.prototype.trimRight);
setProperty(String.prototype, 'trimRight', String.prototype._trimRight ? function() {} : String); setProperty(String.prototype, 'trimRight', String.prototype._trimRight ? function() {} : String);
setProperty(window, '_Set', window.Set);
setProperty(window, 'WinRTError', Error); setProperty(window, 'WinRTError', Error);
setProperty(document, '_createDocumentFragment', document.createDocumentFragment); setProperty(document, '_createDocumentFragment', document.createDocumentFragment);
@@ -129,11 +126,6 @@
} else { } else {
delete Object.keys; delete Object.keys;
} }
if (window._Set) {
setProperty(window, 'Set', _Set);
} else {
setProperty(window, 'Set', undefined);
}
for (var key in { for (var key in {
'contains': true, 'contains': true,
'trim': true, 'trim': true,
@@ -164,11 +156,11 @@
delete Object._keys; delete Object._keys;
} }
addBizarroMethods();
// load Lo-Dash and expose it to the bad extensions/shims // load Lo-Dash and expose it to the bad extensions/shims
document.write('<script src="./asset/set.js"><\/script>'); if (!ui.isModularize) {
document.write('<script src="' + (ui.isModularize ? '../lodash.js' : ui.buildPath) + '"><\/script>'); addBizarroMethods();
document.write('<script src="' + ui.buildPath + '"><\/script>');
}
</script> </script>
<script> <script>
QUnit.config.hidepassed = true; QUnit.config.hidepassed = true;
@@ -180,10 +172,11 @@
}); });
// store Lo-Dash to test for bad extensions/shims // store Lo-Dash to test for bad extensions/shims
var lodashBizarro = window._; if (!ui.isModularize) {
window._ = undefined; var lodashBizarro = window._;
removeBizarroMethods(); window._ = undefined;
removeBizarroMethods();
}
// load Lo-Dash and test scripts // load Lo-Dash and test scripts
document.write(ui.urlParams.loader == 'none' document.write(ui.urlParams.loader == 'none'
? '<script src="' + ui.buildPath + '"><\/script><script src="test.js"><\/script>' ? '<script src="' + ui.buildPath + '"><\/script><script src="test.js"><\/script>'

View File

@@ -240,6 +240,7 @@
(function() { (function() {
if (!amd) { if (!amd) {
try { try {
// add values from a different realm
_.extend(_, require('vm').runInNewContext([ _.extend(_, require('vm').runInNewContext([
'({', '({',
"'_arguments': (function() { return arguments; }(1, 2, 3)),", "'_arguments': (function() { return arguments; }(1, 2, 3)),",
@@ -257,6 +258,9 @@
'})' '})'
].join('\n'))); ].join('\n')));
// load ES6 Set shim
require('./asset/set');
// fake `WinRTError` // fake `WinRTError`
setProperty(global, 'WinRTError', Error); setProperty(global, 'WinRTError', Error);
@@ -267,6 +271,12 @@
return { 'nodeType': 11 }; return { 'nodeType': 11 };
}); });
// allow bypassing native checks
var _toString = Function.prototype.toString;
setProperty(Function.prototype, 'toString', function() {
return this === Set ? this.toString() : _toString.call(this);
});
// add extensions // add extensions
Function.prototype._method = function() {}; Function.prototype._method = function() {};
@@ -301,8 +311,10 @@
var _trimRight = String.prototype.trimRight; var _trimRight = String.prototype.trimRight;
setProperty(String.prototype, 'trimRight', _trimRight ? function() {} : String); setProperty(String.prototype, 'trimRight', _trimRight ? function() {} : String);
// load Lo-Dash and expose it to the bad extensions/shims // clear cache so Lo-Dash can be reloaded
emptyObject(require.cache); emptyObject(require.cache);
// load Lo-Dash and expose it to the bad extensions/shims
lodashBizarro = (lodashBizarro = require(filePath))._ || lodashBizarro; lodashBizarro = (lodashBizarro = require(filePath))._ || lodashBizarro;
// restore native methods // restore native methods
@@ -313,6 +325,8 @@
setProperty(Object, 'getPrototypeOf', _getPrototypeOf); setProperty(Object, 'getPrototypeOf', _getPrototypeOf);
setProperty(Object, 'keys', _keys); setProperty(Object, 'keys', _keys);
setProperty(Function.prototype, 'toString', _toString);
_.forOwn({ _.forOwn({
'contains': _contains, 'contains': _contains,
'trim': _trim, 'trim': _trim,
@@ -435,7 +449,7 @@
} }
}); });
test('should avoid overwritten native methods', 11, function() { test('should avoid overwritten native methods', 12, function() {
function Foo() {} function Foo() {}
function message(methodName) { function message(methodName) {
@@ -443,6 +457,10 @@
} }
var object = { 'a': true }; var object = { 'a': true };
var largeArray = _.times(LARGE_ARRAY_SIZE, function() {
return object;
});
if (lodashBizarro) { if (lodashBizarro) {
try { try {
actual = [lodashBizarro.isArray([]), lodashBizarro.isArray({ 'length': 0 })]; actual = [lodashBizarro.isArray([]), lodashBizarro.isArray({ 'length': 0 })];
@@ -487,6 +505,17 @@
} }
deepEqual(actual, [['a'], []], message('Object.keys')); deepEqual(actual, [['a'], []], message('Object.keys'));
try {
actual = [
lodashBizarro.difference([object], largeArray),
lodashBizarro.intersection(largeArray, [object]),
lodashBizarro.uniq(largeArray)
];
} catch(e) {
actual = null;
}
deepEqual(actual, [[], [object], [object]], message('Set'));
try { try {
actual = lodashBizarro.contains('abc', 'c'); actual = lodashBizarro.contains('abc', 'c');
} catch(e) { } catch(e) {
@@ -509,7 +538,7 @@
}); });
} }
else { else {
skipTest(11); skipTest(12);
} }
}); });
}()); }());