-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_string_test.go
More file actions
155 lines (135 loc) · 3.42 KB
/
parse_string_test.go
File metadata and controls
155 lines (135 loc) · 3.42 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
145
146
147
148
149
150
151
152
153
154
155
package opts_test
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/telemachus/opts"
)
func TestParseString(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
want string
args []string
}{
"Basic value; single dash": {
args: []string{"-file", "test.txt"},
want: "test.txt",
},
"Value with spaces; single dash": {
args: []string{"-file", "test file.txt"},
want: "test file.txt",
},
"Empty string; single dash": {
args: []string{"-file", ""},
want: "",
},
"Space separated; double dash": {
args: []string{"--file", "test.txt"},
want: "test.txt",
},
"With equals; double dash": {
args: []string{"--file=test.txt"},
want: "test.txt",
},
"Value with spaces; double dash": {
args: []string{"--file", "test file.txt"},
want: "test file.txt",
},
"Empty string; double dash": {
args: []string{"--file", ""},
want: "",
},
}
for msg, tc := range testCases {
t.Run(msg, func(t *testing.T) {
t.Parallel()
var got string
og := opts.NewGroup("test-parsing")
og.String(&got, "file", "whatever")
err := og.Parse(tc.args)
if err != nil {
t.Fatalf("og.Parse(%v) returns err == %v; want nil", tc.args, err)
}
if got != tc.want {
t.Errorf("og.Parse(%v) assigns %q to got; want %q", tc.args, got, tc.want)
}
})
}
}
func TestParseStringWithRemainingArgs(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
want string
args []string
postArgs []string
}{
"Args after value; single dash": {
args: []string{"-file", "test.txt", "foo", "bar"},
postArgs: []string{"foo", "bar"},
want: "test.txt",
},
"Args after value; double dash": {
args: []string{"--file", "test.txt", "foo", "bar"},
postArgs: []string{"foo", "bar"},
want: "test.txt",
},
}
for msg, tc := range testCases {
t.Run(msg, func(t *testing.T) {
t.Parallel()
var got string
og := opts.NewGroup("test-parsing")
og.String(&got, "file", "whatever")
remaining, err := og.ParseKnown(tc.args)
if err != nil {
t.Fatalf("og.ParseKnown(%v) returns err == %v; want nil", tc.args, err)
}
if got != tc.want {
t.Errorf("og.ParseKnown(%v) assigns %q to got; want %q", 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 TestParseStringSimpleErrors(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
errWanted error
args []string
}{
"No value argument; single dash": {
args: []string{"-file"},
errWanted: opts.ErrMissingValue,
},
"No value argument; double dash": {
args: []string{"--file"},
errWanted: opts.ErrMissingValue,
},
"Equal and empty value; double dash": {
args: []string{"--file="},
errWanted: opts.ErrMissingValue,
},
"Unknown option; single dash": {
args: []string{"-foo", "bar"},
errWanted: opts.ErrUnknownOption,
},
"Unknown option; double dash": {
args: []string{"--foo", "bar"},
errWanted: opts.ErrUnknownOption,
},
}
for msg, tc := range testCases {
t.Run(msg, func(t *testing.T) {
t.Parallel()
var got string
og := opts.NewGroup("test-parsing")
og.String(&got, "file", "whatever")
err := og.Parse(tc.args)
if !errors.Is(err, tc.errWanted) {
t.Fatalf("og.Parse(%v) returns err == nil; want %v", tc.args, tc.errWanted)
}
})
}
}