summaryrefslogtreecommitdiff
path: root/std/testing/asserts.ts
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2020-07-14 15:24:17 -0400
committerGitHub <noreply@github.com>2020-07-14 15:24:17 -0400
commitcde4dbb35132848ffece59ef9cfaccff32347124 (patch)
treecc7830968c6decde704c8cfb83c9185193dc698f /std/testing/asserts.ts
parent9eca71caa1674c31f9cc5d4e86c03f10b59e0a00 (diff)
Use dprint for internal formatting (#6682)
Diffstat (limited to 'std/testing/asserts.ts')
-rw-r--r--std/testing/asserts.ts87
1 files changed, 46 insertions, 41 deletions
diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts
index 2a1358143..4b9241e55 100644
--- a/std/testing/asserts.ts
+++ b/std/testing/asserts.ts
@@ -22,12 +22,12 @@ export class AssertionError extends Error {
export function _format(v: unknown): string {
let string = globalThis.Deno
? Deno.inspect(v, {
- depth: Infinity,
- sorted: true,
- trailingComma: true,
- compact: false,
- iterableLimit: Infinity,
- })
+ depth: Infinity,
+ sorted: true,
+ trailingComma: true,
+ compact: false,
+ iterableLimit: Infinity,
+ })
: String(v);
if (typeof v == "string") {
string = `"${string.replace(/(?=["\\])/g, "\\")}"`;
@@ -62,9 +62,9 @@ function buildMessage(diffResult: ReadonlyArray<DiffResult<string>>): string[] {
messages.push("");
messages.push("");
messages.push(
- ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${green(
- bold("Expected")
- )}`
+ ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${
+ green(bold("Expected"))
+ }`,
);
messages.push("");
messages.push("");
@@ -163,13 +163,13 @@ export function assert(expr: unknown, msg = ""): asserts expr {
export function assertEquals(
actual: unknown,
expected: unknown,
- msg?: string
+ msg?: string,
): void;
export function assertEquals<T>(actual: T, expected: T, msg?: string): void;
export function assertEquals(
actual: unknown,
expected: unknown,
- msg?: string
+ msg?: string,
): void {
if (equal(actual, expected)) {
return;
@@ -180,7 +180,7 @@ export function assertEquals(
try {
const diffResult = diff(
actualString.split("\n"),
- expectedString.split("\n")
+ expectedString.split("\n"),
);
const diffMsg = buildMessage(diffResult).join("\n");
message = `Values are not equal:\n${diffMsg}`;
@@ -206,13 +206,13 @@ export function assertEquals(
export function assertNotEquals(
actual: unknown,
expected: unknown,
- msg?: string
+ msg?: string,
): void;
export function assertNotEquals<T>(actual: T, expected: T, msg?: string): void;
export function assertNotEquals(
actual: unknown,
expected: unknown,
- msg?: string
+ msg?: string,
): void {
if (!equal(actual, expected)) {
return;
@@ -245,7 +245,7 @@ export function assertNotEquals(
export function assertStrictEquals<T>(
actual: T,
expected: T,
- msg?: string
+ msg?: string,
): void {
if (actual === expected) {
return;
@@ -264,14 +264,15 @@ export function assertStrictEquals<T>(
.split("\n")
.map((l) => ` ${l}`)
.join("\n");
- message = `Values have the same structure but are not reference-equal:\n\n${red(
- withOffset
- )}\n`;
+ message =
+ `Values have the same structure but are not reference-equal:\n\n${
+ red(withOffset)
+ }\n`;
} else {
try {
const diffResult = diff(
actualString.split("\n"),
- expectedString.split("\n")
+ expectedString.split("\n"),
);
const diffMsg = buildMessage(diffResult).join("\n");
message = `Values are not strictly equal:\n${diffMsg}`;
@@ -291,7 +292,7 @@ export function assertStrictEquals<T>(
export function assertStringContains(
actual: string,
expected: string,
- msg?: string
+ msg?: string,
): void {
if (!actual.includes(expected)) {
if (!msg) {
@@ -314,17 +315,17 @@ export function assertStringContains(
export function assertArrayContains(
actual: ArrayLike<unknown>,
expected: ArrayLike<unknown>,
- msg?: string
+ msg?: string,
): void;
export function assertArrayContains<T>(
actual: ArrayLike<T>,
expected: ArrayLike<T>,
- msg?: string
+ msg?: string,
): void;
export function assertArrayContains(
actual: ArrayLike<unknown>,
expected: ArrayLike<unknown>,
- msg?: string
+ msg?: string,
): void {
const missing: unknown[] = [];
for (let i = 0; i < expected.length; i++) {
@@ -343,9 +344,9 @@ export function assertArrayContains(
return;
}
if (!msg) {
- msg = `actual: "${_format(actual)}" expected to contain: "${_format(
- expected
- )}"\nmissing: ${_format(missing)}`;
+ msg = `actual: "${_format(actual)}" expected to contain: "${
+ _format(expected)
+ }"\nmissing: ${_format(missing)}`;
}
throw new AssertionError(msg);
}
@@ -357,7 +358,7 @@ export function assertArrayContains(
export function assertMatch(
actual: string,
expected: RegExp,
- msg?: string
+ msg?: string,
): void {
if (!expected.test(actual)) {
if (!msg) {
@@ -384,7 +385,7 @@ export function assertThrows<T = void>(
fn: () => T,
ErrorClass?: Constructor,
msgIncludes = "",
- msg?: string
+ msg?: string,
): Error {
let doesThrow = false;
let error = null;
@@ -395,18 +396,20 @@ export function assertThrows<T = void>(
throw new AssertionError("A non-Error object was thrown.");
}
if (ErrorClass && !(e instanceof ErrorClass)) {
- msg = `Expected error to be instance of "${ErrorClass.name}", but was "${
- e.constructor.name
- }"${msg ? `: ${msg}` : "."}`;
+ msg =
+ `Expected error to be instance of "${ErrorClass.name}", but was "${e.constructor.name}"${
+ msg ? `: ${msg}` : "."
+ }`;
throw new AssertionError(msg);
}
if (
msgIncludes &&
!stripColor(e.message).includes(stripColor(msgIncludes))
) {
- msg = `Expected error message to include "${msgIncludes}", but got "${
- e.message
- }"${msg ? `: ${msg}` : "."}`;
+ msg =
+ `Expected error message to include "${msgIncludes}", but got "${e.message}"${
+ msg ? `: ${msg}` : "."
+ }`;
throw new AssertionError(msg);
}
doesThrow = true;
@@ -428,7 +431,7 @@ export async function assertThrowsAsync<T = void>(
fn: () => Promise<T>,
ErrorClass?: Constructor,
msgIncludes = "",
- msg?: string
+ msg?: string,
): Promise<Error> {
let doesThrow = false;
let error = null;
@@ -439,18 +442,20 @@ export async function assertThrowsAsync<T = void>(
throw new AssertionError("A non-Error object was thrown or rejected.");
}
if (ErrorClass && !(e instanceof ErrorClass)) {
- msg = `Expected error to be instance of "${ErrorClass.name}", but got "${
- e.name
- }"${msg ? `: ${msg}` : "."}`;
+ msg =
+ `Expected error to be instance of "${ErrorClass.name}", but got "${e.name}"${
+ msg ? `: ${msg}` : "."
+ }`;
throw new AssertionError(msg);
}
if (
msgIncludes &&
!stripColor(e.message).includes(stripColor(msgIncludes))
) {
- msg = `Expected error message to include "${msgIncludes}", but got "${
- e.message
- }"${msg ? `: ${msg}` : "."}`;
+ msg =
+ `Expected error message to include "${msgIncludes}", but got "${e.message}"${
+ msg ? `: ${msg}` : "."
+ }`;
throw new AssertionError(msg);
}
doesThrow = true;