@kerebron/test-utils 0.4.28 → 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.js +1 -0
- package/esm/_dnt.shims.js.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -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.js +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.js.map +1 -0
- package/esm/test-utils.js +1 -0
- package/esm/test-utils.js.map +1 -0
- package/package.json +5 -1
- 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,57 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { equal } from "./equal.js";
|
|
4
|
+
import { format } from "../../internal/1.0.12/format.js";
|
|
5
|
+
import { AssertionError } from "./assertion_error.js";
|
|
6
|
+
|
|
7
|
+
/** An array-like object (`Array`, `Uint8Array`, `NodeList`, etc.) that is not a string */
|
|
8
|
+
export type ArrayLikeArg<T> = ArrayLike<T> & object;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Make an assertion that `actual` includes the `expected` values. If not then
|
|
12
|
+
* an error will be thrown.
|
|
13
|
+
*
|
|
14
|
+
* Type parameter can be specified to ensure values under comparison have the
|
|
15
|
+
* same type.
|
|
16
|
+
*
|
|
17
|
+
* @example Usage
|
|
18
|
+
* ```ts ignore
|
|
19
|
+
* import { assertArrayIncludes } from "@std/assert";
|
|
20
|
+
*
|
|
21
|
+
* assertArrayIncludes([1, 2], [2]); // Doesn't throw
|
|
22
|
+
* assertArrayIncludes([1, 2], [3]); // Throws
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @typeParam T The type of the elements in the array to compare.
|
|
26
|
+
* @param actual The array-like object to check for.
|
|
27
|
+
* @param expected The array-like object to check for.
|
|
28
|
+
* @param msg The optional message to display if the assertion fails.
|
|
29
|
+
*/
|
|
30
|
+
export function assertArrayIncludes<T>(
|
|
31
|
+
actual: ArrayLikeArg<T>,
|
|
32
|
+
expected: ArrayLikeArg<T>,
|
|
33
|
+
msg?: string,
|
|
34
|
+
) {
|
|
35
|
+
const missing: unknown[] = [];
|
|
36
|
+
for (let i = 0; i < expected.length; i++) {
|
|
37
|
+
let found = false;
|
|
38
|
+
for (let j = 0; j < actual.length; j++) {
|
|
39
|
+
if (equal(expected[i], actual[j])) {
|
|
40
|
+
found = true;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!found) {
|
|
45
|
+
missing.push(expected[i]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (missing.length === 0) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
53
|
+
msg = `Expected actual: "${format(actual)}" to include: "${
|
|
54
|
+
format(expected)
|
|
55
|
+
}"${msgSuffix}\nmissing: ${format(missing)}`;
|
|
56
|
+
throw new AssertionError(msg);
|
|
57
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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, error will be thrown if `expr` does not have truthy value.
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts ignore
|
|
10
|
+
* import { assert } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* assert("hello".includes("ello")); // Doesn't throw
|
|
13
|
+
* assert("hello".includes("world")); // Throws
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param expr The expression to test.
|
|
17
|
+
* @param msg The optional message to display if the assertion fails.
|
|
18
|
+
*/
|
|
19
|
+
export function assert(expr: unknown, msg = ""): asserts expr {
|
|
20
|
+
if (!expr) {
|
|
21
|
+
throw new AssertionError(msg);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when an assertion fails.
|
|
6
|
+
*
|
|
7
|
+
* @example Usage
|
|
8
|
+
* ```ts ignore
|
|
9
|
+
* import { AssertionError } from "@std/assert";
|
|
10
|
+
*
|
|
11
|
+
* try {
|
|
12
|
+
* throw new AssertionError("foo", { cause: "bar" });
|
|
13
|
+
* } catch (error) {
|
|
14
|
+
* if (error instanceof AssertionError) {
|
|
15
|
+
* error.message === "foo"; // true
|
|
16
|
+
* error.cause === "bar"; // true
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export class AssertionError extends Error {
|
|
22
|
+
/** Constructs a new instance.
|
|
23
|
+
*
|
|
24
|
+
* @param message The error message.
|
|
25
|
+
* @param options Additional options. This argument is still unstable. It may change in the future release.
|
|
26
|
+
*/
|
|
27
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
28
|
+
super(message, options);
|
|
29
|
+
this.name = "AssertionError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import * as dntShim from "../../../../../_dnt.shims.js";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
type KeyedCollection = Set<unknown> | Map<unknown, unknown>;
|
|
7
|
+
function isKeyedCollection(x: unknown): x is KeyedCollection {
|
|
8
|
+
return x instanceof Set || x instanceof Map;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function prototypesEqual(a: object, b: object) {
|
|
12
|
+
const pa = Object.getPrototypeOf(a);
|
|
13
|
+
const pb = Object.getPrototypeOf(b);
|
|
14
|
+
return pa === pb ||
|
|
15
|
+
pa === Object.prototype && pb === null ||
|
|
16
|
+
pa === null && pb === Object.prototype;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isBasicObjectOrArray(obj: object) {
|
|
20
|
+
const proto = Object.getPrototypeOf(obj);
|
|
21
|
+
return proto === null || proto === Object.prototype ||
|
|
22
|
+
proto === Array.prototype;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Slightly faster than Reflect.ownKeys in V8 as of 12.9.202.13-rusty (2024-10-28)
|
|
26
|
+
function ownKeys(obj: object) {
|
|
27
|
+
return [
|
|
28
|
+
...Object.getOwnPropertyNames(obj),
|
|
29
|
+
...Object.getOwnPropertySymbols(obj),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getKeysDeep(obj: object) {
|
|
34
|
+
const keys = new Set<string | symbol>();
|
|
35
|
+
|
|
36
|
+
while (obj !== Object.prototype && obj !== Array.prototype && obj != null) {
|
|
37
|
+
for (const key of ownKeys(obj)) {
|
|
38
|
+
keys.add(key);
|
|
39
|
+
}
|
|
40
|
+
obj = Object.getPrototypeOf(obj);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return keys;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// deno-lint-ignore no-explicit-any
|
|
47
|
+
const Temporal = (dntShim.dntGlobalThis as any).Temporal ?? Object.create(null);
|
|
48
|
+
|
|
49
|
+
/** A non-exhaustive list of prototypes that can be accurately fast-path compared with `String(instance)` */
|
|
50
|
+
const stringComparablePrototypes = new Set<unknown>(
|
|
51
|
+
[
|
|
52
|
+
Intl.Locale,
|
|
53
|
+
RegExp,
|
|
54
|
+
Temporal.Duration,
|
|
55
|
+
Temporal.Instant,
|
|
56
|
+
Temporal.PlainDate,
|
|
57
|
+
Temporal.PlainDateTime,
|
|
58
|
+
Temporal.PlainTime,
|
|
59
|
+
Temporal.PlainYearMonth,
|
|
60
|
+
Temporal.PlainMonthDay,
|
|
61
|
+
Temporal.ZonedDateTime,
|
|
62
|
+
URL,
|
|
63
|
+
URLSearchParams,
|
|
64
|
+
].filter((x) => x != null).map((x) => x.prototype),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
function isPrimitive(x: unknown) {
|
|
68
|
+
return typeof x === "string" ||
|
|
69
|
+
typeof x === "number" ||
|
|
70
|
+
typeof x === "boolean" ||
|
|
71
|
+
typeof x === "bigint" ||
|
|
72
|
+
typeof x === "symbol" ||
|
|
73
|
+
x == null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type TypedArray = Pick<Uint8Array | BigUint64Array, "length" | number>;
|
|
77
|
+
const TypedArray = Object.getPrototypeOf(Uint8Array);
|
|
78
|
+
function compareTypedArrays(a: TypedArray, b: TypedArray) {
|
|
79
|
+
if (a.length !== b.length) return false;
|
|
80
|
+
for (let i = 0; i < b.length; i++) {
|
|
81
|
+
if (!sameValueZero(a[i], b[i])) return false;
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Check both strict equality (`0 == -0`) and `Object.is` (`NaN == NaN`) */
|
|
87
|
+
function sameValueZero(a: unknown, b: unknown) {
|
|
88
|
+
return a === b || Object.is(a, b);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Deep equality comparison used in assertions.
|
|
93
|
+
*
|
|
94
|
+
* @param a The actual value
|
|
95
|
+
* @param b The expected value
|
|
96
|
+
* @returns `true` if the values are deeply equal, `false` otherwise
|
|
97
|
+
*
|
|
98
|
+
* @example Usage
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { equal } from "@std/assert/equal";
|
|
101
|
+
*
|
|
102
|
+
* equal({ foo: "bar" }, { foo: "bar" }); // Returns `true`
|
|
103
|
+
* equal({ foo: "bar" }, { foo: "baz" }); // Returns `false`
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export function equal(a: unknown, b: unknown): boolean {
|
|
107
|
+
const seen = new Map<unknown, unknown>();
|
|
108
|
+
return (function compare(a: unknown, b: unknown): boolean {
|
|
109
|
+
if (sameValueZero(a, b)) return true;
|
|
110
|
+
if (isPrimitive(a) || isPrimitive(b)) return false;
|
|
111
|
+
|
|
112
|
+
if (a instanceof Date && b instanceof Date) {
|
|
113
|
+
return Object.is(a.getTime(), b.getTime());
|
|
114
|
+
}
|
|
115
|
+
if (a && typeof a === "object" && b && typeof b === "object") {
|
|
116
|
+
if (!prototypesEqual(a, b)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
if (a instanceof TypedArray) {
|
|
120
|
+
return compareTypedArrays(a as TypedArray, b as TypedArray);
|
|
121
|
+
}
|
|
122
|
+
if (
|
|
123
|
+
a instanceof ArrayBuffer ||
|
|
124
|
+
(globalThis.SharedArrayBuffer && a instanceof SharedArrayBuffer)
|
|
125
|
+
) {
|
|
126
|
+
return compareTypedArrays(
|
|
127
|
+
new Uint8Array(a),
|
|
128
|
+
new Uint8Array(b as ArrayBuffer | SharedArrayBuffer),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
if (a instanceof WeakMap) {
|
|
132
|
+
throw new TypeError("Cannot compare WeakMap instances");
|
|
133
|
+
}
|
|
134
|
+
if (a instanceof WeakSet) {
|
|
135
|
+
throw new TypeError("Cannot compare WeakSet instances");
|
|
136
|
+
}
|
|
137
|
+
if (a instanceof WeakRef) {
|
|
138
|
+
return compare(a.deref(), (b as WeakRef<WeakKey>).deref());
|
|
139
|
+
}
|
|
140
|
+
if (seen.get(a) === b) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
if (Object.keys(a).length !== Object.keys(b).length) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
seen.set(a, b);
|
|
147
|
+
if (isKeyedCollection(a) && isKeyedCollection(b)) {
|
|
148
|
+
if (a.size !== b.size) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const aKeys = [...a.keys()];
|
|
153
|
+
const primitiveKeysFastPath = aKeys.every(isPrimitive);
|
|
154
|
+
if (primitiveKeysFastPath) {
|
|
155
|
+
if (a instanceof Set) {
|
|
156
|
+
return a.symmetricDifference(b).size === 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
for (const key of aKeys) {
|
|
160
|
+
if (
|
|
161
|
+
!b.has(key) ||
|
|
162
|
+
!compare(a.get(key), (b as Map<unknown, unknown>).get(key))
|
|
163
|
+
) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
let unmatchedEntries = a.size;
|
|
171
|
+
|
|
172
|
+
for (const [aKey, aValue] of a.entries()) {
|
|
173
|
+
for (const [bKey, bValue] of b.entries()) {
|
|
174
|
+
/* Given that Map keys can be references, we need
|
|
175
|
+
* to ensure that they are also deeply equal */
|
|
176
|
+
|
|
177
|
+
if (!compare(aKey, bKey)) continue;
|
|
178
|
+
|
|
179
|
+
if (
|
|
180
|
+
(aKey === aValue && bKey === bValue) ||
|
|
181
|
+
(compare(aValue, bValue))
|
|
182
|
+
) {
|
|
183
|
+
unmatchedEntries--;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return unmatchedEntries === 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let keys: Iterable<string | symbol>;
|
|
193
|
+
|
|
194
|
+
if (isBasicObjectOrArray(a)) {
|
|
195
|
+
// fast path
|
|
196
|
+
keys = ownKeys({ ...a, ...b });
|
|
197
|
+
} else if (stringComparablePrototypes.has(Object.getPrototypeOf(a))) {
|
|
198
|
+
// medium path
|
|
199
|
+
return String(a) === String(b);
|
|
200
|
+
} else {
|
|
201
|
+
// slow path
|
|
202
|
+
keys = getKeysDeep(a).union(getKeysDeep(b));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (const key of keys) {
|
|
206
|
+
type Key = keyof typeof a;
|
|
207
|
+
if (!compare(a[key as Key], b[key as Key])) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
if (((key in a) && (!(key in b))) || ((key in b) && (!(key in a)))) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
217
|
+
})(a, b);
|
|
218
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { equal } from "./equal.js";
|
|
4
|
+
import { buildMessage } from "../../internal/1.0.12/build_message.js";
|
|
5
|
+
import { diff } from "../../internal/1.0.12/diff.js";
|
|
6
|
+
import { diffStr } from "../../internal/1.0.12/diff_str.js";
|
|
7
|
+
import { format } from "../../internal/1.0.12/format.js";
|
|
8
|
+
|
|
9
|
+
import { AssertionError } from "./assertion_error.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Make an assertion that `actual` and `expected` are equal, deeply. If not
|
|
13
|
+
* deeply equal, then throw.
|
|
14
|
+
*
|
|
15
|
+
* Type parameter can be specified to ensure values under comparison have the
|
|
16
|
+
* same type.
|
|
17
|
+
*
|
|
18
|
+
* Note: When comparing `Blob` objects, you should first convert them to
|
|
19
|
+
* `Uint8Array` using the `Blob.bytes()` method and then compare their
|
|
20
|
+
* contents.
|
|
21
|
+
*
|
|
22
|
+
* @example Usage
|
|
23
|
+
* ```ts ignore
|
|
24
|
+
* import { assertEquals } from "@std/assert";
|
|
25
|
+
*
|
|
26
|
+
* assertEquals("world", "world"); // Doesn't throw
|
|
27
|
+
* assertEquals("hello", "world"); // Throws
|
|
28
|
+
* ```
|
|
29
|
+
* @example Compare `Blob` objects
|
|
30
|
+
* ```ts ignore
|
|
31
|
+
* import { assertEquals } from "@std/assert";
|
|
32
|
+
*
|
|
33
|
+
* const bytes1 = await new Blob(["foo"]).bytes();
|
|
34
|
+
* const bytes2 = await new Blob(["foo"]).bytes();
|
|
35
|
+
*
|
|
36
|
+
* assertEquals(bytes1, bytes2);
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @typeParam T The type of the values to compare. This is usually inferred.
|
|
40
|
+
* @param actual The actual value to compare.
|
|
41
|
+
* @param expected The expected value to compare.
|
|
42
|
+
* @param msg The optional message to display if the assertion fails.
|
|
43
|
+
*/
|
|
44
|
+
export function assertEquals<T>(
|
|
45
|
+
actual: T,
|
|
46
|
+
expected: T,
|
|
47
|
+
msg?: string,
|
|
48
|
+
) {
|
|
49
|
+
if (equal(actual, expected)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
53
|
+
let message = `Values are not equal${msgSuffix}`;
|
|
54
|
+
|
|
55
|
+
const actualString = format(actual);
|
|
56
|
+
const expectedString = format(expected);
|
|
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 = `${message}\n${diffMsg}`;
|
|
65
|
+
throw new AssertionError(message);
|
|
66
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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 is not null or undefined.
|
|
7
|
+
* If not then throw.
|
|
8
|
+
*
|
|
9
|
+
* @example Usage
|
|
10
|
+
* ```ts ignore
|
|
11
|
+
* import { assertExists } from "@std/assert";
|
|
12
|
+
*
|
|
13
|
+
* assertExists("something"); // Doesn't throw
|
|
14
|
+
* assertExists(undefined); // Throws
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @typeParam T The type of the actual value.
|
|
18
|
+
* @param actual The actual value to check.
|
|
19
|
+
* @param msg The optional message to include in the error if the assertion fails.
|
|
20
|
+
*/
|
|
21
|
+
export function assertExists<T>(
|
|
22
|
+
actual: T,
|
|
23
|
+
msg?: string,
|
|
24
|
+
): asserts actual is NonNullable<T> {
|
|
25
|
+
if (actual === undefined || actual === null) {
|
|
26
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
27
|
+
msg =
|
|
28
|
+
`Expected actual: "${actual}" to not be null or undefined${msgSuffix}`;
|
|
29
|
+
throw new AssertionError(msg);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -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
|
+
* Forcefully throws a failed assertion.
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts ignore
|
|
10
|
+
* import { fail } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* fail("Deliberately failed!"); // Throws
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param msg Optional message to include in the error.
|
|
16
|
+
* @returns Never returns, always throws.
|
|
17
|
+
*/
|
|
18
|
+
export function fail(msg?: string): never {
|
|
19
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
20
|
+
throw new AssertionError(`Failed assertion${msgSuffix}`);
|
|
21
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
|
|
5
|
+
/** Assertion condition for {@linkcode assertFalse}. */
|
|
6
|
+
export type Falsy = false | 0 | 0n | "" | null | undefined;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Make an assertion, error will be thrown if `expr` have truthy value.
|
|
10
|
+
*
|
|
11
|
+
* @example Usage
|
|
12
|
+
* ```ts ignore
|
|
13
|
+
* import { assertFalse } from "@std/assert";
|
|
14
|
+
*
|
|
15
|
+
* assertFalse(false); // Doesn't throw
|
|
16
|
+
* assertFalse(true); // Throws
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param expr The expression to test.
|
|
20
|
+
* @param msg The optional message to display if the assertion fails.
|
|
21
|
+
*/
|
|
22
|
+
export function assertFalse(expr: unknown, msg = ""): asserts expr is Falsy {
|
|
23
|
+
if (expr) {
|
|
24
|
+
throw new AssertionError(msg);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { format } from "../../internal/1.0.12/format.js";
|
|
4
|
+
import { AssertionError } from "./assertion_error.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Make an assertion that `actual` is greater than `expected`.
|
|
8
|
+
* If not then throw.
|
|
9
|
+
*
|
|
10
|
+
* @example Usage
|
|
11
|
+
* ```ts ignore
|
|
12
|
+
* import { assertGreater } from "@std/assert";
|
|
13
|
+
*
|
|
14
|
+
* assertGreater(2, 1); // Doesn't throw
|
|
15
|
+
* assertGreater(1, 1); // Throws
|
|
16
|
+
* assertGreater(0, 1); // Throws
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @typeParam T The type of the values to compare.
|
|
20
|
+
* @param actual The actual value to compare.
|
|
21
|
+
* @param expected The expected value to compare.
|
|
22
|
+
* @param msg The optional message to display if the assertion fails.
|
|
23
|
+
*/
|
|
24
|
+
export function assertGreater<T>(actual: T, expected: T, msg?: string) {
|
|
25
|
+
if (actual > expected) return;
|
|
26
|
+
|
|
27
|
+
const actualString = format(actual);
|
|
28
|
+
const expectedString = format(expected);
|
|
29
|
+
throw new AssertionError(msg ?? `Expect ${actualString} > ${expectedString}`);
|
|
30
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { format } from "../../internal/1.0.12/format.js";
|
|
4
|
+
import { AssertionError } from "./assertion_error.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Make an assertion that `actual` is greater than or equal to `expected`.
|
|
8
|
+
* If not then throw.
|
|
9
|
+
*
|
|
10
|
+
* @example Usage
|
|
11
|
+
* ```ts ignore
|
|
12
|
+
* import { assertGreaterOrEqual } from "@std/assert";
|
|
13
|
+
*
|
|
14
|
+
* assertGreaterOrEqual(2, 1); // Doesn't throw
|
|
15
|
+
* assertGreaterOrEqual(1, 1); // Doesn't throw
|
|
16
|
+
* assertGreaterOrEqual(0, 1); // Throws
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @typeParam T The type of the values to compare.
|
|
20
|
+
* @param actual The actual value to compare.
|
|
21
|
+
* @param expected The expected value to compare.
|
|
22
|
+
* @param msg The optional message to display if the assertion fails.
|
|
23
|
+
*/
|
|
24
|
+
export function assertGreaterOrEqual<T>(
|
|
25
|
+
actual: T,
|
|
26
|
+
expected: T,
|
|
27
|
+
msg?: string,
|
|
28
|
+
) {
|
|
29
|
+
if (actual >= expected) return;
|
|
30
|
+
|
|
31
|
+
const actualString = format(actual);
|
|
32
|
+
const expectedString = format(expected);
|
|
33
|
+
throw new AssertionError(
|
|
34
|
+
msg ?? `Expect ${actualString} >= ${expectedString}`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
|
|
5
|
+
/** Any constructor */
|
|
6
|
+
// deno-lint-ignore no-explicit-any
|
|
7
|
+
export type AnyConstructor = new (...args: any[]) => any;
|
|
8
|
+
/** Gets constructor type */
|
|
9
|
+
export type GetConstructorType<T extends AnyConstructor> = InstanceType<T>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Make an assertion that `obj` is an instance of `type`.
|
|
13
|
+
* If not then throw.
|
|
14
|
+
*
|
|
15
|
+
* @example Usage
|
|
16
|
+
* ```ts ignore
|
|
17
|
+
* import { assertInstanceOf } from "@std/assert";
|
|
18
|
+
*
|
|
19
|
+
* assertInstanceOf(new Date(), Date); // Doesn't throw
|
|
20
|
+
* assertInstanceOf(new Date(), Number); // Throws
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @typeParam T The expected type of the object.
|
|
24
|
+
* @param actual The object to check.
|
|
25
|
+
* @param expectedType The expected class constructor.
|
|
26
|
+
* @param msg The optional message to display if the assertion fails.
|
|
27
|
+
*/
|
|
28
|
+
export function assertInstanceOf<
|
|
29
|
+
// deno-lint-ignore no-explicit-any
|
|
30
|
+
T extends abstract new (...args: any[]) => any,
|
|
31
|
+
>(
|
|
32
|
+
actual: unknown,
|
|
33
|
+
expectedType: T,
|
|
34
|
+
msg = "",
|
|
35
|
+
): asserts actual is InstanceType<T> {
|
|
36
|
+
if (actual instanceof expectedType) return;
|
|
37
|
+
|
|
38
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
39
|
+
const expectedTypeStr = expectedType.name;
|
|
40
|
+
|
|
41
|
+
let actualTypeStr = "";
|
|
42
|
+
if (actual === null) {
|
|
43
|
+
actualTypeStr = "null";
|
|
44
|
+
} else if (actual === undefined) {
|
|
45
|
+
actualTypeStr = "undefined";
|
|
46
|
+
} else if (typeof actual === "object") {
|
|
47
|
+
actualTypeStr = actual.constructor?.name ?? "Object";
|
|
48
|
+
} else {
|
|
49
|
+
actualTypeStr = typeof actual;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (expectedTypeStr === actualTypeStr) {
|
|
53
|
+
msg =
|
|
54
|
+
`Expected object to be an instance of "${expectedTypeStr}"${msgSuffix}`;
|
|
55
|
+
} else if (actualTypeStr === "function") {
|
|
56
|
+
msg =
|
|
57
|
+
`Expected object to be an instance of "${expectedTypeStr}" but was not an instanced object${msgSuffix}`;
|
|
58
|
+
} else {
|
|
59
|
+
msg =
|
|
60
|
+
`Expected object to be an instance of "${expectedTypeStr}" but was "${actualTypeStr}"${msgSuffix}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
throw new AssertionError(msg);
|
|
64
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
import { stripAnsiCode } from "../../internal/1.0.12/styles.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Make an assertion that `error` is an `Error`.
|
|
8
|
+
* If not then an error will be thrown.
|
|
9
|
+
* An error class and a string that should be included in the
|
|
10
|
+
* error message can also be asserted.
|
|
11
|
+
*
|
|
12
|
+
* @example Usage
|
|
13
|
+
* ```ts ignore
|
|
14
|
+
* import { assertIsError } from "@std/assert";
|
|
15
|
+
*
|
|
16
|
+
* assertIsError(null); // Throws
|
|
17
|
+
* assertIsError(new RangeError("Out of range")); // Doesn't throw
|
|
18
|
+
* assertIsError(new RangeError("Out of range"), SyntaxError); // Throws
|
|
19
|
+
* assertIsError(new RangeError("Out of range"), SyntaxError, "Out of range"); // Doesn't throw
|
|
20
|
+
* assertIsError(new RangeError("Out of range"), SyntaxError, "Within range"); // Throws
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @typeParam E The type of the error to assert.
|
|
24
|
+
* @param error The error to assert.
|
|
25
|
+
* @param ErrorClass The optional error class to assert.
|
|
26
|
+
* @param msgMatches The optional string or RegExp to assert in the error message.
|
|
27
|
+
* @param msg The optional message to display if the assertion fails.
|
|
28
|
+
*/
|
|
29
|
+
export function assertIsError<E extends Error = Error>(
|
|
30
|
+
error: unknown,
|
|
31
|
+
// deno-lint-ignore no-explicit-any
|
|
32
|
+
ErrorClass?: abstract new (...args: any[]) => E,
|
|
33
|
+
msgMatches?: string | RegExp,
|
|
34
|
+
msg?: string,
|
|
35
|
+
): asserts error is E {
|
|
36
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
37
|
+
if (!(error instanceof Error)) {
|
|
38
|
+
throw new AssertionError(
|
|
39
|
+
`Expected "error" to be an Error object${msgSuffix}`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
if (ErrorClass && !(error instanceof ErrorClass)) {
|
|
43
|
+
msg =
|
|
44
|
+
`Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`;
|
|
45
|
+
throw new AssertionError(msg);
|
|
46
|
+
}
|
|
47
|
+
let msgCheck;
|
|
48
|
+
if (typeof msgMatches === "string") {
|
|
49
|
+
msgCheck = stripAnsiCode(error.message).includes(
|
|
50
|
+
stripAnsiCode(msgMatches),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (msgMatches instanceof RegExp) {
|
|
54
|
+
msgCheck = msgMatches.test(stripAnsiCode(error.message));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (msgMatches && !msgCheck) {
|
|
58
|
+
msg = `Expected error message to include ${
|
|
59
|
+
msgMatches instanceof RegExp
|
|
60
|
+
? msgMatches.toString()
|
|
61
|
+
: JSON.stringify(msgMatches)
|
|
62
|
+
}, but got ${JSON.stringify(error?.message)}${msgSuffix}`;
|
|
63
|
+
throw new AssertionError(msg);
|
|
64
|
+
}
|
|
65
|
+
}
|