Elffuss BraidHub

kikocis/continue public

3ff29c567dd0 · 8.2 KiBRawPermalink
1import { describe, expect, it } from "vitest";
2import {
3 capitalizeFirstLetter,
4 escapeForSVG,
5 kebabOfStr,
6 kebabOfThemeStr,
7 replaceEscapedCharacters,
8} from "./text";
9
10describe("capitalizeFirstLetter", () => {
11 it("should capitalize the first letter of a string", () => {
12 expect(capitalizeFirstLetter("hello")).toBe("Hello");
13 expect(capitalizeFirstLetter("world")).toBe("World");
14 });
15
16 it("should handle already capitalized strings", () => {
17 expect(capitalizeFirstLetter("Hello")).toBe("Hello");
18 });
19
20 it("should handle single character strings", () => {
21 expect(capitalizeFirstLetter("a")).toBe("A");
22 expect(capitalizeFirstLetter("A")).toBe("A");
23 });
24
25 it("should handle empty strings", () => {
26 expect(capitalizeFirstLetter("")).toBe("");
27 });
28
29 it("should handle strings with numbers", () => {
30 expect(capitalizeFirstLetter("123abc")).toBe("123abc");
31 });
32
33 it("should handle strings with special characters", () => {
34 expect(capitalizeFirstLetter("@hello")).toBe("@hello");
35 expect(capitalizeFirstLetter("!world")).toBe("!world");
36 });
37});
38
39describe("replaceEscapedCharacters", () => {
40 it("should replace escaped newlines with actual newlines", () => {
41 expect(replaceEscapedCharacters("Hello\\nWorld")).toBe("Hello\nWorld");
42 });
43
44 it("should replace escaped tabs with actual tabs", () => {
45 expect(replaceEscapedCharacters("Hello\\tWorld")).toBe("Hello\tWorld");
46 });
47
48 it("should replace escaped carriage returns with actual carriage returns", () => {
49 expect(replaceEscapedCharacters("Hello\\rWorld")).toBe("Hello\rWorld");
50 });
51
52 it("should replace escaped backslashes with actual backslashes", () => {
53 expect(replaceEscapedCharacters("Hello\\\\World")).toBe("Hello\\World");
54 });
55
56 it("should replace escaped double quotes with actual double quotes", () => {
57 expect(replaceEscapedCharacters('Hello\\"World')).toBe('Hello"World');
58 });
59
60 it("should replace escaped single quotes with actual single quotes", () => {
61 expect(replaceEscapedCharacters("Hello\\'World")).toBe("Hello'World");
62 });
63
64 it("should handle multiple escaped characters", () => {
65 expect(
66 replaceEscapedCharacters("Line1\\nLine2\\tTabbed\\\\Backslash"),
67 ).toBe("Line1\nLine2\tTabbed\\Backslash");
68 });
69
70 it("should handle strings without escaped characters", () => {
71 expect(replaceEscapedCharacters("Hello World")).toBe("Hello World");
72 });
73
74 it("should handle empty strings", () => {
75 expect(replaceEscapedCharacters("")).toBe("");
76 });
77
78 it("should not replace invalid escape sequences", () => {
79 expect(replaceEscapedCharacters("Hello\\xWorld")).toBe("Hello\\xWorld");
80 });
81});
82
83describe("escapeForSVG", () => {
84 it("should escape XML entities", () => {
85 expect(escapeForSVG("Hello & World")).toBe("Hello & World");
86 expect(escapeForSVG("Hello < World")).toBe("Hello &lt; World");
87 expect(escapeForSVG("Hello > World")).toBe("Hello &gt; World");
88 expect(escapeForSVG('Hello "World"')).toBe("Hello &quot;World&quot;");
89 expect(escapeForSVG("Hello 'World'")).toBe("Hello &apos;World&apos;");
90 });
91
92 it("should escape whitespace characters as literal escape sequences", () => {
93 expect(escapeForSVG("Hello\nWorld")).toBe("Hello\\nWorld");
94 expect(escapeForSVG("Hello\tWorld")).toBe("Hello\\tWorld");
95 expect(escapeForSVG("Hello\rWorld")).toBe("Hello\\rWorld");
96 });
97
98 it("should handle multiple types of characters together", () => {
99 expect(escapeForSVG("Line1\nLine2&<>\"'Tab\tHere")).toBe(
100 "Line1\\nLine2&amp;&lt;&gt;&quot;&apos;Tab\\tHere",
101 );
102 });
103
104 it("should handle empty strings", () => {
105 expect(escapeForSVG("")).toBe("");
106 });
107
108 it("should handle strings with only whitespace characters", () => {
109 expect(escapeForSVG("\n\t\r")).toBe("\\n\\t\\r");
110 });
111
112 it("should handle strings without special characters", () => {
113 expect(escapeForSVG("Hello World 123")).toBe("Hello World 123");
114 });
115
116 it("should handle complex multiline code examples", () => {
117 const codeExample = `function test() {
118 console.log("Hello & World");
119 return true;
120}`;
121 const expected = `function test() {\\n console.log(&quot;Hello &amp; World&quot;);\\n return true;\\n}`;
122 expect(escapeForSVG(codeExample)).toBe(expected);
123 });
124
125 it("should handle edge case with all escape types", () => {
126 expect(escapeForSVG("&<>\"'\n\t\r")).toBe(
127 "&amp;&lt;&gt;&quot;&apos;\\n\\t\\r",
128 );
129 });
130});
131
132describe("kebabOfStr", () => {
133 it("should convert camelCase to kebab-case", () => {
134 expect(kebabOfStr("camelCase")).toBe("camel-case");
135 expect(kebabOfStr("someVariableName")).toBe("some-variable-name");
136 });
137
138 it("should convert PascalCase to kebab-case", () => {
139 expect(kebabOfStr("PascalCase")).toBe("pascal-case");
140 expect(kebabOfStr("SomeClassName")).toBe("some-class-name");
141 });
142
143 it("should convert spaces to hyphens", () => {
144 expect(kebabOfStr("hello world")).toBe("hello-world");
145 expect(kebabOfStr("multiple spaces")).toBe("multiple-spaces");
146 });
147
148 it("should convert underscores to hyphens", () => {
149 expect(kebabOfStr("snake_case")).toBe("snake-case");
150 expect(kebabOfStr("multiple___underscores")).toBe("multiple-underscores");
151 });
152
153 it("should convert mixed formats", () => {
154 expect(kebabOfStr("mixedCase_with spaces")).toBe("mixed-case-with-spaces");
155 expect(kebabOfStr("PascalCase_Snake SPACE")).toBe(
156 "pascal-case-snake-space",
157 );
158 });
159
160 it("should handle already kebab-case strings", () => {
161 expect(kebabOfStr("already-kebab")).toBe("already-kebab");
162 });
163
164 it("should handle strings with numbers", () => {
165 expect(kebabOfStr("version2Beta")).toBe("version2-beta");
166 expect(kebabOfStr("test123Case")).toBe("test123-case");
167 });
168
169 it("should handle empty strings", () => {
170 expect(kebabOfStr("")).toBe("");
171 });
172
173 it("should handle single characters", () => {
174 expect(kebabOfStr("A")).toBe("a");
175 expect(kebabOfStr("a")).toBe("a");
176 });
177
178 it("should handle special characters", () => {
179 expect(kebabOfStr("hello@world")).toBe("hello@world");
180 expect(kebabOfStr("test.file.name")).toBe("test.file.name");
181 });
182
183 it("should convert to lowercase", () => {
184 expect(kebabOfStr("UPPERCASE")).toBe("uppercase");
185 expect(kebabOfStr("MiXeD cAsE")).toBe("mi-xe-d-c-as-e");
186 });
187});
188
189describe("kebabOfThemeStr", () => {
190 it("should convert spaces to hyphens", () => {
191 expect(kebabOfThemeStr("hello world")).toBe("hello-world");
192 expect(kebabOfThemeStr("multiple spaces")).toBe("multiple-spaces");
193 });
194
195 it("should convert underscores to hyphens", () => {
196 expect(kebabOfThemeStr("snake_case")).toBe("snake-case");
197 expect(kebabOfThemeStr("multiple___underscores")).toBe(
198 "multiple-underscores",
199 );
200 });
201
202 it("should convert mixed spaces and underscores", () => {
203 expect(kebabOfThemeStr("mixed_ case")).toBe("mixed-case");
204 expect(kebabOfThemeStr("theme name_variant")).toBe("theme-name-variant");
205 });
206
207 it("should convert to lowercase", () => {
208 expect(kebabOfThemeStr("UPPERCASE")).toBe("uppercase");
209 expect(kebabOfThemeStr("MiXeD CaSe")).toBe("mixed-case");
210 });
211
212 it("should handle already kebab-case strings", () => {
213 expect(kebabOfThemeStr("already-kebab")).toBe("already-kebab");
214 });
215
216 it("should handle empty strings", () => {
217 expect(kebabOfThemeStr("")).toBe("");
218 });
219
220 it("should handle single characters", () => {
221 expect(kebabOfThemeStr("A")).toBe("a");
222 expect(kebabOfThemeStr("a")).toBe("a");
223 });
224
225 it("should preserve other special characters", () => {
226 expect(kebabOfThemeStr("hello@world")).toBe("hello@world");
227 expect(kebabOfThemeStr("test.file.name")).toBe("test.file.name");
228 });
229
230 it("should not convert camelCase (unlike kebabOfStr)", () => {
231 expect(kebabOfThemeStr("camelCase")).toBe("camelcase");
232 expect(kebabOfThemeStr("PascalCase")).toBe("pascalcase");
233 });
234
235 it("should handle strings with numbers", () => {
236 expect(kebabOfThemeStr("theme_v2 beta")).toBe("theme-v2-beta");
237 expect(kebabOfThemeStr("version_123_FINAL")).toBe("version-123-final");
238 });
239
240 it("should handle strings with parentheses", () => {
241 expect(kebabOfThemeStr("theme_v2 (beta)")).toBe("theme-v2-beta");
242 expect(kebabOfThemeStr("Gruvbox Dark (Hard)")).toBe("gruvbox-dark-hard");
243 });
244});