-
Notifications
You must be signed in to change notification settings - Fork 32
/
args_test.go
174 lines (163 loc) · 4.25 KB
/
args_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
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.
package cmd_test
import (
"fmt"
"io/ioutil"
"github.com/juju/gnuflag"
"github.com/juju/testing"
gc "gopkg.in/check.v1"
"github.com/juju/cmd/v4"
)
type ArgsSuite struct {
testing.LoggingSuite
}
var _ = gc.Suite(&ArgsSuite{})
func (*ArgsSuite) TestFlagsUsage(c *gc.C) {
for i, test := range []struct {
message string
defaultValue []string
args []string
expectedValue []string
}{{
message: "nil default and no arg",
}, {
message: "default value and not set by args",
defaultValue: []string{"foo", "bar"},
expectedValue: []string{"foo", "bar"},
}, {
message: "no value set by args",
args: []string{"--value", "foo,bar"},
expectedValue: []string{"foo", "bar"},
}, {
message: "default value and set by args",
defaultValue: []string{"omg"},
args: []string{"--value", "foo,bar"},
expectedValue: []string{"foo", "bar"},
}} {
c.Log(fmt.Sprintf("%v: %s", i, test.message))
f := gnuflag.NewFlagSet("test", gnuflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
var value []string
f.Var(cmd.NewStringsValue(test.defaultValue, &value), "value", "help")
err := f.Parse(false, test.args)
c.Check(err, gc.IsNil)
c.Check(value, gc.DeepEquals, test.expectedValue)
}
}
func (*ArgsSuite) TestNewStringsValue(c *gc.C) {
for i, test := range []struct {
message string
defaultValue []string
}{{
message: "null default",
}, {
message: "empty default",
defaultValue: []string{},
}, {
message: "single value",
defaultValue: []string{"foo"},
}, {
message: "multiple values",
defaultValue: []string{"foo", "bar", "baz"},
}} {
c.Log(fmt.Sprintf("%v: %s", i, test.message))
var underlyingValue []string
_ = cmd.NewStringsValue(test.defaultValue, &underlyingValue)
c.Assert(underlyingValue, gc.DeepEquals, test.defaultValue)
}
}
func (*ArgsSuite) TestSet(c *gc.C) {
for i, test := range []struct {
message string
arg string
expected []string
}{{
message: "empty",
expected: []string{""},
}, {
message: "just whitespace",
arg: " ",
expected: []string{" "},
}, {
message: "whitespace and comma",
arg: " , ",
expected: []string{" ", " "},
}, {
message: "single value",
arg: "foo",
expected: []string{"foo"},
}, {
message: "single value with comma",
arg: "foo,",
expected: []string{"foo", ""},
}, {
message: "single value with whitespace",
arg: " foo ",
expected: []string{" foo "},
}, {
message: "multiple values",
arg: "foo,bar,baz",
expected: []string{"foo", "bar", "baz"},
}, {
message: "multiple values with spaces",
arg: "foo, bar, baz",
expected: []string{"foo", " bar", " baz"},
}} {
c.Log(fmt.Sprintf("%v: %s", i, test.message))
var result []string
value := cmd.NewStringsValue(nil, &result)
error := value.Set(test.arg)
c.Check(error, gc.IsNil)
c.Check(result, gc.DeepEquals, test.expected)
}
}
func (*ArgsSuite) TestString(c *gc.C) {
for i, test := range []struct {
message string
target []string
expected string
}{{
message: "null",
expected: "",
}, {
message: "empty",
target: []string{},
expected: "",
}, {
message: "single value",
target: []string{"foo"},
expected: "foo",
}, {
message: "multiple values",
target: []string{"foo", "bar", "baz"},
expected: "foo,bar,baz",
}} {
c.Log(fmt.Sprintf("%v: %s", i, test.message))
var temp []string
value := cmd.NewStringsValue(test.target, &temp)
c.Assert(value.String(), gc.Equals, test.expected)
}
}
func (*ArgsSuite) TestAppendStringsUsage(c *gc.C) {
for i, test := range []struct {
message string
args []string
expectedValue []string
}{{
message: "no args",
}, {
message: "value set by args",
args: []string{"--value", "foo", "--value=bar"},
expectedValue: []string{"foo", "bar"},
}} {
c.Log(fmt.Sprintf("%v: %s", i, test.message))
f := gnuflag.NewFlagSet("test", gnuflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
var value []string
f.Var(cmd.NewAppendStringsValue(&value), "value", "help")
err := f.Parse(false, test.args)
c.Check(err, gc.IsNil)
c.Check(value, gc.DeepEquals, test.expectedValue)
}
}