UNPKG

2.78 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const errors_1 = require("../errors");
4const stringification_1 = require("../stringification");
5const diff_1 = require("./diff");
6const spying_1 = require("../spying");
7class Matcher {
8 constructor(actualValue) {
9 this._shouldMatch = true;
10 this._actualValue = actualValue;
11 }
12 get actualValue() {
13 return this._actualValue;
14 }
15 get shouldMatch() {
16 return this._shouldMatch;
17 }
18 get not() {
19 this._shouldMatch = !this.shouldMatch;
20 return this;
21 }
22 toBe(expectedValue) {
23 this._registerMatcher((expectedValue === this._actualValue) === this.shouldMatch, `Expected ${stringification_1.stringify(this.actualValue)} ${!this.shouldMatch ? "not " : ""}` + `to be ${stringification_1.stringify(expectedValue)}.`, expectedValue);
24 }
25 toEqual(expectedValue) {
26 this._checkTypeMatcherEqual(expectedValue, this.toEqualCheck);
27 }
28 toBeDefined() {
29 this._registerMatcher((this._actualValue !== undefined) === this.shouldMatch, `Expected ${stringification_1.stringify(this.actualValue)} ${this.shouldMatch ? "not " : ""}` + `to be undefined.`, undefined);
30 }
31 toBeNull() {
32 this._registerMatcher((this._actualValue === null) === this.shouldMatch, `Expected ${stringification_1.stringify(this.actualValue)} ${!this.shouldMatch ? "not " : ""}` + `to be null.`, null);
33 }
34 toBeTruthy() {
35 this._registerMatcher((this._actualValue && this.shouldMatch) ||
36 (!this._actualValue && !this.shouldMatch), `Expected ${stringification_1.stringify(this.actualValue)} ${!this.shouldMatch ? "not " : ""}to be truthy.`, this.shouldMatch ? "truthy" : "falsy");
37 }
38 _registerMatcher(isMatch, failureMessage, expectedValue, extras) {
39 if (isMatch === false) {
40 throw new errors_1.MatchError(failureMessage, expectedValue, this._actualValue, extras);
41 }
42 }
43 _checkTypeMatcherEqual(expected, alternativeCheck) {
44 if (expected instanceof spying_1.TypeMatcher) {
45 this._registerMatcher(expected.test(this.actualValue) === this._shouldMatch, `Expected values ${!this.shouldMatch ? "not " : ""}to be equal`, expected, {
46 diff: diff_1.diff(expected, this._actualValue)
47 });
48 }
49 else {
50 alternativeCheck.call(this, expected);
51 }
52 }
53 toEqualCheck(expectedValue) {
54 this._registerMatcher((expectedValue == this._actualValue) === this._shouldMatch, `Expected values ${!this.shouldMatch ? "not " : ""}to be equal`, expectedValue, {
55 diff: diff_1.diff(expectedValue, this._actualValue)
56 });
57 }
58}
59exports.Matcher = Matcher;
60//# sourceMappingURL=matcher.js.map
\No newline at end of file