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

View File

@@ -240,6 +240,7 @@
(function() {
if (!amd) {
try {
// add values from a different realm
_.extend(_, require('vm').runInNewContext([
'({',
"'_arguments': (function() { return arguments; }(1, 2, 3)),",
@@ -257,6 +258,9 @@
'})'
].join('\n')));
// load ES6 Set shim
require('./asset/set');
// fake `WinRTError`
setProperty(global, 'WinRTError', Error);
@@ -267,6 +271,12 @@
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
Function.prototype._method = function() {};
@@ -301,8 +311,10 @@
var _trimRight = String.prototype.trimRight;
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);
// load Lo-Dash and expose it to the bad extensions/shims
lodashBizarro = (lodashBizarro = require(filePath))._ || lodashBizarro;
// restore native methods
@@ -313,6 +325,8 @@
setProperty(Object, 'getPrototypeOf', _getPrototypeOf);
setProperty(Object, 'keys', _keys);
setProperty(Function.prototype, 'toString', _toString);
_.forOwn({
'contains': _contains,
'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 message(methodName) {
@@ -443,6 +457,10 @@
}
var object = { 'a': true };
var largeArray = _.times(LARGE_ARRAY_SIZE, function() {
return object;
});
if (lodashBizarro) {
try {
actual = [lodashBizarro.isArray([]), lodashBizarro.isArray({ 'length': 0 })];
@@ -487,6 +505,17 @@
}
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 {
actual = lodashBizarro.contains('abc', 'c');
} catch(e) {
@@ -509,7 +538,7 @@
});
}
else {
skipTest(11);
skipTest(12);
}
});
}());