alsatian
Version:
TypeScript and JavaScript testing framework for beautiful and readable tests
76 lines • 3.83 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const function_spy_call_count_matcher_1 = require("./function-spy-call-count-matcher");
const spy_call_count_type_1 = require("./spy-call-count-type");
const errors_1 = require("../errors");
const stringification_1 = require("../stringification");
class FunctionSpyMatcher {
constructor(spy, expectedArguments) {
this._expectedArguments = null;
if (spy === null || spy === undefined) {
throw new TypeError("spy must not be null or undefined.");
}
if (expectedArguments) {
this._expectedArguments = expectedArguments;
}
this._spy = spy;
}
exactly(expectedCallCount) {
return this._match(count => count !== expectedCallCount, expectedCallCount, "expectedCallCount", spy_call_count_type_1.SpyCallCountType.Exactly, true);
}
anythingBut(unexpectedCallCount) {
return this._match(count => count === unexpectedCallCount, unexpectedCallCount, "unexpectedCallCount", spy_call_count_type_1.SpyCallCountType.Exactly, false);
}
greaterThan(minimumCallCount) {
return this._match(count => count <= minimumCallCount, minimumCallCount, "minimumCallCount", spy_call_count_type_1.SpyCallCountType.GreaterThan, true);
}
lessThan(maximumCallCount) {
return this._match(count => count >= maximumCallCount, maximumCallCount, "maximumCallCount", spy_call_count_type_1.SpyCallCountType.LessThan, true);
}
_validateCallCount(callCount, callCountName) {
if (callCount < 1) {
throw new TypeError(`${callCountName} must be greater than 0.`);
}
}
_matchingCallsCount() {
if (this._expectedArguments === null) {
return this._spy.calls.length;
}
return this._matchingArguments();
}
_matchingArguments() {
return this._spy.callsWithArguments.apply(this._spy, this._expectedArguments).length;
}
_match(countIsNotCorrect, callCount, callCountName, callCountType, shouldMatch) {
this._validateCallCount(callCount, callCountName);
const actualCallCount = this._matchingCallsCount();
if (countIsNotCorrect(actualCallCount)) {
throw new errors_1.MatchError(`Expected function ${!shouldMatch ? "not " : ""}to be called` +
`${this._expectedArguments ? " with " + stringification_1.stringify(this._expectedArguments) : ""}` +
`${this.stringifyExpectedCallCount(callCount, callCountType)}.`, `function ${!shouldMatch ? "not " : ""}to be called${this.stringifyExpectedCallCount(callCount, callCountType)}.`, `function was called ${this.stringifyCallCount(actualCallCount)}.`, {
actualCallCount: stringification_1.stringify(actualCallCount),
expectedCallCount: this.stringifyExpectedCallCount(callCount, callCountType),
expectedArguments: stringification_1.stringify(this._expectedArguments)
});
}
return new function_spy_call_count_matcher_1.FunctionSpyCallCountMatcher();
}
stringifyExpectedCallCount(callCount, callCountType) {
return `${this.stringifyCallCountType(callCountType)} ${this.stringifyCallCount(callCount)}`;
}
stringifyCallCount(callCount) {
return `${callCount} time${callCount === 1 ? "" : "s"}`;
}
stringifyCallCountType(callCountType) {
switch (callCountType) {
case spy_call_count_type_1.SpyCallCountType.GreaterThan:
return " greater than";
case spy_call_count_type_1.SpyCallCountType.LessThan:
return " less than";
default:
return "";
}
}
}
exports.FunctionSpyMatcher = FunctionSpyMatcher;
//# sourceMappingURL=function-spy-matcher.js.map
;