mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 03:47:50 +00:00
Add bizarro tests for _.parseInt.
This commit is contained in:
@@ -55,7 +55,8 @@
|
|||||||
fnToString = funcProto.toString,
|
fnToString = funcProto.toString,
|
||||||
nativeString = fnToString.call(objectProto.toString),
|
nativeString = fnToString.call(objectProto.toString),
|
||||||
propertyIsEnumerable = objectProto.propertyIsEnumerable,
|
propertyIsEnumerable = objectProto.propertyIsEnumerable,
|
||||||
reToString = /toString/g;
|
reToString = /toString/g,
|
||||||
|
whitespace = ' \t\x0B\f\xA0\ufeff\n\r\u2028\u2029\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000';
|
||||||
|
|
||||||
function constant(value) {
|
function constant(value) {
|
||||||
return function() {
|
return function() {
|
||||||
@@ -155,8 +156,27 @@
|
|||||||
return Float64Array;
|
return Float64Array;
|
||||||
}());
|
}());
|
||||||
}
|
}
|
||||||
|
setProperty(window, '_parseInt', parseInt);
|
||||||
|
setProperty(window, 'parseInt', (function(_parseInt) {
|
||||||
|
var checkStr = whitespace + '08',
|
||||||
|
isFaked = _parseInt(checkStr) != 8,
|
||||||
|
reHexPrefix = /^0[xX]/,
|
||||||
|
reTrim = RegExp('^[' + whitespace + ']+|[' + whitespace + ']+$');
|
||||||
|
|
||||||
|
return function(value, radix) {
|
||||||
|
if (value == checkStr && !isFaked) {
|
||||||
|
isFaked = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
value = String(value == null ? '' : value).replace(reTrim, '');
|
||||||
|
return _parseInt(value, +radix || (reHexPrefix.test(value) ? 16 : 10));
|
||||||
|
};
|
||||||
|
}(_parseInt)));
|
||||||
|
|
||||||
|
// fake `WinRTError`
|
||||||
setProperty(window, 'WinRTError', Error);
|
setProperty(window, 'WinRTError', Error);
|
||||||
|
|
||||||
|
// fake free variable `global`
|
||||||
setProperty(window, 'exports', window);
|
setProperty(window, 'exports', window);
|
||||||
setProperty(window, 'global', window);
|
setProperty(window, 'global', window);
|
||||||
setProperty(window, 'module', {});
|
setProperty(window, 'module', {});
|
||||||
@@ -216,6 +236,10 @@
|
|||||||
ArrayBuffer = _ArrayBuffer;
|
ArrayBuffer = _ArrayBuffer;
|
||||||
}
|
}
|
||||||
setProperty(window, '_ArrayBuffer', undefined);
|
setProperty(window, '_ArrayBuffer', undefined);
|
||||||
|
|
||||||
|
setProperty(window, 'parseInt', window._parseInt);
|
||||||
|
setProperty(window, '_parseInt', undefined);
|
||||||
|
|
||||||
setProperty(window, 'WinRTError', undefined);
|
setProperty(window, 'WinRTError', undefined);
|
||||||
|
|
||||||
setProperty(window, 'exports', undefined);
|
setProperty(window, 'exports', undefined);
|
||||||
|
|||||||
52
test/test.js
52
test/test.js
@@ -422,6 +422,23 @@
|
|||||||
return Float64Array;
|
return Float64Array;
|
||||||
}()));
|
}()));
|
||||||
}
|
}
|
||||||
|
var _parseInt = parseInt;
|
||||||
|
setProperty(root, 'parseInt', (function() {
|
||||||
|
var checkStr = whitespace + '08',
|
||||||
|
isFaked = _parseInt(checkStr) != 8,
|
||||||
|
reHexPrefix = /^0[xX]/,
|
||||||
|
reTrim = RegExp('^[' + whitespace + ']+|[' + whitespace + ']+$');
|
||||||
|
|
||||||
|
return function(value, radix) {
|
||||||
|
if (value == checkStr && !isFaked) {
|
||||||
|
isFaked = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
value = String(value == null ? '' : value).replace(reTrim, '');
|
||||||
|
return _parseInt(value, +radix || (reHexPrefix.test(value) ? 16 : 10));
|
||||||
|
};
|
||||||
|
}()));
|
||||||
|
|
||||||
// fake `WinRTError`
|
// fake `WinRTError`
|
||||||
setProperty(root, 'WinRTError', Error);
|
setProperty(root, 'WinRTError', Error);
|
||||||
|
|
||||||
@@ -447,6 +464,7 @@
|
|||||||
setProperty(Object, 'keys', _keys);
|
setProperty(Object, 'keys', _keys);
|
||||||
|
|
||||||
setProperty(objectProto, 'hasOwnProperty', _hasOwnProperty);
|
setProperty(objectProto, 'hasOwnProperty', _hasOwnProperty);
|
||||||
|
setProperty(root, 'parseInt', _parseInt);
|
||||||
|
|
||||||
if (_isFinite) {
|
if (_isFinite) {
|
||||||
setProperty(Number, 'isFinite', _isFinite);
|
setProperty(Number, 'isFinite', _isFinite);
|
||||||
@@ -7923,16 +7941,34 @@
|
|||||||
strictEqual(_.parseInt('08', 10), 8);
|
strictEqual(_.parseInt('08', 10), 8);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should parse strings with leading whitespace (test in Chrome, Firefox, and Opera)', 8, function() {
|
test('should parse strings with leading whitespace (test in Chrome, Firefox, and Opera)', 2, function() {
|
||||||
strictEqual(_.parseInt(whitespace + '10'), 10);
|
var expected = [8, 8, 10, 10, 32, 32, 32, 32];
|
||||||
strictEqual(_.parseInt(whitespace + '10', 10), 10);
|
|
||||||
|
|
||||||
strictEqual(_.parseInt(whitespace + '08'), 8);
|
_.times(2, function(index) {
|
||||||
strictEqual(_.parseInt(whitespace + '08', 10), 8);
|
var actual = [],
|
||||||
|
func = (index ? (lodashBizarro || {}) : _).parseInt;
|
||||||
|
|
||||||
_.each(['0x20', '0X20'], function(string) {
|
if (func) {
|
||||||
strictEqual(_.parseInt(whitespace + string), 32);
|
_.times(2, function(otherIndex) {
|
||||||
strictEqual(_.parseInt(whitespace + string, 16), 32);
|
var string = otherIndex ? '10' : '08';
|
||||||
|
actual.push(
|
||||||
|
func(whitespace + string, 10),
|
||||||
|
func(whitespace + string)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
_.each(['0x20', '0X20'], function(string) {
|
||||||
|
actual.push(
|
||||||
|
func(whitespace + string),
|
||||||
|
func(whitespace + string, 16)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user