mirror of
https://github.com/denoland/deno.git
synced 2025-10-01 14:41:15 +00:00
parent
57f4e6a864
commit
0eed9b3029
7 changed files with 1800 additions and 0 deletions
75
testing/pretty.ts
Normal file
75
testing/pretty.ts
Normal file
|
@ -0,0 +1,75 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { equal } from "./mod.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) {
|
||||
switch (diffType) {
|
||||
case "added":
|
||||
return (s: string) => green(bold(s));
|
||||
case "removed":
|
||||
return (s: string) => red(bold(s));
|
||||
default:
|
||||
return white;
|
||||
}
|
||||
}
|
||||
|
||||
function createSign(diffType: DiffType) {
|
||||
switch (diffType) {
|
||||
case "added":
|
||||
return "+ ";
|
||||
case "removed":
|
||||
return "- ";
|
||||
default:
|
||||
return " ";
|
||||
}
|
||||
}
|
||||
|
||||
function buildMessage(diffResult: ReadonlyArray<DiffResult<string>>) {
|
||||
const messages = [];
|
||||
messages.push("");
|
||||
messages.push("");
|
||||
messages.push(
|
||||
` ${gray(bold("[Diff]"))} ${red(bold("Left"))} / ${green(bold("Right"))}`
|
||||
);
|
||||
messages.push("");
|
||||
messages.push("");
|
||||
diffResult.forEach((result: DiffResult<string>) => {
|
||||
const c = createColor(result.type);
|
||||
messages.push(c(`${createSign(result.type)}${result.value}`));
|
||||
});
|
||||
messages.push("");
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
export function assertEqual(actual: unknown, expected: unknown) {
|
||||
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`;
|
||||
}
|
||||
throw new Error(message);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue