From 78f8a03da544b07449ade3829bbc47ec0a0e9b7e Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 31 Dec 2013 00:23:30 -0600 Subject: [PATCH] Ensure the `trim` fallback is tested and counted for code coverage. --- test/index.html | 18 ++++++++++++++++++ test/test.js | 26 ++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/test/index.html b/test/index.html index 40463eeb2..f4d575aa3 100644 --- a/test/index.html +++ b/test/index.html @@ -44,6 +44,9 @@ String.prototype._contains = String.prototype.contains; String.prototype.contains = String.prototype._contains ? function() {} : Boolean; + String.prototype._trim = String.prototype.trim; + String.prototype.trim = function() {}; + window.WinRTError = Error; document._createDocumentFragment = document.createDocumentFragment; @@ -86,6 +89,20 @@ } else { delete String.prototype.contains; } + if (String.prototype._trim) { + if (Object.defineProperty) { + Object.defineProperty(String.prototype, 'trim', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': String.prototype._trim + }); + } else { + String.prototype.trim = String.prototype._trim; + } + } else { + delete String.prototype.trim; + } window.WinRTError = undefined; document.createDocumentFragment = document._createDocumentFragment; @@ -99,6 +116,7 @@ delete Object._getPrototypeOf; delete Object._keys; delete String.prototype._contains; + delete String.prototype._trim; } addBizarroMethods(); diff --git a/test/test.js b/test/test.js index c02c933ca..76f19f13a 100644 --- a/test/test.js +++ b/test/test.js @@ -264,6 +264,9 @@ var _contains = String.prototype.contains; String.prototype.contains = _contains ? function() {} : Boolean; + var _trim = String.prototype.trim; + String.prototype.trim = _trim ? function() {} : Boolean; + // load Lo-Dash and expose it to the bad extensions/shims lodashBizarro = (lodashBizarro = require(filePath))._ || lodashBizarro; @@ -280,6 +283,18 @@ } else { delete String.prototype.contains; } + if (_trim) { + // avoid a bug where overwriting non-enumerable built-ins makes them enumerable + // https://code.google.com/p/v8/issues/detail?id=1623 + defineProperty(String.prototype, 'trim', { + 'configurable': true, + 'enumerable': false, + 'writable': true, + 'value': _trim + }); + } else { + delete String.prototype.trim; + } delete global.window; delete global.WinRTError; delete Function.prototype._method; @@ -388,7 +403,7 @@ } }); - test('should avoid overwritten native methods', 8, function() { + test('should avoid overwritten native methods', 9, function() { function Foo() {} function message(methodName) { @@ -446,9 +461,16 @@ actual = null; } strictEqual(actual, true, message('String#contains')); + + try { + actual = lodashBizarro.parseInt(' 08 '); + } catch(e) { + actual = null; + } + strictEqual(actual, 8, message('String#trim')); } else { - skipTest(8); + skipTest(9); } }); }());