as-test 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,337 +4,362 @@ import { Node } from "./node";
4
4
  import { Verdict, after_each_callback, before_each_callback } from "..";
5
5
 
6
6
  export class Expectation<T> extends Node {
7
- public verdict: Verdict = Verdict.Unreachable;
8
- private left: T;
9
- private _left: string | null = null;
10
- private right: u64 = 0;
11
- private _right: string | null = null;
12
- private _not: boolean = false;
13
- private op: string = "=";
14
- constructor(left: T) {
15
- super();
16
- this.left = left;
17
- }
18
- get not(): Expectation<T> {
19
- this._not = true;
20
- return this;
21
- }
7
+ public verdict: Verdict = Verdict.Unreachable;
8
+ private left: T;
9
+ private _left: string | null = null;
10
+ private right: u64 = 0;
11
+ private _right: string | null = null;
12
+ private _not: boolean = false;
13
+ private op: string = "=";
14
+ constructor(left: T) {
15
+ super();
16
+ this.left = left;
17
+ }
18
+ get not(): Expectation<T> {
19
+ this._not = true;
20
+ return this;
21
+ }
22
+
23
+ /**
24
+ * Tests if a == null
25
+ * @returns - void
26
+ */
27
+ toBeNull(): void {
28
+ this.verdict =
29
+ isNullable<T>() && changetype<usize>(this.left)
30
+ ? Verdict.Ok
31
+ : Verdict.Fail;
22
32
 
23
- /**
24
- * Tests if a == null
25
- * @returns - void
26
- */
27
- toBeNull(): void {
28
- this.verdict = (isNullable<T>() && changetype<usize>(this.left)) ? Verdict.Ok : Verdict.Fail;
33
+ // @ts-ignore
34
+ store<T>(changetype<usize>(this), null, offsetof<Expectation<T>>("right"));
29
35
 
30
- // @ts-ignore
31
- store<T>(changetype<usize>(this), null, offsetof<Expectation<T>>("right"));
36
+ this.op = "=";
32
37
 
33
- this.op = "="
38
+ // @ts-ignore
39
+ if (after_each_callback) after_each_callback();
40
+ // @ts-ignore
41
+ if (before_each_callback) before_each_callback();
42
+ }
34
43
 
35
- // @ts-ignore
36
- if (after_each_callback) after_each_callback();
37
- // @ts-ignore
38
- if (before_each_callback) before_each_callback();
39
- }
44
+ /**
45
+ * Tests if a > b
46
+ * @param number equals - The value to test
47
+ * @returns - void
48
+ */
49
+ toBeGreaterThan(value: T): void {
50
+ if (!isInteger<T>() && !isFloat<T>())
51
+ ERROR("toBeGreaterThan() can only be used on number types!");
40
52
 
41
- /**
42
- * Tests if a > b
43
- * @param number equals - The value to test
44
- * @returns - void
45
- */
46
- toBeGreaterThan(value: T): void {
47
- if (!isInteger<T>() && !isFloat<T>()) ERROR("toBeGreaterThan() can only be used on number types!");
53
+ this.verdict = this.left > value ? Verdict.Ok : Verdict.Fail;
54
+ store<T>(changetype<usize>(this), value, offsetof<Expectation<T>>("right"));
48
55
 
49
- this.verdict = this.left > value ? Verdict.Ok : Verdict.Fail;
50
- store<T>(changetype<usize>(this), value, offsetof<Expectation<T>>("right"));
56
+ this.op = ">";
51
57
 
52
- this.op = ">";
58
+ // @ts-ignore
59
+ if (after_each_callback) after_each_callback();
60
+ // @ts-ignore
61
+ if (before_each_callback) before_each_callback();
62
+ }
53
63
 
54
- // @ts-ignore
55
- if (after_each_callback) after_each_callback();
56
- // @ts-ignore
57
- if (before_each_callback) before_each_callback();
58
- }
64
+ /**
65
+ * Tests if a >= b
66
+ * @param number equals - The value to test
67
+ * @returns - void
68
+ */
69
+ toBeGreaterOrEqualTo(value: T): void {
70
+ if (!isInteger<T>() && !isFloat<T>())
71
+ ERROR("toBeGreaterOrEqualTo() can only be used on number types!");
59
72
 
60
- /**
61
- * Tests if a >= b
62
- * @param number equals - The value to test
63
- * @returns - void
64
- */
65
- toBeGreaterOrEqualTo(value: T): void {
66
- if (!isInteger<T>() && !isFloat<T>()) ERROR("toBeGreaterOrEqualTo() can only be used on number types!");
73
+ this.verdict = this.left >= value ? Verdict.Ok : Verdict.Fail;
74
+ store<T>(changetype<usize>(this), value, offsetof<Expectation<T>>("right"));
67
75
 
68
- this.verdict = this.left >= value ? Verdict.Ok : Verdict.Fail;
69
- store<T>(changetype<usize>(this), value, offsetof<Expectation<T>>("right"));
76
+ this.op = ">=";
70
77
 
71
- this.op = ">=";
78
+ // @ts-ignore
79
+ if (after_each_callback) after_each_callback();
80
+ // @ts-ignore
81
+ if (before_each_callback) before_each_callback();
82
+ }
72
83
 
73
- // @ts-ignore
74
- if (after_each_callback) after_each_callback();
75
- // @ts-ignore
76
- if (before_each_callback) before_each_callback();
77
- }
84
+ /**
85
+ * Tests if a < b
86
+ * @param number equals - The value to test
87
+ * @returns - void
88
+ */
89
+ toBeLessThan(value: T): void {
90
+ if (!isInteger<T>() && !isFloat<T>())
91
+ ERROR("toBeLessThan() can only be used on number types!");
78
92
 
79
- /**
80
- * Tests if a < b
81
- * @param number equals - The value to test
82
- * @returns - void
83
- */
84
- toBeLessThan(value: T): void {
85
- if (!isInteger<T>() && !isFloat<T>()) ERROR("toBeLessThan() can only be used on number types!");
93
+ this.verdict = this.left < value ? Verdict.Ok : Verdict.Fail;
94
+ store<T>(changetype<usize>(this), value, offsetof<Expectation<T>>("right"));
86
95
 
87
- this.verdict = this.left < value ? Verdict.Ok : Verdict.Fail;
88
- store<T>(changetype<usize>(this), value, offsetof<Expectation<T>>("right"));
96
+ this.op = "<";
89
97
 
90
- this.op = "<";
98
+ // @ts-ignore
99
+ if (after_each_callback) after_each_callback();
100
+ // @ts-ignore
101
+ if (before_each_callback) before_each_callback();
102
+ }
91
103
 
92
- // @ts-ignore
93
- if (after_each_callback) after_each_callback();
94
- // @ts-ignore
95
- if (before_each_callback) before_each_callback();
96
- }
104
+ /**
105
+ * Tests if a <= b
106
+ * @param number equals - The value to test
107
+ * @returns - void
108
+ */
109
+ toBeLessThanOrEqualTo(value: T): void {
110
+ if (!isInteger<T>() && !isFloat<T>())
111
+ ERROR("toBeLessThanOrEqualTo() can only be used on number types!");
97
112
 
98
- /**
99
- * Tests if a <= b
100
- * @param number equals - The value to test
101
- * @returns - void
102
- */
103
- toBeLessThanOrEqualTo(value: T): void {
104
- if (!isInteger<T>() && !isFloat<T>()) ERROR("toBeLessThanOrEqualTo() can only be used on number types!");
113
+ this.verdict = this.left <= value ? Verdict.Ok : Verdict.Fail;
114
+ store<T>(changetype<usize>(this), value, offsetof<Expectation<T>>("right"));
105
115
 
106
- this.verdict = this.left <= value ? Verdict.Ok : Verdict.Fail;
107
- store<T>(changetype<usize>(this), value, offsetof<Expectation<T>>("right"));
116
+ this.op = "<=";
108
117
 
109
- this.op = "<=";
118
+ // @ts-ignore
119
+ if (after_each_callback) after_each_callback();
120
+ // @ts-ignore
121
+ if (before_each_callback) before_each_callback();
122
+ }
110
123
 
111
- // @ts-ignore
112
- if (after_each_callback) after_each_callback();
113
- // @ts-ignore
114
- if (before_each_callback) before_each_callback();
115
- }
124
+ /**
125
+ * Tests if a is string
126
+ * @returns - void
127
+ */
128
+ toBeString(): void {
129
+ this.verdict = isString<T>() ? Verdict.Ok : Verdict.Fail;
116
130
 
117
- /**
118
- * Tests if a is string
119
- * @returns - void
120
- */
121
- toBeString(): void {
122
- this.verdict = isString<T>() ? Verdict.Ok : Verdict.Fail;
131
+ this._left = nameof<T>();
132
+ this._right = "string";
123
133
 
124
- this._left = nameof<T>();
125
- this._right = "string";
134
+ this.op = "type";
126
135
 
127
- this.op = "type";
136
+ // @ts-ignore
137
+ if (after_each_callback) after_each_callback();
138
+ // @ts-ignore
139
+ if (before_each_callback) before_each_callback();
140
+ }
128
141
 
129
- // @ts-ignore
130
- if (after_each_callback) after_each_callback();
131
- // @ts-ignore
132
- if (before_each_callback) before_each_callback();
133
- }
142
+ /**
143
+ * Tests if a is boolean
144
+ * @returns - void
145
+ */
146
+ toBeBoolean(): void {
147
+ this.verdict = isBoolean<T>() ? Verdict.Ok : Verdict.Fail;
134
148
 
135
- /**
136
- * Tests if a is boolean
137
- * @returns - void
138
- */
139
- toBeBoolean(): void {
140
- this.verdict = isBoolean<T>() ? Verdict.Ok : Verdict.Fail;
149
+ this._left = nameof<T>();
150
+ this._right = "boolean";
141
151
 
142
- this._left = nameof<T>();
143
- this._right = "boolean";
152
+ this.op = "type";
144
153
 
145
- this.op = "type";
154
+ // @ts-ignore
155
+ if (after_each_callback) after_each_callback();
156
+ // @ts-ignore
157
+ if (before_each_callback) before_each_callback();
158
+ }
146
159
 
147
- // @ts-ignore
148
- if (after_each_callback) after_each_callback();
149
- // @ts-ignore
150
- if (before_each_callback) before_each_callback();
151
- }
160
+ /**
161
+ * Tests if a is array
162
+ * @returns - void
163
+ */
164
+ toBeArray(): void {
165
+ this.verdict = isArray<T>() ? Verdict.Ok : Verdict.Fail;
152
166
 
153
- /**
154
- * Tests if a is array
155
- * @returns - void
156
- */
157
- toBeArray(): void {
158
- this.verdict = isArray<T>() ? Verdict.Ok : Verdict.Fail;
167
+ this._left = nameof<T>();
168
+ this._right = "Array<any>";
159
169
 
160
- this._left = nameof<T>();
161
- this._right = "Array<any>";
170
+ this.op = "type";
162
171
 
163
- this.op = "type";
172
+ // @ts-ignore
173
+ if (after_each_callback) after_each_callback();
174
+ // @ts-ignore
175
+ if (before_each_callback) before_each_callback();
176
+ }
164
177
 
165
- // @ts-ignore
166
- if (after_each_callback) after_each_callback();
167
- // @ts-ignore
168
- if (before_each_callback) before_each_callback();
169
- }
178
+ /**
179
+ * Tests if a is number
180
+ * @returns - void
181
+ */
182
+ toBeNumber(): void {
183
+ this.verdict = isFloat<T>() || isInteger<T>() ? Verdict.Ok : Verdict.Fail;
170
184
 
171
- /**
172
- * Tests if a is number
173
- * @returns - void
174
- */
175
- toBeNumber(): void {
176
- this.verdict = (isFloat<T>() || isInteger<T>()) ? Verdict.Ok : Verdict.Fail;
185
+ this._left = nameof<T>();
186
+ this._right = "number";
177
187
 
178
- this._left = nameof<T>();
179
- this._right = "number";
188
+ this.op = "type";
180
189
 
181
- this.op = "type";
190
+ // @ts-ignore
191
+ if (after_each_callback) after_each_callback();
192
+ // @ts-ignore
193
+ if (before_each_callback) before_each_callback();
194
+ }
182
195
 
183
- // @ts-ignore
184
- if (after_each_callback) after_each_callback();
185
- // @ts-ignore
186
- if (before_each_callback) before_each_callback();
187
- }
196
+ /**
197
+ * Tests if a is integer
198
+ * @returns - void
199
+ */
200
+ toBeInteger(): void {
201
+ this.verdict = isInteger<T>() ? Verdict.Ok : Verdict.Fail;
188
202
 
189
- /**
190
- * Tests if a is integer
191
- * @returns - void
192
- */
193
- toBeInteger(): void {
194
- this.verdict = isInteger<T>() ? Verdict.Ok : Verdict.Fail;
203
+ this._left = nameof<T>();
204
+ this._right = "float";
195
205
 
196
- this._left = nameof<T>();
197
- this._right = "float";
206
+ this.op = "type";
198
207
 
199
- this.op = "type";
208
+ // @ts-ignore
209
+ if (after_each_callback) after_each_callback();
210
+ // @ts-ignore
211
+ if (before_each_callback) before_each_callback();
212
+ }
200
213
 
201
- // @ts-ignore
202
- if (after_each_callback) after_each_callback();
203
- // @ts-ignore
204
- if (before_each_callback) before_each_callback();
205
- }
214
+ /**
215
+ * Tests if a is float
216
+ * @returns - void
217
+ */
218
+ toBeFloat(): void {
219
+ this.verdict = isFloat<T>() ? Verdict.Ok : Verdict.Fail;
206
220
 
207
- /**
208
- * Tests if a is float
209
- * @returns - void
210
- */
211
- toBeFloat(): void {
212
- this.verdict = isFloat<T>() ? Verdict.Ok : Verdict.Fail;
221
+ this._left = nameof<T>();
222
+ this._right = "integer";
213
223
 
214
- this._left = nameof<T>();
215
- this._right = "integer";
224
+ this.op = "type";
216
225
 
217
- this.op = "type";
226
+ // @ts-ignore
227
+ if (after_each_callback) after_each_callback();
228
+ // @ts-ignore
229
+ if (before_each_callback) before_each_callback();
230
+ }
231
+
232
+ /**
233
+ * Tests if a is finite
234
+ * @returns - void
235
+ */
236
+ toBeFinite(): void {
237
+ // @ts-ignore
238
+ this.verdict =
239
+ (isFloat<T>() || isInteger<T>()) && isFinite(this.left)
240
+ ? Verdict.Ok
241
+ : Verdict.Fail;
218
242
 
219
- // @ts-ignore
220
- if (after_each_callback) after_each_callback();
221
- // @ts-ignore
222
- if (before_each_callback) before_each_callback();
223
- }
243
+ this._left = "Infinity";
244
+ this._right = "Finite";
224
245
 
225
- /**
226
- * Tests if a is finite
227
- * @returns - void
228
- */
229
- toBeFinite(): void {
230
- // @ts-ignore
231
- this.verdict = ((isFloat<T>() || isInteger<T>()) && isFinite(this.left)) ? Verdict.Ok : Verdict.Fail;
246
+ this.op = "=";
232
247
 
233
- this._left = "Infinity";
234
- this._right = "Finite";
248
+ // @ts-ignore
249
+ if (after_each_callback) after_each_callback();
250
+ // @ts-ignore
251
+ if (before_each_callback) before_each_callback();
252
+ }
253
+
254
+ /**
255
+ * Tests if an array has length x
256
+ *
257
+ * @param {i32} value - The value to check
258
+ * @returns - void
259
+ */
260
+ toHaveLength(value: i32): void {
261
+ // @ts-ignore
262
+ this.verdict =
263
+ isArray<T>() && this.left.length == value ? Verdict.Ok : Verdict.Fail;
235
264
 
236
- this.op = "=";
265
+ // @ts-ignore
266
+ this._left = this.left.length.toString();
267
+ this._right = value.toString();
237
268
 
238
- // @ts-ignore
239
- if (after_each_callback) after_each_callback();
240
- // @ts-ignore
241
- if (before_each_callback) before_each_callback();
242
- }
269
+ this.op = "length";
243
270
 
244
- /**
245
- * Tests if an array has length x
246
- *
247
- * @param {i32} value - The value to check
248
- * @returns - void
249
- */
250
- toHaveLength(value: i32): void {
251
- // @ts-ignore
252
- this.verdict = (isArray<T>() && this.left.length == value) ? Verdict.Ok : Verdict.Fail;
253
-
254
- // @ts-ignore
255
- this._left = this.left.length.toString();
256
- this._right = value.toString();
257
-
258
- this.op = "length";
259
-
260
- // @ts-ignore
261
- if (after_each_callback) after_each_callback();
262
- // @ts-ignore
263
- if (before_each_callback) before_each_callback();
264
- }
271
+ // @ts-ignore
272
+ if (after_each_callback) after_each_callback();
273
+ // @ts-ignore
274
+ if (before_each_callback) before_each_callback();
275
+ }
276
+
277
+ /**
278
+ * Tests if an array contains an element
279
+ *
280
+ * @param { valueof<T> } value - The value to check
281
+ * @returns - void
282
+ */
283
+ // @ts-ignore
284
+ toContain(value: valueof<T>): void {
285
+ // @ts-ignore
286
+ this.verdict =
287
+ isArray<T>() && this.left.includes(value) ? Verdict.Ok : Verdict.Fail;
265
288
 
266
- /**
267
- * Tests if an array contains an element
268
- *
269
- * @param { valueof<T> } value - The value to check
270
- * @returns - void
271
- */
272
- // @ts-ignore
273
- toContain(value: valueof<T>): void {
274
- // @ts-ignore
275
- this.verdict = (isArray<T>() && this.left.includes(value)) ? Verdict.Ok : Verdict.Fail;
276
-
277
- // @ts-ignore
278
- this._left = "includes value";
279
- this._right = "does not include value";
280
- this.op = "=";
281
-
282
- // @ts-ignore
283
- if (after_each_callback) after_each_callback();
284
- // @ts-ignore
285
- if (before_each_callback) before_each_callback();
286
- }
289
+ // @ts-ignore
290
+ this._left = "includes value";
291
+ this._right = "does not include value";
292
+ this.op = "=";
287
293
 
288
- /**
289
- * Tests for equality
290
- * @param {T} equals - The value to test
291
- * @returns - void
292
- */
293
- toBe(equals: T): void {
294
- store<T>(changetype<usize>(this), equals, offsetof<Expectation<T>>("right"));
295
- if (isBoolean<T>()) {
296
- this.verdict = this.left === equals
297
- ? Verdict.Ok
298
- : Verdict.Fail;
299
-
300
- } else if (isString<T>()) {
301
- this.verdict = this.left === equals
302
- ? Verdict.Ok
303
- : Verdict.Fail;
304
- } else if (isInteger<T>() || isFloat<T>()) {
305
- this.verdict = this.left === equals
306
- ? Verdict.Ok
307
- : Verdict.Fail;
308
- } else if (isArray<T>()) {
309
- // getArrayDepth<T>();
310
- } else {
311
- this.verdict = Verdict.Unreachable;
312
- }
313
-
314
- this.op = "=";
315
-
316
- // @ts-ignore
317
- if (after_each_callback) after_each_callback();
318
- // @ts-ignore
319
- if (before_each_callback) before_each_callback();
294
+ // @ts-ignore
295
+ if (after_each_callback) after_each_callback();
296
+ // @ts-ignore
297
+ if (before_each_callback) before_each_callback();
298
+ }
299
+
300
+ /**
301
+ * Tests for equality
302
+ * @param {T} equals - The value to test
303
+ * @returns - void
304
+ */
305
+ toBe(equals: T): void {
306
+ store<T>(
307
+ changetype<usize>(this),
308
+ equals,
309
+ offsetof<Expectation<T>>("right"),
310
+ );
311
+ if (isBoolean<T>()) {
312
+ this.verdict = this.left === equals ? Verdict.Ok : Verdict.Fail;
313
+ } else if (isString<T>()) {
314
+ this.verdict = this.left === equals ? Verdict.Ok : Verdict.Fail;
315
+ } else if (isInteger<T>() || isFloat<T>()) {
316
+ this.verdict = this.left === equals ? Verdict.Ok : Verdict.Fail;
317
+ } else if (isArray<T>()) {
318
+ // getArrayDepth<T>();
319
+ } else {
320
+ this.verdict = Verdict.Unreachable;
320
321
  }
321
322
 
322
- report(): string | null {
323
- if (!this._not && this.verdict === Verdict.Ok) return null;
324
-
325
- const left = this._left || visualize(this.left);
326
- const right = this._right || visualize(load<T>(changetype<usize>(this), offsetof<Expectation<T>>("right")));
323
+ this.op = "=";
327
324
 
328
- if (this._not) {
329
- if (this.verdict === Verdict.Fail) return null;
330
- const dif = diff(left, right, true);
331
- return rainbow.red(" - Test failed") + "\n" + rainbow.italicMk(` ${rainbow.dimMk("(expected) ->")} ${dif.left.toString()}\n ${rainbow.dimMk("[ !" + this.op + " ]")}\n ${rainbow.dimMk("(recieved) ->")} ${dif.right.toString()}`);
332
- }
325
+ // @ts-ignore
326
+ if (after_each_callback) after_each_callback();
327
+ // @ts-ignore
328
+ if (before_each_callback) before_each_callback();
329
+ }
330
+
331
+ report(): string | null {
332
+ if (!this._not && this.verdict === Verdict.Ok) return null;
333
+
334
+ const left = this._left || visualize(this.left);
335
+ const right =
336
+ this._right ||
337
+ visualize(
338
+ load<T>(changetype<usize>(this), offsetof<Expectation<T>>("right")),
339
+ );
340
+
341
+ if (this._not) {
342
+ if (this.verdict === Verdict.Fail) return null;
343
+ const dif = diff(left, right, true);
344
+ return (
345
+ rainbow.red(" - Test failed") +
346
+ "\n" +
347
+ rainbow.italicMk(
348
+ ` ${rainbow.dimMk("(expected) ->")} ${dif.left.toString()}\n ${rainbow.dimMk("[ !" + this.op + " ]")}\n ${rainbow.dimMk("(recieved) ->")} ${dif.right.toString()}`,
349
+ )
350
+ );
351
+ }
333
352
 
334
- if (left == right) return null;
353
+ if (left == right) return null;
335
354
 
336
- const dif = diff(left, right);
355
+ const dif = diff(left, right);
337
356
 
338
- return rainbow.red(" - Test failed") + "\n" + rainbow.italicMk(` ${rainbow.dimMk("(expected) ->")} ${dif.left.toString()}\n ${rainbow.dimMk("[ " + this.op + " ]")}\n ${rainbow.dimMk("(recieved) ->")} ${dif.right.toString()}`);
339
- }
340
- }
357
+ return (
358
+ rainbow.red(" - Test failed") +
359
+ "\n" +
360
+ rainbow.italicMk(
361
+ ` ${rainbow.dimMk("(expected) ->")} ${dif.left.toString()}\n ${rainbow.dimMk("[ " + this.op + " ]")}\n ${rainbow.dimMk("(recieved) ->")} ${dif.right.toString()}`,
362
+ )
363
+ );
364
+ }
365
+ }