1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

fix(std/testing): Provide message and diff for assertStrictEq (#5417)

closes #5416
This commit is contained in:
Stéphane Klein 2020-05-15 15:49:11 +02:00 committed by GitHub
parent 06f3268f3a
commit 62a7fcbdc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 20 deletions

View file

@ -156,7 +156,8 @@ export function assertEquals(
actualString.split("\n"), actualString.split("\n"),
expectedString.split("\n") expectedString.split("\n")
); );
message = buildMessage(diffResult).join("\n"); const diffMsg = buildMessage(diffResult).join("\n");
message = `Values are not equal:\n${diffMsg}`;
} catch (e) { } catch (e) {
message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`; message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`;
} }
@ -205,24 +206,41 @@ export function assertStrictEq(
expected: unknown, expected: unknown,
msg?: string msg?: string
): void { ): void {
if (actual !== expected) { if (actual === expected) {
let actualString: string; return;
let expectedString: string; }
let message: string;
if (msg) {
message = msg;
} else {
const actualString = format(actual);
const expectedString = format(expected);
if (actualString === expectedString) {
const withOffset = actualString
.split("\n")
.map((l) => ` ${l}`)
.join("\n");
message = `Values have the same structure but are not reference-equal:\n\n${red(
withOffset
)}\n`;
} else {
try { try {
actualString = String(actual); const diffResult = diff(
actualString.split("\n"),
expectedString.split("\n")
);
const diffMsg = buildMessage(diffResult).join("\n");
message = `Values are not strictly equal:\n${diffMsg}`;
} catch (e) { } catch (e) {
actualString = "[Cannot display]"; message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`;
} }
try {
expectedString = String(expected);
} catch (e) {
expectedString = "[Cannot display]";
} }
if (!msg) {
msg = `actual: ${actualString} expected: ${expectedString}`;
}
throw new AssertionError(msg);
} }
throw new AssertionError(message);
} }
/** /**

View file

@ -7,6 +7,7 @@ import {
assertArrayContains, assertArrayContains,
assertMatch, assertMatch,
assertEquals, assertEquals,
assertStrictEq,
assertThrows, assertThrows,
AssertionError, AssertionError,
equal, equal,
@ -278,7 +279,13 @@ test({
assertThrows( assertThrows(
(): void => assertEquals(1, 2), (): void => assertEquals(1, 2),
AssertionError, AssertionError,
[...createHeader(), removed(`- 1`), added(`+ 2`), ""].join("\n") [
"Values are not equal:",
...createHeader(),
removed(`- 1`),
added(`+ 2`),
"",
].join("\n")
); );
}, },
}); });
@ -289,7 +296,12 @@ test({
assertThrows( assertThrows(
(): void => assertEquals(1, "1"), (): void => assertEquals(1, "1"),
AssertionError, AssertionError,
[...createHeader(), removed(`- 1`), added(`+ "1"`)].join("\n") [
"Values are not equal:",
...createHeader(),
removed(`- 1`),
added(`+ "1"`),
].join("\n")
); );
}, },
}); });
@ -301,6 +313,7 @@ test({
(): void => assertEquals([1, "2", 3], ["1", "2", 3]), (): void => assertEquals([1, "2", 3], ["1", "2", 3]),
AssertionError, AssertionError,
[ [
"Values are not equal:",
...createHeader(), ...createHeader(),
removed(`- [ 1, "2", 3 ]`), removed(`- [ 1, "2", 3 ]`),
added(`+ [ "1", "2", 3 ]`), added(`+ [ "1", "2", 3 ]`),
@ -317,6 +330,7 @@ test({
(): void => assertEquals({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }), (): void => assertEquals({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }),
AssertionError, AssertionError,
[ [
"Values are not equal:",
...createHeader(), ...createHeader(),
removed(`- { a: 1, b: "2", c: 3 }`), removed(`- { a: 1, b: "2", c: 3 }`),
added(`+ { a: 1, b: 2, c: [ 3 ] }`), added(`+ { a: 1, b: 2, c: [ 3 ] }`),
@ -325,3 +339,51 @@ test({
); );
}, },
}); });
test({
name: "strict pass case",
fn(): void {
assertStrictEq(true, true);
assertStrictEq(10, 10);
assertStrictEq("abc", "abc");
const xs = [1, false, "foo"];
const ys = xs;
assertStrictEq(xs, ys);
const x = { a: 1 };
const y = x;
assertStrictEq(x, y);
},
});
test({
name: "strict failed with structure diff",
fn(): void {
assertThrows(
(): void => assertStrictEq({ a: 1, b: 2 }, { a: 1, c: [3] }),
AssertionError,
[
"Values are not strictly equal:",
...createHeader(),
removed("- { a: 1, b: 2 }"),
added("+ { a: 1, c: [ 3 ] }"),
"",
].join("\n")
);
},
});
test({
name: "strict failed with reference diff",
fn(): void {
assertThrows(
(): void => assertStrictEq({ a: 1, b: 2 }, { a: 1, b: 2 }),
AssertionError,
[
"Values have the same structure but are not reference-equal:\n",
red(" { a: 1, b: 2 }"),
].join("\n")
);
},
});