all files / core/ expect.ts

76% Statements 57/75
38.89% Branches 14/36
57.89% Functions 11/19
78.87% Lines 56/71
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133    436×         436×   109× 109×     436×     135× 79×       51× 27×                 36× 13×       46× 23×                                               120× 120×   120× 120×     50× 50×     120×         42× 42×   42× 42×     42×   42× 42×       42×                          
import { MatchError} from "./errors/match-error";
import { ExactMatchError } from "./errors/exact-match-error";
import { EqualMatchError } from "./errors/equal-match-error";
import { RegexMatchError } from "./errors/regex-match-error";
import { TruthyMatchError } from "./errors/truthy-match-error";
import { ContentsMatchError } from "./errors/contents-match-error";
import { LessThanMatchError } from "./errors/less-than-match-error";
import { GreaterThanMatchError } from "./errors/greater-than-match-error";
import { ErrorMatchError } from "./errors/error-match-error";
import { FunctionCallMatchError } from "./errors/function-call-match-error";
 
export function Expect(actualValue: any){
  return new Matcher(actualValue);
}
 
class Matcher {
 
  private _actualValue: any;
  private _shouldMatch: boolean = true;
 
  public get not(): Matcher {
    this._shouldMatch = !this._shouldMatch;
    return this;
  }
 
  public constructor(actualValue: any) {
    this._actualValue = actualValue;
  }
 
  public toBe(expectedValue: any) {
    if (expectedValue !== this._actualValue === this._shouldMatch) {
      throw new ExactMatchError(this._actualValue, expectedValue, this._shouldMatch);
    }
  }
 
  public toEqual(expectedValue: any) {
    if (expectedValue != this._actualValue === this._shouldMatch) {
      throw new EqualMatchError(this._actualValue, expectedValue, this._shouldMatch);
    }
  }
 
  public toMatch(regex: any) {
    if (!regex.test(this._actualValue) === this._shouldMatch) {
      throw new RegexMatchError(this._actualValue, regex, this._shouldMatch);
    }
  }
 
  public toBeDefined() {
    if (this._actualValue === undefined === this._shouldMatch) {
      throw new ExactMatchError(this._actualValue, undefined, !this._shouldMatch);
    }
  }
 
  public toBeNull() {
    if (this._actualValue !== null === this._shouldMatch) {
      throw new ExactMatchError(this._actualValue, null, this._shouldMatch);
    }
  }
 
  public toBeTruthy() {
    if ((this._actualValue && !this._shouldMatch) || (!this._actualValue && this._shouldMatch)) {
      throw new TruthyMatchError(this._actualValue, this._shouldMatch);
    }
  }
 
  public toContain(expectedContent: any) {
    if (this._actualValue.indexOf(expectedContent) === -1 === this._shouldMatch) {
      throw new ContentsMatchError(this._actualValue, expectedContent, this._shouldMatch);
    }
  }
 
  public toBeLessThan(upperLimit: any) {
    if (this._actualValue > upperLimit === this._shouldMatch) {
      throw new LessThanMatchError(this._actualValue, upperLimit, this._shouldMatch);
    }
  }
 
  public toBeGreaterThan(lowerLimit: any) {
    if (this._actualValue < lowerLimit === this._shouldMatch) {
      throw new GreaterThanMatchError(this._actualValue, lowerLimit, this._shouldMatch);
    }
  }
 
  public toThrow() {
    let threwError = false;
    let actualError: Error;
 
    try {
      this._actualValue();
    }
    catch(error) {
      actualError = error;
      threwError = true;
    }
 
    Iif (!threwError === this._shouldMatch) {
      throw new ErrorMatchError(actualError, this._shouldMatch);
    }
  }
 
  public toThrowError(errorType: (...args: Array<any>) => Error, errorMessage: string) {
    let threwRightError = false;
    let actualError: Error;
 
    try {
      this._actualValue();
    }
    catch(error) {
      actualError = error;
 
      Eif (error instanceof errorType && error.message === errorMessage) {
        threwRightError = true;
      }
    }
 
    Iif (!threwRightError === this._shouldMatch) {
      throw new ErrorMatchError(actualError, this._shouldMatch, (<any>errorType), errorMessage);
    }
  }
 
  public toHaveBeenCalled() {
    Iif (this._actualValue.calls.length === 0 === this._shouldMatch) {
      throw new FunctionCallMatchError(this._actualValue, this._shouldMatch);
    }
  }
 
  public toHaveBeenCalledWith(...args: Array<any>) {
    if (this._actualValue.calls.filter((call: any) => call.args.filter((arg: any, index: number) => arg === args[index]) && call.args.length === args.length).length === 0 === this._shouldMatch) {
      throw new FunctionCallMatchError(this._actualValue, this._shouldMatch, args);
    }
  }
}