-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
382 lines (356 loc) · 7.32 KB
/
format_test.go
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
package hdur
import (
"encoding/json"
"testing"
)
func BenchmarkDurationString(b *testing.B) {
durations := []Duration{
Hours(2),
MustParseDuration("1y 2mo 3d"),
Minutes(90),
Milliseconds(500),
MustParseDuration("2.5h"),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
d := durations[i%len(durations)]
_ = d.String()
}
}
func BenchmarkDurationFormat(b *testing.B) {
d := MustParseDuration("1y 2mo 3d 4h 5m 6s")
formats := []string{
"%y years %M months %d days",
"%h hours %m minutes %s seconds",
"%y-%y-%y",
"%s.%f seconds",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
format := formats[i%len(formats)]
_ = d.Format(format)
}
}
func BenchmarkDurationJSON(b *testing.B) {
type wrapper struct {
D Duration `json:"duration"`
}
w := wrapper{D: MustParseDuration("1y 2mo 3d")}
data, _ := json.Marshal(w)
b.Run("marshal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(w)
}
})
b.Run("unmarshal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var w2 wrapper
_ = json.Unmarshal(data, &w2)
}
})
}
func TestDuration_String(t *testing.T) {
tests := []struct {
name string
duration Duration
want string
}{
{
name: "zero duration",
duration: Duration{},
want: "0s",
},
{
name: "full duration",
duration: Duration{
Years: 1, Months: 2, Days: 3,
Hours: 4, Minutes: 5, Seconds: 6,
},
want: "1y 2mo 3d 4h 5m 6s",
},
{
name: "only large units",
duration: Duration{
Years: 1, Months: 2,
},
want: "1y 2mo",
},
{
name: "only small units",
duration: Duration{
Minutes: 5, Seconds: 6,
},
want: "5m 6s",
},
{
name: "nanoseconds",
duration: Duration{
Nanos: 500000000,
},
want: "500ms",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.duration.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
func TestDuration_String_Edge_Cases(t *testing.T) {
tests := []struct {
name string
d Duration
want string
}{
{
name: "all negative components",
d: Duration{Years: -1, Months: -2, Days: -3, Hours: -4, Minutes: -5, Seconds: -6, Nanos: -7},
want: "-1y 2mo 3d 4h 5m 6s 7ns",
},
{
name: "only nanoseconds",
d: Duration{Nanos: 500},
want: "500ns",
},
{
name: "microseconds exact",
d: Duration{Nanos: 3000},
want: "3µs",
},
{
name: "microseconds with fraction",
d: Duration{Nanos: 3500},
want: "3.500µs",
},
{
name: "milliseconds exact",
d: Duration{Nanos: 3000000},
want: "3ms",
},
{
name: "milliseconds with fraction",
d: Duration{Nanos: 3500000},
want: "3.500ms",
},
{
name: "zero with nanoseconds",
d: Duration{Nanos: 0},
want: "0s",
},
{
name: "negative nanoseconds only",
d: Duration{Nanos: -500},
want: "-500ns",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.d.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
func TestDuration_Format(t *testing.T) {
d := MustParseDuration("1y 2mo 3d 4h 5m 6s")
tests := []struct {
name string
format string
expected string
}{
{
name: "full format",
format: "%y years %M months %d days",
expected: "1 years 2 months 3 days",
},
{
name: "partial format",
format: "%y years and %d days",
expected: "1 years and 3 days",
},
{
name: "repeated specifiers",
format: "%y-%y-%y",
expected: "1-1-1",
},
{
name: "fractional seconds",
format: "%s.%f seconds",
expected: "6.000000000 seconds",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := d.Format(tt.format)
if got != tt.expected {
t.Errorf("got %v, want %v", got, tt.expected)
}
})
}
}
func TestDurationJSON(t *testing.T) {
type wrapper struct {
D Duration `json:"duration"`
}
tests := []struct {
name string
d Duration
expected string
input string
}{
{
name: "simple duration",
d: Hours(2),
expected: `{"duration":"2h"}`,
input: `{"duration":"2h"}`,
},
{
name: "complex duration",
d: MustParseDuration("1y 2mo 3d"),
expected: `{"duration":"1y 2mo 3d"}`,
input: `{"duration":"1y 2mo 3d"}`,
},
{
name: "zero duration",
d: Duration{},
expected: `{"duration":"0s"}`,
input: `{"duration":"0s"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test marshaling
w := wrapper{D: tt.d}
got, err := json.Marshal(w)
if err != nil {
t.Errorf("marshal error: %v", err)
}
if string(got) != tt.expected {
t.Errorf("marshal: got %v, want %v", string(got), tt.expected)
}
// Test unmarshaling
var w2 wrapper
err = json.Unmarshal([]byte(tt.input), &w2)
if err != nil {
t.Errorf("unmarshal error: %v", err)
}
if !w2.D.Equal(tt.d) {
t.Errorf("unmarshal: got %v, want %v", w2.D, tt.d)
}
})
}
}
func TestDurationSQL(t *testing.T) {
tests := []struct {
name string
d Duration
expected string
}{
{
name: "simple duration",
d: Hours(2),
expected: "2h",
},
{
name: "complex duration",
d: MustParseDuration("1y 2mo 3d"),
expected: "1y 2mo 3d",
},
{
name: "zero duration",
d: Duration{},
expected: "0s",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test Value
got, err := tt.d.Value()
if err != nil {
t.Errorf("value error: %v", err)
}
if got != tt.expected {
t.Errorf("value: got %v, want %v", got, tt.expected)
}
// Test Scan
var d2 Duration
err = d2.Scan(tt.expected)
if err != nil {
t.Errorf("scan error: %v", err)
}
if !d2.Equal(tt.d) {
t.Errorf("scan: got %v, want %v", d2, tt.d)
}
// Test Scan with []byte
err = d2.Scan([]byte(tt.expected))
if err != nil {
t.Errorf("scan bytes error: %v", err)
}
if !d2.Equal(tt.d) {
t.Errorf("scan bytes: got %v, want %v", d2, tt.d)
}
// Test Scan with nil
err = d2.Scan(nil)
if err != nil {
t.Errorf("scan nil error: %v", err)
}
if !d2.Equal(Duration{}) {
t.Errorf("scan nil: got %v, want zero duration", d2)
}
// Test Scan with invalid type
err = d2.Scan(123)
if err == nil {
t.Error("expected error scanning invalid type")
}
})
}
}
func TestDuration_UnmarshalJSON_Edge_Cases(t *testing.T) {
tests := []struct {
name string
json string
want Duration
wantErr bool
}{
{
name: "invalid JSON",
json: `{"duration": "1h"}`,
wantErr: true,
},
{
name: "invalid duration string",
json: `"invalid"`,
wantErr: true,
},
{
name: "empty string",
json: `""`,
wantErr: true, // Changed: empty string should error
},
{
name: "null",
json: `null`,
wantErr: true,
},
{
name: "number instead of string",
json: `123`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var d Duration
err := d.UnmarshalJSON([]byte(tt.json))
if (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !d.Equal(tt.want) {
t.Errorf("UnmarshalJSON() = %v, want %v", d, tt.want)
}
})
}
}