@kerebron/test-utils 0.4.27 → 0.4.29
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/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +58 -0
- package/esm/_dnt.shims.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.d.ts +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.js +47 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts +24 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.js +47 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assert.d.ts +16 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assert.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assert.js +23 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assert.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.js +31 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equal.d.ts +17 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equal.js +187 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equal.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equals.d.ts +35 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equals.js +60 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equals.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/exists.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/exists.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/exists.js +28 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/exists.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/fail.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/fail.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/fail.js +21 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/fail.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/false.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/false.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/false.js +23 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/false.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater.js +30 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.js +30 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts +23 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.js +53 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts +25 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.js +54 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less.d.ts +19 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.js +30 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/match.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/match.js +27 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/match.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/mod.d.ts +44 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/mod.js +46 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/mod.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts +21 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.js +34 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.js +30 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.js +27 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts +23 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.js +33 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.js +168 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts +42 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.js +54 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts +24 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.js +59 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.js +27 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/throws.d.ts +45 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/throws.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/throws.js +45 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/throws.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.js +21 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.js +21 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.js.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts +80 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.js +117 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.js.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff.d.ts +140 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff.js +277 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff.js.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts +99 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.js +183 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.js.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/format.d.ts +29 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/format.js +71 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/format.js.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/styles.d.ts +159 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/styles.js +208 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/styles.js.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.d.ts +25 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.js +4 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/test-utils.d.ts +3 -0
- package/esm/test-utils.d.ts.map +1 -0
- package/esm/test-utils.js +12 -0
- package/esm/test-utils.js.map +1 -0
- package/package.json +5 -2
- package/src/_dnt.shims.ts +60 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/almost_equals.ts +55 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/array_includes.ts +57 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/assert.ts +23 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/assertion_error.ts +31 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/equal.ts +218 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/equals.ts +66 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/exists.ts +31 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/fail.ts +21 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/false.ts +26 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/greater.ts +30 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.ts +36 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/instance_of.ts +64 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/is_error.ts +65 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/less.ts +29 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/less_or_equal.ts +36 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/match.ts +30 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/mod.ts +47 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/not_equals.ts +37 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/not_instance_of.ts +33 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/not_match.ts +30 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.ts +42 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/object_match.ts +214 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/rejects.ts +123 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/strict_equals.ts +68 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/string_includes.ts +30 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/throws.ts +111 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/unimplemented.ts +21 -0
- package/src/deps/jsr.io/@std/assert/1.0.16/unreachable.ts +21 -0
- package/src/deps/jsr.io/@std/internal/1.0.12/build_message.ts +147 -0
- package/src/deps/jsr.io/@std/internal/1.0.12/diff.ts +317 -0
- package/src/deps/jsr.io/@std/internal/1.0.12/diff_str.ts +208 -0
- package/src/deps/jsr.io/@std/internal/1.0.12/format.ts +89 -0
- package/src/deps/jsr.io/@std/internal/1.0.12/styles.ts +233 -0
- package/src/deps/jsr.io/@std/internal/1.0.12/types.ts +30 -0
- package/src/test-utils.ts +12 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { assertEquals } from "./equals.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Make an assertion that `expected` object is a subset of `actual` object,
|
|
7
|
+
* deeply. If not, then throw a diff of the objects, with mismatching
|
|
8
|
+
* properties highlighted.
|
|
9
|
+
*
|
|
10
|
+
* @example Usage
|
|
11
|
+
* ```ts ignore
|
|
12
|
+
* import { assertObjectMatch } from "@std/assert";
|
|
13
|
+
*
|
|
14
|
+
* assertObjectMatch({ foo: "bar" }, { foo: "bar" }); // Doesn't throw
|
|
15
|
+
* assertObjectMatch({ foo: "bar" }, { foo: "baz" }); // Throws
|
|
16
|
+
* assertObjectMatch({ foo: 1, bar: 2 }, { foo: 1 }); // Doesn't throw
|
|
17
|
+
* assertObjectMatch({ foo: 1 }, { foo: 1, bar: 2 }); // Throws
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example Usage with nested objects
|
|
21
|
+
* ```ts ignore
|
|
22
|
+
* import { assertObjectMatch } from "@std/assert";
|
|
23
|
+
*
|
|
24
|
+
* assertObjectMatch({ foo: { bar: 3, baz: 4 } }, { foo: { bar: 3 } }); // Doesn't throw
|
|
25
|
+
* assertObjectMatch({ foo: { bar: 3 } }, { foo: { bar: 3, baz: 4 } }); // Throws
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param actual The actual value to be matched.
|
|
29
|
+
* @param expected The expected value to match.
|
|
30
|
+
* @param msg The optional message to display if the assertion fails.
|
|
31
|
+
*/
|
|
32
|
+
export function assertObjectMatch(
|
|
33
|
+
// deno-lint-ignore no-explicit-any
|
|
34
|
+
actual: Record<PropertyKey, any>,
|
|
35
|
+
expected: Record<PropertyKey, unknown>,
|
|
36
|
+
msg?: string,
|
|
37
|
+
): void {
|
|
38
|
+
return assertEquals(
|
|
39
|
+
// get the intersection of "actual" and "expected"
|
|
40
|
+
// side effect: all the instances' constructor field is "Object" now.
|
|
41
|
+
filter(actual, expected),
|
|
42
|
+
// set (nested) instances' constructor field to be "Object" without changing expected value.
|
|
43
|
+
// see https://github.com/denoland/std/pull/1419
|
|
44
|
+
filter(expected, expected),
|
|
45
|
+
msg,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
type Loose = Record<PropertyKey, unknown>;
|
|
50
|
+
|
|
51
|
+
function isObject(val: unknown): boolean {
|
|
52
|
+
return typeof val === "object" && val !== null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function defineProperty(target: object, key: PropertyKey, value: unknown) {
|
|
56
|
+
return Object.defineProperty(target, key, {
|
|
57
|
+
value,
|
|
58
|
+
configurable: true,
|
|
59
|
+
enumerable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function filter(a: Loose, b: Loose): Loose {
|
|
65
|
+
const seen = new WeakMap<Loose | unknown[], Loose | unknown[]>();
|
|
66
|
+
return filterObject(a, b);
|
|
67
|
+
|
|
68
|
+
function filterObject(a: Loose, b: Loose): Loose {
|
|
69
|
+
// Prevent infinite loop with circular references with same filter
|
|
70
|
+
const memo = seen.get(a);
|
|
71
|
+
if (memo && (memo === b)) return a;
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
seen.set(a, b);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
if (err instanceof TypeError) {
|
|
77
|
+
throw new TypeError(
|
|
78
|
+
`Cannot assertObjectMatch ${a === null ? null : `type ${typeof a}`}`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Filter keys and symbols which are present in both actual and expected
|
|
84
|
+
const filtered = {} as Loose;
|
|
85
|
+
const keysA = Reflect.ownKeys(a);
|
|
86
|
+
const keysB = Reflect.ownKeys(b);
|
|
87
|
+
const entries = keysA.filter((key) => keysB.includes(key))
|
|
88
|
+
.map((key) => [key, a[key as string]]) as Array<[string, unknown]>;
|
|
89
|
+
|
|
90
|
+
if (keysA.length && keysB.length && !entries.length) {
|
|
91
|
+
// If both objects are not empty but don't have the same keys or symbols,
|
|
92
|
+
// returns the entries in object a.
|
|
93
|
+
for (const key of keysA) defineProperty(filtered, key, a[key]);
|
|
94
|
+
return filtered;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const [key, value] of entries) {
|
|
98
|
+
// On regexp references, keep value as it to avoid losing pattern and flags
|
|
99
|
+
if (value instanceof RegExp) {
|
|
100
|
+
defineProperty(filtered, key, value);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const subset = (b as Loose)[key];
|
|
105
|
+
|
|
106
|
+
// On array references, build a filtered array and filter nested objects inside
|
|
107
|
+
if (Array.isArray(value) && Array.isArray(subset)) {
|
|
108
|
+
defineProperty(filtered, key, filterArray(value, subset));
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// On nested objects references, build a filtered object recursively
|
|
113
|
+
if (isObject(value) && isObject(subset)) {
|
|
114
|
+
// When both operands are maps, build a filtered map with common keys and filter nested objects inside
|
|
115
|
+
if ((value instanceof Map) && (subset instanceof Map)) {
|
|
116
|
+
defineProperty(
|
|
117
|
+
filtered,
|
|
118
|
+
key,
|
|
119
|
+
new Map(
|
|
120
|
+
[...value].filter(([k]) => subset.has(k)).map(
|
|
121
|
+
([k, v]) => {
|
|
122
|
+
const v2 = subset.get(k);
|
|
123
|
+
if (isObject(v) && isObject(v2)) {
|
|
124
|
+
return [k, filterObject(v as Loose, v2 as Loose)];
|
|
125
|
+
}
|
|
126
|
+
return [k, v];
|
|
127
|
+
},
|
|
128
|
+
),
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// When both operands are set, build a filtered set with common values
|
|
135
|
+
if ((value instanceof Set) && (subset instanceof Set)) {
|
|
136
|
+
defineProperty(filtered, key, value.intersection(subset));
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
defineProperty(
|
|
141
|
+
filtered,
|
|
142
|
+
key,
|
|
143
|
+
filterObject(value as Loose, subset as Loose),
|
|
144
|
+
);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
defineProperty(filtered, key, value);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return filtered;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function filterArray(a: unknown[], b: unknown[]): unknown[] {
|
|
155
|
+
// Prevent infinite loop with circular references with same filter
|
|
156
|
+
const memo = seen.get(a);
|
|
157
|
+
if (memo && (memo === b)) return a;
|
|
158
|
+
|
|
159
|
+
seen.set(a, b);
|
|
160
|
+
|
|
161
|
+
const filtered: unknown[] = [];
|
|
162
|
+
const count = Math.min(a.length, b.length);
|
|
163
|
+
|
|
164
|
+
for (let i = 0; i < count; ++i) {
|
|
165
|
+
const value = a[i];
|
|
166
|
+
const subset = b[i];
|
|
167
|
+
|
|
168
|
+
// On regexp references, keep value as it to avoid losing pattern and flags
|
|
169
|
+
if (value instanceof RegExp) {
|
|
170
|
+
filtered.push(value);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// On array references, build a filtered array and filter nested objects inside
|
|
175
|
+
if (Array.isArray(value) && Array.isArray(subset)) {
|
|
176
|
+
filtered.push(filterArray(value, subset));
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// On nested objects references, build a filtered object recursively
|
|
181
|
+
if (isObject(value) && isObject(subset)) {
|
|
182
|
+
// When both operands are maps, build a filtered map with common keys and filter nested objects inside
|
|
183
|
+
if ((value instanceof Map) && (subset instanceof Map)) {
|
|
184
|
+
const map = new Map(
|
|
185
|
+
[...value].filter(([k]) => subset.has(k))
|
|
186
|
+
.map(([k, v]) => {
|
|
187
|
+
const v2 = subset.get(k);
|
|
188
|
+
if (isObject(v) && isObject(v2)) {
|
|
189
|
+
return [k, filterObject(v as Loose, v2 as Loose)];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return [k, v];
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
filtered.push(map);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// When both operands are set, build a filtered set with common values
|
|
200
|
+
if ((value instanceof Set) && (subset instanceof Set)) {
|
|
201
|
+
filtered.push(value.intersection(subset));
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
filtered.push(filterObject(value as Loose, subset as Loose));
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
filtered.push(value);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return filtered;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
import { assertIsError } from "./is_error.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Executes a function which returns a promise, expecting it to reject.
|
|
8
|
+
*
|
|
9
|
+
* To assert that a synchronous function throws, use {@linkcode assertThrows}.
|
|
10
|
+
*
|
|
11
|
+
* @example Usage
|
|
12
|
+
* ```ts ignore
|
|
13
|
+
* import { assertRejects } from "@std/assert";
|
|
14
|
+
*
|
|
15
|
+
* await assertRejects(async () => Promise.reject(new Error())); // Doesn't throw
|
|
16
|
+
* await assertRejects(async () => console.log("Hello world")); // Throws
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param fn The function to execute.
|
|
20
|
+
* @param msg The optional message to display if the assertion fails.
|
|
21
|
+
* @returns The promise which resolves to the thrown error.
|
|
22
|
+
*/
|
|
23
|
+
export function assertRejects(
|
|
24
|
+
fn: () => PromiseLike<unknown>,
|
|
25
|
+
msg?: string,
|
|
26
|
+
): Promise<unknown>;
|
|
27
|
+
/**
|
|
28
|
+
* Executes a function which returns a promise, expecting it to reject.
|
|
29
|
+
* If it does not, then it throws. An error class and a string that should be
|
|
30
|
+
* included in the error message can also be asserted.
|
|
31
|
+
*
|
|
32
|
+
* To assert that a synchronous function throws, use {@linkcode assertThrows}.
|
|
33
|
+
*
|
|
34
|
+
* @example Usage
|
|
35
|
+
* ```ts ignore
|
|
36
|
+
* import { assertRejects } from "@std/assert";
|
|
37
|
+
*
|
|
38
|
+
* await assertRejects(async () => Promise.reject(new Error()), Error); // Doesn't throw
|
|
39
|
+
* await assertRejects(async () => Promise.reject(new Error()), SyntaxError); // Throws
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @typeParam E The error class to assert.
|
|
43
|
+
* @param fn The function to execute.
|
|
44
|
+
* @param ErrorClass The error class to assert.
|
|
45
|
+
* @param msgIncludes The string that should be included in the error message.
|
|
46
|
+
* @param msg The optional message to display if the assertion fails.
|
|
47
|
+
* @returns The promise which resolves to the thrown error.
|
|
48
|
+
*/
|
|
49
|
+
export function assertRejects<E extends Error = Error>(
|
|
50
|
+
fn: () => PromiseLike<unknown>,
|
|
51
|
+
// deno-lint-ignore no-explicit-any
|
|
52
|
+
ErrorClass: abstract new (...args: any[]) => E,
|
|
53
|
+
msgIncludes?: string,
|
|
54
|
+
msg?: string,
|
|
55
|
+
): Promise<E>;
|
|
56
|
+
export async function assertRejects<E extends Error = Error>(
|
|
57
|
+
fn: () => PromiseLike<unknown>,
|
|
58
|
+
errorClassOrMsg?:
|
|
59
|
+
// deno-lint-ignore no-explicit-any
|
|
60
|
+
| (abstract new (...args: any[]) => E)
|
|
61
|
+
| string,
|
|
62
|
+
msgIncludesOrMsg?: string,
|
|
63
|
+
msg?: string,
|
|
64
|
+
): Promise<E | Error | unknown> {
|
|
65
|
+
// deno-lint-ignore no-explicit-any
|
|
66
|
+
let ErrorClass: (abstract new (...args: any[]) => E) | undefined;
|
|
67
|
+
let msgIncludes: string | undefined;
|
|
68
|
+
let err;
|
|
69
|
+
|
|
70
|
+
if (typeof errorClassOrMsg !== "string") {
|
|
71
|
+
if (
|
|
72
|
+
errorClassOrMsg === undefined ||
|
|
73
|
+
errorClassOrMsg.prototype instanceof Error ||
|
|
74
|
+
errorClassOrMsg.prototype === Error.prototype
|
|
75
|
+
) {
|
|
76
|
+
ErrorClass = errorClassOrMsg;
|
|
77
|
+
msgIncludes = msgIncludesOrMsg;
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
msg = errorClassOrMsg;
|
|
81
|
+
}
|
|
82
|
+
let doesThrow = false;
|
|
83
|
+
let isPromiseReturned = false;
|
|
84
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
85
|
+
try {
|
|
86
|
+
const possiblePromise = fn();
|
|
87
|
+
if (
|
|
88
|
+
possiblePromise &&
|
|
89
|
+
typeof possiblePromise === "object" &&
|
|
90
|
+
typeof possiblePromise.then === "function"
|
|
91
|
+
) {
|
|
92
|
+
isPromiseReturned = true;
|
|
93
|
+
await possiblePromise;
|
|
94
|
+
} else {
|
|
95
|
+
throw new Error();
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (!isPromiseReturned) {
|
|
99
|
+
throw new AssertionError(
|
|
100
|
+
`Function throws when expected to reject${msgSuffix}`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
if (ErrorClass) {
|
|
104
|
+
if (!(error instanceof Error)) {
|
|
105
|
+
throw new AssertionError(`A non-Error object was rejected${msgSuffix}`);
|
|
106
|
+
}
|
|
107
|
+
assertIsError(
|
|
108
|
+
error,
|
|
109
|
+
ErrorClass,
|
|
110
|
+
msgIncludes,
|
|
111
|
+
msg,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
err = error;
|
|
115
|
+
doesThrow = true;
|
|
116
|
+
}
|
|
117
|
+
if (!doesThrow) {
|
|
118
|
+
throw new AssertionError(
|
|
119
|
+
`Expected function to reject${msgSuffix}`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
return err;
|
|
123
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { buildMessage } from "../../internal/1.0.12/build_message.js";
|
|
4
|
+
import { diff } from "../../internal/1.0.12/diff.js";
|
|
5
|
+
import { diffStr } from "../../internal/1.0.12/diff_str.js";
|
|
6
|
+
import { format } from "../../internal/1.0.12/format.js";
|
|
7
|
+
import { red } from "../../internal/1.0.12/styles.js";
|
|
8
|
+
import { AssertionError } from "./assertion_error.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Make an assertion that `actual` and `expected` are strictly equal, using
|
|
12
|
+
* {@linkcode Object.is} for equality comparison. If not, then throw.
|
|
13
|
+
*
|
|
14
|
+
* @example Usage
|
|
15
|
+
* ```ts ignore
|
|
16
|
+
* import { assertStrictEquals } from "@std/assert";
|
|
17
|
+
*
|
|
18
|
+
* const a = {};
|
|
19
|
+
* const b = a;
|
|
20
|
+
* assertStrictEquals(a, b); // Doesn't throw
|
|
21
|
+
*
|
|
22
|
+
* const c = {};
|
|
23
|
+
* const d = {};
|
|
24
|
+
* assertStrictEquals(c, d); // Throws
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @typeParam T The type of the expected value.
|
|
28
|
+
* @param actual The actual value to compare.
|
|
29
|
+
* @param expected The expected value to compare.
|
|
30
|
+
* @param msg The optional message to display if the assertion fails.
|
|
31
|
+
*/
|
|
32
|
+
export function assertStrictEquals<T>(
|
|
33
|
+
actual: unknown,
|
|
34
|
+
expected: T,
|
|
35
|
+
msg?: string,
|
|
36
|
+
): asserts actual is T {
|
|
37
|
+
if (Object.is(actual, expected)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
42
|
+
let message: string;
|
|
43
|
+
|
|
44
|
+
const actualString = format(actual);
|
|
45
|
+
const expectedString = format(expected);
|
|
46
|
+
|
|
47
|
+
if (actualString === expectedString) {
|
|
48
|
+
const withOffset = actualString
|
|
49
|
+
.split("\n")
|
|
50
|
+
.map((l) => ` ${l}`)
|
|
51
|
+
.join("\n");
|
|
52
|
+
message =
|
|
53
|
+
`Values have the same structure but are not reference-equal${msgSuffix}\n\n${
|
|
54
|
+
red(withOffset)
|
|
55
|
+
}\n`;
|
|
56
|
+
} else {
|
|
57
|
+
const stringDiff = (typeof actual === "string") &&
|
|
58
|
+
(typeof expected === "string");
|
|
59
|
+
const diffResult = stringDiff
|
|
60
|
+
? diffStr(actual as string, expected as string)
|
|
61
|
+
: diff(actualString.split("\n"), expectedString.split("\n"));
|
|
62
|
+
const diffMsg = buildMessage(diffResult, { stringDiff }, arguments[3])
|
|
63
|
+
.join("\n");
|
|
64
|
+
message = `Values are not strictly equal${msgSuffix}\n${diffMsg}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw new AssertionError(message);
|
|
68
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Make an assertion that actual includes expected. If not
|
|
7
|
+
* then throw.
|
|
8
|
+
*
|
|
9
|
+
* @example Usage
|
|
10
|
+
* ```ts ignore
|
|
11
|
+
* import { assertStringIncludes } from "@std/assert";
|
|
12
|
+
*
|
|
13
|
+
* assertStringIncludes("Hello", "ello"); // Doesn't throw
|
|
14
|
+
* assertStringIncludes("Hello", "world"); // Throws
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @param actual The actual string to check for inclusion.
|
|
18
|
+
* @param expected The expected string to check for inclusion.
|
|
19
|
+
* @param msg The optional message to display if the assertion fails.
|
|
20
|
+
*/
|
|
21
|
+
export function assertStringIncludes(
|
|
22
|
+
actual: string,
|
|
23
|
+
expected: string,
|
|
24
|
+
msg?: string,
|
|
25
|
+
) {
|
|
26
|
+
if (actual.includes(expected)) return;
|
|
27
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
28
|
+
msg = `Expected actual: "${actual}" to contain: "${expected}"${msgSuffix}`;
|
|
29
|
+
throw new AssertionError(msg);
|
|
30
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { assertIsError } from "./is_error.js";
|
|
4
|
+
import { AssertionError } from "./assertion_error.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Executes a function, expecting it to throw. If it does not, then it
|
|
8
|
+
* throws.
|
|
9
|
+
*
|
|
10
|
+
* To assert that an asynchronous function rejects, use
|
|
11
|
+
* {@linkcode assertRejects}.
|
|
12
|
+
*
|
|
13
|
+
* @example Usage
|
|
14
|
+
* ```ts ignore
|
|
15
|
+
* import { assertThrows } from "@std/assert";
|
|
16
|
+
*
|
|
17
|
+
* assertThrows(() => { throw new TypeError("hello world!"); }); // Doesn't throw
|
|
18
|
+
* assertThrows(() => console.log("hello world!")); // Throws
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param fn The function to execute.
|
|
22
|
+
* @param msg The optional message to display if the assertion fails.
|
|
23
|
+
* @returns The error that was thrown.
|
|
24
|
+
*/
|
|
25
|
+
export function assertThrows(
|
|
26
|
+
fn: () => unknown,
|
|
27
|
+
msg?: string,
|
|
28
|
+
): unknown;
|
|
29
|
+
/**
|
|
30
|
+
* Executes a function, expecting it to throw. If it does not, then it
|
|
31
|
+
* throws. An error class and a string that should be included in the
|
|
32
|
+
* error message can also be asserted.
|
|
33
|
+
*
|
|
34
|
+
* To assert that an asynchronous function rejects, use
|
|
35
|
+
* {@linkcode assertRejects}.
|
|
36
|
+
*
|
|
37
|
+
* @example Usage
|
|
38
|
+
* ```ts ignore
|
|
39
|
+
* import { assertThrows } from "@std/assert";
|
|
40
|
+
*
|
|
41
|
+
* assertThrows(() => { throw new TypeError("hello world!"); }, TypeError); // Doesn't throw
|
|
42
|
+
* assertThrows(() => { throw new TypeError("hello world!"); }, RangeError); // Throws
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @typeParam E The error class to assert.
|
|
46
|
+
* @param fn The function to execute.
|
|
47
|
+
* @param ErrorClass The error class to assert.
|
|
48
|
+
* @param msgIncludes The string that should be included in the error message.
|
|
49
|
+
* @param msg The optional message to display if the assertion fails.
|
|
50
|
+
* @returns The error that was thrown.
|
|
51
|
+
*/
|
|
52
|
+
export function assertThrows<E extends Error = Error>(
|
|
53
|
+
fn: () => unknown,
|
|
54
|
+
// deno-lint-ignore no-explicit-any
|
|
55
|
+
ErrorClass: abstract new (...args: any[]) => E,
|
|
56
|
+
msgIncludes?: string,
|
|
57
|
+
msg?: string,
|
|
58
|
+
): E;
|
|
59
|
+
export function assertThrows<E extends Error = Error>(
|
|
60
|
+
fn: () => unknown,
|
|
61
|
+
errorClassOrMsg?:
|
|
62
|
+
// deno-lint-ignore no-explicit-any
|
|
63
|
+
| (abstract new (...args: any[]) => E)
|
|
64
|
+
| string,
|
|
65
|
+
msgIncludesOrMsg?: string,
|
|
66
|
+
msg?: string,
|
|
67
|
+
): E | Error | unknown {
|
|
68
|
+
// deno-lint-ignore no-explicit-any
|
|
69
|
+
let ErrorClass: (abstract new (...args: any[]) => E) | undefined;
|
|
70
|
+
let msgIncludes: string | undefined;
|
|
71
|
+
let err;
|
|
72
|
+
|
|
73
|
+
if (typeof errorClassOrMsg !== "string") {
|
|
74
|
+
if (
|
|
75
|
+
errorClassOrMsg === undefined ||
|
|
76
|
+
errorClassOrMsg?.prototype instanceof Error ||
|
|
77
|
+
errorClassOrMsg?.prototype === Error.prototype
|
|
78
|
+
) {
|
|
79
|
+
ErrorClass = errorClassOrMsg;
|
|
80
|
+
msgIncludes = msgIncludesOrMsg;
|
|
81
|
+
} else {
|
|
82
|
+
msg = msgIncludesOrMsg;
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
msg = errorClassOrMsg;
|
|
86
|
+
}
|
|
87
|
+
let doesThrow = false;
|
|
88
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
89
|
+
try {
|
|
90
|
+
fn();
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (ErrorClass) {
|
|
93
|
+
if (error instanceof Error === false) {
|
|
94
|
+
throw new AssertionError(`A non-Error object was thrown${msgSuffix}`);
|
|
95
|
+
}
|
|
96
|
+
assertIsError(
|
|
97
|
+
error,
|
|
98
|
+
ErrorClass,
|
|
99
|
+
msgIncludes,
|
|
100
|
+
msg,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
err = error;
|
|
104
|
+
doesThrow = true;
|
|
105
|
+
}
|
|
106
|
+
if (!doesThrow) {
|
|
107
|
+
msg = `Expected function to throw${msgSuffix}`;
|
|
108
|
+
throw new AssertionError(msg);
|
|
109
|
+
}
|
|
110
|
+
return err;
|
|
111
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Use this to stub out methods that will throw when invoked.
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts ignore
|
|
10
|
+
* import { unimplemented } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* unimplemented(); // Throws
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param msg Optional message to include in the error.
|
|
16
|
+
* @returns Never returns, always throws.
|
|
17
|
+
*/
|
|
18
|
+
export function unimplemented(msg?: string): never {
|
|
19
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
20
|
+
throw new AssertionError(`Unimplemented${msgSuffix}`);
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Use this to assert unreachable code.
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts ignore
|
|
10
|
+
* import { unreachable } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* unreachable(); // Throws
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param msg Optional message to include in the error.
|
|
16
|
+
* @returns Never returns, always throws.
|
|
17
|
+
*/
|
|
18
|
+
export function unreachable(msg?: string): never {
|
|
19
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
20
|
+
throw new AssertionError(`Unreachable${msgSuffix}`);
|
|
21
|
+
}
|