diff --git a/bundle/test.ts b/bundle/test.ts index d618e202bb..126b8682b5 100644 --- a/bundle/test.ts +++ b/bundle/test.ts @@ -4,10 +4,10 @@ import { test } from "../testing/mod.ts"; import { assert, AssertionError, + assertEquals, assertStrictEq, assertThrowsAsync } from "../testing/asserts.ts"; -import { assertEquals } from "../testing/pretty.ts"; import { evaluate, instantiate, load, ModuleMetaData } from "./utils.ts"; /* eslint-disable @typescript-eslint/no-namespace */ diff --git a/testing/asserts.ts b/testing/asserts.ts index 904a4a9667..56480c95bd 100644 --- a/testing/asserts.ts +++ b/testing/asserts.ts @@ -1,5 +1,9 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { assertEquals as prettyAssertEqual } from "./pretty.ts"; +import { red, green, white, gray, bold } from "../colors/mod.ts"; +import diff, { DiffType, DiffResult } from "./diff.ts"; +import { format } from "./format.ts"; + +const CAN_NOT_DISPLAY = "[Cannot display]"; interface Constructor { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -13,6 +17,56 @@ export class AssertionError extends Error { } } +function createStr(v: unknown): string { + try { + return format(v); + } catch (e) { + return red(CAN_NOT_DISPLAY); + } +} + +function createColor(diffType: DiffType): (s: string) => string { + switch (diffType) { + case DiffType.added: + return (s: string): string => green(bold(s)); + case DiffType.removed: + return (s: string): string => red(bold(s)); + default: + return white; + } +} + +function createSign(diffType: DiffType): string { + switch (diffType) { + case DiffType.added: + return "+ "; + case DiffType.removed: + return "- "; + default: + return " "; + } +} + +function buildMessage(diffResult: ReadonlyArray>): string[] { + const messages: string[] = []; + messages.push(""); + messages.push(""); + messages.push( + ` ${gray(bold("[Diff]"))} ${red(bold("Left"))} / ${green(bold("Right"))}` + ); + messages.push(""); + messages.push(""); + diffResult.forEach( + (result: DiffResult): void => { + const c = createColor(result.type); + messages.push(c(`${createSign(result.type)}${result.value}`)); + } + ); + messages.push(""); + + return messages; +} + export function equal(c: unknown, d: unknown): boolean { const seen = new Map(); return (function compare(a: unknown, b: unknown): boolean { @@ -77,7 +131,25 @@ export function assertEquals( expected: unknown, msg?: string ): void { - prettyAssertEqual(actual, expected, msg); + if (equal(actual, expected)) { + return; + } + let message = ""; + const actualString = createStr(actual); + const expectedString = createStr(expected); + try { + const diffResult = diff( + actualString.split("\n"), + expectedString.split("\n") + ); + message = buildMessage(diffResult).join("\n"); + } catch (e) { + message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`; + } + if (msg) { + message = msg; + } + throw new AssertionError(message); } /** diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts index e4f05f166e..ba07b3a83b 100644 --- a/testing/asserts_test.ts +++ b/testing/asserts_test.ts @@ -15,6 +15,7 @@ import { unreachable } from "./asserts.ts"; import { test } from "./mod.ts"; +import { red, green, white, gray, bold } from "../colors/mod.ts"; test(function testingEqual(): void { assert(equal("world", "world")); @@ -163,3 +164,90 @@ test(function testingAssertFail(): void { "Failed assertion: foo" ); }); + +const createHeader = (): string[] => [ + "", + "", + ` ${gray(bold("[Diff]"))} ${red(bold("Left"))} / ${green(bold("Right"))}`, + "", + "" +]; + +const added: (s: string) => string = (s: string): string => green(bold(s)); +const removed: (s: string) => string = (s: string): string => red(bold(s)); + +test({ + name: "pass case", + fn(): void { + assertEquals({ a: 10 }, { a: 10 }); + assertEquals(true, true); + assertEquals(10, 10); + assertEquals("abc", "abc"); + assertEquals({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } }); + } +}); + +test({ + name: "failed with number", + fn(): void { + assertThrows( + (): void => assertEquals(1, 2), + AssertionError, + [...createHeader(), removed(`- 1`), added(`+ 2`), ""].join("\n") + ); + } +}); + +test({ + name: "failed with number vs string", + fn(): void { + assertThrows( + (): void => assertEquals(1, "1"), + AssertionError, + [...createHeader(), removed(`- 1`), added(`+ "1"`)].join("\n") + ); + } +}); + +test({ + name: "failed with array", + fn(): void { + assertThrows( + (): void => assertEquals([1, "2", 3], ["1", "2", 3]), + AssertionError, + [ + ...createHeader(), + white(" Array ["), + removed(`- 1,`), + added(`+ "1",`), + white(' "2",'), + white(" 3,"), + white(" ]"), + "" + ].join("\n") + ); + } +}); + +test({ + name: "failed with object", + fn(): void { + assertThrows( + (): void => assertEquals({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }), + AssertionError, + [ + ...createHeader(), + white(" Object {"), + white(` "a": 1,`), + added(`+ "b": 2,`), + added(`+ "c": Array [`), + added(`+ 3,`), + added(`+ ],`), + removed(`- "b": "2",`), + removed(`- "c": 3,`), + white(" }"), + "" + ].join("\n") + ); + } +}); diff --git a/testing/pretty.ts b/testing/pretty.ts deleted file mode 100644 index f471bd6fd1..0000000000 --- a/testing/pretty.ts +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - -import { equal } from "./asserts.ts"; -import { red, green, white, gray, bold } from "../colors/mod.ts"; -import diff, { DiffType, DiffResult } from "./diff.ts"; -import { format } from "./format.ts"; - -const CAN_NOT_DISPLAY = "[Cannot display]"; - -function createStr(v: unknown): string { - try { - return format(v); - } catch (e) { - return red(CAN_NOT_DISPLAY); - } -} - -function createColor(diffType: DiffType): (s: string) => string { - switch (diffType) { - case DiffType.added: - return (s: string): string => green(bold(s)); - case DiffType.removed: - return (s: string): string => red(bold(s)); - default: - return white; - } -} - -function createSign(diffType: DiffType): string { - switch (diffType) { - case DiffType.added: - return "+ "; - case DiffType.removed: - return "- "; - default: - return " "; - } -} - -function buildMessage(diffResult: ReadonlyArray>): string[] { - const messages: string[] = []; - messages.push(""); - messages.push(""); - messages.push( - ` ${gray(bold("[Diff]"))} ${red(bold("Left"))} / ${green(bold("Right"))}` - ); - messages.push(""); - messages.push(""); - diffResult.forEach( - (result: DiffResult): void => { - const c = createColor(result.type); - messages.push(c(`${createSign(result.type)}${result.value}`)); - } - ); - messages.push(""); - - return messages; -} - -export function assertEquals( - actual: unknown, - expected: unknown, - msg?: string -): void { - if (equal(actual, expected)) { - return; - } - let message = ""; - const actualString = createStr(actual); - const expectedString = createStr(expected); - try { - const diffResult = diff( - actualString.split("\n"), - expectedString.split("\n") - ); - message = buildMessage(diffResult).join("\n"); - } catch (e) { - message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`; - } - if (msg) { - message = msg; - } - throw new Error(message); -} diff --git a/testing/pretty_test.ts b/testing/pretty_test.ts deleted file mode 100644 index 07ab83d5ee..0000000000 --- a/testing/pretty_test.ts +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - -import { test } from "./mod.ts"; -import { red, green, white, gray, bold } from "../colors/mod.ts"; -import { assertEquals } from "./pretty.ts"; -import { assertThrows } from "./asserts.ts"; - -const createHeader = (): string[] => [ - "", - "", - ` ${gray(bold("[Diff]"))} ${red(bold("Left"))} / ${green(bold("Right"))}`, - "", - "" -]; - -const added: (s: string) => string = (s: string): string => green(bold(s)); -const removed: (s: string) => string = (s: string): string => red(bold(s)); - -test({ - name: "pass case", - fn(): void { - assertEquals({ a: 10 }, { a: 10 }); - assertEquals(true, true); - assertEquals(10, 10); - assertEquals("abc", "abc"); - assertEquals({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } }); - } -}); - -test({ - name: "failed with number", - fn(): void { - assertThrows( - (): void => assertEquals(1, 2), - Error, - [...createHeader(), removed(`- 1`), added(`+ 2`), ""].join("\n") - ); - } -}); - -test({ - name: "failed with number vs string", - fn(): void { - assertThrows( - (): void => assertEquals(1, "1"), - Error, - [...createHeader(), removed(`- 1`), added(`+ "1"`)].join("\n") - ); - } -}); - -test({ - name: "failed with array", - fn(): void { - assertThrows( - (): void => assertEquals([1, "2", 3], ["1", "2", 3]), - Error, - [ - ...createHeader(), - white(" Array ["), - removed(`- 1,`), - added(`+ "1",`), - white(' "2",'), - white(" 3,"), - white(" ]"), - "" - ].join("\n") - ); - } -}); - -test({ - name: "failed with object", - fn(): void { - assertThrows( - (): void => assertEquals({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }), - Error, - [ - ...createHeader(), - white(" Object {"), - white(` "a": 1,`), - added(`+ "b": 2,`), - added(`+ "c": Array [`), - added(`+ 3,`), - added(`+ ],`), - removed(`- "b": "2",`), - removed(`- "c": 3,`), - white(" }"), - "" - ].join("\n") - ); - } -}); diff --git a/testing/test.ts b/testing/test.ts index c726ffb1ab..8c86791bab 100644 --- a/testing/test.ts +++ b/testing/test.ts @@ -9,7 +9,6 @@ import { } from "./asserts.ts"; import "./format_test.ts"; import "./diff_test.ts"; -import "./pretty_test.ts"; import "./asserts_test.ts"; import "./bench_test.ts";