-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions_test.go
100 lines (93 loc) · 2.09 KB
/
options_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
package kouch
import (
"net/url"
"testing"
"github.com/flimzy/diff"
"github.com/flimzy/testy"
"github.com/go-kivik/couchdb/chttp"
"github.com/spf13/pflag"
)
func TestSetParam(t *testing.T) {
type spTest struct {
flags *pflag.FlagSet
flag string
expected *Options
err string
status int
}
tests := testy.NewTable()
tests.Add("no flags set", spTest{
flags: pflag.NewFlagSet("foo", 1),
flag: FlagEndKey,
expected: &Options{
Target: &Target{},
Options: &chttp.Options{},
},
})
tests.Add("endkey", func() interface{} {
f := pflag.NewFlagSet("foo", 1)
f.String(FlagEndKey, "", "x")
_ = f.Set(FlagEndKey, "oink")
return spTest{
flags: f,
flag: FlagEndKey,
expected: &Options{
Target: &Target{},
Options: &chttp.Options{
Query: url.Values{
"endkey": []string{"oink"},
},
},
},
}
})
tests.Run(t, func(t *testing.T, test spTest) {
o := NewOptions()
err := o.SetParam(test.flags, test.flag)
testy.ExitStatusError(t, test.err, test.status, err)
if d := diff.Interface(test.expected, o); d != nil {
t.Error(d)
}
})
}
type parseTest struct {
flags *pflag.FlagSet
flag string
expected []string
err string
status int
}
func TestParseParamString(t *testing.T) {
tests := testy.NewTable()
tests.Add("flag not set", parseTest{
flags: pflag.NewFlagSet("foo", 1),
flag: "oink",
})
tests.Add("wrong flag type", func() interface{} {
f := pflag.NewFlagSet("foo", 1)
f.Bool("oink", false, "x")
return parseTest{
flags: f,
flag: "oink",
err: "trying to get string value of flag of type bool",
status: 1,
}
})
tests.Add("success", func() interface{} {
f := pflag.NewFlagSet("foo", 1)
f.String("oink", "", "x")
_ = f.Set("oink", "moo")
return parseTest{
flags: f,
flag: "oink",
expected: []string{"moo"},
}
})
tests.Run(t, func(t *testing.T, test parseTest) {
result, err := parseParamString(test.flags, test.flag)
testy.ExitStatusError(t, test.err, test.status, err)
if d := diff.Interface(test.expected, result); d != nil {
t.Error(d)
}
})
}