-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_test.go
66 lines (59 loc) · 1.5 KB
/
flag_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
package configer
import (
"flag"
"io"
"testing"
)
// TestStringSlice has taken inspiration from Go's `src/flag/flag_test.go` file
// and the function `TestUserDefined`. https://go.dev/src/flag/flag_test.go
func TestStringSlice_multiple(t *testing.T) {
tts := map[string]struct {
args []string
expectedStr string
expectedSize int
}{
"empty set": {
args: []string{},
expectedStr: "",
expectedSize: 0,
},
"single set": {
args: []string{"-c", "crash"},
expectedStr: "crash",
expectedSize: 1,
},
"double set": {
args: []string{"-c", "pulp", "-c", "fiction"},
expectedStr: "pulp,fiction",
expectedSize: 2,
},
"triple set": {
args: []string{"-c", "v", "-c", "for", "-c", "vendetta"},
expectedStr: "v,for,vendetta",
expectedSize: 3,
},
"flag variance set": {
args: []string{"-c", "back", "--c", "to", "-c=the", "--c=future"},
expectedStr: "back,to,the,future",
expectedSize: 4,
},
}
for name, tt := range tts {
t.Run(name, func(t *testing.T) {
var flags flag.FlagSet
flags.Init(name, flag.ContinueOnError)
flags.SetOutput(io.Discard)
var ss StringSlice
flags.Var(&ss, "c", "usage")
if err := flags.Parse(tt.args); err != nil {
t.Error(err)
}
if len(ss) != tt.expectedSize {
t.Fatalf("len(ss) = %d; expected %d", len(ss), tt.expectedSize)
}
if ss.String() != tt.expectedStr {
t.Errorf("ss.String() = %q; expected: %q", ss.String(), tt.expectedStr)
}
})
}
}