toHaveSize
This commit is contained in:
committed by
Gregg Van Hove
parent
f1eac6fb04
commit
c521b4d47c
@@ -290,9 +290,9 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
|
|||||||
return typeof jasmineGlobal.Node !== 'undefined'
|
return typeof jasmineGlobal.Node !== 'undefined'
|
||||||
? obj instanceof jasmineGlobal.Node
|
? obj instanceof jasmineGlobal.Node
|
||||||
: obj !== null &&
|
: obj !== null &&
|
||||||
typeof obj === 'object' &&
|
typeof obj === 'object' &&
|
||||||
typeof obj.nodeType === 'number' &&
|
typeof obj.nodeType === 'number' &&
|
||||||
typeof obj.nodeName === 'string';
|
typeof obj.nodeName === 'string';
|
||||||
// return obj.nodeType > 0;
|
// return obj.nodeType > 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -763,7 +763,7 @@ getJasmineRequireObj().Spec = function(j$) {
|
|||||||
onComplete: function() {
|
onComplete: function() {
|
||||||
onComplete(
|
onComplete(
|
||||||
self.result.status === 'failed' &&
|
self.result.status === 'failed' &&
|
||||||
new j$.StopExecutionError('spec failed')
|
new j$.StopExecutionError('spec failed')
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
userContext: this.userContext()
|
userContext: this.userContext()
|
||||||
@@ -828,8 +828,8 @@ getJasmineRequireObj().Spec = function(j$) {
|
|||||||
this.result.failedExpectations.length > 0 ||
|
this.result.failedExpectations.length > 0 ||
|
||||||
(failSpecWithNoExpectations &&
|
(failSpecWithNoExpectations &&
|
||||||
this.result.failedExpectations.length +
|
this.result.failedExpectations.length +
|
||||||
this.result.passedExpectations.length ===
|
this.result.passedExpectations.length ===
|
||||||
0)
|
0)
|
||||||
) {
|
) {
|
||||||
return 'failed';
|
return 'failed';
|
||||||
}
|
}
|
||||||
@@ -1170,7 +1170,7 @@ getJasmineRequireObj().Env = function(j$) {
|
|||||||
}
|
}
|
||||||
runnableResources[
|
runnableResources[
|
||||||
currentRunnable().id
|
currentRunnable().id
|
||||||
].defaultStrategyFn = defaultStrategyFn;
|
].defaultStrategyFn = defaultStrategyFn;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.addSpyStrategy = function(name, fn) {
|
this.addSpyStrategy = function(name, fn) {
|
||||||
@@ -2565,7 +2565,7 @@ getJasmineRequireObj().ObjectContaining = function(j$) {
|
|||||||
|
|
||||||
for (var property in this.sample) {
|
for (var property in this.sample) {
|
||||||
if (!hasProperty(other, property) ||
|
if (!hasProperty(other, property) ||
|
||||||
!matchersUtil.equals(this.sample[property], other[property])) {
|
!matchersUtil.equals(this.sample[property], other[property])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3373,7 +3373,7 @@ getJasmineRequireObj().DelayedFunctionScheduler = function(j$) {
|
|||||||
// scheduled in a funcToRun from forcing an extra iteration
|
// scheduled in a funcToRun from forcing an extra iteration
|
||||||
currentTime !== endTime &&
|
currentTime !== endTime &&
|
||||||
scheduledLookup[0] <= endTime
|
scheduledLookup[0] <= endTime
|
||||||
);
|
);
|
||||||
|
|
||||||
// ran out of functions to call, but still time left on the clock
|
// ran out of functions to call, but still time left on the clock
|
||||||
if (currentTime !== endTime) {
|
if (currentTime !== endTime) {
|
||||||
@@ -4108,24 +4108,24 @@ getJasmineRequireObj().toBeRejectedWith = function(j$) {
|
|||||||
|
|
||||||
return actualPromise.then(
|
return actualPromise.then(
|
||||||
function() {
|
function() {
|
||||||
return {
|
|
||||||
pass: false,
|
|
||||||
message: prefix(false) + ' but it was resolved.'
|
|
||||||
};
|
|
||||||
},
|
|
||||||
function(actualValue) {
|
|
||||||
if (matchersUtil.equals(actualValue, expectedValue)) {
|
|
||||||
return {
|
|
||||||
pass: true,
|
|
||||||
message: prefix(true) + '.'
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return {
|
return {
|
||||||
pass: false,
|
pass: false,
|
||||||
message: prefix(false) + ' but it was rejected with ' + matchersUtil.pp(actualValue) + '.'
|
message: prefix(false) + ' but it was resolved.'
|
||||||
};
|
};
|
||||||
|
},
|
||||||
|
function(actualValue) {
|
||||||
|
if (matchersUtil.equals(actualValue, expectedValue)) {
|
||||||
|
return {
|
||||||
|
pass: true,
|
||||||
|
message: prefix(true) + '.'
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
pass: false,
|
||||||
|
message: prefix(false) + ' but it was rejected with ' + matchersUtil.pp(actualValue) + '.'
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -4494,9 +4494,9 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
|
|||||||
|
|
||||||
MatchersUtil.prototype.asymmetricMatch_ = function(a, b, aStack, bStack, customTesters, diffBuilder) {
|
MatchersUtil.prototype.asymmetricMatch_ = function(a, b, aStack, bStack, customTesters, diffBuilder) {
|
||||||
var asymmetricA = j$.isAsymmetricEqualityTester_(a),
|
var asymmetricA = j$.isAsymmetricEqualityTester_(a),
|
||||||
asymmetricB = j$.isAsymmetricEqualityTester_(b),
|
asymmetricB = j$.isAsymmetricEqualityTester_(b),
|
||||||
shim,
|
shim,
|
||||||
result;
|
result;
|
||||||
|
|
||||||
if (asymmetricA === asymmetricB) {
|
if (asymmetricA === asymmetricB) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -4731,7 +4731,7 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
|
|||||||
// otherwise explicitly look up the mapKey in the other Map since we want keys with unique
|
// otherwise explicitly look up the mapKey in the other Map since we want keys with unique
|
||||||
// obj identity (that are otherwise equal) to not match.
|
// obj identity (that are otherwise equal) to not match.
|
||||||
if (j$.isAsymmetricEqualityTester_(mapKey) || j$.isAsymmetricEqualityTester_(cmpKey) &&
|
if (j$.isAsymmetricEqualityTester_(mapKey) || j$.isAsymmetricEqualityTester_(cmpKey) &&
|
||||||
this.eq_(mapKey, cmpKey, aStack, bStack, customTesters, j$.NullDiffBuilder())) {
|
this.eq_(mapKey, cmpKey, aStack, bStack, customTesters, j$.NullDiffBuilder())) {
|
||||||
mapValueB = b.get(cmpKey);
|
mapValueB = b.get(cmpKey);
|
||||||
} else {
|
} else {
|
||||||
mapValueB = b.get(mapKey);
|
mapValueB = b.get(mapKey);
|
||||||
@@ -4800,9 +4800,9 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
|
|||||||
// or `Array`s from different frames are.
|
// or `Array`s from different frames are.
|
||||||
var aCtor = a.constructor, bCtor = b.constructor;
|
var aCtor = a.constructor, bCtor = b.constructor;
|
||||||
if (aCtor !== bCtor &&
|
if (aCtor !== bCtor &&
|
||||||
isFunction(aCtor) && isFunction(bCtor) &&
|
isFunction(aCtor) && isFunction(bCtor) &&
|
||||||
a instanceof aCtor && b instanceof bCtor &&
|
a instanceof aCtor && b instanceof bCtor &&
|
||||||
!(aCtor instanceof aCtor && bCtor instanceof bCtor)) {
|
!(aCtor instanceof aCtor && bCtor instanceof bCtor)) {
|
||||||
|
|
||||||
diffBuilder.recordMismatch(constructorsAreDifferentFormatter.bind(null, this.pp));
|
diffBuilder.recordMismatch(constructorsAreDifferentFormatter.bind(null, this.pp));
|
||||||
return false;
|
return false;
|
||||||
@@ -4849,13 +4849,13 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
|
|||||||
function keys(obj, isArray) {
|
function keys(obj, isArray) {
|
||||||
var allKeys = Object.keys ? Object.keys(obj) :
|
var allKeys = Object.keys ? Object.keys(obj) :
|
||||||
(function(o) {
|
(function(o) {
|
||||||
var keys = [];
|
var keys = [];
|
||||||
for (var key in o) {
|
for (var key in o) {
|
||||||
if (j$.util.has(o, key)) {
|
if (j$.util.has(o, key)) {
|
||||||
keys.push(key);
|
keys.push(key);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return keys;
|
}
|
||||||
|
return keys;
|
||||||
})(obj);
|
})(obj);
|
||||||
|
|
||||||
if (!isArray) {
|
if (!isArray) {
|
||||||
@@ -4863,7 +4863,7 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (allKeys.length === 0) {
|
if (allKeys.length === 0) {
|
||||||
return allKeys;
|
return allKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
var extraKeys = [];
|
var extraKeys = [];
|
||||||
@@ -4882,10 +4882,10 @@ getJasmineRequireObj().MatchersUtil = function(j$) {
|
|||||||
|
|
||||||
function objectKeysAreDifferentFormatter(pp, actual, expected, path) {
|
function objectKeysAreDifferentFormatter(pp, actual, expected, path) {
|
||||||
var missingProperties = j$.util.objectDifference(expected, actual),
|
var missingProperties = j$.util.objectDifference(expected, actual),
|
||||||
extraProperties = j$.util.objectDifference(actual, expected),
|
extraProperties = j$.util.objectDifference(actual, expected),
|
||||||
missingPropertiesMessage = formatKeyValuePairs(pp, missingProperties),
|
missingPropertiesMessage = formatKeyValuePairs(pp, missingProperties),
|
||||||
extraPropertiesMessage = formatKeyValuePairs(pp, extraProperties),
|
extraPropertiesMessage = formatKeyValuePairs(pp, extraProperties),
|
||||||
messages = [];
|
messages = [];
|
||||||
|
|
||||||
if (!path.depth()) {
|
if (!path.depth()) {
|
||||||
path = 'object';
|
path = 'object';
|
||||||
@@ -5302,15 +5302,15 @@ getJasmineRequireObj().toBeInstanceOf = function(j$) {
|
|||||||
return {
|
return {
|
||||||
compare: function(actual, expected) {
|
compare: function(actual, expected) {
|
||||||
var actualType = actual && actual.constructor ? j$.fnNameFor(actual.constructor) : matchersUtil.pp(actual),
|
var actualType = actual && actual.constructor ? j$.fnNameFor(actual.constructor) : matchersUtil.pp(actual),
|
||||||
expectedType = expected ? j$.fnNameFor(expected) : matchersUtil.pp(expected),
|
expectedType = expected ? j$.fnNameFor(expected) : matchersUtil.pp(expected),
|
||||||
expectedMatcher,
|
expectedMatcher,
|
||||||
pass;
|
pass;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
expectedMatcher = new j$.Any(expected);
|
expectedMatcher = new j$.Any(expected);
|
||||||
pass = expectedMatcher.asymmetricMatch(actual);
|
pass = expectedMatcher.asymmetricMatch(actual);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(usageError('Expected value is not a constructor function'));
|
throw new Error(usageError('Expected value is not a constructor function'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pass) {
|
if (pass) {
|
||||||
@@ -5805,7 +5805,7 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
var diffs = actual.calls.allArgs().map(function(argsForCall, callIx) {
|
var diffs = actual.calls.allArgs().map(function(argsForCall, callIx) {
|
||||||
var diffBuilder = new j$.DiffBuilder();
|
var diffBuilder = new j$.DiffBuilder();
|
||||||
matchersUtil.equals(argsForCall, expectedArgs, diffBuilder);
|
matchersUtil.equals(argsForCall, expectedArgs, diffBuilder);
|
||||||
return 'Call ' + callIx + ':\n' +
|
return 'Call ' + callIx + ':\n' +
|
||||||
diffBuilder.getMessage().replace(/^/mg, ' ');
|
diffBuilder.getMessage().replace(/^/mg, ' ');
|
||||||
@@ -6050,7 +6050,7 @@ getJasmineRequireObj().toThrowError = function(j$) {
|
|||||||
|
|
||||||
function thrownDescription(thrown) {
|
function thrownDescription(thrown) {
|
||||||
var thrownName = errorType ? j$.fnNameFor(thrown.constructor) : 'an exception',
|
var thrownName = errorType ? j$.fnNameFor(thrown.constructor) : 'an exception',
|
||||||
thrownMessage = '';
|
thrownMessage = '';
|
||||||
|
|
||||||
if (expected) {
|
if (expected) {
|
||||||
thrownMessage = ' with message ' + matchersUtil.pp(thrown.message);
|
thrownMessage = ' with message ' + matchersUtil.pp(thrown.message);
|
||||||
@@ -6157,10 +6157,10 @@ getJasmineRequireObj().toThrowMatching = function(j$) {
|
|||||||
if (predicate(thrown)) {
|
if (predicate(thrown)) {
|
||||||
return pass('Expected function not to throw an exception matching a predicate.');
|
return pass('Expected function not to throw an exception matching a predicate.');
|
||||||
} else {
|
} else {
|
||||||
return fail(function() {
|
return fail(function() {
|
||||||
return 'Expected function to throw an exception matching a predicate, ' +
|
return 'Expected function to throw an exception matching a predicate, ' +
|
||||||
'but it threw ' + thrownDescription(thrown) + '.';
|
'but it threw ' + thrownDescription(thrown) + '.';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -6378,8 +6378,8 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
|||||||
} else if (j$.util.arrayContains(this.seen, value)) {
|
} else if (j$.util.arrayContains(this.seen, value)) {
|
||||||
this.emitScalar(
|
this.emitScalar(
|
||||||
'<circular reference: ' +
|
'<circular reference: ' +
|
||||||
(j$.isArray_(value) ? 'Array' : 'Object') +
|
(j$.isArray_(value) ? 'Array' : 'Object') +
|
||||||
'>'
|
'>'
|
||||||
);
|
);
|
||||||
} else if (j$.isArray_(value) || j$.isA_('Object', value)) {
|
} else if (j$.isArray_(value) || j$.isA_('Object', value)) {
|
||||||
this.seen.push(value);
|
this.seen.push(value);
|
||||||
@@ -6648,14 +6648,14 @@ getJasmineRequireObj().makePrettyPrinter = function(j$) {
|
|||||||
var allKeys = Object.keys
|
var allKeys = Object.keys
|
||||||
? Object.keys(obj)
|
? Object.keys(obj)
|
||||||
: (function(o) {
|
: (function(o) {
|
||||||
var keys = [];
|
var keys = [];
|
||||||
for (var key in o) {
|
for (var key in o) {
|
||||||
if (j$.util.has(o, key)) {
|
if (j$.util.has(o, key)) {
|
||||||
keys.push(key);
|
keys.push(key);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return keys;
|
}
|
||||||
})(obj);
|
return keys;
|
||||||
|
})(obj);
|
||||||
|
|
||||||
if (!isArray) {
|
if (!isArray) {
|
||||||
return allKeys;
|
return allKeys;
|
||||||
@@ -6841,11 +6841,11 @@ getJasmineRequireObj().QueueRunner = function(j$) {
|
|||||||
timeoutId = self.setTimeout(function() {
|
timeoutId = self.setTimeout(function() {
|
||||||
var error = new Error(
|
var error = new Error(
|
||||||
'Timeout - Async function did not complete within ' +
|
'Timeout - Async function did not complete within ' +
|
||||||
timeoutInterval +
|
timeoutInterval +
|
||||||
'ms ' +
|
'ms ' +
|
||||||
(queueableFn.timeout
|
(queueableFn.timeout
|
||||||
? '(custom timeout)'
|
? '(custom timeout)'
|
||||||
: '(set by jasmine.DEFAULT_TIMEOUT_INTERVAL)')
|
: '(set by jasmine.DEFAULT_TIMEOUT_INTERVAL)')
|
||||||
);
|
);
|
||||||
onException(error);
|
onException(error);
|
||||||
next();
|
next();
|
||||||
@@ -7563,10 +7563,10 @@ getJasmineRequireObj().Spy = function(j$) {
|
|||||||
if (argsStrategies.any() && !baseStrategy.isConfigured()) {
|
if (argsStrategies.any() && !baseStrategy.isConfigured()) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Spy '" +
|
"Spy '" +
|
||||||
strategyArgs.name +
|
strategyArgs.name +
|
||||||
"' received a call with arguments " +
|
"' received a call with arguments " +
|
||||||
j$.pp(Array.prototype.slice.call(args)) +
|
j$.pp(Array.prototype.slice.call(args)) +
|
||||||
' but all configured strategies specify other arguments.'
|
' but all configured strategies specify other arguments.'
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
strategy = baseStrategy;
|
strategy = baseStrategy;
|
||||||
@@ -7789,8 +7789,8 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
|
|||||||
throw new Error(
|
throw new Error(
|
||||||
getErrorMsg(
|
getErrorMsg(
|
||||||
'spyOn could not find an object to spy upon for ' +
|
'spyOn could not find an object to spy upon for ' +
|
||||||
propertyName +
|
propertyName +
|
||||||
''
|
''
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -7815,9 +7815,9 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
|
|||||||
throw new Error(
|
throw new Error(
|
||||||
getErrorMsg(
|
getErrorMsg(
|
||||||
'Property ' +
|
'Property ' +
|
||||||
propertyName +
|
propertyName +
|
||||||
' does not have access type ' +
|
' does not have access type ' +
|
||||||
accessType
|
accessType
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -7948,7 +7948,7 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
|||||||
if (!Promise) {
|
if (!Promise) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
name +
|
name +
|
||||||
' requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`'
|
' requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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