-
Notifications
You must be signed in to change notification settings - Fork 1
/
flags_test.go
142 lines (138 loc) · 4.89 KB
/
flags_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
package alligotor
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("flags", func() {
Describe("readFlagConfig", func() {
Context("invalid input", func() {
It("should return err on more than 3 sets", func() {
_, err := readFlagConfig("a b c")
Expect(err).Should(HaveOccurred())
Expect(err).To(Equal(ErrMalformedFlagConfig))
})
It("should return error if longname has less than 2 letters", func() {
for _, configStr := range []string{"a b", "long long"} {
_, err := readFlagConfig(configStr)
Expect(err).Should(HaveOccurred())
Expect(err).To(Equal(ErrMalformedFlagConfig))
}
})
})
Context("valid input", func() {
It("should return valid flag when short and long are set", func() {
for _, configStr := range []string{"a awd", "awd a"} {
f, err := readFlagConfig(configStr)
Expect(err).ShouldNot(HaveOccurred())
Expect(f).To(Equal(flag{ShortName: "a", LongName: "awd"}))
}
})
It("should return valid flag when only short is set", func() {
f, err := readFlagConfig("a")
Expect(err).ShouldNot(HaveOccurred())
Expect(f).To(Equal(flag{ShortName: "a", LongName: ""}))
})
It("should return valid flag when only long is set", func() {
f, err := readFlagConfig("awd")
Expect(err).ShouldNot(HaveOccurred())
Expect(f).To(Equal(flag{ShortName: "", LongName: "awd"}))
})
})
})
Describe("FlagsSource", func() {
var (
s *FlagsSource
fields []Field
separator = "-"
name = "name"
flagName = "--" + name
)
BeforeEach(func() {
s = &FlagsSource{
separator: separator,
fieldToFlagInfo: map[string]*flagInfo{},
}
fields = []Field{{name: name}}
})
Describe("initFlagMap", func() {
It("contains flag for field", func() {
Expect(s.initFlagMap(fields, []string{flagName, "3000"})).To(Succeed())
flagInfo, ok := s.fieldToFlagInfo[key(&fields[0])]
Expect(ok).To(BeTrue())
Expect(*flagInfo.valueStr).To(Equal("3000"))
})
It("supports overwriting longname", func() {
fields[0].configs = map[string]string{flagKey: "overwrite"}
Expect(s.initFlagMap(fields, []string{"--overwrite", "4000"})).To(Succeed())
flagInfo, ok := s.fieldToFlagInfo[key(&fields[0])]
Expect(ok).To(BeTrue())
Expect(*flagInfo.valueStr).To(Equal("4000"))
})
It("returns ErrHelp if --help is specified", func() {
err := s.initFlagMap(nil, []string{"--help"})
Expect(err).To(MatchError(ErrHelp))
})
})
Describe("Read", func() {
It("returns nil if not set", func() {
Expect(s.initFlagMap(fields, []string{})).To(Succeed())
val, err := s.Read(&fields[0])
Expect(err).ToNot(HaveOccurred())
Expect(val).To(BeNil())
})
It("returns empty string if set to empty string", func() {
Expect(s.initFlagMap(fields, []string{flagName, ""})).To(Succeed())
val, err := s.Read(&fields[0])
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("")))
})
It("uses name as normal flag name", func() {
Expect(s.initFlagMap(fields, []string{flagName, "3000"})).To(Succeed())
val, err := s.Read(&fields[0])
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("3000")))
})
It("uses configured long name", func() {
fields[0].configs = map[string]string{flagKey: "overwrite"}
Expect(s.initFlagMap(fields, []string{"--overwrite", "3000"})).To(Succeed())
val, err := s.Read(&fields[0])
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("3000")))
})
It("uses configured short name", func() {
fields[0].configs = map[string]string{flagKey: "o"}
Expect(s.initFlagMap(fields, []string{"-o", "3000"})).To(Succeed())
val, err := s.Read(&fields[0])
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("3000")))
})
Context("nested", func() {
var base = "base"
BeforeEach(func() {
fields[0].base = []Field{{name: base}}
flagName = "--" + base + separator + name
})
It("uses separator", func() {
Expect(s.initFlagMap(fields, []string{flagName, "3000"})).To(Succeed())
val, err := s.Read(&fields[0])
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("3000")))
})
It("can use defaults", func() {
fields[0].configs = map[string]string{flagKey: "overwrite"}
Expect(s.initFlagMap(fields, []string{"--" + base + separator + "overwrite", "3000"})).To(Succeed())
val, err := s.Read(&fields[0])
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("3000")))
})
It("supports overwriting base", func() {
fields[0].base = []Field{{name: base, configs: map[string]string{flagKey: "overwrittenbase"}}}
Expect(s.initFlagMap(fields, []string{"--" + "overwrittenbase" + separator + name, "3000"})).To(Succeed())
val, err := s.Read(&fields[0])
Expect(err).ToNot(HaveOccurred())
Expect(val).To(Equal([]byte("3000")))
})
})
})
})
})