Clean up toHaveSize

This commit is contained in:
Gregg Van Hove
2020-03-18 08:12:40 -07:00
parent c521b4d47c
commit ec3ebcb7bb
4 changed files with 263 additions and 198 deletions

View File

@@ -9,70 +9,33 @@ getJasmineRequireObj().toHaveSize = function(j$) {
* array = [1,2];
* expect(array).toHaveSize(2);
*/
function toHaveSize(matchersUtil) {
function toHaveSize() {
return {
compare: function(actual, expected) {
var result = {
pass: false
},
simpleEqualityTesters = [function(a, b) {
return a === b;
}],
diffBuilder = j$.DiffBuilder();
};
// Avoid misleading collections size matching
if (actual instanceof WeakSet
|| actual instanceof WeakMap
|| actual instanceof DataView) {
result.message = 'Cannot get size of ' + actual + '.';
return result;
if (actual instanceof WeakSet || actual instanceof WeakMap || actual instanceof DataView) {
throw new Error('Cannot get size of ' + actual + '.');
}
// Ref https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
if (Array.isArray(actual) || isArrayLike(actual))
result.pass = matchersUtil.equals(actual.length, expected, simpleEqualityTesters, diffBuilder);
else if ( actual instanceof String || typeof actual === 'string')
result.pass = matchersUtil.equals(actual.length, expected, simpleEqualityTesters, diffBuilder);
else if (actual instanceof Set || actual instanceof Map)
result.pass = matchersUtil.equals(actual.size, expected, simpleEqualityTesters, diffBuilder);
// instanceof Object
else
result.pass = matchersUtil.equals(Object.keys(actual).length, expected, simpleEqualityTesters, diffBuilder);
if (actual instanceof Set || actual instanceof Map) {
result.pass = actual.size === expected;
} else if (isLength(actual.length)) {
result.pass = actual.length === expected;
} else {
result.pass = Object.keys(actual).length === expected;
}
if(!result.pass)
result.message = diffBuilder.getMessage() ;
return result;
}
};
}
/**
* Checks if `value` is array-like. A value is considered array-like if it's
* not a function and has a `value.length` that's an integer greater than or
* equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
* From lodash
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is array-like, else `false`.
* @example
* _.isArrayLike([1, 2, 3]);
* // => true
* _.isArrayLike(document.body.children);
* // => true
* _.isArrayLike('abc');
* // => true
* _.isArrayLike(_.noop);
* // => false
*/
function isArrayLike(value) {
return value != null && isLength(value.length) && !isFunction(value);
}
var MAX_SAFE_INTEGER = 9007199254740991;
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
function isLength(value) {
return (typeof value == 'number') && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
}
var functionTags = ['[object Function]','[object GeneratorFunction]','[object AsyncFunction]','[object Proxy]'];
function isFunction(functionToCheck) {
return functionToCheck && functionTags.indexOf( Object.prototype.toString.call(functionToCheck) ) != -1;
return (typeof value == 'number') && value > -1 && value % 1 === 0 && value <= MAX_SAFE_INTEGER;
}
return toHaveSize;