-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
126 lines (106 loc) · 2.52 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
import { tests, state } from "./tests";
export class Assert
{
public equals(expected: any, actual: any, message?: string)
{
if (expected !== actual)
throw new AssertionError("equals", expected, actual, message);
}
public truthy(value: any, message?: string)
{
if (value != true)
throw new AssertionError("truthy", undefined, undefined, message);
}
public throws(func: Function, message?: string)
{
let threw = false;
try
{
func();
}
catch(e)
{
threw = true;
}
if (!threw)
throw new AssertionError("throws", undefined, undefined, message);
}
private isPrimitive(obj: any)
{
return (obj !== Object(obj));
}
public deepEqual(expected: any, actual: any, message?: string): void
{
if (expected === actual) // it's just the same object. No need to compare.
return;
if (this.isPrimitive(expected) && this.isPrimitive(actual)) // compare primitives
{
if(expected !== actual)
throw new AssertionError("deepEqual", expected, actual, message);
}
if (Object.keys(expected).length !== Object.keys(actual).length)
throw new AssertionError("deepEqual", expected, actual, message);
// compare objects with same number of keys
for (let key in expected)
{
if(!(key in actual)) //other object doesn't have this prop
throw new AssertionError("deepEqual", expected, actual, message);
this.deepEqual(expected[key], actual[key]);
}
}
}
export class AssertionError
{
constructor (public type: "equals" | "truthy" | "throws" | "deepEqual", public expected?: any, public actual?: any, public message?: string) { }
toJSON()
{
return {
type: this.type,
expected: this.expected,
actual: this.actual,
message: this.message,
};
}
static fromJSON(errorObj: ReturnType<AssertionError["toJSON"]>): AssertionError
{
return new AssertionError(errorObj.type, errorObj.expected, errorObj.actual, errorObj.message);
}
}
export interface Test
{
name: string;
dependencies?: string[];
func: (assert: Assert) => Promise<void>;
filePath?: string;
}
export interface TestModule
{
name: string | null;
testInit?: () => void;
tests: Test[];
}
export function registerTest(test: Test): void
{
if (tests.length === 0)
{
tests.push({
name: null,
tests: [],
});
}
tests[tests.length - 1].tests.push({
...test,
filePath: state.currentTestFilePath!,
});
}
export function registerTestInit(func: () => void): void
{
tests[tests.length - 1].testInit = func;
}
export function registerModule(name: string): void
{
tests.push({
name: name,
tests: [],
});
}