Skip to content

Commit

Permalink
feat(cli/console): Add Deno.InspectOptions::colors (denoland/deno#7742)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored and caspervonb committed Jan 31, 2021
1 parent 7acf506 commit bbb5d0f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
23 changes: 7 additions & 16 deletions fmt/printf_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { sprintf } from "./printf.ts";
import { assertEquals } from "../testing/asserts.ts";
import { cyan, yellow } from "./colors.ts";

const S = sprintf;

Expand Down Expand Up @@ -607,12 +606,12 @@ Deno.test("testWeirdos", function (): void {
Deno.test("formatV", function (): void {
const a = { a: { a: { a: { a: { a: { a: { a: {} } } } } } } };
assertEquals(S("%v", a), "[object Object]");
assertEquals(S("%#v", a), `{ a: { a: { a: { a: ${cyan("[Object]")} } } } }`);
assertEquals(S("%#v", a), `{ a: { a: { a: { a: [Object] } } } }`);
assertEquals(
S("%#.8v", a),
"{ a: { a: { a: { a: { a: { a: { a: {} } } } } } } }",
);
assertEquals(S("%#.1v", a), `{ a: ${cyan("[Object]")} }`);
assertEquals(S("%#.1v", a), `{ a: [Object] }`);
});

Deno.test("formatJ", function (): void {
Expand All @@ -625,9 +624,7 @@ Deno.test("flagLessThan", function (): void {
const aArray = [a, a, a];
assertEquals(
S("%<#.1v", aArray),
`[ { a: ${cyan("[Object]")} }, { a: ${cyan("[Object]")} }, { a: ${
cyan("[Object]")
} } ]`,
`[ { a: [Object] }, { a: [Object] }, { a: [Object] } ]`,
);
const fArray = [1.2345, 0.98765, 123456789.5678];
assertEquals(S("%<.2f", fArray), "[ 1.23, 0.99, 123456789.57 ]");
Expand All @@ -649,27 +646,21 @@ Deno.test("testErrors", function (): void {
assertEquals(S("%.*f", "a", 1.1), "%!(BAD PREC 'a')");
assertEquals(
S("%.[2]*f", 1.23, "p"),
`%!(BAD PREC 'p')%!(EXTRA '${yellow("1.23")}')`,
`%!(BAD PREC 'p')%!(EXTRA '1.23')`,
);
assertEquals(S("%.[2]*[1]f Yippie!", 1.23, "p"), "%!(BAD PREC 'p') Yippie!");

assertEquals(S("%[1]*.2f", "a", "p"), "%!(BAD WIDTH 'a')");

assertEquals(
S("A", "a", "p"),
`A%!(EXTRA '\x1b[32m"a"\x1b[39m' '\x1b[32m"p"\x1b[39m')`,
);
assertEquals(
S("%[2]s %[2]s", "a", "p"),
`p p%!(EXTRA '\x1b[32m"a"\x1b[39m')`,
);
assertEquals(S("A", "a", "p"), `A%!(EXTRA '"a"' '"p"')`);
assertEquals(S("%[2]s %[2]s", "a", "p"), `p p%!(EXTRA '"a"')`);

// remains to be determined how to handle bad indices ...
// (realistically) the entire error handling is still up for grabs.
assertEquals(S("%[hallo]s %d %d %d", 1, 2, 3, 4), "%!(BAD INDEX) 2 3 4");
assertEquals(
S("%[5]s", 1, 2, 3, 4),
`%!(BAD INDEX)%!(EXTRA '${yellow("2")}' '${yellow("3")}' '${yellow("4")}')`,
`%!(BAD INDEX)%!(EXTRA '2' '3' '4')`,
);
assertEquals(S("%[5]f"), "%!(BAD INDEX)");
assertEquals(S("%.[5]f"), "%!(BAD INDEX)");
Expand Down
4 changes: 2 additions & 2 deletions testing/asserts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export class AssertionError extends Error {

export function _format(v: unknown): string {
return globalThis.Deno
? stripColor(Deno.inspect(v, {
? Deno.inspect(v, {
depth: Infinity,
sorted: true,
trailingComma: true,
compact: false,
iterableLimit: Infinity,
}))
})
: `"${String(v).replace(/(?=["\\])/g, "\\")}"`;
}

Expand Down

0 comments on commit bbb5d0f

Please sign in to comment.