-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_bool_test.go
More file actions
144 lines (123 loc) · 3.08 KB
/
parse_bool_test.go
File metadata and controls
144 lines (123 loc) · 3.08 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
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
package opts_test
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/telemachus/opts"
)
func TestParseBool(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
args []string
want bool
}{
"no args; one dash": {
args: []string{"-v"},
want: true,
},
"no args; two dashes": {
args: []string{"--verbose"},
want: true,
},
}
for msg, tc := range testCases {
t.Run(msg, func(t *testing.T) {
t.Parallel()
var got bool
og := opts.NewGroup("test-parsing")
og.Bool(&got, "v")
og.Bool(&got, "verbose")
err := og.Parse(tc.args)
if err != nil {
t.Fatalf("og.Parse(%v) returns %v as err; want nil", tc.args, err)
}
if got != tc.want {
t.Errorf("og.Parse(%v) assigns %t to got; want %t", tc.args, got, tc.want)
}
})
}
}
func TestParseBoolWithRemainingArgs(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
args []string
postArgs []string
want bool
}{
"args after option; single dash": {
args: []string{"-v", "foo", "bar"},
postArgs: []string{"foo", "bar"},
want: true,
},
"args after option; two dashes": {
args: []string{"--verbose", "foo", "bar"},
postArgs: []string{"foo", "bar"},
want: true,
},
}
for msg, tc := range testCases {
t.Run(msg, func(t *testing.T) {
t.Parallel()
var got bool
og := opts.NewGroup("test-parsing")
og.Bool(&got, "v")
og.Bool(&got, "verbose")
remaining, err := og.ParseKnown(tc.args)
if err != nil {
t.Fatalf("og.ParseKnown(%v) returns %v as err; want nil", tc.args, err)
}
if got != tc.want {
t.Errorf("og.ParseKnown(%v) assigns %t to got; want %t", tc.args, got, tc.want)
}
if diff := cmp.Diff(tc.postArgs, remaining); diff != "" {
t.Errorf("og.ParseKnown(%v); (-want +got):\n%s", tc.args, diff)
}
})
}
}
func TestParseBoolErrors(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
errWanted error
args []string
}{
"bool with equals true": {
args: []string{"--verbose=true"},
errWanted: opts.ErrBooleanWithValue,
},
"bool with equals false": {
args: []string{"--verbose=false"},
errWanted: opts.ErrBooleanWithValue,
},
"bool with equals empty": {
args: []string{"--verbose="},
errWanted: opts.ErrBooleanWithValue,
},
"bool with equals random string": {
args: []string{"--verbose=yadda-yadda"},
errWanted: opts.ErrBooleanWithValue,
},
"unknown option": {
args: []string{"--foobar"},
errWanted: opts.ErrUnknownOption,
},
}
for msg, tc := range testCases {
t.Run(msg, func(t *testing.T) {
t.Parallel()
var got bool
og := opts.NewGroup("test-parsing")
og.Bool(&got, "verbose")
err := og.Parse(tc.args)
if !errors.Is(err, tc.errWanted) {
t.Errorf("og.Parse(%v) returns %v as err; want %v", tc.args, err, tc.errWanted)
}
og2 := opts.NewGroup("test-parsing")
og2.Bool(&got, "verbose")
_, err2 := og2.ParseKnown(tc.args)
if !errors.Is(err2, tc.errWanted) {
t.Errorf("og.Parse(%v) returns %v as err; want %v", tc.args, err2, tc.errWanted)
}
})
}
}