-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduration_test.go
123 lines (97 loc) · 2.76 KB
/
duration_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
package dynflags_test
import (
"testing"
"time"
"github.com/containeroo/dynflags"
"github.com/stretchr/testify/assert"
)
func TestDurationValue_Parse(t *testing.T) {
t.Parallel()
t.Run("ValidDuration", func(t *testing.T) {
t.Parallel()
d := &dynflags.DurationValue{}
value, err := d.Parse("2h")
assert.NoError(t, err)
assert.Equal(t, 2*time.Hour, value)
})
t.Run("InvalidDuration", func(t *testing.T) {
t.Parallel()
d := &dynflags.DurationValue{}
_, err := d.Parse("invalid")
assert.Error(t, err)
})
}
func TestDurationValue_Set(t *testing.T) {
t.Parallel()
t.Run("SetValidDuration", func(t *testing.T) {
t.Parallel()
var bound time.Duration
d := &dynflags.DurationValue{Bound: &bound}
err := d.Set(1 * time.Minute)
assert.NoError(t, err)
assert.Equal(t, 1*time.Minute, bound)
})
t.Run("SetInvalidType", func(t *testing.T) {
t.Parallel()
var bound time.Duration
d := &dynflags.DurationValue{Bound: &bound}
err := d.Set("not a duration")
assert.Error(t, err)
assert.Equal(t, time.Duration(0), bound)
})
}
func TestGroupConfig_Duration(t *testing.T) {
t.Parallel()
t.Run("DurationDefault", func(t *testing.T) {
t.Parallel()
group := &dynflags.ConfigGroup{Flags: make(map[string]*dynflags.Flag)}
defaultValue := 5 * time.Second
bound := group.Duration("timeout", defaultValue, "Timeout duration")
assert.Equal(t, defaultValue, bound.Default)
assert.Contains(t, group.Flags, "timeout")
assert.Equal(t, defaultValue, group.Flags["timeout"].Default)
assert.Equal(t, dynflags.FlagTypeDuration, group.Flags["timeout"].Type)
})
}
func TestParsedGroup_GetDuration(t *testing.T) {
t.Parallel()
t.Run("GetValidDuration", func(t *testing.T) {
t.Parallel()
parsed := &dynflags.ParsedGroup{
Name: "test",
Values: map[string]interface{}{"timeout": 30 * time.Second},
}
dur, err := parsed.GetDuration("timeout")
assert.NoError(t, err)
assert.Equal(t, 30*time.Second, dur)
})
t.Run("GetDurationNotFound", func(t *testing.T) {
t.Parallel()
parsed := &dynflags.ParsedGroup{
Name: "test",
Values: map[string]interface{}{},
}
_, err := parsed.GetDuration("missing")
assert.Error(t, err)
})
t.Run("GetDurationWrongType", func(t *testing.T) {
t.Parallel()
parsed := &dynflags.ParsedGroup{
Name: "test",
Values: map[string]interface{}{"timeout": "not a duration"},
}
_, err := parsed.GetDuration("timeout")
assert.Error(t, err)
})
}
func TestDurationGetBound(t *testing.T) {
t.Run("DurationValue - GetBound", func(t *testing.T) {
var d *time.Duration
val := 2 * time.Second
d = &val
durationValue := dynflags.DurationValue{Bound: d}
assert.Equal(t, val, durationValue.GetBound())
durationValue = dynflags.DurationValue{Bound: nil}
assert.Nil(t, durationValue.GetBound())
})
}