@augment-vir/assert 30.8.4 → 31.0.1
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.
- package/dist/assertions/boolean.d.ts +20 -26
- package/dist/assertions/boolean.js +185 -41
- package/dist/assertions/boundary.d.ts +40 -256
- package/dist/assertions/boundary.js +265 -229
- package/dist/assertions/enum.d.ts +12 -13
- package/dist/assertions/enum.js +98 -20
- package/dist/assertions/equality/entry-equality.d.ts +11 -15
- package/dist/assertions/equality/entry-equality.js +210 -43
- package/dist/assertions/equality/json-equality.d.ts +11 -15
- package/dist/assertions/equality/json-equality.js +144 -43
- package/dist/assertions/equality/simple-equality.d.ts +39 -46
- package/dist/assertions/equality/simple-equality.js +316 -61
- package/dist/assertions/extendable-assertions.d.ts +0 -12
- package/dist/assertions/extendable-assertions.js +0 -12
- package/dist/assertions/http.d.ts +10 -14
- package/dist/assertions/http.js +96 -28
- package/dist/assertions/instance.d.ts +10 -18
- package/dist/assertions/instance.js +92 -26
- package/dist/assertions/keys.d.ts +59 -138
- package/dist/assertions/keys.js +279 -163
- package/dist/assertions/length.d.ts +30 -212
- package/dist/assertions/length.js +117 -175
- package/dist/assertions/nullish.d.ts +8 -20
- package/dist/assertions/nullish.js +85 -27
- package/dist/assertions/numeric.d.ts +67 -81
- package/dist/assertions/numeric.js +564 -133
- package/dist/assertions/output.d.ts +2 -3
- package/dist/assertions/output.js +1 -7
- package/dist/assertions/primitive.d.ts +33 -40
- package/dist/assertions/primitive.js +232 -66
- package/dist/assertions/promise.d.ts +20 -30
- package/dist/assertions/promise.js +244 -53
- package/dist/assertions/regexp.d.ts +12 -14
- package/dist/assertions/regexp.js +84 -21
- package/dist/assertions/runtime-type.d.ts +99 -207
- package/dist/assertions/runtime-type.js +805 -276
- package/dist/assertions/throws.d.ts +24 -25
- package/dist/assertions/throws.js +43 -5
- package/dist/assertions/uuid.d.ts +11 -16
- package/dist/assertions/uuid.js +91 -22
- package/dist/assertions/values.d.ts +81 -210
- package/dist/assertions/values.js +627 -234
- package/dist/augments/assertion-exports.d.ts +0 -1
- package/dist/augments/assertion-exports.js +1 -1
- package/dist/augments/guards/assert-wrap.d.ts +7 -4
- package/dist/augments/guards/assert-wrap.js +5 -4
- package/dist/augments/guards/check-wrap.d.ts +7 -5
- package/dist/augments/guards/check-wrap.js +5 -4
- package/dist/augments/guards/check.d.ts +5 -5
- package/dist/augments/guards/check.js +5 -4
- package/dist/augments/guards/wait-until.d.ts +8 -4
- package/dist/augments/guards/wait-until.js +7 -8
- package/dist/guard-types/guard-group.d.ts +5 -2
- package/dist/guard-types/wait-until-function.d.ts +2 -10
- package/dist/guard-types/wait-until-function.js +1 -9
- package/dist/index.d.ts +1 -0
- package/package.json +2 -2
- package/dist/guard-types/assert-wrap-function.d.ts +0 -12
- package/dist/guard-types/assert-wrap-function.js +0 -14
- package/dist/guard-types/check-function.d.ts +0 -14
- package/dist/guard-types/check-function.js +0 -22
- package/dist/guard-types/check-wrap-wrapper-function.d.ts +0 -12
- package/dist/guard-types/check-wrap-wrapper-function.js +0 -19
- package/dist/guard-types/guard-override.d.ts +0 -4
- package/dist/guard-types/guard-override.js +0 -10
|
@@ -1,43 +1,94 @@
|
|
|
1
1
|
import { stringify } from '@augment-vir/core';
|
|
2
2
|
import { AssertionError } from '../../augments/assertion.error.js';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
import { createWaitUntil } from '../../guard-types/wait-until-function.js';
|
|
4
|
+
const assertions = {
|
|
5
|
+
/**
|
|
6
|
+
* Asserts that two objects are deeply equal by checking only their top-level values for strict
|
|
7
|
+
* (non-deep, reference, using
|
|
8
|
+
* [`===`](https://developer.mozilla.org/docs/Web/JavaScript/Equality_comparisons_and_sameness#strict_equality_using))
|
|
9
|
+
* equality.
|
|
10
|
+
*
|
|
11
|
+
* Type guards the first value.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import {assert} from '@augment-vir/assert';
|
|
17
|
+
*
|
|
18
|
+
* assert.entriesEqual({a: 'a'}, {a: 'a'}); // passes
|
|
19
|
+
*
|
|
20
|
+
* assert.entriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // fails
|
|
21
|
+
*
|
|
22
|
+
* const bExample = {b: 'b'};
|
|
23
|
+
* assert.entriesEqual({a: bExample}, {a: bExample}); // passes
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @throws {@link AssertionError} If both inputs are not equal.
|
|
27
|
+
* @see
|
|
28
|
+
* - {@link assert.notEntriesEqual} : the opposite assertion.
|
|
29
|
+
* - {@link assert.jsonEquals} : another deep equality assertion.
|
|
30
|
+
* - {@link assert.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
31
|
+
*/
|
|
32
|
+
entriesEqual(actual, expected, failureMessage) {
|
|
9
33
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
const allKeys = Array.from(new Set([
|
|
15
|
-
...Reflect.ownKeys(actual),
|
|
16
|
-
...Reflect.ownKeys(expected),
|
|
17
|
-
]));
|
|
18
|
-
allKeys.forEach((key) => {
|
|
19
|
-
const actualValue = actual[key];
|
|
20
|
-
const expectedValue = expected[key];
|
|
21
|
-
try {
|
|
22
|
-
strictEquals(actualValue, expectedValue);
|
|
34
|
+
if (!actual || typeof actual !== 'object') {
|
|
35
|
+
throw new AssertionError(`${stringify(actual)} is not an object.`, failureMessage);
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
23
37
|
}
|
|
24
|
-
|
|
25
|
-
throw new AssertionError(
|
|
38
|
+
else if (!expected || typeof expected !== 'object') {
|
|
39
|
+
throw new AssertionError(`${stringify(expected)} is not an object.`, failureMessage);
|
|
26
40
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
const allKeys = Array.from(new Set([...Reflect.ownKeys(actual), ...Reflect.ownKeys(expected)]));
|
|
42
|
+
allKeys.forEach((key) => {
|
|
43
|
+
const actualValue = actual[key];
|
|
44
|
+
const expectedValue = expected[key];
|
|
45
|
+
if (actualValue !== expectedValue) {
|
|
46
|
+
throw new AssertionError(`Entries are not equal at key '${String(key)}'.`, failureMessage);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
/**
|
|
51
|
+
* Asserts that two objects are _not_ deeply equal by checking only their top-level values for
|
|
52
|
+
* strict (non-deep, reference, using
|
|
53
|
+
* [`===`](https://developer.mozilla.org/docs/Web/JavaScript/Equality_comparisons_and_sameness#strict_equality_using))
|
|
54
|
+
* equality.
|
|
55
|
+
*
|
|
56
|
+
* Performs no type guarding.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* import {assert} from '@augment-vir/assert';
|
|
62
|
+
*
|
|
63
|
+
* assert.notEntriesEqual({a: 'a'}, {a: 'a'}); // fails
|
|
64
|
+
*
|
|
65
|
+
* assert.notEntriesEqual({a: {b: 'b'}}, {a: {b: 'b'}}); // passes
|
|
66
|
+
*
|
|
67
|
+
* const bExample = {b: 'b'};
|
|
68
|
+
* assert.notEntriesEqual({a: bExample}, {a: bExample}); // fails
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @throws {@link AssertionError} If both inputs are equal.
|
|
72
|
+
* @see
|
|
73
|
+
* - {@link assert.entriesEqual} : the opposite assertion.
|
|
74
|
+
* - {@link assert.notJsonEquals} : another not deep equality assertion.
|
|
75
|
+
* - {@link assert.notDeepEquals} : the most thorough (but also slow) not deep equality assertion.
|
|
76
|
+
*/
|
|
77
|
+
notEntriesEqual(actual, expected, failureMessage) {
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
79
|
+
if (!actual || typeof actual !== 'object' || !expected || typeof expected !== 'object') {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const allKeys = Array.from(new Set([...Reflect.ownKeys(actual), ...Reflect.ownKeys(expected)]));
|
|
83
|
+
const valid = allKeys.some((key) => {
|
|
84
|
+
const actualValue = actual[key];
|
|
85
|
+
const expectedValue = expected[key];
|
|
86
|
+
return actualValue !== expectedValue;
|
|
87
|
+
});
|
|
88
|
+
if (!valid) {
|
|
89
|
+
throw new AssertionError('Entries are equal.', failureMessage);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
41
92
|
};
|
|
42
93
|
export const entryEqualityGuards = {
|
|
43
94
|
assert: assertions,
|
|
@@ -68,7 +119,23 @@ export const entryEqualityGuards = {
|
|
|
68
119
|
* - {@link check.jsonEquals} : another deep equality check.
|
|
69
120
|
* - {@link check.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
70
121
|
*/
|
|
71
|
-
entriesEqual
|
|
122
|
+
entriesEqual(actual, expected) {
|
|
123
|
+
if (
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
125
|
+
!actual ||
|
|
126
|
+
typeof actual !== 'object' ||
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
128
|
+
!expected ||
|
|
129
|
+
typeof expected !== 'object') {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
const allKeys = Array.from(new Set([...Reflect.ownKeys(actual), ...Reflect.ownKeys(expected)]));
|
|
133
|
+
return allKeys.every((key) => {
|
|
134
|
+
const actualValue = actual[key];
|
|
135
|
+
const expectedValue = expected[key];
|
|
136
|
+
return actualValue === expectedValue;
|
|
137
|
+
});
|
|
138
|
+
},
|
|
72
139
|
/**
|
|
73
140
|
* Checks that two objects are _not_ deeply equal by checking only their top-level values
|
|
74
141
|
* for strict (non-deep, reference, using
|
|
@@ -95,7 +162,23 @@ export const entryEqualityGuards = {
|
|
|
95
162
|
* - {@link check.notJsonEquals} : another not deep equality check.
|
|
96
163
|
* - {@link check.notDeepEquals} : the most thorough (but also slow) not deep equality check.
|
|
97
164
|
*/
|
|
98
|
-
notEntriesEqual
|
|
165
|
+
notEntriesEqual(actual, expected) {
|
|
166
|
+
if (
|
|
167
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
168
|
+
!actual ||
|
|
169
|
+
typeof actual !== 'object' ||
|
|
170
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
171
|
+
!expected ||
|
|
172
|
+
typeof expected !== 'object') {
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
const allKeys = Array.from(new Set([...Reflect.ownKeys(actual), ...Reflect.ownKeys(expected)]));
|
|
176
|
+
return allKeys.some((key) => {
|
|
177
|
+
const actualValue = actual[key];
|
|
178
|
+
const expectedValue = expected[key];
|
|
179
|
+
return actualValue !== expectedValue;
|
|
180
|
+
});
|
|
181
|
+
},
|
|
99
182
|
},
|
|
100
183
|
assertWrap: {
|
|
101
184
|
/**
|
|
@@ -126,7 +209,25 @@ export const entryEqualityGuards = {
|
|
|
126
209
|
* - {@link assertWrap.jsonEquals} : another deep equality assertion.
|
|
127
210
|
* - {@link assertWrap.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
128
211
|
*/
|
|
129
|
-
entriesEqual
|
|
212
|
+
entriesEqual(actual, expected, failureMessage) {
|
|
213
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
214
|
+
if (!actual || typeof actual !== 'object') {
|
|
215
|
+
throw new AssertionError(`${stringify(actual)} is not an object.`, failureMessage);
|
|
216
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
217
|
+
}
|
|
218
|
+
else if (!expected || typeof expected !== 'object') {
|
|
219
|
+
throw new AssertionError(`${stringify(expected)} is not an object.`, failureMessage);
|
|
220
|
+
}
|
|
221
|
+
const allKeys = Array.from(new Set([...Reflect.ownKeys(actual), ...Reflect.ownKeys(expected)]));
|
|
222
|
+
allKeys.forEach((key) => {
|
|
223
|
+
const actualValue = actual[key];
|
|
224
|
+
const expectedValue = expected[key];
|
|
225
|
+
if (actualValue !== expectedValue) {
|
|
226
|
+
throw new AssertionError(`Entries are not equal at key '${String(key)}'.`, failureMessage);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
return actual;
|
|
230
|
+
},
|
|
130
231
|
/**
|
|
131
232
|
* Asserts that two objects are _not_ deeply equal by checking only their top-level values
|
|
132
233
|
* for strict (non-deep, reference, using
|
|
@@ -155,7 +256,29 @@ export const entryEqualityGuards = {
|
|
|
155
256
|
* - {@link assertWrap.notJsonEquals} : another not deep equality assertion.
|
|
156
257
|
* - {@link assertWrap.notDeepEquals} : the most thorough (but also slow) not deep equality assertion.
|
|
157
258
|
*/
|
|
158
|
-
notEntriesEqual
|
|
259
|
+
notEntriesEqual(actual, expected, failureMessage) {
|
|
260
|
+
if (
|
|
261
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
262
|
+
!actual ||
|
|
263
|
+
typeof actual !== 'object' ||
|
|
264
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
265
|
+
!expected ||
|
|
266
|
+
typeof expected !== 'object') {
|
|
267
|
+
return actual;
|
|
268
|
+
}
|
|
269
|
+
const allKeys = Array.from(new Set([...Reflect.ownKeys(actual), ...Reflect.ownKeys(expected)]));
|
|
270
|
+
const valid = allKeys.some((key) => {
|
|
271
|
+
const actualValue = actual[key];
|
|
272
|
+
const expectedValue = expected[key];
|
|
273
|
+
return actualValue !== expectedValue;
|
|
274
|
+
});
|
|
275
|
+
if (valid) {
|
|
276
|
+
return actual;
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
throw new AssertionError('Entries are equal.', failureMessage);
|
|
280
|
+
}
|
|
281
|
+
},
|
|
159
282
|
},
|
|
160
283
|
checkWrap: {
|
|
161
284
|
/**
|
|
@@ -186,7 +309,29 @@ export const entryEqualityGuards = {
|
|
|
186
309
|
* - {@link checkWrap.jsonEquals} : another deep equality check.
|
|
187
310
|
* - {@link checkWrap.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
188
311
|
*/
|
|
189
|
-
entriesEqual
|
|
312
|
+
entriesEqual(actual, expected) {
|
|
313
|
+
if (
|
|
314
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
315
|
+
!actual ||
|
|
316
|
+
typeof actual !== 'object' ||
|
|
317
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
318
|
+
!expected ||
|
|
319
|
+
typeof expected !== 'object') {
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
const allKeys = Array.from(new Set([...Reflect.ownKeys(actual), ...Reflect.ownKeys(expected)]));
|
|
323
|
+
const valid = allKeys.every((key) => {
|
|
324
|
+
const actualValue = actual[key];
|
|
325
|
+
const expectedValue = expected[key];
|
|
326
|
+
return actualValue === expectedValue;
|
|
327
|
+
});
|
|
328
|
+
if (valid) {
|
|
329
|
+
return actual;
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
},
|
|
190
335
|
/**
|
|
191
336
|
* Checks that two objects are _not_ deeply equal by checking only their top-level values
|
|
192
337
|
* for strict (non-deep, reference, using
|
|
@@ -215,7 +360,29 @@ export const entryEqualityGuards = {
|
|
|
215
360
|
* - {@link checkWrap.notJsonEquals} : another not deep equality check.
|
|
216
361
|
* - {@link checkWrap.notDeepEquals} : the most thorough (but also slow) not deep equality check.
|
|
217
362
|
*/
|
|
218
|
-
notEntriesEqual
|
|
363
|
+
notEntriesEqual(actual, expected) {
|
|
364
|
+
if (
|
|
365
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
366
|
+
!actual ||
|
|
367
|
+
typeof actual !== 'object' ||
|
|
368
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
369
|
+
!expected ||
|
|
370
|
+
typeof expected !== 'object') {
|
|
371
|
+
return actual;
|
|
372
|
+
}
|
|
373
|
+
const allKeys = Array.from(new Set([...Reflect.ownKeys(actual), ...Reflect.ownKeys(expected)]));
|
|
374
|
+
const valid = allKeys.some((key) => {
|
|
375
|
+
const actualValue = actual[key];
|
|
376
|
+
const expectedValue = expected[key];
|
|
377
|
+
return actualValue !== expectedValue;
|
|
378
|
+
});
|
|
379
|
+
if (valid) {
|
|
380
|
+
return actual;
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
return undefined;
|
|
384
|
+
}
|
|
385
|
+
},
|
|
219
386
|
},
|
|
220
387
|
waitUntil: {
|
|
221
388
|
/**
|
|
@@ -252,7 +419,7 @@ export const entryEqualityGuards = {
|
|
|
252
419
|
* - {@link waitUntil.jsonEquals} : another deep equality assertion.
|
|
253
420
|
* - {@link waitUntil.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
254
421
|
*/
|
|
255
|
-
entriesEqual:
|
|
422
|
+
entriesEqual: createWaitUntil(assertions.entriesEqual),
|
|
256
423
|
/**
|
|
257
424
|
* Repeatedly calls a callback until its output is _not_ deeply equal to the first input by
|
|
258
425
|
* checking only their top-level values for strict (non-deep, reference, using
|
|
@@ -288,6 +455,6 @@ export const entryEqualityGuards = {
|
|
|
288
455
|
* - {@link waitUntil.notJsonEquals} : another not deep equality assertion.
|
|
289
456
|
* - {@link waitUntil.notDeepEquals} : the most thorough (but also slow) not deep equality assertion.
|
|
290
457
|
*/
|
|
291
|
-
notEntriesEqual:
|
|
458
|
+
notEntriesEqual: createWaitUntil(assertions.notEntriesEqual),
|
|
292
459
|
},
|
|
293
460
|
};
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { type MaybePromise, type NarrowToExpected } from '@augment-vir/core';
|
|
2
|
-
import {
|
|
3
|
-
import { type WaitUntilOptions } from '../../guard-types/wait-until-function.js';
|
|
4
|
-
declare function jsonEquals<const Actual, const Expected extends Actual>(actual: Actual, expected: Expected, failureMessage?: string | undefined): asserts actual is Expected;
|
|
5
|
-
declare function notJsonEquals(actual: unknown, expected: unknown, failureMessage?: string | undefined): void;
|
|
2
|
+
import { WaitUntilOptions } from '../../guard-types/wait-until-function.js';
|
|
6
3
|
export declare const jsonEqualityGuards: {
|
|
7
4
|
assert: {
|
|
8
5
|
/**
|
|
@@ -28,7 +25,7 @@ export declare const jsonEqualityGuards: {
|
|
|
28
25
|
* - {@link assert.entriesEqual} : another deep equality assertion.
|
|
29
26
|
* - {@link assert.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
30
27
|
*/
|
|
31
|
-
jsonEquals:
|
|
28
|
+
jsonEquals<const Actual, const Expected extends Actual>(this: void, actual: Actual, expected: Expected, failureMessage?: string | undefined): asserts actual is Expected;
|
|
32
29
|
/**
|
|
33
30
|
* Asserts that two values are _not_ deeply equal when stringified into JSON. This may not make
|
|
34
31
|
* any sense if the values are not valid JSON. This internally sorts all given object keys so it
|
|
@@ -52,7 +49,7 @@ export declare const jsonEqualityGuards: {
|
|
|
52
49
|
* - {@link assert.entriesEqual} : another deep equality assertion.
|
|
53
50
|
* - {@link assert.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
54
51
|
*/
|
|
55
|
-
notJsonEquals:
|
|
52
|
+
notJsonEquals(this: void, actual: unknown, expected: unknown, failureMessage?: string | undefined): void;
|
|
56
53
|
};
|
|
57
54
|
check: {
|
|
58
55
|
/**
|
|
@@ -77,7 +74,7 @@ export declare const jsonEqualityGuards: {
|
|
|
77
74
|
* - {@link check.entriesEqual} : another deep equality check.
|
|
78
75
|
* - {@link check.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
79
76
|
*/
|
|
80
|
-
jsonEquals
|
|
77
|
+
jsonEquals<Actual, Expected extends Actual>(this: void, actual: Actual, expected: Expected): actual is Expected;
|
|
81
78
|
/**
|
|
82
79
|
* Checks that two values are _not_ deeply equal when stringified into JSON. This may not
|
|
83
80
|
* make any sense if the values are not valid JSON. This internally sorts all given object
|
|
@@ -100,7 +97,7 @@ export declare const jsonEqualityGuards: {
|
|
|
100
97
|
* - {@link check.entriesEqual} : another deep equality check.
|
|
101
98
|
* - {@link check.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
102
99
|
*/
|
|
103
|
-
notJsonEquals:
|
|
100
|
+
notJsonEquals(this: void, actual: unknown, expected: unknown): boolean;
|
|
104
101
|
};
|
|
105
102
|
assertWrap: {
|
|
106
103
|
/**
|
|
@@ -127,7 +124,7 @@ export declare const jsonEqualityGuards: {
|
|
|
127
124
|
* - {@link assertWrap.entriesEqual} : another deep equality assertion.
|
|
128
125
|
* - {@link assertWrap.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
129
126
|
*/
|
|
130
|
-
jsonEquals
|
|
127
|
+
jsonEquals<Actual, Expected extends Actual>(this: void, actual: Actual, expected: Expected, failureMessage?: string | undefined): NarrowToExpected<Actual, Expected>;
|
|
131
128
|
/**
|
|
132
129
|
* Asserts that two values are _not_ deeply equal when stringified into JSON. This may not
|
|
133
130
|
* make any sense if the values are not valid JSON. This internally sorts all given object
|
|
@@ -152,7 +149,7 @@ export declare const jsonEqualityGuards: {
|
|
|
152
149
|
* - {@link assertWrap.entriesEqual} : another deep equality assertion.
|
|
153
150
|
* - {@link assertWrap.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
154
151
|
*/
|
|
155
|
-
notJsonEquals:
|
|
152
|
+
notJsonEquals<Actual>(this: void, actual: Actual, expected: unknown, failureMessage?: string | undefined): Actual;
|
|
156
153
|
};
|
|
157
154
|
checkWrap: {
|
|
158
155
|
/**
|
|
@@ -178,7 +175,7 @@ export declare const jsonEqualityGuards: {
|
|
|
178
175
|
* - {@link checkWrap.entriesEqual} : another deep equality check.
|
|
179
176
|
* - {@link checkWrap.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
180
177
|
*/
|
|
181
|
-
jsonEquals
|
|
178
|
+
jsonEquals<Actual, Expected extends Actual>(this: void, actual: Actual, expected: Expected): NarrowToExpected<Actual, Expected> | undefined;
|
|
182
179
|
/**
|
|
183
180
|
* Checks that two values are _not_ deeply equal when stringified into JSON. This may not
|
|
184
181
|
* make any sense if the values are not valid JSON. This internally sorts all given object
|
|
@@ -202,7 +199,7 @@ export declare const jsonEqualityGuards: {
|
|
|
202
199
|
* - {@link checkWrap.entriesEqual} : another deep equality check.
|
|
203
200
|
* - {@link checkWrap.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
204
201
|
*/
|
|
205
|
-
notJsonEquals:
|
|
202
|
+
notJsonEquals<Actual>(this: void, actual: Actual, expected: unknown): Actual | undefined;
|
|
206
203
|
};
|
|
207
204
|
waitUntil: {
|
|
208
205
|
/**
|
|
@@ -234,7 +231,7 @@ export declare const jsonEqualityGuards: {
|
|
|
234
231
|
* - {@link waitUntil.entriesEqual} : another deep equality assertion.
|
|
235
232
|
* - {@link waitUntil.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
236
233
|
*/
|
|
237
|
-
jsonEquals: <Actual, Expected extends Actual>(expected: Expected, callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToExpected<Actual, Expected>>;
|
|
234
|
+
jsonEquals: <Actual, Expected extends Actual>(this: void, expected: Expected, callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToExpected<Actual, Expected>>;
|
|
238
235
|
/**
|
|
239
236
|
* Repeatedly calls a callback until its output is _not_ deeply equal to the first input
|
|
240
237
|
* when stringified into JSON. This may not make any sense if the values are not valid JSON.
|
|
@@ -265,7 +262,6 @@ export declare const jsonEqualityGuards: {
|
|
|
265
262
|
* - {@link waitUntil.entriesEqual} : another not deep equality assertion.
|
|
266
263
|
* - {@link waitUntil.deepEquals} : the most thorough (but also slow) not deep equality assertion.
|
|
267
264
|
*/
|
|
268
|
-
notJsonEquals:
|
|
265
|
+
notJsonEquals: <Actual>(this: void, expected: unknown, callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Actual>;
|
|
269
266
|
};
|
|
270
267
|
};
|
|
271
|
-
export {};
|
|
@@ -1,30 +1,40 @@
|
|
|
1
1
|
import { extractErrorMessage, } from '@augment-vir/core';
|
|
2
2
|
import { AssertionError } from '../../augments/assertion.error.js';
|
|
3
|
-
import {
|
|
3
|
+
import { createWaitUntil } from '../../guard-types/wait-until-function.js';
|
|
4
4
|
function baseJsonEquals(a, b) {
|
|
5
5
|
return JSON.stringify(a) === JSON.stringify(b);
|
|
6
6
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
recursiveJsonEquals(actual, expected);
|
|
10
|
-
}
|
|
11
|
-
catch (error) {
|
|
12
|
-
throw new AssertionError(extractErrorMessage(error), failureMessage);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function notJsonEquals(actual, expected, failureMessage) {
|
|
16
|
-
try {
|
|
17
|
-
jsonEquals(actual, expected);
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
7
|
+
function recursiveAssertJsonEquals(actual, expected) {
|
|
8
|
+
if (actual === expected || baseJsonEquals(actual, expected)) {
|
|
20
9
|
return;
|
|
21
10
|
}
|
|
22
|
-
|
|
11
|
+
if (actual != null &&
|
|
12
|
+
expected != null &&
|
|
13
|
+
typeof actual === 'object' &&
|
|
14
|
+
typeof expected === 'object') {
|
|
15
|
+
const aKeys = Object.keys(actual).sort();
|
|
16
|
+
const bKeys = Object.keys(expected).sort();
|
|
17
|
+
if (aKeys.length !== bKeys.length) {
|
|
18
|
+
throw new Error('Values are not JSON equal.');
|
|
19
|
+
}
|
|
20
|
+
const areKeysEqual = baseJsonEquals(aKeys, bKeys);
|
|
21
|
+
if (!areKeysEqual) {
|
|
22
|
+
throw new Error('Values are JSON equal.');
|
|
23
|
+
}
|
|
24
|
+
Object.keys(actual).forEach((key) => {
|
|
25
|
+
try {
|
|
26
|
+
recursiveAssertJsonEquals(actual[key], expected[key]);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
throw new Error(`JSON objects are not equal at key '${key}': ${extractErrorMessage(error)}`);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
throw new Error('Values are not JSON equal.');
|
|
23
34
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return;
|
|
35
|
+
function recursiveCheckJsonEquals(actual, expected) {
|
|
36
|
+
if (actual === expected || baseJsonEquals(actual, expected)) {
|
|
37
|
+
return true;
|
|
28
38
|
}
|
|
29
39
|
if (actual != null &&
|
|
30
40
|
expected != null &&
|
|
@@ -32,26 +42,83 @@ function recursiveJsonEquals(actual, expected) {
|
|
|
32
42
|
typeof expected === 'object') {
|
|
33
43
|
const aKeys = Object.keys(actual).sort();
|
|
34
44
|
const bKeys = Object.keys(expected).sort();
|
|
35
|
-
if (aKeys.length
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
jsonEquals(actual[key], expected[key]);
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
throw new Error(`JSON objects are not equal at key '${key}': ${extractErrorMessage(error)}`);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
45
|
+
if (aKeys.length !== bKeys.length) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const areKeysEqual = baseJsonEquals(aKeys, bKeys);
|
|
49
|
+
if (!areKeysEqual) {
|
|
50
|
+
return false;
|
|
48
51
|
}
|
|
52
|
+
return Object.keys(actual).every((key) => {
|
|
53
|
+
return recursiveCheckJsonEquals(actual[key], expected[key]);
|
|
54
|
+
});
|
|
49
55
|
}
|
|
50
|
-
|
|
56
|
+
return false;
|
|
51
57
|
}
|
|
52
58
|
const assertions = {
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Asserts that two values are deeply equal when stringified into JSON. This will fail or may
|
|
61
|
+
* not make any sense if the values are not valid JSON. This internally sorts all given object
|
|
62
|
+
* keys so it is insensitive to object key order.
|
|
63
|
+
*
|
|
64
|
+
* Type guards the first value.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* import {assert} from '@augment-vir/assert';
|
|
70
|
+
*
|
|
71
|
+
* assert.jsonEquals({a: 'a'}, {a: 'a'}); // passes
|
|
72
|
+
*
|
|
73
|
+
* assert.jsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // fails
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @throws {@link AssertionError} If both inputs are not equal.
|
|
77
|
+
* @see
|
|
78
|
+
* - {@link assert.notJsonEquals} : the opposite assertion.
|
|
79
|
+
* - {@link assert.entriesEqual} : another deep equality assertion.
|
|
80
|
+
* - {@link assert.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
81
|
+
*/
|
|
82
|
+
jsonEquals(actual, expected, failureMessage) {
|
|
83
|
+
try {
|
|
84
|
+
recursiveAssertJsonEquals(actual, expected);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new AssertionError(extractErrorMessage(error), failureMessage);
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
/**
|
|
91
|
+
* Asserts that two values are _not_ deeply equal when stringified into JSON. This may not make
|
|
92
|
+
* any sense if the values are not valid JSON. This internally sorts all given object keys so it
|
|
93
|
+
* is insensitive to object key order.
|
|
94
|
+
*
|
|
95
|
+
* Performs no type guarding.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
* import {assert} from '@augment-vir/assert';
|
|
101
|
+
*
|
|
102
|
+
* assert.notJsonEquals({a: 'a'}, {a: 'a'}); // fails
|
|
103
|
+
*
|
|
104
|
+
* assert.notJsonEquals({a: {b: 'b'}}, {a: {b: 'c'}}); // passes
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @throws {@link AssertionError} If both inputs are not equal.
|
|
108
|
+
* @see
|
|
109
|
+
* - {@link assert.jsonEquals} : the opposite assertion.
|
|
110
|
+
* - {@link assert.entriesEqual} : another deep equality assertion.
|
|
111
|
+
* - {@link assert.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
112
|
+
*/
|
|
113
|
+
notJsonEquals(actual, expected, failureMessage) {
|
|
114
|
+
try {
|
|
115
|
+
recursiveAssertJsonEquals(actual, expected);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
throw new AssertionError('Values are JSON equal.', failureMessage);
|
|
121
|
+
},
|
|
55
122
|
};
|
|
56
123
|
export const jsonEqualityGuards = {
|
|
57
124
|
assert: assertions,
|
|
@@ -78,7 +145,9 @@ export const jsonEqualityGuards = {
|
|
|
78
145
|
* - {@link check.entriesEqual} : another deep equality check.
|
|
79
146
|
* - {@link check.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
80
147
|
*/
|
|
81
|
-
jsonEquals
|
|
148
|
+
jsonEquals(actual, expected) {
|
|
149
|
+
return recursiveCheckJsonEquals(actual, expected);
|
|
150
|
+
},
|
|
82
151
|
/**
|
|
83
152
|
* Checks that two values are _not_ deeply equal when stringified into JSON. This may not
|
|
84
153
|
* make any sense if the values are not valid JSON. This internally sorts all given object
|
|
@@ -101,7 +170,9 @@ export const jsonEqualityGuards = {
|
|
|
101
170
|
* - {@link check.entriesEqual} : another deep equality check.
|
|
102
171
|
* - {@link check.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
103
172
|
*/
|
|
104
|
-
notJsonEquals
|
|
173
|
+
notJsonEquals(actual, expected) {
|
|
174
|
+
return !recursiveCheckJsonEquals(actual, expected);
|
|
175
|
+
},
|
|
105
176
|
},
|
|
106
177
|
assertWrap: {
|
|
107
178
|
/**
|
|
@@ -128,7 +199,15 @@ export const jsonEqualityGuards = {
|
|
|
128
199
|
* - {@link assertWrap.entriesEqual} : another deep equality assertion.
|
|
129
200
|
* - {@link assertWrap.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
130
201
|
*/
|
|
131
|
-
jsonEquals
|
|
202
|
+
jsonEquals(actual, expected, failureMessage) {
|
|
203
|
+
try {
|
|
204
|
+
recursiveAssertJsonEquals(actual, expected);
|
|
205
|
+
return actual;
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
throw new AssertionError(extractErrorMessage(error), failureMessage);
|
|
209
|
+
}
|
|
210
|
+
},
|
|
132
211
|
/**
|
|
133
212
|
* Asserts that two values are _not_ deeply equal when stringified into JSON. This may not
|
|
134
213
|
* make any sense if the values are not valid JSON. This internally sorts all given object
|
|
@@ -153,7 +232,15 @@ export const jsonEqualityGuards = {
|
|
|
153
232
|
* - {@link assertWrap.entriesEqual} : another deep equality assertion.
|
|
154
233
|
* - {@link assertWrap.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
155
234
|
*/
|
|
156
|
-
notJsonEquals
|
|
235
|
+
notJsonEquals(actual, expected, failureMessage) {
|
|
236
|
+
try {
|
|
237
|
+
recursiveAssertJsonEquals(actual, expected);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return actual;
|
|
241
|
+
}
|
|
242
|
+
throw new AssertionError('Values are JSON equal.', failureMessage);
|
|
243
|
+
},
|
|
157
244
|
},
|
|
158
245
|
checkWrap: {
|
|
159
246
|
/**
|
|
@@ -179,7 +266,14 @@ export const jsonEqualityGuards = {
|
|
|
179
266
|
* - {@link checkWrap.entriesEqual} : another deep equality check.
|
|
180
267
|
* - {@link checkWrap.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
181
268
|
*/
|
|
182
|
-
jsonEquals
|
|
269
|
+
jsonEquals(actual, expected) {
|
|
270
|
+
if (recursiveCheckJsonEquals(actual, expected)) {
|
|
271
|
+
return actual;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
},
|
|
183
277
|
/**
|
|
184
278
|
* Checks that two values are _not_ deeply equal when stringified into JSON. This may not
|
|
185
279
|
* make any sense if the values are not valid JSON. This internally sorts all given object
|
|
@@ -203,7 +297,14 @@ export const jsonEqualityGuards = {
|
|
|
203
297
|
* - {@link checkWrap.entriesEqual} : another deep equality check.
|
|
204
298
|
* - {@link checkWrap.deepEquals} : the most thorough (but also slow) deep equality check.
|
|
205
299
|
*/
|
|
206
|
-
notJsonEquals
|
|
300
|
+
notJsonEquals(actual, expected) {
|
|
301
|
+
if (recursiveCheckJsonEquals(actual, expected)) {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
return actual;
|
|
306
|
+
}
|
|
307
|
+
},
|
|
207
308
|
},
|
|
208
309
|
waitUntil: {
|
|
209
310
|
/**
|
|
@@ -235,7 +336,7 @@ export const jsonEqualityGuards = {
|
|
|
235
336
|
* - {@link waitUntil.entriesEqual} : another deep equality assertion.
|
|
236
337
|
* - {@link waitUntil.deepEquals} : the most thorough (but also slow) deep equality assertion.
|
|
237
338
|
*/
|
|
238
|
-
jsonEquals:
|
|
339
|
+
jsonEquals: createWaitUntil(assertions.jsonEquals),
|
|
239
340
|
/**
|
|
240
341
|
* Repeatedly calls a callback until its output is _not_ deeply equal to the first input
|
|
241
342
|
* when stringified into JSON. This may not make any sense if the values are not valid JSON.
|
|
@@ -266,6 +367,6 @@ export const jsonEqualityGuards = {
|
|
|
266
367
|
* - {@link waitUntil.entriesEqual} : another not deep equality assertion.
|
|
267
368
|
* - {@link waitUntil.deepEquals} : the most thorough (but also slow) not deep equality assertion.
|
|
268
369
|
*/
|
|
269
|
-
notJsonEquals:
|
|
370
|
+
notJsonEquals: createWaitUntil(assertions.notJsonEquals),
|
|
270
371
|
},
|
|
271
372
|
};
|