-
Notifications
You must be signed in to change notification settings - Fork 576
/
_build_message.ts
62 lines (53 loc) · 1.7 KB
/
_build_message.ts
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { red } from "../fmt/colors.ts";
import { CAN_NOT_DISPLAY } from "./_constants.ts";
import { buildMessage, diff, diffstr, format } from "../internal/mod.ts";
import type { EqualOptions } from "./_types.ts";
type EqualErrorMessageOptions = Pick<
EqualOptions,
"formatter" | "msg"
>;
export function buildEqualErrorMessage<T>(
actual: T,
expected: T,
options: EqualErrorMessageOptions,
): string {
const { formatter = format, msg } = options || {};
const msgSuffix = msg ? `: ${msg}` : ".";
const actualString = formatter(actual);
const expectedString = formatter(expected);
let message = `Values are not equal${msgSuffix}`;
try {
const stringDiff = (typeof actual === "string") &&
(typeof expected === "string");
const diffResult = stringDiff
? diffstr(actual as string, expected as string)
: diff(actualString.split("\n"), expectedString.split("\n"));
const diffMsg = buildMessage(diffResult, { stringDiff }).join("\n");
message = `${message}\n${diffMsg}`;
} catch {
message = `${message}\n${red(CAN_NOT_DISPLAY)} + \n\n`;
}
return message;
}
export function buildNotEqualErrorMessage<T>(
actual: T,
expected: T,
options: EqualErrorMessageOptions,
): string {
const { msg } = options || {};
let actualString: string;
let expectedString: string;
try {
actualString = String(actual);
} catch {
actualString = CAN_NOT_DISPLAY;
}
try {
expectedString = String(expected);
} catch {
expectedString = CAN_NOT_DISPLAY;
}
const msgSuffix = msg ? `: ${msg}` : ".";
return `Expected actual: ${actualString} not to be: ${expectedString}${msgSuffix}`;
}