-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtristate_test.go
205 lines (174 loc) · 5.1 KB
/
tristate_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018 Timothy E. Peoples
package tristate
import (
"fmt"
"strings"
"testing"
"github.com/spf13/pflag"
)
func TestSet(t *testing.T) {
tests := map[TriState][]string{
None: {"-1", "A", "ALL", "ANY", "All", "Any", "B", "BOTH", "Both", "E", "EITHER", "Either", "NIL", "NONE", "NULL", "Nil", "None", "Null", "U", "UNKNOWN", "Unknown", "a", "all", "any", "b", "both", "e", "either", "nil", "none", "null", "u", "unknown"},
False: {"0", "F", "FALSE", "False", "N", "NO", "No", "f", "false", "n", "no"},
True: {"1", "T", "TRUE", "True", "Y", "YES", "Yes", "t", "true", "y", "yes"},
}
for want, list := range tests {
for _, s := range list {
ts := new(TriState)
if err := ts.Set(s); err != nil || *ts != want {
t.Errorf("ts.Set(%q) == %v (*ts=%s); Wanted %v (*ts=%v)", s, err, ts, nil, want)
}
}
}
ts := new(TriState)
if err := ts.Set("doug"); err != ErrBadTriStateValue {
t.Errorf("ts.Set(%q) == %v; Wanted %v", "doug", err, ErrBadTriStateValue)
}
}
func TestString(t *testing.T) {
tests := map[TriState]string{
None: "None",
False: "False",
True: "True",
}
for ts, want := range tests {
if got := ts.String(); got != want {
t.Errorf("%#v.String() == %q; wanted %q", ts, got, want)
}
p := &ts
if got := p.String(); got != want {
t.Errorf("%#v.String() == %q; wanted %q", p, got, want)
}
}
}
func TestMatch(t *testing.T) {
for _, ts := range []TriState{None, False, True} {
for _, nv := range []bool{false, true} {
for _, v := range []bool{false, true} {
var want bool
switch ts {
case None:
want = nv
case False:
want = !v
case True:
want = v
}
if got := ts.Match(v, nv); got != want {
t.Errorf("ts.Match(%v, %v) == %v; Wanted %v", v, nv, got, want)
}
}
}
}
}
func TestIsSet(t *testing.T) {
for _, ts := range []TriState{None, False, True} {
want := ts != None
if got := ts.IsSet(); got != want {
t.Errorf("TriState(%v).IsSet() == %v; Wanted %v", ts, got, want)
}
}
}
type flagOptions struct {
dflt TriState
args string
want TriState
err error
}
func TestFlags(t *testing.T) {
var (
flagopts []*flagOptions
flagPopts []*flagOptions
)
for _, dflt := range []TriState{None, False, True} {
flagopts = append(flagopts, &flagOptions{dflt, "", dflt, nil})
for _, want := range []TriState{None, False, True} {
flagopts = append(flagopts, &flagOptions{dflt, fmt.Sprintf("--tristate=%s", want.String()), want, nil})
flagopts = append(flagopts, &flagOptions{dflt, fmt.Sprintf("--tristate %s", want.String()), want, nil})
flagPopts = append(flagPopts, &flagOptions{dflt, fmt.Sprintf("-t %s", want.String()), want, nil})
}
}
var tests []*testcase
for _, what := range []string{"Flag", "FlagP", "FlagVar", "FlagVarP", "FlagFS", "FlagPFS", "FlagVarFS", "FlagVarPFS"} {
for _, fo := range flagopts {
tests = append(tests, mkTestcase(what, fo.args, fo.dflt, fo.want, fo.err))
}
if strings.HasSuffix(what, "P") {
for _, fo := range flagPopts {
tests = append(tests, mkTestcase(what, fo.args, fo.dflt, fo.want, fo.err))
}
}
}
for _, tc := range tests {
t.Run(tc.name, tc.test)
}
}
type testcase struct {
name string
setup func() (*pflag.FlagSet, *TriState)
args string
want TriState
err error
}
func (tc *testcase) test(t *testing.T) {
defer func(fs *pflag.FlagSet) { CommandLine = fs }(CommandLine)
fs, got := tc.setup()
if err := fs.Parse(strings.Fields(tc.args)); err != tc.err || *got != tc.want {
t.Errorf("%s: got %v (err=%v); wanted %v (err=%v)", t.Name(), *got, err, tc.want, tc.err)
}
}
func mkTestcase(what, args string, dflt, want TriState, err error) *testcase {
fs := pflag.NewFlagSet("test:"+what, pflag.ContinueOnError)
var setup func() (*pflag.FlagSet, *TriState)
switch what {
case "Flag":
setup = func() (*pflag.FlagSet, *TriState) {
CommandLine = fs
ts := Flag("tristate", dflt, "tristate flag")
return fs, ts
}
case "FlagP":
setup = func() (*pflag.FlagSet, *TriState) {
CommandLine = fs
ts := FlagP("tristate", "t", dflt, "tristate flag")
return fs, ts
}
case "FlagVar":
setup = func() (*pflag.FlagSet, *TriState) {
CommandLine = fs
var ts TriState
FlagVar(&ts, "tristate", dflt, "tristate flag")
return fs, &ts
}
case "FlagVarP":
setup = func() (*pflag.FlagSet, *TriState) {
CommandLine = fs
var ts TriState
FlagVarP(&ts, "tristate", "t", dflt, "tristate flag")
return fs, &ts
}
case "FlagFS":
setup = func() (*pflag.FlagSet, *TriState) {
ts := FlagFS(fs, "tristate", dflt, "tristate flag")
return fs, ts
}
case "FlagPFS":
setup = func() (*pflag.FlagSet, *TriState) {
ts := FlagPFS(fs, "tristate", "t", dflt, "tristate flag")
return fs, ts
}
case "FlagVarFS":
setup = func() (*pflag.FlagSet, *TriState) {
var ts TriState
FlagVarFS(fs, &ts, "tristate", dflt, "tristate flag")
return fs, &ts
}
case "FlagVarPFS":
setup = func() (*pflag.FlagSet, *TriState) {
var ts TriState
FlagVarPFS(fs, &ts, "tristate", "t", dflt, "tristate flag")
return fs, &ts
}
}
return &testcase{fmt.Sprintf("F:%s+D:%s+W:%s+A:%s", what, dflt, want, args), setup, args, want, err}
}