-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
161 lines (153 loc) · 3.19 KB
/
parse_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
package hdur
import "testing"
func BenchmarkParseDuration(b *testing.B) {
inputs := []string{
"2h",
"1y 2mo 3d",
"1h 30m 45s",
"500ms",
"2.5h",
"1 year 2 months 3 days and 4 hours",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
input := inputs[i%len(inputs)]
_, err := ParseDuration(input)
if err != nil {
b.Fatal(err)
}
}
}
func TestMustParseDuration_Panic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("MustParseDuration() did not panic with invalid input")
}
}()
MustParseDuration("invalid duration")
}
func TestParseDuration(t *testing.T) {
tests := []struct {
name string
input string
expected Duration
wantErr bool
}{
{
name: "simple seconds",
input: "30s",
expected: Duration{Seconds: 30},
},
{
name: "complex duration",
input: "1 year 2 months 3 days 4 hours 5 minutes 6 seconds",
expected: Duration{Years: 1, Months: 2, Days: 3, Hours: 4, Minutes: 5, Seconds: 6},
},
{
name: "multiple units with and",
input: "2 days and 4 hours",
expected: Duration{Days: 2, Hours: 4},
},
{
name: "mixed case and plural",
input: "1 YEAR 1 Month 1 DAY",
expected: Duration{Years: 1, Months: 1, Days: 1},
},
{
name: "alternative unit names",
input: "1 yr 1 mo 1 d",
expected: Duration{Years: 1, Months: 1, Days: 1},
},
{
name: "weeks and fortnights",
input: "2 weeks 1 fortnight",
expected: Duration{Days: 28}, // 14 days + 14 days
},
{
name: "nanoseconds",
input: "500ns",
expected: Duration{Nanos: 500},
},
{
name: "microseconds",
input: "500us",
expected: Duration{Nanos: 500000},
},
{
name: "milliseconds",
input: "500ms",
expected: Duration{Nanos: 500000000},
},
{
name: "invalid unit",
input: "5 invalid",
wantErr: true,
},
{
name: "invalid number",
input: "invalid days",
wantErr: true,
},
{
name: "empty string",
input: "",
wantErr: true,
},
{
name: "just invalid",
input: "invalid",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseDuration(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseDuration() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
return
}
if got != tt.expected {
t.Errorf("ParseDuration() = %v, want %v", got, tt.expected)
}
})
}
}
func TestParseDurationFromNanos(t *testing.T) {
tests := []struct {
name string
nanos int64
want Duration
}{
{
name: "zero",
nanos: 0,
want: Duration{},
},
{
name: "one second",
nanos: 1_000_000_000,
want: Duration{Seconds: 1},
},
{
name: "negative one second",
nanos: -1_000_000_000,
want: Duration{Seconds: -1},
},
{
name: "complex duration",
nanos: 90_061_000_000_000, // 25 hours 1 minute 1 second
want: Duration{Days: 1, Hours: 1, Minutes: 1, Seconds: 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ParseDurationFromNanos(tt.nanos)
if !got.Equal(tt.want) {
t.Errorf("ParseDurationFromNanos() = %v, want %v", got, tt.want)
}
})
}
}