-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstringbuilder.test.js
More file actions
296 lines (238 loc) · 9.75 KB
/
Copy pathstringbuilder.test.js
File metadata and controls
296 lines (238 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"use strict";
const StringBuilder = require("../src/stringbuilder");
const { Stream } = require("stream");
describe("StringBuilder", () => {
describe("constructor", () => {
it("creates an empty instance", () => {
const sb = new StringBuilder();
expect(sb.toString()).toBe("");
});
it("accepts an initial value", () => {
expect(new StringBuilder("hello").toString()).toBe("hello");
});
it("ignores null as initial value", () => {
expect(new StringBuilder(null).toString()).toBe("");
});
it("extends Stream", () => {
expect(new StringBuilder()).toBeInstanceOf(Stream);
});
it("exposes pipe from Stream", () => {
expect(typeof new StringBuilder().pipe).toBe("function");
});
});
describe("append", () => {
it("appends a string", () => {
expect(new StringBuilder().append("hello").toString()).toBe("hello");
});
it("appends multiple values", () => {
const sb = new StringBuilder();
sb.append("a").append("b").append("c");
expect(sb.toString()).toBe("abc");
});
it("appends numbers", () => {
expect(new StringBuilder().append(42).toString()).toBe("42");
});
it("appends zero without skipping", () => {
expect(new StringBuilder().append(0).toString()).toBe("0");
});
it("appends false without skipping", () => {
expect(new StringBuilder().append(false).toString()).toBe("false");
});
it("appends a Buffer", () => {
const buf = Buffer.from([0x30, 0x31, 0x32]);
expect(new StringBuilder().append(buf).toString()).toBe("012");
});
it("ignores null", () => {
expect(new StringBuilder().append(null).toString()).toBe("");
});
it("ignores undefined", () => {
expect(new StringBuilder().append(undefined).toString()).toBe("");
});
it("returns this for chaining", () => {
const sb = new StringBuilder();
expect(sb.append("x")).toBe(sb);
});
});
describe("appendLine", () => {
it("appends a newline", () => {
const sb = new StringBuilder().appendLine();
expect(sb.toString()).toBe(sb.newline);
});
it("appends newline then value", () => {
const sb = new StringBuilder("a").appendLine("b");
expect(sb.toString()).toBe("a" + sb.newline + "b");
});
it("ignores null value but still adds newline", () => {
const sb = new StringBuilder().appendLine(null);
expect(sb.toString()).toBe(sb.newline);
});
it("returns this for chaining", () => {
const sb = new StringBuilder();
expect(sb.appendLine()).toBe(sb);
});
});
describe("appendFormat", () => {
it("replaces positional placeholders", () => {
const sb = new StringBuilder().appendFormat("{0} + {1} = {2}", 1, 2, 3);
expect(sb.toString()).toBe("1 + 2 = 3");
});
it("replaces named placeholders with object", () => {
const sb = new StringBuilder().appendFormat("hello {name}!", { name: "world" });
expect(sb.toString()).toBe("hello world!");
});
it("handles escaped braces {{...}}", () => {
const sb = new StringBuilder().appendFormat("{{0}}", "ignored");
expect(sb.toString()).toBe("{0}");
});
it("accepts float values", () => {
const sb = new StringBuilder().appendFormat("{0}", 3.14);
expect(sb.toString()).toBe("3.14");
});
it("returns this for chaining", () => {
const sb = new StringBuilder();
expect(sb.appendFormat("{0}", "x")).toBe(sb);
});
it("applies :U specifier (uppercase)", () => {
expect(new StringBuilder().appendFormat("{0:U}", "hello").toString()).toBe("HELLO");
});
it("applies :L specifier (lowercase)", () => {
expect(new StringBuilder().appendFormat("{0:L}", "WORLD").toString()).toBe("world");
});
it("applies :n specifier (thousand separator)", () => {
const result = new StringBuilder().appendFormat("{0:n}", 1234567).toString();
expect(result).toBe((1234567).toLocaleString());
});
it("applies specifier with named placeholder", () => {
expect(new StringBuilder().appendFormat("{name:U}", { name: "alice" }).toString()).toBe("ALICE");
});
it("leaves value unchanged for unknown specifier", () => {
expect(new StringBuilder().appendFormat("{0:z}", "test").toString()).toBe("test");
});
});
describe("prepend", () => {
it("adds value to the beginning", () => {
expect(new StringBuilder("world").prepend("hello ").toString()).toBe("hello world");
});
it("ignores null", () => {
expect(new StringBuilder("x").prepend(null).toString()).toBe("x");
});
it("returns this for chaining", () => {
const sb = new StringBuilder();
expect(sb.prepend("x")).toBe(sb);
});
it("chains prepend and append", () => {
expect(new StringBuilder("B").prepend("A").append("C").toString()).toBe("ABC");
});
});
describe("replace", () => {
it("replaces first string occurrence", () => {
expect(new StringBuilder("foo foo").replace("foo", "bar").toString()).toBe("bar foo");
});
it("replaces with RegExp", () => {
expect(new StringBuilder("hello world").replace(/world/, "there").toString()).toBe("hello there");
});
it("returns this for chaining", () => {
const sb = new StringBuilder("x");
expect(sb.replace("x", "y")).toBe(sb);
});
it("does nothing on empty builder", () => {
expect(new StringBuilder().replace("x", "y").toString()).toBe("");
});
});
describe("replaceAll", () => {
it("replaces all string occurrences", () => {
expect(new StringBuilder("foo foo foo").replaceAll("foo", "bar").toString()).toBe("bar bar bar");
});
it("replaces all with RegExp", () => {
expect(new StringBuilder("a1b2c3").replaceAll(/\d/g, "-").toString()).toBe("a-b-c-");
});
it("adds global flag to non-global RegExp", () => {
expect(new StringBuilder("aaa").replaceAll(/a/, "b").toString()).toBe("bbb");
});
it("returns this for chaining", () => {
const sb = new StringBuilder("x");
expect(sb.replaceAll("x", "y")).toBe(sb);
});
});
describe("appendJoin", () => {
it("joins array with separator", () => {
expect(new StringBuilder().appendJoin(["a", "b", "c"], ", ").toString()).toBe("a, b, c");
});
it("joins with empty separator by default", () => {
expect(new StringBuilder().appendJoin(["a", "b", "c"]).toString()).toBe("abc");
});
it("works with numbers", () => {
expect(new StringBuilder().appendJoin([1, 2, 3], "-").toString()).toBe("1-2-3");
});
it("ignores null array", () => {
expect(new StringBuilder().appendJoin(null).toString()).toBe("");
});
it("returns this for chaining", () => {
const sb = new StringBuilder();
expect(sb.appendJoin(["x"])).toBe(sb);
});
});
describe("length", () => {
it("returns 0 for empty builder", () => {
expect(new StringBuilder().length).toBe(0);
});
it("returns correct character count", () => {
expect(new StringBuilder("hello").append(" world").length).toBe(11);
});
it("counts numbers and booleans correctly", () => {
expect(new StringBuilder().append(42).append(false).length).toBe(7);
});
it("updates after clear", () => {
const sb = new StringBuilder("hello");
sb.clear();
expect(sb.length).toBe(0);
});
});
describe("isEmpty", () => {
it("returns true for empty builder", () => {
expect(new StringBuilder().isEmpty).toBe(true);
});
it("returns false after append", () => {
expect(new StringBuilder("x").isEmpty).toBe(false);
});
it("returns true after clear", () => {
const sb = new StringBuilder("hello");
sb.clear();
expect(sb.isEmpty).toBe(true);
});
});
describe("clear", () => {
it("empties the buffer", () => {
const sb = new StringBuilder("hello");
sb.clear();
expect(sb.toString()).toBe("");
});
it("allows appending after clear", () => {
const sb = new StringBuilder("hello");
sb.clear();
sb.append("world");
expect(sb.toString()).toBe("world");
});
});
describe("toString", () => {
it("returns empty string when empty", () => {
expect(new StringBuilder().toString()).toBe("");
});
it("joins all parts correctly", () => {
const sb = new StringBuilder("a").append("b").append("c");
expect(sb.toString()).toBe("abc");
});
});
describe("chaining", () => {
it("chains all methods together", () => {
const sb = new StringBuilder();
const result = sb
.append("hello")
.appendFormat(" {0}", "world")
.appendLine()
.append("done")
.toString();
expect(result).toBe("hello world" + sb.newline + "done");
});
});
});