UNPKG

alsatian

Version:

TypeScript and JavaScript testing framework for beautiful and readable tests

60 lines 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("../errors"); const stringification_1 = require("../stringification"); const diff_1 = require("./diff"); const spying_1 = require("../spying"); class Matcher { constructor(actualValue) { this._shouldMatch = true; this._actualValue = actualValue; } get actualValue() { return this._actualValue; } get shouldMatch() { return this._shouldMatch; } get not() { this._shouldMatch = !this.shouldMatch; return this; } toBe(expectedValue) { this._registerMatcher((expectedValue === this._actualValue) === this.shouldMatch, `Expected ${stringification_1.stringify(this.actualValue)} ${!this.shouldMatch ? "not " : ""}` + `to be ${stringification_1.stringify(expectedValue)}.`, expectedValue); } toEqual(expectedValue) { this._checkTypeMatcherEqual(expectedValue, this.toEqualCheck); } toBeDefined() { this._registerMatcher((this._actualValue !== undefined) === this.shouldMatch, `Expected ${stringification_1.stringify(this.actualValue)} ${this.shouldMatch ? "not " : ""}` + `to be undefined.`, undefined); } toBeNull() { this._registerMatcher((this._actualValue === null) === this.shouldMatch, `Expected ${stringification_1.stringify(this.actualValue)} ${!this.shouldMatch ? "not " : ""}` + `to be null.`, null); } toBeTruthy() { this._registerMatcher((this._actualValue && this.shouldMatch) || (!this._actualValue && !this.shouldMatch), `Expected ${stringification_1.stringify(this.actualValue)} ${!this.shouldMatch ? "not " : ""}to be truthy.`, this.shouldMatch ? "truthy" : "falsy"); } _registerMatcher(isMatch, failureMessage, expectedValue, extras) { if (isMatch === false) { throw new errors_1.MatchError(failureMessage, expectedValue, this._actualValue, extras); } } _checkTypeMatcherEqual(expected, alternativeCheck) { if (expected instanceof spying_1.TypeMatcher) { this._registerMatcher(expected.test(this.actualValue) === this._shouldMatch, `Expected values ${!this.shouldMatch ? "not " : ""}to be equal`, expected, { diff: diff_1.diff(expected, this._actualValue) }); } else { alternativeCheck.call(this, expected); } } toEqualCheck(expectedValue) { this._registerMatcher((expectedValue == this._actualValue) === this._shouldMatch, `Expected values ${!this.shouldMatch ? "not " : ""}to be equal`, expectedValue, { diff: diff_1.diff(expectedValue, this._actualValue) }); } } exports.Matcher = Matcher; //# sourceMappingURL=matcher.js.map