toHaveSize
This commit is contained in:
committed by
Gregg Van Hove
parent
f1eac6fb04
commit
c521b4d47c
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "jasmine-core",
|
"name": "jasmine-core",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"version": "3.5.0",
|
"version": "3.5.1",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/jasmine/jasmine.git"
|
"url": "https://github.com/jasmine/jasmine.git"
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ describe('Matchers (Integration)', function() {
|
|||||||
expect(result.failedExpectations[0].message)
|
expect(result.failedExpectations[0].message)
|
||||||
.withContext('Failed with a thrown error rather than a matcher failure')
|
.withContext('Failed with a thrown error rather than a matcher failure')
|
||||||
.not.toMatch(/^Error: /);
|
.not.toMatch(/^Error: /);
|
||||||
|
expect(result.failedExpectations[0].message)
|
||||||
|
.withContext('Failed with a thrown type error rather than a matcher failure')
|
||||||
|
.not.toMatch(/^TypeError: /);
|
||||||
expect(result.failedExpectations[0].matcherName).withContext('Matcher name')
|
expect(result.failedExpectations[0].matcherName).withContext('Matcher name')
|
||||||
.not.toEqual('');
|
.not.toEqual('');
|
||||||
};
|
};
|
||||||
@@ -477,9 +480,9 @@ describe('Matchers (Integration)', function() {
|
|||||||
verifyFailsWithCustomObjectFormatters({
|
verifyFailsWithCustomObjectFormatters({
|
||||||
formatter: function(val) {
|
formatter: function(val) {
|
||||||
if (val === 5) {
|
if (val === 5) {
|
||||||
return "five"
|
return 'five';
|
||||||
} else if (val === 4) {
|
} else if (val === 4) {
|
||||||
return "four"
|
return 'four';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
expectations: function(env) {
|
expectations: function(env) {
|
||||||
@@ -489,6 +492,72 @@ describe('Matchers (Integration)', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('toHaveSize', function() {
|
||||||
|
verifyPasses(function(env) {
|
||||||
|
env.expect(['a','b']).toHaveSize(2);
|
||||||
|
});
|
||||||
|
verifyFails(function(env) {
|
||||||
|
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() {
|
||||||
verifyPasses(function(env) {
|
verifyPasses(function(env) {
|
||||||
var spy = env.createSpy('spy');
|
var spy = env.createSpy('spy');
|
||||||
|
|||||||
21
spec/core/matchers/toHaveSizeSpec.js
Normal file
21
spec/core/matchers/toHaveSizeSpec.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
describe('toHaveSize', function() {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
it('delegates to equals function', function() {
|
||||||
|
var matchersUtil = {
|
||||||
|
equals: jasmine.createSpy('delegated-equals').and.returnValue(true),
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
@@ -20,6 +20,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
|
|||||||
'toBeUndefined',
|
'toBeUndefined',
|
||||||
'toContain',
|
'toContain',
|
||||||
'toEqual',
|
'toEqual',
|
||||||
|
'toHaveSize',
|
||||||
'toHaveBeenCalled',
|
'toHaveBeenCalled',
|
||||||
'toHaveBeenCalledBefore',
|
'toHaveBeenCalledBefore',
|
||||||
'toHaveBeenCalledTimes',
|
'toHaveBeenCalledTimes',
|
||||||
|
|||||||
79
src/core/matchers/toHaveSize.js
Normal file
79
src/core/matchers/toHaveSize.js
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
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(matchersUtil) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(!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;
|
||||||
|
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 toHaveSize;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user