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

@@ -144,6 +144,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
'toBeUndefined', 'toBeUndefined',
'toContain', 'toContain',
'toEqual', 'toEqual',
'toHaveSize',
'toHaveBeenCalled', 'toHaveBeenCalled',
'toHaveBeenCalledBefore', 'toHaveBeenCalledBefore',
'toHaveBeenCalledTimes', 'toHaveBeenCalledTimes',
@@ -5862,6 +5863,49 @@ getJasmineRequireObj().toHaveClass = function(j$) {
return toHaveClass; return toHaveClass;
}; };
getJasmineRequireObj().toHaveSize = function(j$) {
/**
* {@link expect} the actual size to be equal to the expected, using array-like length or object keys size.
* @function
* @name matchers#toHaveSize
* @since 3.5.1
* @param {Object} expected - Expected size
* @example
* array = [1,2];
* expect(array).toHaveSize(2);
*/
function toHaveSize() {
return {
compare: function(actual, expected) {
var result = {
pass: false
};
if (actual instanceof WeakSet || actual instanceof WeakMap || actual instanceof DataView) {
throw new Error('Cannot get size of ' + actual + '.');
}
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;
}
return result;
}
};
}
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;
}
return toHaveSize;
};
getJasmineRequireObj().toMatch = function(j$) { getJasmineRequireObj().toMatch = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toMatch>', 'expect(<expectation>).toMatch(<string> || <regexp>)'); var getErrorMsg = j$.formatErrorMsg('<toMatch>', 'expect(<expectation>).toMatch(<string> || <regexp>)');
@@ -8724,5 +8768,5 @@ getJasmineRequireObj().UserContext = function(j$) {
}; };
getJasmineRequireObj().version = function() { getJasmineRequireObj().version = function() {
return '3.5.0'; return '3.5.1';
}; };

View File

