@executablemd/testing 0.3.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/LICENSE +21 -0
- package/esm/core/mod.js +55 -0
- package/esm/core/src/api.js +31 -0
- package/esm/core/src/collect.js +24 -0
- package/esm/core/src/component-api.js +47 -0
- package/esm/core/src/content-context.js +23 -0
- package/esm/core/src/deno-compiler.js +51 -0
- package/esm/core/src/errors.js +13 -0
- package/esm/core/src/eval-context.js +13 -0
- package/esm/core/src/eval-handler.js +79 -0
- package/esm/core/src/eval-interpolate.js +63 -0
- package/esm/core/src/eval-transform.js +378 -0
- package/esm/core/src/execute.js +378 -0
- package/esm/core/src/expand.js +988 -0
- package/esm/core/src/frontmatter.js +98 -0
- package/esm/core/src/heal.js +29 -0
- package/esm/core/src/interpolate.js +39 -0
- package/esm/core/src/modifiers/daemon.js +60 -0
- package/esm/core/src/modifiers/persist.js +35 -0
- package/esm/core/src/modifiers/timeout.js +55 -0
- package/esm/core/src/modifiers.js +113 -0
- package/esm/core/src/output/normalize.js +33 -0
- package/esm/core/src/output/terminal.js +20 -0
- package/esm/core/src/render.js +30 -0
- package/esm/core/src/replay-stream.js +58 -0
- package/esm/core/src/sample-api.js +45 -0
- package/esm/core/src/scanner.js +632 -0
- package/esm/core/src/temp-file-compiler.js +68 -0
- package/esm/core/src/types.js +7 -0
- package/esm/core/src/validate.js +82 -0
- package/esm/durable-streams/combinators.js +227 -0
- package/esm/durable-streams/context.js +13 -0
- package/esm/durable-streams/divergence.js +56 -0
- package/esm/durable-streams/each.js +193 -0
- package/esm/durable-streams/effect.js +273 -0
- package/esm/durable-streams/ephemeral.js +80 -0
- package/esm/durable-streams/errors.js +92 -0
- package/esm/durable-streams/http-stream.js +207 -0
- package/esm/durable-streams/mod.js +34 -0
- package/esm/durable-streams/operations.js +88 -0
- package/esm/durable-streams/replay-guard.js +87 -0
- package/esm/durable-streams/replay-index.js +132 -0
- package/esm/durable-streams/run.js +137 -0
- package/esm/durable-streams/serialize.js +65 -0
- package/esm/durable-streams/stream.js +56 -0
- package/esm/durable-streams/types.js +10 -0
- package/esm/package.json +3 -0
- package/esm/runtime/apis.js +237 -0
- package/esm/runtime/find-free-port.js +33 -0
- package/esm/runtime/mod.js +20 -0
- package/esm/testing/npm/src/_dnt.polyfills.js +1 -0
- package/esm/testing/npm/src/_dnt.shims.js +57 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/almost_equals.js +47 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/array_includes.js +65 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/assert.js +23 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/assertion_error.js +30 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/equal.js +194 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/equals.js +61 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/exists.js +27 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/fail.js +20 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/false.js +23 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/greater.js +29 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/greater_or_equal.js +29 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/instance_of.js +52 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/is_error.js +53 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/less.js +28 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/less_or_equal.js +29 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/match.js +26 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/mod.js +45 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/not_equals.js +39 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/not_instance_of.js +29 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/not_match.js +26 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/not_strict_equals.js +32 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/object_match.js +177 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/rejects.js +53 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/strict_equals.js +58 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/string_includes.js +26 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/throws.js +44 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/unimplemented.js +20 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/assert/1.0.19/unreachable.js +20 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/build_message.js +116 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/diff.js +277 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/diff_str.js +181 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/format.js +70 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/styles.js +207 -0
- package/esm/testing/npm/src/deps/jsr.io/@std/internal/1.0.14/types.js +3 -0
- package/esm/testing/npm/src/mod.js +27 -0
- package/esm/testing/npm/src/src/assertions.js +250 -0
- package/esm/testing/npm/src/src/handlers.js +245 -0
- package/esm/testing/npm/src/src/journal.js +152 -0
- package/esm/testing/npm/src/src/test-api.js +33 -0
- package/esm/testing/npm/src/src/use-testing.js +79 -0
- package/esm/testing/npm/src/src/vocabulary.js +111 -0
- package/package.json +35 -0
- package/types/_dnt.polyfills.d.ts +6 -0
- package/types/_dnt.shims.d.ts +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/almost_equals.d.ts +29 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/array_includes.d.ts +28 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/assert.d.ts +16 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/assertion_error.d.ts +25 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/equal.d.ts +24 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/equals.d.ts +36 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/exists.d.ts +17 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/fail.d.ts +14 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/false.d.ts +18 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/greater.d.ts +19 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/greater_or_equal.d.ts +19 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/instance_of.d.ts +22 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/is_error.d.ts +24 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/less.d.ts +18 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/less_or_equal.d.ts +19 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/match.d.ts +17 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/mod.d.ts +43 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/not_equals.d.ts +26 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/not_instance_of.d.ts +19 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/not_match.d.ts +17 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/not_strict_equals.d.ts +22 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/object_match.d.ts +28 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/rejects.d.ts +41 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/strict_equals.d.ts +23 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/string_includes.d.ts +17 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/throws.d.ts +44 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/unimplemented.d.ts +14 -0
- package/types/deps/jsr.io/@std/assert/1.0.19/unreachable.d.ts +14 -0
- package/types/deps/jsr.io/@std/internal/1.0.14/build_message.d.ts +79 -0
- package/types/deps/jsr.io/@std/internal/1.0.14/diff.d.ts +139 -0
- package/types/deps/jsr.io/@std/internal/1.0.14/diff_str.d.ts +98 -0
- package/types/deps/jsr.io/@std/internal/1.0.14/format.d.ts +28 -0
- package/types/deps/jsr.io/@std/internal/1.0.14/styles.d.ts +158 -0
- package/types/deps/jsr.io/@std/internal/1.0.14/types.d.ts +24 -0
- package/types/mod.d.ts +29 -0
- package/types/src/assertions.d.ts +55 -0
- package/types/src/handlers.d.ts +18 -0
- package/types/src/journal.d.ts +29 -0
- package/types/src/test-api.d.ts +54 -0
- package/types/src/use-testing.d.ts +25 -0
- package/types/src/vocabulary.d.ts +34 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import * as dntShim from "../../../../../_dnt.shims.js";
|
|
4
|
+
function isKeyedCollection(x) {
|
|
5
|
+
return x instanceof Set || x instanceof Map;
|
|
6
|
+
}
|
|
7
|
+
function prototypesEqual(a, b) {
|
|
8
|
+
const pa = Object.getPrototypeOf(a);
|
|
9
|
+
const pb = Object.getPrototypeOf(b);
|
|
10
|
+
return pa === pb ||
|
|
11
|
+
pa === Object.prototype && pb === null ||
|
|
12
|
+
pa === null && pb === Object.prototype;
|
|
13
|
+
}
|
|
14
|
+
function isBasicObjectOrArray(obj) {
|
|
15
|
+
const proto = Object.getPrototypeOf(obj);
|
|
16
|
+
return proto === null || proto === Object.prototype ||
|
|
17
|
+
proto === Array.prototype;
|
|
18
|
+
}
|
|
19
|
+
// Slightly faster than Reflect.ownKeys in V8 as of 12.9.202.13-rusty (2024-10-28)
|
|
20
|
+
function ownKeys(obj) {
|
|
21
|
+
return [
|
|
22
|
+
...Object.getOwnPropertyNames(obj),
|
|
23
|
+
...Object.getOwnPropertySymbols(obj),
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
function getKeysDeep(obj) {
|
|
27
|
+
const keys = new Set();
|
|
28
|
+
while (obj !== Object.prototype && obj !== Array.prototype && obj != null) {
|
|
29
|
+
for (const key of ownKeys(obj)) {
|
|
30
|
+
keys.add(key);
|
|
31
|
+
}
|
|
32
|
+
obj = Object.getPrototypeOf(obj);
|
|
33
|
+
}
|
|
34
|
+
return keys;
|
|
35
|
+
}
|
|
36
|
+
// deno-lint-ignore no-explicit-any
|
|
37
|
+
const Temporal = dntShim.dntGlobalThis.Temporal ?? Object.create(null);
|
|
38
|
+
/** A non-exhaustive list of prototypes that can be accurately fast-path compared with `String(instance)` */
|
|
39
|
+
const stringComparablePrototypes = new Set([
|
|
40
|
+
Intl.Locale,
|
|
41
|
+
RegExp,
|
|
42
|
+
Temporal.Duration,
|
|
43
|
+
Temporal.Instant,
|
|
44
|
+
Temporal.PlainDate,
|
|
45
|
+
Temporal.PlainDateTime,
|
|
46
|
+
Temporal.PlainTime,
|
|
47
|
+
Temporal.PlainYearMonth,
|
|
48
|
+
Temporal.PlainMonthDay,
|
|
49
|
+
Temporal.ZonedDateTime,
|
|
50
|
+
URL,
|
|
51
|
+
URLSearchParams,
|
|
52
|
+
].filter((x) => x != null).map((x) => x.prototype));
|
|
53
|
+
function isPrimitive(x) {
|
|
54
|
+
return typeof x === "string" ||
|
|
55
|
+
typeof x === "number" ||
|
|
56
|
+
typeof x === "boolean" ||
|
|
57
|
+
typeof x === "bigint" ||
|
|
58
|
+
typeof x === "symbol" ||
|
|
59
|
+
x == null;
|
|
60
|
+
}
|
|
61
|
+
const TypedArray = Object.getPrototypeOf(Uint8Array);
|
|
62
|
+
function compareTypedArrays(a, b) {
|
|
63
|
+
if (a.length !== b.length)
|
|
64
|
+
return false;
|
|
65
|
+
for (let i = 0; i < b.length; i++) {
|
|
66
|
+
if (!sameValueZero(a[i], b[i]))
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
/** Check both strict equality (`0 == -0`) and `Object.is` (`NaN == NaN`) */
|
|
72
|
+
function sameValueZero(a, b) {
|
|
73
|
+
return a === b || Object.is(a, b);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Deep equality comparison used in assertions.
|
|
77
|
+
*
|
|
78
|
+
* This function is based on value equality, but for some cases (such as data
|
|
79
|
+
* that can only be read asynchronously or function properties) value equality
|
|
80
|
+
* is not possible to determine. In such cases, reference equality is used
|
|
81
|
+
* instead, which may cause false negatives for objects such as `Blob`s or
|
|
82
|
+
* `Request`s.
|
|
83
|
+
*
|
|
84
|
+
* @param a The actual value
|
|
85
|
+
* @param b The expected value
|
|
86
|
+
* @returns `true` if the values are deeply equal, `false` otherwise
|
|
87
|
+
*
|
|
88
|
+
* @throws {TypeError} If either value is a `WeakMap` or `WeakSet`.
|
|
89
|
+
*
|
|
90
|
+
* @example Usage
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { equal } from "@std/assert/equal";
|
|
93
|
+
*
|
|
94
|
+
* equal({ foo: "bar" }, { foo: "bar" }); // Returns `true`
|
|
95
|
+
* equal({ foo: "bar" }, { foo: "baz" }); // Returns `false`
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export function equal(a, b) {
|
|
99
|
+
const seen = new Map();
|
|
100
|
+
return (function compare(a, b) {
|
|
101
|
+
if (sameValueZero(a, b))
|
|
102
|
+
return true;
|
|
103
|
+
if (isPrimitive(a) || isPrimitive(b))
|
|
104
|
+
return false;
|
|
105
|
+
if (a instanceof Date && b instanceof Date) {
|
|
106
|
+
return Object.is(a.getTime(), b.getTime());
|
|
107
|
+
}
|
|
108
|
+
if (a && typeof a === "object" && b && typeof b === "object") {
|
|
109
|
+
if (!prototypesEqual(a, b)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
if (a instanceof TypedArray) {
|
|
113
|
+
return compareTypedArrays(a, b);
|
|
114
|
+
}
|
|
115
|
+
if (a instanceof ArrayBuffer ||
|
|
116
|
+
(globalThis.SharedArrayBuffer && a instanceof SharedArrayBuffer)) {
|
|
117
|
+
return compareTypedArrays(new Uint8Array(a), new Uint8Array(b));
|
|
118
|
+
}
|
|
119
|
+
if (a instanceof WeakMap) {
|
|
120
|
+
throw new TypeError("Cannot compare WeakMap instances");
|
|
121
|
+
}
|
|
122
|
+
if (a instanceof WeakSet) {
|
|
123
|
+
throw new TypeError("Cannot compare WeakSet instances");
|
|
124
|
+
}
|
|
125
|
+
if (a instanceof WeakRef) {
|
|
126
|
+
return compare(a.deref(), b.deref());
|
|
127
|
+
}
|
|
128
|
+
if (seen.get(a) === b) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
if (Object.keys(a).length !== Object.keys(b).length) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
seen.set(a, b);
|
|
135
|
+
if (isKeyedCollection(a) && isKeyedCollection(b)) {
|
|
136
|
+
if (a.size !== b.size) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
const aKeys = [...a.keys()];
|
|
140
|
+
const primitiveKeysFastPath = aKeys.every(isPrimitive);
|
|
141
|
+
if (primitiveKeysFastPath) {
|
|
142
|
+
if (a instanceof Set) {
|
|
143
|
+
return a.symmetricDifference(b).size === 0;
|
|
144
|
+
}
|
|
145
|
+
for (const key of aKeys) {
|
|
146
|
+
if (!b.has(key) ||
|
|
147
|
+
!compare(a.get(key), b.get(key))) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
let unmatchedEntries = a.size;
|
|
154
|
+
for (const [aKey, aValue] of a.entries()) {
|
|
155
|
+
for (const [bKey, bValue] of b.entries()) {
|
|
156
|
+
/* Given that Map keys can be references, we need
|
|
157
|
+
* to ensure that they are also deeply equal */
|
|
158
|
+
if (!compare(aKey, bKey))
|
|
159
|
+
continue;
|
|
160
|
+
if ((aKey === aValue && bKey === bValue) ||
|
|
161
|
+
(compare(aValue, bValue))) {
|
|
162
|
+
unmatchedEntries--;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return unmatchedEntries === 0;
|
|
168
|
+
}
|
|
169
|
+
let keys;
|
|
170
|
+
if (isBasicObjectOrArray(a)) {
|
|
171
|
+
// fast path
|
|
172
|
+
keys = ownKeys({ ...a, ...b });
|
|
173
|
+
}
|
|
174
|
+
else if (stringComparablePrototypes.has(Object.getPrototypeOf(a))) {
|
|
175
|
+
// medium path
|
|
176
|
+
return String(a) === String(b);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
// slow path
|
|
180
|
+
keys = getKeysDeep(a).union(getKeysDeep(b));
|
|
181
|
+
}
|
|
182
|
+
for (const key of keys) {
|
|
183
|
+
if (!compare(a[key], b[key])) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
if (((key in a) && (!(key in b))) || ((key in b) && (!(key in a)))) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
})(a, b);
|
|
194
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { equal } from "./equal.js";
|
|
4
|
+
import { buildMessage } from "../../internal/1.0.14/build_message.js";
|
|
5
|
+
import { diff } from "../../internal/1.0.14/diff.js";
|
|
6
|
+
import { diffStr } from "../../internal/1.0.14/diff_str.js";
|
|
7
|
+
import { format } from "../../internal/1.0.14/format.js";
|
|
8
|
+
import { AssertionError } from "./assertion_error.js";
|
|
9
|
+
/**
|
|
10
|
+
* Make an assertion that `actual` and `expected` are equal, deeply. If not
|
|
11
|
+
* deeply equal, then throw.
|
|
12
|
+
*
|
|
13
|
+
* Type parameter can be specified to ensure values under comparison have the
|
|
14
|
+
* same type.
|
|
15
|
+
*
|
|
16
|
+
* Note: This function is based on value equality, but for some cases (such as
|
|
17
|
+
* data that can only be read asynchronously or function properties) value
|
|
18
|
+
* equality is not possible to determine. In such cases, reference equality is
|
|
19
|
+
* used instead, which may cause false negatives for objects such as `Blob`s or
|
|
20
|
+
* `Request`s.
|
|
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(actual, expected, msg) {
|
|
45
|
+
if (equal(actual, expected)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
49
|
+
let message = `Values are not equal${msgSuffix}`;
|
|
50
|
+
const actualString = format(actual);
|
|
51
|
+
const expectedString = format(expected);
|
|
52
|
+
const stringDiff = (typeof actual === "string") &&
|
|
53
|
+
(typeof expected === "string");
|
|
54
|
+
const diffResult = stringDiff
|
|
55
|
+
? diffStr(actual, expected)
|
|
56
|
+
: diff(actualString.split("\n"), expectedString.split("\n"));
|
|
57
|
+
const diffMsg = buildMessage(diffResult, { stringDiff }, arguments[3])
|
|
58
|
+
.join("\n");
|
|
59
|
+
message = `${message}\n${diffMsg}`;
|
|
60
|
+
throw new AssertionError(message);
|
|
61
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Make an assertion that actual is not null or undefined.
|
|
6
|
+
* If not then throw.
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts ignore
|
|
10
|
+
* import { assertExists } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* assertExists("something"); // Doesn't throw
|
|
13
|
+
* assertExists(undefined); // Throws
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @typeParam T The type of the actual value.
|
|
17
|
+
* @param actual The actual value to check.
|
|
18
|
+
* @param msg The optional message to include in the error if the assertion fails.
|
|
19
|
+
*/
|
|
20
|
+
export function assertExists(actual, msg) {
|
|
21
|
+
if (actual === undefined || actual === null) {
|
|
22
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
23
|
+
msg =
|
|
24
|
+
`Expected actual: "${actual}" to not be null or undefined${msgSuffix}`;
|
|
25
|
+
throw new AssertionError(msg);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Forcefully throws a failed assertion.
|
|
6
|
+
*
|
|
7
|
+
* @example Usage
|
|
8
|
+
* ```ts ignore
|
|
9
|
+
* import { fail } from "@std/assert";
|
|
10
|
+
*
|
|
11
|
+
* fail("Deliberately failed!"); // Throws
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @param msg Optional message to include in the error.
|
|
15
|
+
* @returns Never returns, always throws.
|
|
16
|
+
*/
|
|
17
|
+
export function fail(msg) {
|
|
18
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
19
|
+
throw new AssertionError(`Failed assertion${msgSuffix}`);
|
|
20
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Make an assertion, an error will be thrown if `expr` has a truthy value.
|
|
6
|
+
*
|
|
7
|
+
* @example Usage
|
|
8
|
+
* ```ts ignore
|
|
9
|
+
* import { assertFalse } from "@std/assert";
|
|
10
|
+
*
|
|
11
|
+
* assertFalse(false); // Doesn't throw
|
|
12
|
+
* assertFalse(true); // Throws
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param expr The expression to test.
|
|
16
|
+
* @param msg The optional message to display if the assertion fails.
|
|
17
|
+
* @throws {AssertionError} If `expr` is truthy.
|
|
18
|
+
*/
|
|
19
|
+
export function assertFalse(expr, msg = "") {
|
|
20
|
+
if (expr) {
|
|
21
|
+
throw new AssertionError(msg);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { format } from "../../internal/1.0.14/format.js";
|
|
4
|
+
import { AssertionError } from "./assertion_error.js";
|
|
5
|
+
/**
|
|
6
|
+
* Make an assertion that `actual` is greater than `expected`.
|
|
7
|
+
* If not then throw.
|
|
8
|
+
*
|
|
9
|
+
* @example Usage
|
|
10
|
+
* ```ts ignore
|
|
11
|
+
* import { assertGreater } from "@std/assert";
|
|
12
|
+
*
|
|
13
|
+
* assertGreater(2, 1); // Doesn't throw
|
|
14
|
+
* assertGreater(1, 1); // Throws
|
|
15
|
+
* assertGreater(0, 1); // Throws
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @typeParam T The type of the values to compare.
|
|
19
|
+
* @param actual The actual value to compare.
|
|
20
|
+
* @param expected The expected value to compare.
|
|
21
|
+
* @param msg The optional message to display if the assertion fails.
|
|
22
|
+
*/
|
|
23
|
+
export function assertGreater(actual, expected, msg) {
|
|
24
|
+
if (actual > expected)
|
|
25
|
+
return;
|
|
26
|
+
const actualString = format(actual);
|
|
27
|
+
const expectedString = format(expected);
|
|
28
|
+
throw new AssertionError(msg ?? `Expect ${actualString} > ${expectedString}`);
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { format } from "../../internal/1.0.14/format.js";
|
|
4
|
+
import { AssertionError } from "./assertion_error.js";
|
|
5
|
+
/**
|
|
6
|
+
* Make an assertion that `actual` is greater than or equal to `expected`.
|
|
7
|
+
* If not then throw.
|
|
8
|
+
*
|
|
9
|
+
* @example Usage
|
|
10
|
+
* ```ts ignore
|
|
11
|
+
* import { assertGreaterOrEqual } from "@std/assert";
|
|
12
|
+
*
|
|
13
|
+
* assertGreaterOrEqual(2, 1); // Doesn't throw
|
|
14
|
+
* assertGreaterOrEqual(1, 1); // Doesn't throw
|
|
15
|
+
* assertGreaterOrEqual(0, 1); // Throws
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @typeParam T The type of the values to compare.
|
|
19
|
+
* @param actual The actual value to compare.
|
|
20
|
+
* @param expected The expected value to compare.
|
|
21
|
+
* @param msg The optional message to display if the assertion fails.
|
|
22
|
+
*/
|
|
23
|
+
export function assertGreaterOrEqual(actual, expected, msg) {
|
|
24
|
+
if (actual >= expected)
|
|
25
|
+
return;
|
|
26
|
+
const actualString = format(actual);
|
|
27
|
+
const expectedString = format(expected);
|
|
28
|
+
throw new AssertionError(msg ?? `Expect ${actualString} >= ${expectedString}`);
|
|
29
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Make an assertion that `obj` is an instance of `type`.
|
|
6
|
+
* If not then throw.
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts ignore
|
|
10
|
+
* import { assertInstanceOf } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* assertInstanceOf(new Date(), Date); // Doesn't throw
|
|
13
|
+
* assertInstanceOf(new Date(), Number); // Throws
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @typeParam T The expected type of the object.
|
|
17
|
+
* @param actual The object to check.
|
|
18
|
+
* @param expectedType The expected class constructor.
|
|
19
|
+
* @param msg The optional message to display if the assertion fails.
|
|
20
|
+
*/
|
|
21
|
+
export function assertInstanceOf(actual, expectedType, msg = "") {
|
|
22
|
+
if (actual instanceof expectedType)
|
|
23
|
+
return;
|
|
24
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
25
|
+
const expectedTypeStr = expectedType.name;
|
|
26
|
+
let actualTypeStr = "";
|
|
27
|
+
if (actual === null) {
|
|
28
|
+
actualTypeStr = "null";
|
|
29
|
+
}
|
|
30
|
+
else if (actual === undefined) {
|
|
31
|
+
actualTypeStr = "undefined";
|
|
32
|
+
}
|
|
33
|
+
else if (typeof actual === "object") {
|
|
34
|
+
actualTypeStr = actual.constructor?.name ?? "Object";
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
actualTypeStr = typeof actual;
|
|
38
|
+
}
|
|
39
|
+
if (expectedTypeStr === actualTypeStr) {
|
|
40
|
+
msg =
|
|
41
|
+
`Expected object to be an instance of "${expectedTypeStr}"${msgSuffix}`;
|
|
42
|
+
}
|
|
43
|
+
else if (actualTypeStr === "function") {
|
|
44
|
+
msg =
|
|
45
|
+
`Expected object to be an instance of "${expectedTypeStr}" but was not an instanced object${msgSuffix}`;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
msg =
|
|
49
|
+
`Expected object to be an instance of "${expectedTypeStr}" but was "${actualTypeStr}"${msgSuffix}`;
|
|
50
|
+
}
|
|
51
|
+
throw new AssertionError(msg);
|
|
52
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Copyright 2018-2026 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.14/styles.js";
|
|
5
|
+
/**
|
|
6
|
+
* Make an assertion that `error` is an `Error`.
|
|
7
|
+
* If not then an error will be thrown.
|
|
8
|
+
* An error class and a string that should be included in the
|
|
9
|
+
* error message can also be asserted.
|
|
10
|
+
*
|
|
11
|
+
* @example Usage
|
|
12
|
+
* ```ts ignore
|
|
13
|
+
* import { assertIsError } from "@std/assert";
|
|
14
|
+
*
|
|
15
|
+
* assertIsError(null); // Throws
|
|
16
|
+
* assertIsError(new RangeError("Out of range")); // Doesn't throw
|
|
17
|
+
* assertIsError(new RangeError("Out of range"), SyntaxError); // Throws
|
|
18
|
+
* assertIsError(new RangeError("Out of range"), SyntaxError, "Out of range"); // Doesn't throw
|
|
19
|
+
* assertIsError(new RangeError("Out of range"), SyntaxError, "Within range"); // Throws
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @typeParam E The type of the error to assert.
|
|
23
|
+
* @param error The error to assert.
|
|
24
|
+
* @param ErrorClass The optional error class to assert.
|
|
25
|
+
* @param msgMatches The optional string or RegExp to assert in the error message.
|
|
26
|
+
* @param msg The optional message to display if the assertion fails.
|
|
27
|
+
*/
|
|
28
|
+
export function assertIsError(error,
|
|
29
|
+
// deno-lint-ignore no-explicit-any
|
|
30
|
+
ErrorClass, msgMatches, msg) {
|
|
31
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
32
|
+
if (!(error instanceof Error)) {
|
|
33
|
+
throw new AssertionError(`Expected "error" to be an Error object${msgSuffix}`);
|
|
34
|
+
}
|
|
35
|
+
if (ErrorClass && !(error instanceof ErrorClass)) {
|
|
36
|
+
msg =
|
|
37
|
+
`Expected error to be instance of "${ErrorClass.name}", but was "${error?.constructor?.name}"${msgSuffix}`;
|
|
38
|
+
throw new AssertionError(msg);
|
|
39
|
+
}
|
|
40
|
+
let msgCheck;
|
|
41
|
+
if (typeof msgMatches === "string") {
|
|
42
|
+
msgCheck = stripAnsiCode(error.message).includes(stripAnsiCode(msgMatches));
|
|
43
|
+
}
|
|
44
|
+
if (msgMatches instanceof RegExp) {
|
|
45
|
+
msgCheck = msgMatches.test(stripAnsiCode(error.message));
|
|
46
|
+
}
|
|
47
|
+
if (msgMatches && !msgCheck) {
|
|
48
|
+
msg = `Expected error message to include ${msgMatches instanceof RegExp
|
|
49
|
+
? msgMatches.toString()
|
|
50
|
+
: JSON.stringify(msgMatches)}, but got ${JSON.stringify(error?.message)}${msgSuffix}`;
|
|
51
|
+
throw new AssertionError(msg);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { format } from "../../internal/1.0.14/format.js";
|
|
4
|
+
import { AssertionError } from "./assertion_error.js";
|
|
5
|
+
/**
|
|
6
|
+
* Make an assertion that `actual` is less than `expected`.
|
|
7
|
+
* If not then throw.
|
|
8
|
+
*
|
|
9
|
+
* @example Usage
|
|
10
|
+
* ```ts ignore
|
|
11
|
+
* import { assertLess } from "@std/assert";
|
|
12
|
+
*
|
|
13
|
+
* assertLess(1, 2); // Doesn't throw
|
|
14
|
+
* assertLess(2, 1); // Throws
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @typeParam T The type of the values to compare.
|
|
18
|
+
* @param actual The actual value to compare.
|
|
19
|
+
* @param expected The expected value to compare.
|
|
20
|
+
* @param msg The optional message to display if the assertion fails.
|
|
21
|
+
*/
|
|
22
|
+
export function assertLess(actual, expected, msg) {
|
|
23
|
+
if (actual < expected)
|
|
24
|
+
return;
|
|
25
|
+
const actualString = format(actual);
|
|
26
|
+
const expectedString = format(expected);
|
|
27
|
+
throw new AssertionError(msg ?? `Expect ${actualString} < ${expectedString}`);
|
|
28
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { format } from "../../internal/1.0.14/format.js";
|
|
4
|
+
import { AssertionError } from "./assertion_error.js";
|
|
5
|
+
/**
|
|
6
|
+
* Make an assertion that `actual` is less than or equal to `expected`.
|
|
7
|
+
* If not then throw.
|
|
8
|
+
*
|
|
9
|
+
* @example Usage
|
|
10
|
+
* ```ts ignore
|
|
11
|
+
* import { assertLessOrEqual } from "@std/assert";
|
|
12
|
+
*
|
|
13
|
+
* assertLessOrEqual(1, 2); // Doesn't throw
|
|
14
|
+
* assertLessOrEqual(1, 1); // Doesn't throw
|
|
15
|
+
* assertLessOrEqual(1, 0); // Throws
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @typeParam T The type of the values to compare.
|
|
19
|
+
* @param actual The actual value to compare.
|
|
20
|
+
* @param expected The expected value to compare.
|
|
21
|
+
* @param msg The optional message to display if the assertion fails.
|
|
22
|
+
*/
|
|
23
|
+
export function assertLessOrEqual(actual, expected, msg) {
|
|
24
|
+
if (actual <= expected)
|
|
25
|
+
return;
|
|
26
|
+
const actualString = format(actual);
|
|
27
|
+
const expectedString = format(expected);
|
|
28
|
+
throw new AssertionError(msg ?? `Expect ${actualString} <= ${expectedString}`);
|
|
29
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Make an assertion that `actual` match RegExp `expected`. If not
|
|
6
|
+
* then throw.
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts ignore
|
|
10
|
+
* import { assertMatch } from "@std/assert";
|
|
11
|
+
*
|
|
12
|
+
* assertMatch("Raptor", /Raptor/); // Doesn't throw
|
|
13
|
+
* assertMatch("Denosaurus", /Raptor/); // Throws
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param actual The actual value to be matched.
|
|
17
|
+
* @param expected The expected pattern to match.
|
|
18
|
+
* @param msg The optional message to display if the assertion fails.
|
|
19
|
+
*/
|
|
20
|
+
export function assertMatch(actual, expected, msg) {
|
|
21
|
+
if (expected.test(actual))
|
|
22
|
+
return;
|
|
23
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
24
|
+
msg = `Expected actual: "${actual}" to match: "${expected}"${msgSuffix}`;
|
|
25
|
+
throw new AssertionError(msg);
|
|
26
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
/** A library of assertion functions.
|
|
4
|
+
* If the assertion is false an `AssertionError` will be thrown which will
|
|
5
|
+
* result in pretty-printed diff of the failing assertion.
|
|
6
|
+
*
|
|
7
|
+
* This module is browser compatible, but do not rely on good formatting of
|
|
8
|
+
* values for AssertionError messages in browsers.
|
|
9
|
+
*
|
|
10
|
+
* ```ts ignore
|
|
11
|
+
* import { assert } from "@std/assert";
|
|
12
|
+
*
|
|
13
|
+
* assert("I am truthy"); // Doesn't throw
|
|
14
|
+
* assert(false); // Throws `AssertionError`
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
export * from "./almost_equals.js";
|
|
20
|
+
export * from "./array_includes.js";
|
|
21
|
+
export * from "./equals.js";
|
|
22
|
+
export * from "./exists.js";
|
|
23
|
+
export * from "./false.js";
|
|
24
|
+
export * from "./greater_or_equal.js";
|
|
25
|
+
export * from "./greater.js";
|
|
26
|
+
export * from "./instance_of.js";
|
|
27
|
+
export * from "./is_error.js";
|
|
28
|
+
export * from "./less_or_equal.js";
|
|
29
|
+
export * from "./less.js";
|
|
30
|
+
export * from "./match.js";
|
|
31
|
+
export * from "./not_equals.js";
|
|
32
|
+
export * from "./not_instance_of.js";
|
|
33
|
+
export * from "./not_match.js";
|
|
34
|
+
export * from "./not_strict_equals.js";
|
|
35
|
+
export * from "./object_match.js";
|
|
36
|
+
export * from "./rejects.js";
|
|
37
|
+
export * from "./strict_equals.js";
|
|
38
|
+
export * from "./string_includes.js";
|
|
39
|
+
export * from "./throws.js";
|
|
40
|
+
export * from "./assert.js";
|
|
41
|
+
export * from "./assertion_error.js";
|
|
42
|
+
export * from "./equal.js";
|
|
43
|
+
export * from "./fail.js";
|
|
44
|
+
export * from "./unimplemented.js";
|
|
45
|
+
export * from "./unreachable.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright 2018-2026 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { equal } from "./equal.js";
|
|
4
|
+
import { AssertionError } from "./assertion_error.js";
|
|
5
|
+
import { format } from "../../internal/1.0.14/format.js";
|
|
6
|
+
/**
|
|
7
|
+
* Make an assertion that `actual` and `expected` are not equal, deeply.
|
|
8
|
+
* If not then throw.
|
|
9
|
+
*
|
|
10
|
+
* Type parameter can be specified to ensure values under comparison have the same type.
|
|
11
|
+
*
|
|
12
|
+
* Note: This function is based on value equality, but for some cases (such as
|
|
13
|
+
* data that can only be read asynchronously or function properties) value
|
|
14
|
+
* equality is not possible to determine. In such cases, reference equality is
|
|
15
|
+
* used instead, which may cause false negatives for objects such as `Blob`s or
|
|
16
|
+
* `Request`s.
|
|
17
|
+
*
|
|
18
|
+
* @example Usage
|
|
19
|
+
* ```ts ignore
|
|
20
|
+
* import { assertNotEquals } from "@std/assert";
|
|
21
|
+
*
|
|
22
|
+
* assertNotEquals(1, 2); // Doesn't throw
|
|
23
|
+
* assertNotEquals(1, 1); // Throws
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @typeParam T The type of the values to compare.
|
|
27
|
+
* @param actual The actual value to compare.
|
|
28
|
+
* @param expected The expected value to compare.
|
|
29
|
+
* @param msg The optional message to display if the assertion fails.
|
|
30
|
+
*/
|
|
31
|
+
export function assertNotEquals(actual, expected, msg) {
|
|
32
|
+
if (!equal(actual, expected)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const actualString = format(actual);
|
|
36
|
+
const expectedString = format(expected);
|
|
37
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
38
|
+
throw new AssertionError(`Expected actual: ${actualString} not to be: ${expectedString}${msgSuffix}`);
|
|
39
|
+
}
|