@kerebron/test-utils 0.4.28 → 0.4.30
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,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
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
|
|
4
|
+
import { bgGreen, bgRed, bold, gray, green, red, white } from "./styles.js";
|
|
5
|
+
import type { DiffResult, DiffType } from "./types.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Colors the output of assertion diffs.
|
|
9
|
+
*
|
|
10
|
+
* @param diffType Difference type, either added or removed.
|
|
11
|
+
* @param background If true, colors the background instead of the text.
|
|
12
|
+
*
|
|
13
|
+
* @returns A function that colors the input string.
|
|
14
|
+
*
|
|
15
|
+
* @example Usage
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { createColor } from "@std/internal";
|
|
18
|
+
* import { assertEquals } from "@std/assert";
|
|
19
|
+
* import { bold, green, red, white } from "@std/fmt/colors";
|
|
20
|
+
*
|
|
21
|
+
* assertEquals(createColor("added")("foo"), green(bold("foo")));
|
|
22
|
+
* assertEquals(createColor("removed")("foo"), red(bold("foo")));
|
|
23
|
+
* assertEquals(createColor("common")("foo"), white("foo"));
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function createColor(
|
|
27
|
+
diffType: DiffType,
|
|
28
|
+
/**
|
|
29
|
+
* TODO(@littledivy): Remove this when we can detect true color terminals. See
|
|
30
|
+
* https://github.com/denoland/std/issues/2575.
|
|
31
|
+
*/
|
|
32
|
+
background = false,
|
|
33
|
+
): (s: string) => string {
|
|
34
|
+
switch (diffType) {
|
|
35
|
+
case "added":
|
|
36
|
+
return (s) => background ? bgGreen(white(s)) : green(bold(s));
|
|
37
|
+
case "removed":
|
|
38
|
+
return (s) => background ? bgRed(white(s)) : red(bold(s));
|
|
39
|
+
case "truncation":
|
|
40
|
+
return gray;
|
|
41
|
+
default:
|
|
42
|
+
return white;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Prefixes `+` or `-` in diff output.
|
|
48
|
+
*
|
|
49
|
+
* @param diffType Difference type, either added or removed
|
|
50
|
+
*
|
|
51
|
+
* @returns A string representing the sign.
|
|
52
|
+
*
|
|
53
|
+
* @example Usage
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { createSign } from "@std/internal";
|
|
56
|
+
* import { assertEquals } from "@std/assert";
|
|
57
|
+
*
|
|
58
|
+
* assertEquals(createSign("added"), "+ ");
|
|
59
|
+
* assertEquals(createSign("removed"), "- ");
|
|
60
|
+
* assertEquals(createSign("common"), " ");
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export function createSign(diffType: DiffType): string {
|
|
64
|
+
switch (diffType) {
|
|
65
|
+
case "added":
|
|
66
|
+
return "+ ";
|
|
67
|
+
case "removed":
|
|
68
|
+
return "- ";
|
|
69
|
+
default:
|
|
70
|
+
return " ";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Options for {@linkcode buildMessage}. */
|
|
75
|
+
export interface BuildMessageOptions {
|
|
76
|
+
/**
|
|
77
|
+
* Whether to output the diff as a single string.
|
|
78
|
+
* @default {false}
|
|
79
|
+
*/
|
|
80
|
+
stringDiff?: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Builds a message based on the provided diff result.
|
|
85
|
+
*
|
|
86
|
+
* @param diffResult The diff result array.
|
|
87
|
+
* @param options Optional parameters for customizing the message.
|
|
88
|
+
* @param truncateDiff Function to truncate the diff (default is no truncation).
|
|
89
|
+
*
|
|
90
|
+
* @returns An array of strings representing the built message.
|
|
91
|
+
*
|
|
92
|
+
* @example Usage
|
|
93
|
+
* ```ts no-assert
|
|
94
|
+
* import { diffStr, buildMessage } from "@std/internal";
|
|
95
|
+
*
|
|
96
|
+
* diffStr("Hello, world!", "Hello, world");
|
|
97
|
+
* // [
|
|
98
|
+
* // "",
|
|
99
|
+
* // "",
|
|
100
|
+
* // " [Diff] Actual / Expected",
|
|
101
|
+
* // "",
|
|
102
|
+
* // "",
|
|
103
|
+
* // "- Hello, world!",
|
|
104
|
+
* // "+ Hello, world",
|
|
105
|
+
* // "",
|
|
106
|
+
* // ]
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export function buildMessage(
|
|
110
|
+
diffResult: ReadonlyArray<DiffResult<string>>,
|
|
111
|
+
options: BuildMessageOptions = {},
|
|
112
|
+
truncateDiff?: (
|
|
113
|
+
diffResult: ReadonlyArray<DiffResult<string>>,
|
|
114
|
+
stringDiff: boolean,
|
|
115
|
+
contextLength?: number | null,
|
|
116
|
+
) => ReadonlyArray<DiffResult<string>>,
|
|
117
|
+
): string[] {
|
|
118
|
+
if (truncateDiff != null) {
|
|
119
|
+
diffResult = truncateDiff(diffResult, options.stringDiff ?? false);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const { stringDiff = false } = options;
|
|
123
|
+
const messages = [
|
|
124
|
+
"",
|
|
125
|
+
"",
|
|
126
|
+
` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${
|
|
127
|
+
green(bold("Expected"))
|
|
128
|
+
}`,
|
|
129
|
+
"",
|
|
130
|
+
"",
|
|
131
|
+
];
|
|
132
|
+
const diffMessages = diffResult.map((result) => {
|
|
133
|
+
const color = createColor(result.type);
|
|
134
|
+
|
|
135
|
+
const line = result.type === "added" || result.type === "removed"
|
|
136
|
+
? result.details?.map((detail) =>
|
|
137
|
+
detail.type !== "common"
|
|
138
|
+
? createColor(detail.type, true)(detail.value)
|
|
139
|
+
: detail.value
|
|
140
|
+
).join("") ?? result.value
|
|
141
|
+
: result.value;
|
|
142
|
+
|
|
143
|
+
return color(`${createSign(result.type)}${line}`);
|
|
144
|
+
});
|
|
145
|
+
messages.push(...(stringDiff ? [diffMessages.join("")] : diffMessages), "");
|
|
146
|
+
return messages;
|
|
147
|
+
}
|