From 26b0fa530fc1181f7ba32a562b458464e38cbc4a Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sat, 11 Jul 2020 05:52:18 +0100 Subject: [PATCH] feat(Deno.inspect): Add sorted, trailingComma, compact and iterableLimit to InspectOptions (denoland/deno#6591) --- README.md | 7 +- testing/asserts.ts | 26 ++++--- testing/asserts_test.ts | 153 ++++++++++++++++++++++++++++++++-------- 3 files changed, 144 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index f128934c5..a7f47e554 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,10 @@ Don't link to / import any module whose path: - Is that of a test module or test data: `test.ts`, `foo_test.ts`, `testdata/bar.txt`. -No stability is guaranteed for these files. +Don't import any symbol with an underscore prefix: `export function _baz() {}`. + +These elements are not considered part of the public API, thus no stability is +guaranteed for them. ## Documentation @@ -38,7 +41,7 @@ To browse documentation for modules: - Go to https://deno.land/std/. - Navigate to any module of interest. -- Click the "DOCUMENTATION" link. +- Click "View Documentation". ## Contributing diff --git a/testing/asserts.ts b/testing/asserts.ts index 10e2b9c97..b1164090d 100644 --- a/testing/asserts.ts +++ b/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( 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/testing/asserts_test.ts b/testing/asserts_test.ts index 011e98590..c7fa5a737 100644 --- a/testing/asserts_test.ts +++ b/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", +]` + ); +});