mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 12:49:10 +00:00
refactor 'assertEquals' (denoland/deno_std#560)
* merge 'testing/pretty.ts' into 'testing/asserts.ts'
* throw AssertionError in assertEquals
* update misc tests use AssertionError
Original: ff2d7f24db
This commit is contained in:
parent
63bbe2a2e0
commit
eab0647bd1
6 changed files with 163 additions and 181 deletions
|
@ -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<DiffResult<string>>): 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<string>): 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue