forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistflag_test.go
130 lines (103 loc) · 2.86 KB
/
listflag_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
package config
import (
"testing"
"gopkg.in/yaml.v2"
"github.com/google/go-cmp/cmp"
)
func TestListFlag(t *testing.T) {
const yamlList = `- foo
- bar
- baz`
t.Run("custom separator", func(t *testing.T) {
var (
expected = []string{"foo", "bar", "baz"}
current = newListFlag(":")
)
if err := current.Set("foo:bar:baz"); err != nil {
t.Fatal(err)
}
if cmp.Equal(expected, current.values) == false {
t.Error("failed to parse flags", current.values)
}
if err := yaml.Unmarshal([]byte(yamlList), current); err != nil {
t.Fatal(err)
}
if cmp.Equal(expected, current.values) == false {
t.Error("failed to parse yaml", current.values)
}
if current.value != "foo:bar:baz" {
t.Error("invalid value composed by yaml parser")
}
})
t.Run("comma separator", func(t *testing.T) {
f := commaListFlag()
if err := f.Set("foo,bar,baz"); err != nil {
t.Fatal(err)
}
if cmp.Equal([]string{"foo", "bar", "baz"}, f.values) == false {
t.Error("failed to parse flags", f.values)
}
})
t.Run("restricted values", func(t *testing.T) {
t.Run("good", func(t *testing.T) {
var (
expected = []string{"foo", "bar", "baz"}
current = commaListFlag("foo", "bar", "baz", "qux")
)
if err := current.Set("foo,bar,baz"); err != nil {
t.Fatal(err)
}
if cmp.Equal(expected, current.values) == false {
t.Error("failed to parse flags", current.values)
}
if err := yaml.Unmarshal([]byte(yamlList), current); err != nil {
t.Fatal(err)
}
if cmp.Equal(expected, current.values) == false {
t.Error("failed to parse yaml", current.values)
}
})
t.Run("bad", func(t *testing.T) {
current := commaListFlag("foo", "bar")
if err := current.Set("foo,bar,baz"); err == nil {
t.Error("failed to fail")
}
if err := yaml.Unmarshal([]byte(yamlList), current); err == nil {
t.Error("failed to fail")
}
})
})
t.Run("string representation", func(t *testing.T) {
const input = "foo,bar,baz"
current := commaListFlag()
if err := current.Set(input); err != nil {
t.Error(err)
}
output := current.String()
if output != input {
t.Error("unexpected string representation", output, input)
}
if err := yaml.Unmarshal([]byte(yamlList), current); err != nil {
t.Fatal(err)
}
if output != input {
t.Error("unexpected string representation from yaml", output, input)
}
})
t.Run("unmarshal error", func(t *testing.T) {
const input = "invalid yaml"
current := commaListFlag()
if err := yaml.Unmarshal([]byte(input), current); err == nil {
t.Errorf("Failed to get error from Unmarshal() for invalid input: %q", input)
}
})
t.Run("empty value", func(t *testing.T) {
f := commaListFlag()
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if f.value != "" || f.values != nil {
t.Errorf("failed to parse flags: %q %v (%d)", f.value, f.values, len(f.values))
}
})
}