@@ -496,66 +496,10 @@ describe('Matchers (Integration)', function() {
verifyPasses(function(env) { verifyPasses(function(env) {
env.expect(['a','b']).toHaveSize(2); env.expect(['a','b']).toHaveSize(2);
}); });
verifyFails(function(env) { verifyFails(function(env) {
env.expect(['a','b']).toHaveSize(1); env.expect(['a','b']).toHaveSize(1);
}); });
verifyPasses(function(env) {
env.expect({a: 1, b: 2}).toHaveSize(2);
});
verifyFails(function(env) {
env.expect({a: 1, b: 2}).toHaveSize(1);
});
verifyPasses(function(env) {
env.expect({a: 1, b: 2, length: 5}).toHaveSize(5);
});
verifyFails(function(env) {
env.expect({a: 1, b: 2, length: 5}).toHaveSize(1);
});
verifyPasses(function(env) {
env.expect('ab').toHaveSize(2);
});
verifyFails(function(env) {
env.expect('ab').toHaveSize(1);
});
verifyPasses(function(env) {
var map = new Map();
map.set('a',1);
map.set('b',2);
env.expect(map).toHaveSize(2);
});
verifyFails(function(env) {
var map = new Map();
map.set('a',1);
map.set('b',2);
env.expect(map).toHaveSize(1);
});
verifyPasses(function(env) {
var set = new Set();
set.add('a');
set.add('b');
env.expect(set).toHaveSize(2);
});
verifyFails(function(env) {
var set = new Set();
set.add('a');
set.add('b');
env.expect(set).toHaveSize(1);
});
verifyFails(function(env) {
env.expect(new WeakSet()).toHaveSize(1);
});
verifyFails(function(env) {
env.expect(new WeakMap()).toHaveSize(1);
});
verifyFails(function(env) {
env.expect(new DataView(new ArrayBuffer(128))).toHaveSize(1);
});
}); });
describe('toHaveBeenCalled', function() { describe('toHaveBeenCalled', function() {

View File

@@ -1,21 +1,135 @@
describe('toHaveSize', function() { describe('toHaveSize', function() {
'use strict'; 'use strict';
it('delegates to equals function', function() { it('passes for an array whose length matches', function() {
var matchersUtil = { var matcher = jasmineUnderTest.matchers.toHaveSize(),
equals: jasmine.createSpy('delegated-equals').and.returnValue(true), result = matcher.compare([1, 2], 2);
buildFailureMessage: function() {
return 'does not matter';
},
DiffBuilder: new jasmineUnderTest.DiffBuilder()
},
matcher = jasmineUnderTest.matchers.toHaveSize(matchersUtil),
result;
result = matcher.compare([1], 1);
expect(matchersUtil.equals).toHaveBeenCalledWith(1, 1, jasmine.anything(), jasmine.anything());
expect(result.pass).toBe(true); expect(result.pass).toBe(true);
}); });
it('fails for an array whose length does not match', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare([1, 2, 3], 2);
expect(result.pass).toBe(false);
});
it('passes for an object with the proper number of keys', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare({a: 1, b: 2}, 2);
expect(result.pass).toBe(true);
});
it('fails for an object with a different number of keys', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare({a: 1, b: 2}, 1);
expect(result.pass).toBe(false);
});
it('passes for an object with an explicit `length` property that matches', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare({a: 1, b: 2, length: 5}, 5);
expect(result.pass).toBe(true);
});
it('fails for an object with an explicit `length` property that does not match', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare({a: 1, b: 2, length: 5}, 1);
expect(result.pass).toBe(false);
});
it('passes for a string whose length matches', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare('ab', 2);
expect(result.pass).toBe(true);
});
it('fails for a string whose length does not match', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare('abc', 2);
expect(result.pass).toBe(false);
});
it('passes for a Map whose length matches', function() {
jasmine.getEnv().requireFunctioningMaps();
var map = new Map();
map.set('a',1);
map.set('b',2);
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare(map, 2);
expect(result.pass).toBe(true);
});
it('fails for a Map whose length does not match', function() {
jasmine.getEnv().requireFunctioningMaps();
var map = new Map();
map.set('a',1);
map.set('b',2);
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare(map, 1);
expect(result.pass).toBe(false);
});
it('passes for a Set whose length matches', function() {
jasmine.getEnv().requireFunctioningSets();
var set = new Set();
set.add('a');
set.add('b');
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare(set, 2);
expect(result.pass).toBe(true);
});
it('fails for a Set whose length does not match', function() {
jasmine.getEnv().requireFunctioningSets();
var set = new Set();
set.add('a');
set.add('b');
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare(set, 1);
expect(result.pass).toBe(false);
});
it('throws an error for WeakSet', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize();
expect(function() {
matcher.compare(new WeakSet(), 2);
}).toThrowError('Cannot get size of [object WeakSet].');
});
it('throws an error for WeakMap', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize();
expect(function() {
matcher.compare(new WeakMap(), 2);
}).toThrowError('Cannot get size of [object WeakMap].');
});
it('throws an error for DataView', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize();
expect(function() {
matcher.compare(new DataView(new ArrayBuffer(128)), 2);
}).toThrowError('Cannot get size of [object DataView].');
});
}); });

View File

@@ -9,70 +9,33 @@ getJasmineRequireObj().toHaveSize = function(j$) {
* array = [1,2]; * array = [1,2];
* expect(array).toHaveSize(2); * expect(array).toHaveSize(2);
*/ */
function toHaveSize(matchersUtil) { function toHaveSize() {
return { return {
compare: function(actual, expected) { compare: function(actual, expected) {
var result = { var result = {
pass: false 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) {
if (actual instanceof WeakSet throw new Error('Cannot get size of ' + actual + '.');
|| actual instanceof WeakMap
|| actual instanceof DataView) {
result.message = 'Cannot get size of ' + actual + '.';
return result;
} }
// Ref https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects if (actual instanceof Set || actual instanceof Map) {
if (Array.isArray(actual) || isArrayLike(actual)) result.pass = actual.size === expected;
result.pass = matchersUtil.equals(actual.length, expected, simpleEqualityTesters, diffBuilder); } else if (isLength(actual.length)) {
else if ( actual instanceof String || typeof actual === 'string') result.pass = actual.length === expected;
result.pass = matchersUtil.equals(actual.length, expected, simpleEqualityTesters, diffBuilder); } else {
else if (actual instanceof Set || actual instanceof Map) result.pass = Object.keys(actual).length === expected;
result.pass = matchersUtil.equals(actual.size, expected, simpleEqualityTesters, diffBuilder); }
// instanceof Object
else
result.pass = matchersUtil.equals(Object.keys(actual).length, expected, simpleEqualityTesters, diffBuilder);
if(!result.pass)
result.message = diffBuilder.getMessage() ;
return result; return result;
} }
}; };
} }
/** var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
* 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;
function isLength(value) { function isLength(value) {
return (typeof value == 'number') && value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER; 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 toHaveSize; return toHaveSize;