-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogUtils.test.ts
349 lines (297 loc) · 11.8 KB
/
LogUtils.test.ts
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright (c) 2023. Heusala Group Oy <info@hg.fi>. All rights reserved.
import { LogUtils } from "./LogUtils";
import "../testing/jest/matchers";
describe('LogUtils', () => {
describe('#stringifyArray', () => {
it('returns empty string when given an empty array', () => {
expect(LogUtils.stringifyArray([])).toEqual('');
});
it('converts an array of primitive types to a string', () => {
expect(LogUtils.stringifyArray([1, 'two', true])).toEqual('1 two true');
});
it('converts an array of objects to a string', () => {
expect(LogUtils.stringifyArray([{ name: 'John' }, { name: 'Jane' }])).toEqual('{"name":"John"} {"name":"Jane"}');
});
it('converts an array of mixed types to a string', () => {
expect(LogUtils.stringifyArray([1, 'two', { name: 'John' }, true])).toEqual('1 two {"name":"John"} true');
});
});
describe('#stringifyValue', () => {
it('converts a string to a string', () => {
expect(LogUtils.stringifyValue('hello')).toEqual('hello');
});
it('converts a number to a string', () => {
expect(LogUtils.stringifyValue(42)).toEqual('42');
});
it('converts a boolean to a string', () => {
expect(LogUtils.stringifyValue(true)).toEqual('true');
expect(LogUtils.stringifyValue(false)).toEqual('false');
});
it('converts an object to a string', () => {
expect(LogUtils.stringifyValue({ name: 'John' })).toEqual('{"name":"John"}');
});
it('converts an Error object to a string', () => {
expect(LogUtils.stringifyValue(new Error('Fail'))).toEqual('Error: Fail');
});
it('converts an TypeError object to a string', () => {
expect(LogUtils.stringifyValue(new TypeError('Fail'))).toEqual('TypeError: Fail');
});
it('converts undefined to the string "undefined"', () => {
expect(LogUtils.stringifyValue(undefined)).toEqual('undefined');
});
it('converts null to the string "null"', () => {
expect(LogUtils.stringifyValue(null)).toEqual('null');
});
it('returns the value as a string when it cannot be JSON.stringify\'d', () => {
const value = { toString: () => 'custom object' };
expect(LogUtils.stringifyValue(value)).toEqual('custom object');
});
it('prefers toString() over toJSON() for custom objects', () => {
const value = { toString: () => 'custom text', toJSON: () => 'custom json' };
expect(LogUtils.stringifyValue(value)).toEqual('custom text');
});
it('returns a string for a Date object', () => {
const date = new Date(Date.UTC(2023, 4, 9, 9, 30, 0, 0));
const result = LogUtils.stringifyValue(date);
expect(result).toBe('2023-05-09T09:30:00.000Z');
});
});
describe('#splitStringValue', () => {
it('can split long string to five rows', () => {
const rows = LogUtils.splitStringValue(
'1234567890abcdefghj\n',
10,
'>>>',
'...\n'
);
expect(rows).toBeArray();
expect(rows[0]).toBe('123456...\n');
expect(rows[1]).toBe('>>>789...\n');
expect(rows[2]).toBe('>>>0ab...\n');
expect(rows[3]).toBe('>>>cde...\n');
expect(rows[4]).toBe('>>>fghj\n');
expect(rows.length).toBe(5);
});
it('can split long string to four rows 1', () => {
const rows = LogUtils.splitStringValue(
'1234567890abcdefgh\n',
10,
'>>>',
'...\n'
);
expect(rows).toBeArray();
expect(rows[0]).toBe('123456...\n');
expect(rows[1]).toBe('>>>789...\n');
expect(rows[2]).toBe('>>>0ab...\n');
expect(rows[3]).toBe('>>>cdefgh\n');
expect(rows.length).toBe(4);
});
it('can split long string four rows 2', () => {
const rows = LogUtils.splitStringValue(
'1234567890abcdefg\n',
10,
'>>>',
'...\n'
);
expect(rows).toBeArray();
expect(rows[0]).toBe('123456...\n');
expect(rows[1]).toBe('>>>789...\n');
expect(rows[2]).toBe('>>>0ab...\n');
expect(rows[3]).toBe('>>>cdefg\n');
expect(rows.length).toBe(4);
});
it('can split long string to four rows 3', () => {
const rows = LogUtils.splitStringValue(
'1234567890abcdef\n',
10,
'>>>',
'...\n'
);
expect(rows).toBeArray();
expect(rows[0]).toBe('123456...\n');
expect(rows[1]).toBe('>>>789...\n');
expect(rows[2]).toBe('>>>0ab...\n');
expect(rows[3]).toBe('>>>cdef\n');
expect(rows.length).toBe(4);
});
it('can split long string to two rows', () => {
const rows = LogUtils.splitStringValue(
'1234567890\n',
10,
'>>>',
'...\n'
);
expect(rows).toBeArray();
expect(rows[0]).toBe('123456...\n');
expect(rows[1]).toBe('>>>7890\n');
expect(rows.length).toBe(2);
});
it('can split long string to one row 1', () => {
const rows = LogUtils.splitStringValue(
'123456789\n',
10,
'>>>',
'...\n'
);
expect(rows).toBeArray();
expect(rows[0]).toBe('123456789\n');
expect(rows.length).toBe(1);
});
it('can split long string to one row 1', () => {
const rows = LogUtils.splitStringValue(
'12345\n',
10,
'>>>',
'...\n'
);
expect(rows).toBeArray();
expect(rows[0]).toBe('12345\n');
expect(rows.length).toBe(1);
});
it('can split short strings 1', () => {
const rows = LogUtils.splitStringValue(
'12345\n',
5,
'> ',
'<\n'
);
expect(rows).toBeArray();
expect(rows[0]).toBe('123<\n');
expect(rows[1]).toBe('> 45\n');
expect(rows.length).toBe(2);
});
it('cannot split too short rows', () => {
expect( () => LogUtils.splitStringValue(
'12345\n',
4,
'> ',
'<\n'
) ).toThrow(new TypeError(`Max size to splitStringValue() must be greater than the length of prefix and suffix (4 < 4)`));
});
});
describe('#splitStringArray', () => {
let maxSize: number;
let prefix: string;
let suffix: string;
let linebreak: string;
beforeEach(() => {
maxSize = 20;
prefix = '>>>';
suffix = '...\n';
linebreak = '\n';
});
it('splits an array of strings into chunks of maximum length', () => {
const input = [
'This is a long sentence that will be split.',
'Another long sentence that will be split as well.'
];
const result = LogUtils.splitStringArray(input, maxSize, prefix, suffix, linebreak);
expect(result).toStrictEqual(
[
'This is a long s...',
'>>>entence that ...',
'>>>will be split.',
'Another long sen...',
'>>>tence that wi...',
'>>>ll be split a...',
'>>>s well.'
]
);
});
it('handles an empty input array', () => {
const input: string[] = [];
const result = LogUtils.splitStringArray(input, maxSize, prefix, suffix, linebreak);
expect(result).toStrictEqual([]);
});
it('handles an array of short strings', () => {
const input = [
'Short string.',
'Another short word.' // Notice: this is exactly 19 characters. It should not be split.
];
const result = LogUtils.splitStringArray(input, maxSize, prefix, suffix, linebreak);
expect(result).toStrictEqual(
[
'Short string.',
'Another short word.'
]
);
});
it('handles an array of mixed length strings', () => {
const input = [
'Short string.',
'This is a long sentence that will be split.',
'Short string.',
'Another long sentence that will be split as well.'
];
const result = LogUtils.splitStringArray(input, maxSize, prefix, suffix, linebreak);
expect(result).toStrictEqual(
[
'Short string.',
'This is a long s...',
'>>>entence that ...',
'>>>will be split.',
'Short string.',
'Another long sen...',
'>>>tence that wi...',
'>>>ll be split a...',
'>>>s well.'
]
);
});
it('returns an unchanged array if maxSize is large enough for all strings', () => {
const input = [
'Short string.',
'Another short string.'
];
maxSize = 50;
const result = LogUtils.splitStringArray(input, maxSize, prefix, suffix, linebreak);
expect(result).toStrictEqual(input);
});
});
describe('#mergeStringArray', () => {
it('merges an empty array into an empty array', () => {
const chunks = LogUtils.mergeStringArray([], 10, '\n');
expect(chunks).toStrictEqual([]);
});
it('merges a single short row into a single chunk', () => {
const chunks = LogUtils.mergeStringArray(['hello'], 10, '\n');
expect(chunks).toStrictEqual(['hello']);
});
it('merges multiple short rows into a single chunk', () => {
const chunks = LogUtils.mergeStringArray(['hello', 'world'], 20, '\n');
expect(chunks).toStrictEqual(['hello\nworld']);
});
it('merges multiple rows into multiple chunks', () => {
const chunks = LogUtils.mergeStringArray(['hello', 'world'], 10, '\n');
expect(chunks).toStrictEqual(['hello', 'world']);
});
it('merges rows with varying lengths into multiple chunks', () => {
const chunks = LogUtils.mergeStringArray(
[
'one',
'two',
'three',
'four4',
'five'
],
12,
'\n'
);
expect(chunks).toStrictEqual(
[
'one\ntwo',
'three\nfour4', // This line requires exactly 12 characters with new line
'five'
]
);
});
it('merges rows with custom line break character', () => {
const chunks = LogUtils.mergeStringArray(['hello', 'world'], 20, '\r\n');
expect(chunks).toStrictEqual(['hello\r\nworld']);
});
it('handles empty rows', () => {
const chunks = LogUtils.mergeStringArray(['hello', '', 'world'], 15, '\n');
expect(chunks).toStrictEqual(['hello\n\nworld']);
});
});
});