• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { AssertionError } from "./assertionError.js"
2
3export class Assert {
4    private static defaultMessage(actual: any, expect: any, flag: boolean = true) {
5        if (flag == true) {
6            return "expected '" + expect + "' ,but was '" + actual + "'.";
7        } else {
8            return "expected not '" + expect + "' ,but was '" + actual + "'.";
9        }
10
11    }
12    static equal(actual: any, expect: any, msg?: string) {
13        if (actual != expect) {
14            throw new AssertionError(msg ? msg : this.defaultMessage(actual, expect));
15        }
16    }
17    static notEqual(actual: any, expect: any, msg?: string) {
18        if (actual == expect) {
19            throw new AssertionError(msg ? msg : this.defaultMessage(actual, expect, false));
20        }
21    }
22    static isTrue(actual: any, msg?: string) {
23        this.equal(actual, true, msg);
24    }
25    static isFalse(flag: any, msg?: string) {
26        this.equal(flag, false, msg);
27    }
28    static isNumber(x: any, msg?: string) {
29        this.equal(typeof x, "number", msg);
30    }
31    static isString(s: any, msg?: string) {
32        this.equal(typeof s, "string", msg);
33    }
34    static isBoolean(s: any, msg?: string) {
35        this.equal(typeof s, "boolean", msg);
36    }
37    static isSymbol(actual: any, msg?: string) {
38        this.equal(typeof actual, "symbol", msg);
39    }
40    static isFunction(fun: any, msg?: string) {
41        this.equal(typeof fun, "function", msg);
42    }
43    static notNULL(v: any, msg?: string) {
44        this.notEqual(v, null, msg);
45    }
46    static isUndefined(actual: any, msg?: string) {
47        this.equal(actual, undefined, msg);
48    }
49   static isObject(obj: any, msg?: string) {
50    this.equal(typeof obj, "object", msg);
51  }
52}