Skip to content

Commit

Permalink
feat(helper): add color to assertion message
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Dec 21, 2021
1 parent 54ea0e5 commit 0859d3a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
15 changes: 11 additions & 4 deletions helper/format.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.
import { green, red, yellow } from "../deps.ts";
import { blue, green, magenta, red, yellow } from "../deps.ts";

/** Whatever globalThis have `Deno` or not */
function isDeno(): boolean {
return "Deno" in globalThis;
}

/** helper for stringify */
function stringify(value: unknown): string {
const _stringify = isDeno() ? Deno.inspect : String;
return _stringify(value);
}

/** iterable stringer */
function printIterable(iterable: readonly unknown[]): string[] {
return iterable.map((item) => stringify(item));
}

/** make nested text */
function nestText(text: string, space: number): string {
const whiteSpace = " ";
return `${whiteSpace.repeat(space)}${text}`.replaceAll(
Expand All @@ -31,6 +35,7 @@ type StringifyExpectArgs = {
postModifier: string;
};

/** expect block stringer */
function stringifyExpect({
actual,
matcherArgs,
Expand Down Expand Up @@ -63,6 +68,7 @@ type StringifyAssertArgs = {
actualHint?: string;
};

/** assert message stringer */
function stringifyAssert({
resultActual,
expected,
Expand All @@ -79,6 +85,7 @@ ${nestText(red(stringify(expected)), 2)}
);
}

/** assert stringer */
function stringifyResult(
{
actual,
Expand All @@ -95,10 +102,10 @@ function stringifyResult(
return `${
stringifyExpect({
actual: stringify(actual),
matcherArgs: matcherArgs ? printIterable(matcherArgs).join(", ") : "",
matcherArgs: printIterable(matcherArgs).join(", "),
matcher: yellow(String(matcherName)),
preModifier: preModifierNames.join("."),
postModifier: postModifierNames.join("."),
preModifier: preModifierNames.map(String).map(magenta).join("."),
postModifier: postModifierNames.map(String).map(blue).join("."),
})
}
Expand Down
28 changes: 27 additions & 1 deletion helper/format_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
import { stringify, stringifyExpect } from "./format.ts";
import { stringify, stringifyExpect, stringifyResult } from "./format.ts";
import { assertEquals } from "../dev_deps.ts";
import { blue, green, magenta, red, yellow } from "../deps.ts";
import type { StringifyExpectArgs } from "./format.ts";
Deno.test({
name: "stringify",
Expand Down Expand Up @@ -110,3 +111,28 @@ Deno.test({
);
},
});

Deno.test("stringifyResult", () => {
assertEquals(
stringifyResult({
actual: "test",
actualHint: "Actual:",
matcherArgs: ["testa"],
resultActual: "testa",
matcherName: "toBe",
expected: "test",
expectedHint: "Expected:",
postModifierNames: ["not", "debug"],
preModifierNames: ["string", "trim"],
}),
`expect("test").${magenta("string")}.${magenta("trim")}.${blue("not")}.${
blue("debug")
}.${yellow("toBe")}("testa")
Actual:
${green(`"testa"`)}
Expected:
${red(`"test"`)}
`,
);
});

0 comments on commit 0859d3a

Please sign in to comment.