Skip to content

Commit 3de29c3

Browse files
Dev update.
1 parent c6ec961 commit 3de29c3

File tree

1 file changed

+220
-5
lines changed

1 file changed

+220
-5
lines changed

tests/StringBuilder.test.ts

Lines changed: 220 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,225 @@ import StringBuilder from '../src/StringBuilder';
33

44
describe('StringBuilder', () => {
55

6-
it('should match expected value', () => {
7-
const sb = new StringBuilder();
8-
sb.append('a', 'b', 'c');
9-
sb.append('1', '2', '3');
10-
expect(sb.toString()).equal('abc123');
6+
describe('constructor', () => {
7+
it('should create empty StringBuilder', () => {
8+
const sb = new StringBuilder();
9+
expect(sb.toString()).toBe('');
10+
expect(sb.isEmpty).toBe(true);
11+
});
12+
13+
it('should initialize with values', () => {
14+
const sb = new StringBuilder('hello', ' ', 'world');
15+
expect(sb.toString()).toBe('hello world');
16+
expect(sb.isEmpty).toBe(false);
17+
});
18+
19+
it('should handle null/undefined in constructor', () => {
20+
const sb = new StringBuilder('a', null, 'b', undefined, 'c');
21+
expect(sb.toString()).toBe('abc');
22+
});
23+
});
24+
25+
describe('append methods', () => {
26+
it('should append strings', () => {
27+
const sb = new StringBuilder();
28+
sb.append('a', 'b', 'c');
29+
sb.append('1', '2', '3');
30+
expect(sb.toString()).toBe('abc123');
31+
});
32+
33+
it('should append different types', () => {
34+
const sb = new StringBuilder();
35+
sb.append(123, true, false);
36+
expect(sb.toString()).toBe('123truefalse');
37+
});
38+
39+
it('should handle objects with toString', () => {
40+
const sb = new StringBuilder();
41+
const obj = { toString: () => 'custom' };
42+
sb.append(obj);
43+
expect(sb.toString()).toBe('custom');
44+
});
45+
46+
it('should handle functions', () => {
47+
const sb = new StringBuilder();
48+
const func = function() { return 'test'; };
49+
sb.append(func);
50+
expect(sb.toString()).toContain('function');
51+
});
52+
53+
it('should skip null/undefined items', () => {
54+
const sb = new StringBuilder();
55+
sb.append('a', null, 'b', undefined, 'c');
56+
expect(sb.toString()).toBe('abc');
57+
});
58+
59+
it('should chain append calls', () => {
60+
const sb = new StringBuilder();
61+
const result = sb.append('a').append('b').append('c');
62+
expect(result).toBe(sb); // Should return this for chaining
63+
expect(sb.toString()).toBe('abc');
64+
});
65+
});
66+
67+
describe('appendSingle', () => {
68+
it('should append single item', () => {
69+
const sb = new StringBuilder();
70+
sb.appendSingle('test');
71+
expect(sb.toString()).toBe('test');
72+
});
73+
74+
it('should skip null/undefined', () => {
75+
const sb = new StringBuilder();
76+
sb.appendSingle('a');
77+
sb.appendSingle(null);
78+
sb.appendSingle('b');
79+
sb.appendSingle(undefined);
80+
expect(sb.toString()).toBe('ab');
81+
});
82+
83+
it('should chain appendSingle calls', () => {
84+
const sb = new StringBuilder();
85+
const result = sb.appendSingle('a').appendSingle('b');
86+
expect(result).toBe(sb);
87+
expect(sb.toString()).toBe('ab');
88+
});
89+
});
90+
91+
describe('appendLine methods', () => {
92+
it('should append lines with default newline', () => {
93+
const sb = new StringBuilder();
94+
sb.appendLine('first', 'second');
95+
expect(sb.toString()).toBe('first\nsecond\n');
96+
});
97+
98+
it('should append lines with custom newline', () => {
99+
const sb = new StringBuilder();
100+
sb.setNewLine('\r\n');
101+
sb.appendLine('first', 'second');
102+
expect(sb.toString()).toBe('first\r\nsecond\r\n');
103+
});
104+
105+
it('should skip null/undefined in appendLine', () => {
106+
const sb = new StringBuilder();
107+
sb.appendLine('a', null, 'b', undefined);
108+
expect(sb.toString()).toBe('a\nb\n');
109+
});
110+
111+
it('should chain appendLine calls', () => {
112+
const sb = new StringBuilder();
113+
const result = sb.appendLine('a').appendLine('b');
114+
expect(result).toBe(sb);
115+
});
116+
117+
it('should work with appendLines array method', () => {
118+
const sb = new StringBuilder();
119+
sb.appendLines(['first', 'second']);
120+
expect(sb.toString()).toBe('first\nsecond\n');
121+
});
122+
});
123+
124+
describe('newLine handling', () => {
125+
it('should have default newline', () => {
126+
const sb = new StringBuilder();
127+
expect(sb.newLine).toBe('\n');
128+
});
129+
130+
it('should set custom newline', () => {
131+
const sb = new StringBuilder();
132+
const result = sb.setNewLine('\r\n');
133+
expect(result).toBe(sb); // Should chain
134+
expect(sb.newLine).toBe('\r\n');
135+
});
136+
137+
it('should throw error for null newline', () => {
138+
const sb = new StringBuilder();
139+
expect(() => sb.setNewLine(null as any)).toThrow('\'newLine\' cannot be null or undefined.');
140+
});
141+
142+
it('should throw error for undefined newline', () => {
143+
const sb = new StringBuilder();
144+
expect(() => sb.setNewLine(undefined as any)).toThrow('\'newLine\' cannot be null or undefined.');
145+
});
146+
});
147+
148+
describe('utility methods', () => {
149+
it('should check isEmpty correctly', () => {
150+
const sb = new StringBuilder();
151+
expect(sb.isEmpty).toBe(true);
152+
153+
sb.append('test');
154+
expect(sb.isEmpty).toBe(false);
155+
156+
sb.clear();
157+
expect(sb.isEmpty).toBe(true);
158+
});
159+
160+
it('should join with custom delimiter', () => {
161+
const sb = new StringBuilder();
162+
sb.append('a', 'b', 'c');
163+
expect(sb.join('-')).toBe('a-b-c');
164+
expect(sb.join(', ')).toBe('a, b, c');
165+
});
166+
167+
it('should clear content', () => {
168+
const sb = new StringBuilder();
169+
sb.append('test');
170+
expect(sb.isEmpty).toBe(false);
171+
172+
sb.clear();
173+
expect(sb.isEmpty).toBe(true);
174+
expect(sb.toString()).toBe('');
175+
});
176+
177+
it('should dispose and clear', () => {
178+
const sb = new StringBuilder();
179+
sb.append('test');
180+
181+
sb.dispose();
182+
expect(sb.isEmpty).toBe(true);
183+
expect(sb.toString()).toBe('');
184+
});
185+
});
186+
187+
describe('toString caching', () => {
188+
it('should cache toString result', () => {
189+
const sb = new StringBuilder();
190+
sb.append('test');
191+
192+
const first = sb.toString();
193+
const second = sb.toString();
194+
expect(first).toBe(second);
195+
expect(first).toBe('test');
196+
});
197+
198+
it('should invalidate cache on new content', () => {
199+
const sb = new StringBuilder();
200+
sb.append('test');
201+
expect(sb.toString()).toBe('test');
202+
203+
sb.append('more');
204+
expect(sb.toString()).toBe('testmore');
205+
});
206+
});
207+
208+
describe('edge cases', () => {
209+
it('should handle empty string appends', () => {
210+
const sb = new StringBuilder();
211+
sb.append('', 'test', '');
212+
expect(sb.toString()).toBe('test');
213+
});
214+
215+
it('should handle mixed null/undefined/empty', () => {
216+
const sb = new StringBuilder();
217+
sb.append('a', '', null, 'b', undefined, '', 'c');
218+
expect(sb.toString()).toBe('abc');
219+
});
220+
221+
it('should work with numbers and booleans', () => {
222+
const sb = new StringBuilder();
223+
sb.append(0, 1, true, false);
224+
expect(sb.toString()).toBe('01truefalse');
225+
});
11226
});
12227
});

0 commit comments

Comments
 (0)