Repository navigation
Expand file tree
/
Copy pathparsers_test.go
More file actions
112 lines (102 loc) · 2.45 KB
/
Copy pathparsers_test.go
File metadata and controls
112 lines (102 loc) · 2.45 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
package cfg
import (
"testing"
"time"
"github.com/olebedev/config"
)
func Test_ParseAsMapOrList(t *testing.T) {
tests := []struct {
name string
configKey string
yaml string
expectedCount int
}{
{
name: "as empty set",
configKey: "data",
yaml: "",
expectedCount: 0,
},
{
name: "as map",
configKey: "data",
yaml: "data:\n a: cat\n b: dog",
expectedCount: 2,
},
{
name: "as list",
configKey: "data",
yaml: "data:\n - cat\n - dog\n - rat\n",
expectedCount: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ymlConfig, err := config.ParseYaml(tt.yaml)
if err != nil {
t.Errorf("\nexpected: no error\n got: %v", err)
}
actual := ParseAsMapOrList(ymlConfig, tt.configKey)
if tt.expectedCount != len(actual) {
t.Errorf("\nexpected: %d\n got: %d", tt.expectedCount, len(actual))
}
})
}
}
func Test_ParseTimeString(t *testing.T) {
tests := []struct {
name string
configKey string
yaml string
expectedCount time.Duration
}{
{
name: "normal integer",
configKey: "refreshInterval",
yaml: "refreshInterval: 3",
expectedCount: 3 * time.Second,
},
{
name: "microseconds",
configKey: "refreshInterval",
yaml: "refreshInterval: 5µs",
expectedCount: 5 * time.Microsecond,
},
{
name: "microseconds different notation",
configKey: "refreshInterval",
yaml: "refreshInterval: 5us",
expectedCount: 5 * time.Microsecond,
},
{
name: "mixed duration",
configKey: "refreshInterval",
yaml: "refreshInterval: 2h45m",
expectedCount: 2*time.Hour + 45*time.Minute,
},
{
name: "default",
configKey: "refreshInterval",
yaml: "",
expectedCount: 60 * time.Second,
},
{
name: "bad input",
configKey: "refreshInterval",
yaml: "refreshInterval: abc",
expectedCount: 1 * time.Second,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ymlConfig, err := config.ParseYaml(tt.yaml)
if err != nil {
t.Errorf("\nexpected: no error\n got: %v", err)
}
actual := ParseTimeString(ymlConfig, tt.configKey, "60s")
if tt.expectedCount != actual {
t.Errorf("\nexpected: %d\n got: %v", tt.expectedCount, actual)
}
})
}
}