summaryrefslogtreecommitdiff
path: root/std/testing
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2020-07-11 05:52:18 +0100
committerGitHub <noreply@github.com>2020-07-11 00:52:18 -0400
commit5ec41cbcc2778a80b6ee91f0c391fc2edec0a8e0 (patch)
tree71d44638e72871624aef63deca19eb271ffa8604 /std/testing
parent40d081d3d9f64bcd2524da86fb78808ac1d7b888 (diff)
feat(Deno.inspect): Add sorted, trailingComma, compact and iterableLimit to InspectOptions (#6591)
Diffstat (limited to 'std/testing')
-rw-r--r--std/testing/asserts.ts26
-rw-r--r--std/testing/asserts_test.ts153
2 files changed, 139 insertions, 40 deletions
diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts
index 10e2b9c97..b1164090d 100644
--- a/std/testing/asserts.ts
+++ b/std/testing/asserts.ts
@@ -19,8 +19,16 @@ export class AssertionError extends Error {
}
}
-function format(v: unknown): string {
- let string = globalThis.Deno ? Deno.inspect(v) : String(v);
+export function _format(v: unknown): string {
+ let string = globalThis.Deno
+ ? Deno.inspect(v, {
+ depth: Infinity,
+ sorted: true,
+ trailingComma: true,
+ compact: false,
+ iterableLimit: Infinity,
+ })
+ : String(v);
if (typeof v == "string") {
string = `"${string.replace(/(?=["\\])/g, "\\")}"`;
}
@@ -167,8 +175,8 @@ export function assertEquals(
return;
}
let message = "";
- const actualString = format(actual);
- const expectedString = format(expected);
+ const actualString = _format(actual);
+ const expectedString = _format(expected);
try {
const diffResult = diff(
actualString.split("\n"),
@@ -248,13 +256,13 @@ export function assertStrictEquals<T>(
if (msg) {
message = msg;
} else {
- const actualString = format(actual);
- const expectedString = format(expected);
+ const actualString = _format(actual);
+ const expectedString = _format(expected);
if (actualString === expectedString) {
const withOffset = actualString
.split("\n")
- .map((l) => ` ${l}`)
+ .map((l) => ` ${l}`)
.join("\n");
message = `Values have the same structure but are not reference-equal:\n\n${red(
withOffset
@@ -335,9 +343,9 @@ export function assertArrayContains(
return;
}
if (!msg) {
- msg = `actual: "${format(actual)}" expected to contain: "${format(
+ msg = `actual: "${_format(actual)}" expected to contain: "${_format(
expected
- )}"\nmissing: ${format(missing)}`;
+ )}"\nmissing: ${_format(missing)}`;
}
throw new AssertionError(msg);
}
diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts
index 011e98590..c7fa5a737 100644
--- a/std/testing/asserts_test.ts
+++ b/std/testing/asserts_test.ts
@@ -1,5 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
+ _format,
assert,
assertNotEquals,
assertStringContains,
@@ -15,7 +16,7 @@ import {
unimplemented,
unreachable,
} from "./asserts.ts";
-import { red, green, gray, bold, yellow } from "../fmt/colors.ts";
+import { red, green, gray, bold, yellow, stripColor } from "../fmt/colors.ts";
Deno.test("testingEqual", function (): void {
assert(equal("world", "world"));
@@ -176,7 +177,23 @@ Deno.test("testingArrayContains", function (): void {
assertThrows(
(): void => assertArrayContains(fixtureObject, [{ deno: "node" }]),
AssertionError,
- `actual: "[ { deno: "luv" }, { deno: "Js" } ]" expected to contain: "[ { deno: "node" } ]"\nmissing: [ { deno: "node" } ]`
+ `actual: "[
+ {
+ deno: "luv",
+ },
+ {
+ deno: "Js",
+ },
+]" expected to contain: "[
+ {
+ deno: "node",
+ },
+]"
+missing: [
+ {
+ deno: "node",
+ },
+]`
);
});
@@ -342,13 +359,13 @@ Deno.test({
assertThrows(
(): void => assertEquals([1, "2", 3], ["1", "2", 3]),
AssertionError,
- [
- "Values are not equal:",
- ...createHeader(),
- removed(`- [ ${yellow("1")}, ${green('"2"')}, ${yellow("3")} ]`),
- added(`+ [ ${green('"1"')}, ${green('"2"')}, ${yellow("3")} ]`),
- "",
- ].join("\n")
+ `
+ [
+- 1,
++ "1",
+ "2",
+ 3,
+ ]`
);
},
});
@@ -359,17 +376,16 @@ Deno.test({
assertThrows(
(): void => assertEquals({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }),
AssertionError,
- [
- "Values are not equal:",
- ...createHeader(),
- removed(
- `- { a: ${yellow("1")}, b: ${green('"2"')}, c: ${yellow("3")} }`
- ),
- added(
- `+ { a: ${yellow("1")}, b: ${yellow("2")}, c: [ ${yellow("3")} ] }`
- ),
- "",
- ].join("\n")
+ `
+ {
+ a: 1,
++ b: 2,
++ c: [
++ 3,
++ ],
+- b: "2",
+- c: 3,
+ }`
);
},
});
@@ -418,13 +434,14 @@ Deno.test({
assertThrows(
(): void => assertStrictEquals({ a: 1, b: 2 }, { a: 1, c: [3] }),
AssertionError,
- [
- "Values are not strictly equal:",
- ...createHeader(),
- removed(`- { a: ${yellow("1")}, b: ${yellow("2")} }`),
- added(`+ { a: ${yellow("1")}, c: [ ${yellow("3")} ] }`),
- "",
- ].join("\n")
+ `
+ {
+ a: 1,
++ c: [
++ 3,
++ ],
+- b: 2,
+ }`
);
},
});
@@ -435,10 +452,12 @@ Deno.test({
assertThrows(
(): void => assertStrictEquals({ a: 1, b: 2 }, { a: 1, b: 2 }),
AssertionError,
- [
- "Values have the same structure but are not reference-equal:\n",
- red(` { a: ${yellow("1")}, b: ${yellow("2")} }`),
- ].join("\n")
+ `Values have the same structure but are not reference-equal:
+
+ {
+ a: 1,
+ b: 2,
+ }`
);
},
});
@@ -535,3 +554,75 @@ Deno.test("Assert Throws Async Non-Error Fail", () => {
"A non-Error object was thrown or rejected."
);
});
+
+Deno.test("assertEquals diff for differently ordered objects", () => {
+ assertThrows(
+ () => {
+ assertEquals(
+ {
+ aaaaaaaaaaaaaaaaaaaaaaaa: 0,
+ bbbbbbbbbbbbbbbbbbbbbbbb: 0,
+ ccccccccccccccccccccccc: 0,
+ },
+ {
+ ccccccccccccccccccccccc: 1,
+ aaaaaaaaaaaaaaaaaaaaaaaa: 0,
+ bbbbbbbbbbbbbbbbbbbbbbbb: 0,
+ }
+ );
+ },
+ AssertionError,
+ `
+ {
+ aaaaaaaaaaaaaaaaaaaaaaaa: 0,
+ bbbbbbbbbbbbbbbbbbbbbbbb: 0,
+- ccccccccccccccccccccccc: 0,
++ ccccccccccccccccccccccc: 1,
+ }`
+ );
+});
+
+// Check that the diff formatter overrides some default behaviours of
+// `Deno.inspect()` which are problematic for diffing.
+Deno.test("assert diff formatting", () => {
+ // Wraps objects into multiple lines even when they are small. Prints trailing
+ // commas.
+ assertEquals(
+ stripColor(_format({ a: 1, b: 2 })),
+ `{
+ a: 1,
+ b: 2,
+}`
+ );
+
+ // Same for nested small objects.
+ assertEquals(
+ stripColor(_format([{ x: { a: 1, b: 2 }, y: ["a", "b"] }])),
+ `[
+ {
+ x: {
+ a: 1,
+ b: 2,
+ },
+ y: [
+ "a",
+ "b",
+ ],
+ },
+]`
+ );
+
+ // Grouping is disabled.
+ assertEquals(
+ stripColor(_format(["i", "i", "i", "i", "i", "i", "i"])),
+ `[
+ "i",
+ "i",
+ "i",
+ "i",
+ "i",
+ "i",
+ "i",
+]`
+ );
+});