-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathhelper_unit_test.go
More file actions
456 lines (428 loc) · 10.7 KB
/
helper_unit_test.go
File metadata and controls
456 lines (428 loc) · 10.7 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package carbon
import (
"testing"
"time"
)
// Test format2layout function
func TestFormat2layout(t *testing.T) {
tests := []struct {
name string
format string
expected string
}{
{
name: "Empty format",
format: "",
expected: "",
},
{
name: "Simple date format",
format: "Y-m-d",
expected: "2006-01-02",
},
{
name: "Date and time format",
format: "Y-m-d H:i:s",
expected: "2006-01-02 15:04:05",
},
{
name: "Full datetime format",
format: "Y-m-d H:i:s.u",
expected: "2006-01-02 15:04:05.999",
},
{
name: "Format with escape characters",
format: "Y-m-d \\T H:i:s",
expected: "2006-01-02 T 15:04:05",
},
{
name: "Format with mixed characters",
format: "Y-m-d H:i:s [UTC]",
expected: "2006-01-02 15:04:05 [unixMilliTC]",
},
{
name: "All format characters",
format: "Y-y-m-n-d-j D-l H-h-g a-A i-s u-v-x",
expected: "2006-06-01-1-02-2 Mon-Monday 15-03-3 pm-PM 04-05 999-999999-999999999",
},
{
name: "Timezone formats",
format: "O-P-Q-R-Z",
expected: "-0700--07:00-Z0700-Z07:00-MST",
},
{
name: "Timestamp formats",
format: "S-U-V-X",
expected: "unix-unixMilli-unixMicro-unixNano",
},
{
name: "Multiple escape characters",
format: "Y\\-m\\-d H\\:i\\:s",
expected: "2006-01-02 15:04:05",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := format2layout(tt.format)
if result != tt.expected {
t.Errorf("format2layout(%q) = %q, want %q", tt.format, result, tt.expected)
}
})
}
}
// Test format2layout caching
func TestFormat2layout_Caching(t *testing.T) {
format := "Y-m-d H:i:s"
expected := "2006-01-02 15:04:05"
// First call should convert and cache
result1 := format2layout(format)
if result1 != expected {
t.Fatalf("First format2layout call returned %q, want %q", result1, expected)
}
// Second call should return cached result
result2 := format2layout(format)
if result2 != expected {
t.Fatalf("Second format2layout call returned %q, want %q", result2, expected)
}
// Cache should contain the format
if cached, exists := layoutCache.Load(format); !exists {
t.Errorf("format2layout cache should contain %q", format)
} else if cached.(string) != expected {
t.Errorf("format2layout cache contains %q, want %q", cached, expected)
}
}
// Test format2layout cache limit
func TestFormat2layout_CacheLimit(t *testing.T) {
// Test short format (should be cached)
shortFormat := "Y-m-d"
result := format2layout(shortFormat)
if result == "" {
t.Fatalf("format2layout(%q) failed", shortFormat)
}
if _, exists := layoutCache.Load(shortFormat); !exists {
t.Errorf("Short format %q should be cached", shortFormat)
}
// Test long format (should not be cached)
longFormat := "verylongformatstringthatislongerthan50charactersandshouldnotbecached"
result = format2layout(longFormat)
// This should succeed, but not be cached
if result == "" {
t.Fatalf("format2layout(%q) should have succeeded", longFormat)
}
if _, exists := layoutCache.Load(longFormat); exists {
t.Errorf("Long format %q should not be cached", longFormat)
}
}
// Test parseTimezone function
func TestParseTimezone(t *testing.T) {
tests := []struct {
name string
timezone string
expectError bool
}{
{
name: "Empty timezone",
timezone: "",
expectError: true,
},
{
name: "Valid timezone UTC",
timezone: "UTC",
expectError: false,
},
{
name: "Valid timezone Local",
timezone: "Local",
expectError: false,
},
{
name: "Valid timezone America/New_York",
timezone: "America/New_York",
expectError: false,
},
{
name: "Valid timezone Europe/London",
timezone: "Europe/London",
expectError: false,
},
{
name: "Valid timezone Asia/Shanghai",
timezone: "Asia/Shanghai",
expectError: false,
},
{
name: "Invalid timezone",
timezone: "Invalid/Timezone",
expectError: true,
},
{
name: "Invalid timezone format",
timezone: "InvalidTimezone",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
loc, err := parseTimezone(tt.timezone)
if tt.expectError {
if err == nil {
t.Errorf("parseTimezone(%q) expected error, got nil", tt.timezone)
}
if loc != nil {
t.Errorf("parseTimezone(%q) expected nil location, got %v", tt.timezone, loc)
}
} else {
if err != nil {
t.Errorf("parseTimezone(%q) unexpected error: %v", tt.timezone, err)
}
if loc == nil {
t.Errorf("parseTimezone(%q) expected location, got nil", tt.timezone)
}
}
})
}
}
// Test parseTimezone caching
func TestParseTimezone_Caching(t *testing.T) {
timezone := "UTC"
// First call should parse and cache
loc1, err1 := parseTimezone(timezone)
if err1 != nil {
t.Fatalf("First parseTimezone call failed: %v", err1)
}
// Second call should return cached result
loc2, err2 := parseTimezone(timezone)
if err2 != nil {
t.Fatalf("Second parseTimezone call failed: %v", err2)
}
// Should be the same location instance (cached)
if loc1 != loc2 {
t.Errorf("parseTimezone caching failed: got different instances")
}
// Cache should contain the timezone
if _, exists := timezoneCache.Load(timezone); !exists {
t.Errorf("parseTimezone cache should contain %q", timezone)
}
}
// Test parseDuration function
func TestParseDuration(t *testing.T) {
tests := []struct {
name string
duration string
expectError bool
expectedDur time.Duration
}{
{
name: "Empty duration",
duration: "",
expectError: true,
},
{
name: "Valid duration 1s",
duration: "1s",
expectError: false,
expectedDur: time.Second,
},
{
name: "Valid duration 1m",
duration: "1m",
expectError: false,
expectedDur: time.Minute,
},
{
name: "Valid duration 1h",
duration: "1h",
expectError: false,
expectedDur: time.Hour,
},
{
name: "Valid duration 24h",
duration: "24h",
expectError: false,
expectedDur: 24 * time.Hour,
},
{
name: "Valid duration 0.5s",
duration: "0.5s",
expectError: false,
expectedDur: 500 * time.Millisecond,
},
{
name: "Valid duration 1.5h",
duration: "1.5h",
expectError: false,
expectedDur: 90 * time.Minute,
},
{
name: "Valid duration 2h30m",
duration: "2h30m",
expectError: false,
expectedDur: 2*time.Hour + 30*time.Minute,
},
{
name: "Valid duration 1h2m3s",
duration: "1h2m3s",
expectError: false,
expectedDur: time.Hour + 2*time.Minute + 3*time.Second,
},
{
name: "Valid duration 1ms",
duration: "1ms",
expectError: false,
expectedDur: time.Millisecond,
},
{
name: "Valid duration 1us",
duration: "1us",
expectError: false,
expectedDur: time.Microsecond,
},
{
name: "Valid duration 1ns",
duration: "1ns",
expectError: false,
expectedDur: time.Nanosecond,
},
{
name: "Invalid duration xyz",
duration: "xyz",
expectError: true,
},
{
name: "Invalid duration 1x",
duration: "1x",
expectError: true,
},
{
name: "Invalid duration 1h2x",
duration: "1h2x",
expectError: true,
},
{
name: "Invalid duration verylongdurationstring",
duration: "verylongdurationstring",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dur, err := parseDuration(tt.duration)
if tt.expectError {
if err == nil {
t.Errorf("parseDuration(%q) expected error, got nil", tt.duration)
}
if dur != 0 {
t.Errorf("parseDuration(%q) expected 0 duration, got %v", tt.duration, dur)
}
} else {
if err != nil {
t.Errorf("parseDuration(%q) unexpected error: %v", tt.duration, err)
}
if dur != tt.expectedDur {
t.Errorf("parseDuration(%q) = %v, want %v", tt.duration, dur, tt.expectedDur)
}
}
})
}
}
// Test parseDuration caching
func TestParseDuration_Caching(t *testing.T) {
duration := "1s"
expectedDur := time.Second
// First call should parse and cache
dur1, err1 := parseDuration(duration)
if err1 != nil {
t.Fatalf("First parseDuration call failed: %v", err1)
}
if dur1 != expectedDur {
t.Fatalf("First parseDuration call returned %v, want %v", dur1, expectedDur)
}
// Second call should return cached result
dur2, err2 := parseDuration(duration)
if err2 != nil {
t.Fatalf("Second parseDuration call failed: %v", err2)
}
if dur2 != expectedDur {
t.Fatalf("Second parseDuration call returned %v, want %v", dur2, expectedDur)
}
// Cache should contain the duration
if cached, exists := durationCache.Load(duration); !exists {
t.Errorf("parseDuration cache should contain %q", duration)
} else if cached.(Duration) != expectedDur {
t.Errorf("parseDuration cache contains %v, want %v", cached, expectedDur)
}
}
// Test parseDuration cache limit
func TestParseDuration_CacheLimit(t *testing.T) {
// Test short duration (should be cached)
shortDur := "1s"
_, err := parseDuration(shortDur)
if err != nil {
t.Fatalf("parseDuration(%q) failed: %v", shortDur, err)
}
if _, exists := durationCache.Load(shortDur); !exists {
t.Errorf("Short duration %q should be cached", shortDur)
}
// Test long duration (should not be cached)
longDur := "verylongdurationstringthatislongerthan10characters"
_, err = parseDuration(longDur)
// This should fail, so we expect an error
if err == nil {
t.Fatalf("parseDuration(%q) should have failed", longDur)
}
if _, exists := durationCache.Load(longDur); exists {
t.Errorf("Long duration %q should not be cached", longDur)
}
}
// Test getAbsValue function
func TestGetAbsValue(t *testing.T) {
tests := []struct {
name string
value int64
expected int64
}{
{
name: "Positive value 0",
value: 0,
expected: 0,
},
{
name: "Positive value 1",
value: 1,
expected: 1,
},
{
name: "Positive value 123456",
value: 123456,
expected: 123456,
},
{
name: "Positive value max int64",
value: 9223372036854775807,
expected: 9223372036854775807,
},
{
name: "Negative value -1",
value: -1,
expected: 1,
},
{
name: "Negative value -123456",
value: -123456,
expected: 123456,
},
{
name: "Negative value -9223372036854775807",
value: -9223372036854775807,
expected: 9223372036854775807,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := getAbsValue(tt.value)
if result != tt.expected {
t.Errorf("getAbsValue(%d) = %d, want %d", tt.value, result, tt.expected)
}
})
}
}