Files
test-repo/tests/unit/whitespace-utils.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-02-24 15:41:20 +08:00
import WhitespaceUtils from "../../src/utils/whitespace-utils.js";
it("constructor", () => {
expect(() => new WhitespaceUtils()).toThrow(TypeError);
expect(() => new WhitespaceUtils(1)).toThrow(TypeError);
expect(() => new WhitespaceUtils("")).toThrow(TypeError);
expect(() => new WhitespaceUtils([])).toThrow(TypeError);
expect(() => new WhitespaceUtils([""])).toThrow(TypeError);
expect(() => new WhitespaceUtils(["a"])).toThrow(TypeError);
expect(() => new WhitespaceUtils("a")).toThrow(TypeError);
expect(() => new WhitespaceUtils(["\r\n"])).toThrow(TypeError);
expect(new WhitespaceUtils("\r\n")).toBeInstanceOf(WhitespaceUtils);
expect(new WhitespaceUtils(["\r", "\n"])).toBeInstanceOf(WhitespaceUtils);
});
const utils = new WhitespaceUtils(" ");
describe("trimStart", () => {
for (const [string, expected] of [
["", ""],
[" ", ""],
[" ".repeat(3), ""],
["a", "a"],
["a" + " ".repeat(3), "a "],
[" ".repeat(3) + "a", "a"],
[" ".repeat(3) + "a" + " ".repeat(3), "a "],
]) {
it(JSON.stringify(string), () => {
expect(utils.trimStart(string)).toBe(expected);
});
}
});
describe("trimEnd", () => {
for (const [string, expected] of [
["", ""],
[" ", ""],
[" ".repeat(3), ""],
["a", "a"],
["a" + " ".repeat(3), "a"],
[" ".repeat(3) + "a", " a"],
[" ".repeat(3) + "a" + " ".repeat(3), " a"],
]) {
it(JSON.stringify(string), () => {
expect(utils.trimEnd(string)).toBe(expected);
});
}
});
describe("trim", () => {
for (const [string, expected] of [
["", ""],
[" ", ""],
[" ".repeat(3), ""],
["a", "a"],
["a" + " ".repeat(3), "a"],
[" ".repeat(3) + "a", "a"],
[" ".repeat(3) + "a" + " ".repeat(3), "a"],
]) {
it(JSON.stringify(string), () => {
expect(utils.trim(string)).toBe(expected);
});
}
});
describe("getLeadingWhitespaceCount", () => {
for (const [string, expected] of [
["", 0],
[" ", 1],
[" ".repeat(3), 3],
["a", 0],
["a" + " ".repeat(3), 0],
[" ".repeat(3) + "a", 3],
[" ".repeat(3) + "a" + " ".repeat(3), 3],
]) {
it(JSON.stringify(string), () => {
expect(utils.getLeadingWhitespaceCount(string)).toBe(expected);
});
}
});
describe("getTrailingWhitespaceCount", () => {
for (const [string, expected] of [
["", 0],
[" ", 1],
[" ".repeat(3), 3],
["a", 0],
["a" + " ".repeat(3), 3],
[" ".repeat(3) + "a", 0],
[" ".repeat(3) + "a" + " ".repeat(3), 3],
]) {
it(JSON.stringify(string), () => {
expect(utils.getTrailingWhitespaceCount(string)).toBe(expected);
});
}
});
describe("split", () => {
for (const string of [
"",
" ",
" ".repeat(3),
" a",
" ".repeat(3) + "a",
"a ",
"a" + " ".repeat(3),
" a ",
" ".repeat(3) + "a" + " ".repeat(3),
" a a a ",
]) {
it(JSON.stringify(string), () => {
expect(utils.split(string)).toEqual(string.split(/ +/u));
2023-02-24 15:41:20 +08:00
});
}
});