all files / core/ expect.js

36.36% Statements 28/77
0% Branches 0/36
5.26% Functions 1/19
37.33% Lines 28/75
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                                                                                                                                     
"use strict";
var exact_match_error_1 = require("./errors/exact-match-error");
var equal_match_error_1 = require("./errors/equal-match-error");
var regex_match_error_1 = require("./errors/regex-match-error");
var truthy_match_error_1 = require("./errors/truthy-match-error");
var contents_match_error_1 = require("./errors/contents-match-error");
var less_than_match_error_1 = require("./errors/less-than-match-error");
var greater_than_match_error_1 = require("./errors/greater-than-match-error");
var error_match_error_1 = require("./errors/error-match-error");
var function_call_match_error_1 = require("./errors/function-call-match-error");
function Expect(actualValue) {
    return new Matcher(actualValue);
}
exports.Expect = Expect;
var Matcher = (function () {
    function Matcher(actualValue) {
        this._shouldMatch = true;
        this._actualValue = actualValue;
    }
    Object.defineProperty(Matcher.prototype, "not", {
        get: function () {
            this._shouldMatch = !this._shouldMatch;
            return this;
        },
        enumerable: true,
        configurable: true
    });
    Matcher.prototype.toBe = function (expectedValue) {
        if (expectedValue !== this._actualValue === this._shouldMatch) {
            throw new exact_match_error_1.ExactMatchError(this._actualValue, expectedValue, this._shouldMatch);
        }
    };
    Matcher.prototype.toEqual = function (expectedValue) {
        if (expectedValue != this._actualValue === this._shouldMatch) {
            throw new equal_match_error_1.EqualMatchError(this._actualValue, expectedValue, this._shouldMatch);
        }
    };
    Matcher.prototype.toMatch = function (regex) {
        if (!regex.test(this._actualValue) === this._shouldMatch) {
            throw new regex_match_error_1.RegexMatchError(this._actualValue, regex, this._shouldMatch);
        }
    };
    Matcher.prototype.toBeDefined = function () {
        if (this._actualValue === undefined === this._shouldMatch) {
            throw new exact_match_error_1.ExactMatchError(this._actualValue, undefined, !this._shouldMatch);
        }
    };
    Matcher.prototype.toBeNull = function () {
        if (this._actualValue !== null === this._shouldMatch) {
            throw new exact_match_error_1.ExactMatchError(this._actualValue, null, this._shouldMatch);
        }
    };
    Matcher.prototype.toBeTruthy = function () {
        if ((this._actualValue && !this._shouldMatch) || (!this._actualValue && this._shouldMatch)) {
            throw new truthy_match_error_1.TruthyMatchError(this._actualValue, this._shouldMatch);
        }
    };
    Matcher.prototype.toContain = function (expectedContent) {
        if (this._actualValue.indexOf(expectedContent) === -1 === this._shouldMatch) {
            throw new contents_match_error_1.ContentsMatchError(this._actualValue, expectedContent, this._shouldMatch);
        }
    };
    Matcher.prototype.toBeLessThan = function (upperLimit) {
        if (this._actualValue > upperLimit === this._shouldMatch) {
            throw new less_than_match_error_1.LessThanMatchError(this._actualValue, upperLimit, this._shouldMatch);
        }
    };
    Matcher.prototype.toBeGreaterThan = function (lowerLimit) {
        if (this._actualValue < lowerLimit === this._shouldMatch) {
            throw new greater_than_match_error_1.GreaterThanMatchError(this._actualValue, lowerLimit, this._shouldMatch);
        }
    };
    Matcher.prototype.toThrow = function () {
        var threwError = false;
        var actualError;
        try {
            this._actualValue();
        }
        catch (error) {
            actualError = error;
            threwError = true;
        }
        if (!threwError === this._shouldMatch) {
            throw new error_match_error_1.ErrorMatchError(actualError, this._shouldMatch);
        }
    };
    Matcher.prototype.toThrowError = function (errorType, errorMessage) {
        var threwRightError = false;
        var actualError;
        try {
            this._actualValue();
        }
        catch (error) {
            actualError = error;
            if (error instanceof errorType && error.message === errorMessage) {
                threwRightError = true;
            }
        }
        if (!threwRightError === this._shouldMatch) {
            throw new error_match_error_1.ErrorMatchError(actualError, this._shouldMatch, errorType, errorMessage);
        }
    };
    Matcher.prototype.toHaveBeenCalled = function () {
        if (this._actualValue.calls.length === 0 === this._shouldMatch) {
            throw new function_call_match_error_1.FunctionCallMatchError(this._actualValue, this._shouldMatch);
        }
    };
    Matcher.prototype.toHaveBeenCalledWith = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i - 0] = arguments[_i];
        }
        if (this._actualValue.calls.filter(function (call) { return call.args.filter(function (arg, index) { return arg === args[index]; }) && call.args.length === args.length; }).length === 0 === this._shouldMatch) {
            throw new function_call_match_error_1.FunctionCallMatchError(this._actualValue, this._shouldMatch, args);
        }
    };
    return Matcher;
}());
//# sourceMappingURL=expect.js.map