Skip to content

Commit

Permalink
Remove testing/format.ts (denoland/deno#4749)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored and caspervonb committed Jan 31, 2021
1 parent c838e1a commit 3d76ea8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1,386 deletions.
15 changes: 7 additions & 8 deletions testing/asserts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { red, green, white, gray, bold } from "../fmt/colors.ts";
import diff, { DiffType, DiffResult } from "./diff.ts";
import { format } from "./format.ts";

const CAN_NOT_DISPLAY = "[Cannot display]";

Expand All @@ -17,12 +16,12 @@ export class AssertionError extends Error {
}
}

function createStr(v: unknown): string {
try {
return format(v);
} catch (e) {
return red(CAN_NOT_DISPLAY);
function format(v: unknown): string {
let string = Deno.inspect(v);
if (typeof v == "string") {
string = `"${string.replace(/(?=["\\])/g, "\\")}"`;
}
return string;
}

function createColor(diffType: DiffType): (s: string) => string {
Expand Down Expand Up @@ -150,8 +149,8 @@ export function assertEquals(
return;
}
let message = "";
const actualString = createStr(actual);
const expectedString = createStr(expected);
const actualString = format(actual);
const expectedString = format(expected);
try {
const diffResult = diff(
actualString.split("\n"),
Expand Down
21 changes: 5 additions & 16 deletions testing/asserts_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
unimplemented,
unreachable,
} from "./asserts.ts";
import { red, green, white, gray, bold } from "../fmt/colors.ts";
import { red, green, gray, bold } from "../fmt/colors.ts";
const { test } = Deno;

test(function testingEqual(): void {
Expand Down Expand Up @@ -302,12 +302,8 @@ test({
AssertionError,
[
...createHeader(),
white(" Array ["),
removed(`- 1,`),
added(`+ "1",`),
white(' "2",'),
white(" 3,"),
white(" ]"),
removed(`- [ 1, "2", 3 ]`),
added(`+ [ "1", "2", 3 ]`),
"",
].join("\n")
);
Expand All @@ -322,15 +318,8 @@ test({
AssertionError,
[
...createHeader(),
white(" Object {"),
white(` "a": 1,`),
added(`+ "b": 2,`),
added(`+ "c": Array [`),
added(`+ 3,`),
added(`+ ],`),
removed(`- "b": "2",`),
removed(`- "c": 3,`),
white(" }"),
removed(`- { a: 1, b: "2", c: 3 }`),
added(`+ { a: 1, b: 2, c: [ 3 ] }`),
"",
].join("\n")
);
Expand Down
Loading

0 comments on commit 3d76ea8

Please sign in to comment.