-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuntime.spec.ts
115 lines (107 loc) · 5.9 KB
/
Runtime.spec.ts
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
import { expect } from "chai";
import "mocha";
import { NullPointerException, WrongParameterException, WrongTypeException } from "../errors";
import { MockData } from "../MockData";
import { Runtime } from "./Runtime";
describe("Runtime", () => {
describe("Check variable types", () => {
it("Check array", () => {
expect(Runtime.isArray([1, 2, 3])).to.deep.equal([1, 2, 3]);
expect(Runtime.isArray([])).to.deep.equal([]);
expect(() => Runtime.isArray("Vlado" as any)).to.throw(WrongTypeException);
});
it("Check string", () => {
expect(Runtime.isString("")).to.be.equal("");
expect(Runtime.isString("Name")).to.be.equal("Name");
expect(() => Runtime.isString(21 as any)).to.throw(WrongTypeException);
});
it("Check number", () => {
expect(Runtime.isNumber(21)).to.be.equal(21);
expect(Runtime.isNumber(2.3)).to.be.equal(2.3);
expect(() => Runtime.isNumber("Name" as any)).to.throw(WrongTypeException);
});
it("Check boolean", () => {
expect(Runtime.isBoolean(true)).to.be.true;
expect(Runtime.isBoolean(false)).to.be.false;
expect(() => Runtime.isBoolean("Name" as any)).to.throw(WrongTypeException);
});
it("Check function", () => {
const funcA = (a: number, b: number): number => a + b;
expect(Runtime.isFunction(funcA)).to.be.equal(funcA);
expect(Runtime.isFunction(MockData.functionSum)).to.be.equal(MockData.functionSum);
expect(() => Runtime.isFunction("Name" as any)).to.throw(WrongTypeException);
});
it("Check function validation", () => {
expect(Runtime.checkFunction(() => {
throw new Error();
})).to.be.false;
expect(Runtime.checkFunction(() => 23)).to.be.true;
expect(Runtime.checkFunction(MockData.functionSum, [5, 5])).to.be.true;
expect(Runtime.checkFunction(MockData.functionSum, [5, "gabo" as any])).to.be.false;
});
});
describe("Check math", () => {
it("Check min", () => {
expect(Runtime.min(23, 20)).to.be.equal(23);
expect(Runtime.min(2.3, 2.01)).to.be.equal(2.3);
expect(() => Runtime.min(15, 20)).to.throw(WrongParameterException);
});
it("Check max", () => {
expect(Runtime.max(20, 23)).to.be.equal(20);
expect(Runtime.max(2.01, 2.3)).to.be.equal(2.01);
expect(() => Runtime.max(20, 15)).to.throw(WrongParameterException);
});
});
describe("Check existence", () => {
it("Check exists", () => {
expect(Runtime.exists(234)).to.be.equal(234);
expect(Runtime.exists(true)).to.be.true;
expect(Runtime.exists(false)).to.be.false;
expect(Runtime.exists({a: "a"})).to.deep.equal({a: "a"});
expect(Runtime.exists(MockData.randomArray)).to.deep.equal(MockData.randomArray);
expect(Runtime.exists("Hello")).to.be.equal("Hello");
expect(() => Runtime.exists(undefined)).to.throw(Error);
expect(() => Runtime.exists(null)).to.throw(Error);
expect(() => Runtime.exists("")).to.throw(Error);
expect(Runtime.notNull(234)).to.be.equal(234);
expect(Runtime.notNull(true)).to.be.true;
expect(Runtime.notNull(false)).to.be.false;
expect(Runtime.notNull({a: "a"})).to.deep.equal({a: "a"});
expect(() => Runtime.notNull(null)).to.throw(NullPointerException);
});
it("Check option for disable runtime checkers", () => {
expect(() => Runtime.isArray("Vlado" as any)).to.throw(WrongTypeException);
expect(() => Runtime.isString(21 as any)).to.throw(WrongTypeException);
expect(() => Runtime.isNumber("Name" as any)).to.throw(WrongTypeException);
expect(() => Runtime.isFunction("Name" as any)).to.throw(WrongTypeException);
expect(() => Runtime.min(15, 20)).to.throw(WrongParameterException);
expect(() => Runtime.max(20, 15)).to.throw(WrongParameterException);
expect(() => Runtime.exists(undefined)).to.throw(Error);
expect(() => Runtime.exists(null)).to.throw(Error);
expect(() => Runtime.exists("")).to.throw(Error);
expect(() => Runtime.notNull(null)).to.throw(NullPointerException);
Runtime.useRuntimeExceptions(false);
expect(Runtime.isArray("Vlado" as any)).to.be.equal("Vlado");
expect(Runtime.isString(21 as any)).to.be.equal(21);
expect(Runtime.isNumber("Name" as any)).to.be.equal("Name");
expect(Runtime.isFunction("Name" as any)).to.be.equal("Name");
expect(Runtime.min(15, 20)).to.be.equal(15);
expect(Runtime.max(20, 15)).to.be.equal(20);
expect(Runtime.exists(undefined)).to.be.equal(undefined);
expect(Runtime.exists(null)).to.be.null;
expect(Runtime.exists("")).to.be.equal("");
expect(Runtime.notNull(null)).to.be.null;
Runtime.useRuntimeExceptions(true);
expect(() => Runtime.isArray("Vlado" as any)).to.throw(WrongTypeException);
expect(() => Runtime.isString(21 as any)).to.throw(WrongTypeException);
expect(() => Runtime.isNumber("Name" as any)).to.throw(WrongTypeException);
expect(() => Runtime.isFunction("Name" as any)).to.throw(WrongTypeException);
expect(() => Runtime.min(15, 20)).to.throw(WrongParameterException);
expect(() => Runtime.max(20, 15)).to.throw(WrongParameterException);
expect(() => Runtime.exists(undefined)).to.throw(Error);
expect(() => Runtime.exists(null)).to.throw(Error);
expect(() => Runtime.exists("")).to.throw(Error);
expect(() => Runtime.notNull(null)).to.throw(NullPointerException);
});
});
});