std/internal/styles_test.ts
Michael Herzner a5bc643e8e
test(internal): improve test coverage (#4779)
* test(internal): improve test coverage

* test(internal): improve test coverage

* test(internal): implement review remarks

* test(internal): implement review remarks
2024-05-21 22:54:22 +10:00

81 lines
2.1 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import * as c from "./styles.ts";
Deno.test("red() single color", function () {
assertEquals(c.red("foo bar"), "foo bar");
});
Deno.test("red() replaces close characters", function () {
assertEquals(c.red("Hello"), "Hello");
});
Deno.test("getColorEnabled() handles enabled colors", function () {
assertEquals(c.red("foo bar"), "foo bar");
});
Deno.test("bold()", function () {
assertEquals(c.bold("foo bar"), "foo bar");
});
Deno.test("red()", function () {
assertEquals(c.red("foo bar"), "foo bar");
});
Deno.test("green()", function () {
assertEquals(c.green("foo bar"), "foo bar");
});
Deno.test("white()", function () {
assertEquals(c.white("foo bar"), "foo bar");
});
Deno.test("gray()", function () {
assertEquals(c.gray("foo bar"), "foo bar");
});
Deno.test("bgRed()", function () {
assertEquals(c.bgRed("foo bar"), "foo bar");
});
Deno.test("bgGreen()", function () {
assertEquals(c.bgGreen("foo bar"), "foo bar");
});
// https://github.com/chalk/strip-ansi/blob/2b8c961e75760059699373f9a69101065c3ded3a/test.js#L4-L6
Deno.test("stripAnsiCode()", function () {
assertEquals(
c.stripAnsiCode(
"\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m",
),
"foofoo",
);
});
Deno.test("noColor", async function () {
const fixtures = [
["true", "foo bar\n"],
["1", "foo bar\n"],
["", "foo bar\n"],
] as const;
const code = `
import * as c from "${import.meta.resolve("./styles.ts")}";
console.log(c.red("foo bar"));
`;
for await (const [fixture, expected] of fixtures) {
const command = new Deno.Command(Deno.execPath(), {
args: ["eval", "--no-lock", code],
clearEnv: true,
env: {
NO_COLOR: fixture,
},
});
const { stdout } = await command.output();
const decoder = new TextDecoder();
const output = decoder.decode(stdout);
assertEquals(output, expected);
}
});