diff options
Diffstat (limited to 'std')
-rw-r--r-- | std/fmt/colors.ts | 13 | ||||
-rw-r--r-- | std/fmt/sprintf_test.ts | 19 | ||||
-rw-r--r-- | std/testing/asserts_test.ts | 26 |
3 files changed, 42 insertions, 16 deletions
diff --git a/std/fmt/colors.ts b/std/fmt/colors.ts index 64b4458fc..832f3b164 100644 --- a/std/fmt/colors.ts +++ b/std/fmt/colors.ts @@ -205,3 +205,16 @@ export function bgRgb24(str: string, color: Rgb): string { ) ); } + +// https://github.com/chalk/ansi-regex/blob/2b56fb0c7a07108e5b54241e8faec160d393aedb/index.js +const ANSI_PATTERN = new RegExp( + [ + "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)", + "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))", + ].join("|"), + "g" +); + +export function stripColor(string: string): string { + return string.replace(ANSI_PATTERN, ""); +} diff --git a/std/fmt/sprintf_test.ts b/std/fmt/sprintf_test.ts index 777fb2a15..8955487ac 100644 --- a/std/fmt/sprintf_test.ts +++ b/std/fmt/sprintf_test.ts @@ -1,6 +1,7 @@ import { sprintf } from "./sprintf.ts"; import { assertEquals } from "../testing/asserts.ts"; +import { cyan, yellow } from "./colors.ts"; const S = sprintf; @@ -600,12 +601,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: [Object] } } } }"); + assertEquals(S("%#v", a), `{ a: { a: { a: { a: ${cyan("[Object]")} } } } }`); assertEquals( S("%#.8v", a), "{ a: { a: { a: { a: { a: { a: { a: {} } } } } } } }" ); - assertEquals(S("%#.1v", a), "{ a: [Object] }"); + assertEquals(S("%#.1v", a), `{ a: ${cyan("[Object]")} }`); }); Deno.test("formatJ", function (): void { @@ -618,7 +619,9 @@ Deno.test("flagLessThan", function (): void { const aArray = [a, a, a]; assertEquals( S("%<#.1v", aArray), - "[ { a: [Object] }, { a: [Object] }, { a: [Object] } ]" + `[ { a: ${cyan("[Object]")} }, { a: ${cyan("[Object]")} }, { a: ${cyan( + "[Object]" + )} } ]` ); const fArray = [1.2345, 0.98765, 123456789.5678]; assertEquals(S("%<.2f", fArray), "[ 1.23, 0.99, 123456789.57 ]"); @@ -638,7 +641,10 @@ Deno.test("testErrors", function (): void { assertEquals(S("%*.2f", "a", 1.1), "%!(BAD WIDTH 'a')"); assertEquals(S("%.*f", "a", 1.1), "%!(BAD PREC 'a')"); - assertEquals(S("%.[2]*f", 1.23, "p"), "%!(BAD PREC 'p')%!(EXTRA '1.23')"); + assertEquals( + S("%.[2]*f", 1.23, "p"), + `%!(BAD PREC 'p')%!(EXTRA '${yellow("1.23")}')` + ); assertEquals(S("%.[2]*[1]f Yippie!", 1.23, "p"), "%!(BAD PREC 'p') Yippie!"); assertEquals(S("%[1]*.2f", "a", "p"), "%!(BAD WIDTH 'a')"); @@ -649,7 +655,10 @@ Deno.test("testErrors", function (): void { // 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 '2' '3' '4')"); + assertEquals( + S("%[5]s", 1, 2, 3, 4), + `%!(BAD INDEX)%!(EXTRA '${yellow("2")}' '${yellow("3")}' '${yellow("4")}')` + ); assertEquals(S("%[5]f"), "%!(BAD INDEX)"); assertEquals(S("%.[5]f"), "%!(BAD INDEX)"); assertEquals(S("%.[5]*f"), "%!(BAD INDEX)"); diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts index 1b0f6dd5f..14eabca61 100644 --- a/std/testing/asserts_test.ts +++ b/std/testing/asserts_test.ts @@ -15,7 +15,7 @@ import { unimplemented, unreachable, } from "./asserts.ts"; -import { red, green, gray, bold } from "../fmt/colors.ts"; +import { red, green, gray, bold, yellow } from "../fmt/colors.ts"; const { test } = Deno; test("testingEqual", function (): void { @@ -282,8 +282,8 @@ test({ [ "Values are not equal:", ...createHeader(), - removed(`- 1`), - added(`+ 2`), + removed(`- ${yellow("1")}`), + added(`+ ${yellow("2")}`), "", ].join("\n") ); @@ -299,7 +299,7 @@ test({ [ "Values are not equal:", ...createHeader(), - removed(`- 1`), + removed(`- ${yellow("1")}`), added(`+ "1"`), ].join("\n") ); @@ -315,8 +315,8 @@ test({ [ "Values are not equal:", ...createHeader(), - removed(`- [ 1, "2", 3 ]`), - added(`+ [ "1", "2", 3 ]`), + removed(`- [ ${yellow("1")}, ${green('"2"')}, ${yellow("3")} ]`), + added(`+ [ ${green('"1"')}, ${green('"2"')}, ${yellow("3")} ]`), "", ].join("\n") ); @@ -332,8 +332,12 @@ test({ [ "Values are not equal:", ...createHeader(), - removed(`- { a: 1, b: "2", c: 3 }`), - added(`+ { a: 1, b: 2, c: [ 3 ] }`), + removed( + `- { a: ${yellow("1")}, b: ${green('"2"')}, c: ${yellow("3")} }` + ), + added( + `+ { a: ${yellow("1")}, b: ${yellow("2")}, c: [ ${yellow("3")} ] }` + ), "", ].join("\n") ); @@ -366,8 +370,8 @@ test({ [ "Values are not strictly equal:", ...createHeader(), - removed("- { a: 1, b: 2 }"), - added("+ { a: 1, c: [ 3 ] }"), + removed(`- { a: ${yellow("1")}, b: ${yellow("2")} }`), + added(`+ { a: ${yellow("1")}, c: [ ${yellow("3")} ] }`), "", ].join("\n") ); @@ -382,7 +386,7 @@ test({ AssertionError, [ "Values have the same structure but are not reference-equal:\n", - red(" { a: 1, b: 2 }"), + red(` { a: ${yellow("1")}, b: ${yellow("2")} }`), ].join("\n") ); }, |