| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | capitalizeFirstLetter, |
| 4 | escapeForSVG, |
| 5 | kebabOfStr, |
| 6 | replaceEscapedCharacters, |
| 7 | } from "./text"; |
| 8 | |
| 9 | describe("capitalizeFirstLetter", () => { |
| 10 | it("should capitalize the first letter of a string", () => { |
| 11 | expect(capitalizeFirstLetter("hello")).toBe("Hello"); |
| 12 | expect(capitalizeFirstLetter("world")).toBe("World"); |
| 13 | }); |
| 14 | |
| 15 | it("should handle already capitalized strings", () => { |
| 16 | expect(capitalizeFirstLetter("Hello")).toBe("Hello"); |
| 17 | }); |
| 18 | |
| 19 | it("should handle single character strings", () => { |
| 20 | expect(capitalizeFirstLetter("a")).toBe("A"); |
| 21 | expect(capitalizeFirstLetter("A")).toBe("A"); |
| 22 | }); |
| 23 | |
| 24 | it("should handle empty strings", () => { |
| 25 | expect(capitalizeFirstLetter("")).toBe(""); |
| 26 | }); |
| 27 | |
| 28 | it("should handle strings with numbers", () => { |
| 29 | expect(capitalizeFirstLetter("123abc")).toBe("123abc"); |
| 30 | }); |
| 31 | |
| 32 | it("should handle strings with special characters", () => { |
| 33 | expect(capitalizeFirstLetter("@hello")).toBe("@hello"); |
| 34 | expect(capitalizeFirstLetter("!world")).toBe("!world"); |
| 35 | }); |
| 36 | }); |
| 37 | |
| 38 | describe("replaceEscapedCharacters", () => { |
| 39 | it("should replace escaped newlines with actual newlines", () => { |
| 40 | expect(replaceEscapedCharacters("Hello\\nWorld")).toBe("Hello\nWorld"); |
| 41 | }); |
| 42 | |
| 43 | it("should replace escaped tabs with actual tabs", () => { |
| 44 | expect(replaceEscapedCharacters("Hello\\tWorld")).toBe("Hello\tWorld"); |
| 45 | }); |
| 46 | |
| 47 | it("should replace escaped carriage returns with actual carriage returns", () => { |
| 48 | expect(replaceEscapedCharacters("Hello\\rWorld")).toBe("Hello\rWorld"); |
| 49 | }); |
| 50 | |
| 51 | it("should replace escaped backslashes with actual backslashes", () => { |
| 52 | expect(replaceEscapedCharacters("Hello\\\\World")).toBe("Hello\\World"); |
| 53 | }); |
| 54 | |
| 55 | it("should replace escaped double quotes with actual double quotes", () => { |
| 56 | expect(replaceEscapedCharacters('Hello\\"World')).toBe('Hello"World'); |
| 57 | }); |
| 58 | |
| 59 | it("should replace escaped single quotes with actual single quotes", () => { |
| 60 | expect(replaceEscapedCharacters("Hello\\'World")).toBe("Hello'World"); |
| 61 | }); |
| 62 | |
| 63 | it("should handle multiple escaped characters", () => { |
| 64 | expect( |
| 65 | replaceEscapedCharacters("Line1\\nLine2\\tTabbed\\\\Backslash"), |
| 66 | ).toBe("Line1\nLine2\tTabbed\\Backslash"); |
| 67 | }); |
| 68 | |
| 69 | it("should handle strings without escaped characters", () => { |
| 70 | expect(replaceEscapedCharacters("Hello World")).toBe("Hello World"); |
| 71 | }); |
| 72 | |
| 73 | it("should handle empty strings", () => { |
| 74 | expect(replaceEscapedCharacters("")).toBe(""); |
| 75 | }); |
| 76 | |
| 77 | it("should not replace invalid escape sequences", () => { |
| 78 | expect(replaceEscapedCharacters("Hello\\xWorld")).toBe("Hello\\xWorld"); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | describe("escapeForSVG", () => { |
| 83 | it("should escape XML entities", () => { |
| 84 | expect(escapeForSVG("Hello & World")).toBe("Hello & World"); |
| 85 | expect(escapeForSVG("Hello < World")).toBe("Hello < World"); |
| 86 | expect(escapeForSVG("Hello > World")).toBe("Hello > World"); |
| 87 | expect(escapeForSVG('Hello "World"')).toBe("Hello "World""); |
| 88 | expect(escapeForSVG("Hello 'World'")).toBe("Hello 'World'"); |
| 89 | }); |
| 90 | |
| 91 | it("should escape whitespace characters as literal escape sequences", () => { |
| 92 | expect(escapeForSVG("Hello\nWorld")).toBe("Hello\\nWorld"); |
| 93 | expect(escapeForSVG("Hello\tWorld")).toBe("Hello\\tWorld"); |
| 94 | expect(escapeForSVG("Hello\rWorld")).toBe("Hello\\rWorld"); |
| 95 | }); |
| 96 | |
| 97 | it("should handle multiple types of characters together", () => { |
| 98 | expect(escapeForSVG("Line1\nLine2&<>\"'Tab\tHere")).toBe( |
| 99 | "Line1\\nLine2&<>"'Tab\\tHere", |
| 100 | ); |
| 101 | }); |
| 102 | |
| 103 | it("should handle empty strings", () => { |
| 104 | expect(escapeForSVG("")).toBe(""); |
| 105 | }); |
| 106 | |
| 107 | it("should handle strings with only whitespace characters", () => { |
| 108 | expect(escapeForSVG("\n\t\r")).toBe("\\n\\t\\r"); |
| 109 | }); |
| 110 | |
| 111 | it("should handle strings without special characters", () => { |
| 112 | expect(escapeForSVG("Hello World 123")).toBe("Hello World 123"); |
| 113 | }); |
| 114 | |
| 115 | it("should handle complex multiline code examples", () => { |
| 116 | const codeExample = `function test() { |
| 117 | console.log("Hello & World"); |
| 118 | return true; |
| 119 | }`; |
| 120 | const expected = `function test() {\\n console.log("Hello & World");\\n return true;\\n}`; |
| 121 | expect(escapeForSVG(codeExample)).toBe(expected); |
| 122 | }); |
| 123 | |
| 124 | it("should handle edge case with all escape types", () => { |
| 125 | expect(escapeForSVG("&<>\"'\n\t\r")).toBe( |
| 126 | "&<>"'\\n\\t\\r", |
| 127 | ); |
| 128 | }); |
| 129 | }); |
| 130 | |
| 131 | describe("kebabOfStr", () => { |
| 132 | it("should convert camelCase to kebab-case", () => { |
| 133 | expect(kebabOfStr("camelCase")).toBe("camel-case"); |
| 134 | expect(kebabOfStr("someVariableName")).toBe("some-variable-name"); |
| 135 | }); |
| 136 | |
| 137 | it("should convert PascalCase to kebab-case", () => { |
| 138 | expect(kebabOfStr("PascalCase")).toBe("pascal-case"); |
| 139 | expect(kebabOfStr("SomeClassName")).toBe("some-class-name"); |
| 140 | }); |
| 141 | |
| 142 | it("should convert spaces to hyphens", () => { |
| 143 | expect(kebabOfStr("hello world")).toBe("hello-world"); |
| 144 | expect(kebabOfStr("multiple spaces")).toBe("multiple-spaces"); |
| 145 | }); |
| 146 | |
| 147 | it("should convert underscores to hyphens", () => { |
| 148 | expect(kebabOfStr("snake_case")).toBe("snake-case"); |
| 149 | expect(kebabOfStr("multiple___underscores")).toBe("multiple-underscores"); |
| 150 | }); |
| 151 | |
| 152 | it("should convert mixed formats", () => { |
| 153 | expect(kebabOfStr("mixedCase_with spaces")).toBe("mixed-case-with-spaces"); |
| 154 | expect(kebabOfStr("PascalCase_Snake SPACE")).toBe( |
| 155 | "pascal-case-snake-space", |
| 156 | ); |
| 157 | }); |
| 158 | |
| 159 | it("should handle already kebab-case strings", () => { |
| 160 | expect(kebabOfStr("already-kebab")).toBe("already-kebab"); |
| 161 | }); |
| 162 | |
| 163 | it("should handle strings with numbers", () => { |
| 164 | expect(kebabOfStr("version2Beta")).toBe("version2-beta"); |
| 165 | expect(kebabOfStr("test123Case")).toBe("test123-case"); |
| 166 | }); |
| 167 | |
| 168 | it("should handle empty strings", () => { |
| 169 | expect(kebabOfStr("")).toBe(""); |
| 170 | }); |
| 171 | |
| 172 | it("should handle single characters", () => { |
| 173 | expect(kebabOfStr("A")).toBe("a"); |
| 174 | expect(kebabOfStr("a")).toBe("a"); |
| 175 | }); |
| 176 | |
| 177 | it("should handle special characters", () => { |
| 178 | expect(kebabOfStr("hello@world")).toBe("hello@world"); |
| 179 | expect(kebabOfStr("test.file.name")).toBe("test.file.name"); |
| 180 | }); |
| 181 | |
| 182 | it("should convert to lowercase", () => { |
| 183 | expect(kebabOfStr("UPPERCASE")).toBe("uppercase"); |
| 184 | expect(kebabOfStr("MiXeD cAsE")).toBe("mi-xe-d-c-as-e"); |
| 185 | }); |
| 186 | }); |