blob: b27b3ccd75655eeac60b1dfeb1589ce8c423320a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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): (s: string) => string {
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): string {
switch (diffType) {
case "added":
return "+ ";
case "removed":
return "- ";
default:
return " ";
}
}
function buildMessage(diffResult: ReadonlyArray<DiffResult<string>>): 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): 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`;
}
throw new Error(message);
}
|