std/cli/unicode_width_test.ts
2024-04-29 11:57:30 +09:00

61 lines
2.8 KiB
TypeScript
Raw 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 { unicodeWidth } from "./unicode_width.ts";
import { assertEquals } from "@std/assert";
Deno.test("unicodeWidth()", async (t) => {
await t.step("checks ASCII input", () => {
const lorem =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
assertEquals(unicodeWidth(lorem), lorem.length);
});
await t.step("checks CJK input", () => {
const qianZiWen =
"宿調使";
assertEquals(unicodeWidth(qianZiWen), qianZiWen.length * 2);
});
const str = "á";
await t.step("checks NFC normalized input", () => {
const nfc = str.normalize("NFC");
assertEquals(nfc.length, 1);
assertEquals(unicodeWidth(nfc), 1);
});
await t.step("checks NFD normalized input", () => {
const nfd = str.normalize("NFD");
assertEquals(nfd.length, 2);
assertEquals(unicodeWidth(nfd), 1);
});
await t.step("checks emoji input", () => {
assertEquals(unicodeWidth("👩"), 2); // Woman
assertEquals(unicodeWidth("🔬"), 2); // Microscope
// Note: Returns 4 for the below case, following the upstream crate
// `unicode_width`. Another possibility might be returning 2, which is what
// `npm:string-width` returns.
// See discussion at https://github.com/denoland/deno_std/pull/3297#discussion_r1166289430
assertEquals(unicodeWidth("👩🔬"), 4); // Woman Scientist
});
await t.step("checks escape sequences", () => {
assertEquals(unicodeWidth("\0"), 0);
assertEquals(unicodeWidth("\n"), 0);
assertEquals(unicodeWidth("\r"), 0);
assertEquals(unicodeWidth("\t"), 0);
});
await t.step("checks C1 controls", () => {
assertEquals(unicodeWidth("\u0080"), 0);
assertEquals(unicodeWidth("\u008A"), 0);
assertEquals(unicodeWidth("\u0093"), 0);
assertEquals(unicodeWidth("\u009F"), 0);
});
});