-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_test.go
158 lines (127 loc) · 4.76 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
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
package cmdy
import (
"strings"
"testing"
"time"
"github.com/ArtProcessors/cmdy/internal/assert"
)
const expectedUsage = `
-b Morbi ac elementum massa. Sed bibendum vel magna eget sagittis. Integer elit ac
efficitur sodales, nibh felis pulvinar neque, eu pellentesque odio risus sed
risus. Nulla ac sem ex. Suspendisse in orci pellentesque, posuere massa nec
(default: true)
-dv=<duration> (formats: '1h2s', '-3.4ms', units: h, m, s, ms, us, ns)
Morbi ac elementum massa. Sed bibendum vel magna eget sagittis. (default: 1s)
-iv=<int>
Morbi ac elementum massa. Sed bibendum vel magna eget sagittis. (default: 2)
-pants
Morbi ac elementum massa. Sed bibendum vel magna eget sagittis. Integer
tristique, elit ac efficitur sodales, nibh felis (default: true)
-str=<string>
Morbi ac elementum massa. Sed bibendum vel magna eget sagittis. (default: "yep")
`
func TestFlagUsage(t *testing.T) {
tt := assert.WrapTB(t)
var pants bool
var str string
var iv int
var dv time.Duration
fs := NewFlagSet()
fs.BoolVar(&pants, "pants", true, ""+
"Morbi ac elementum massa. Sed bibendum vel magna eget sagittis. "+
"Integer tristique, elit ac efficitur sodales, nibh felis")
fs.StringVar(&str, "str", "yep", ""+
"Morbi ac elementum massa. Sed bibendum vel magna eget sagittis.")
fs.IntVar(&iv, "iv", 2, ""+
"Morbi ac elementum massa. Sed bibendum vel magna eget sagittis.")
fs.DurationVar(&dv, "dv", 1*time.Second, ""+
"Morbi ac elementum massa. Sed bibendum vel magna eget sagittis.")
fs.BoolVar(&pants, "b", true, ""+
"Morbi ac elementum massa. Sed bibendum vel magna eget sagittis. "+
"Integer elit ac efficitur sodales, nibh felis "+
"pulvinar neque, eu pellentesque odio risus sed risus. Nulla "+
"ac sem ex. Suspendisse in orci pellentesque, posuere massa nec")
// FIXME: brittle test, but adequate for now.
tt.MustEqual(expectedUsage, "\n"+fs.Usage())
}
func TestFlagDoubleDash(t *testing.T) {
tt := assert.WrapTB(t)
FlagDoubleDash = true
defer func() {
FlagDoubleDash = false
}()
var a, b, a2, b2 bool
var s, u, s2, u2 string
fs := NewFlagSet()
fs.BoolVar(&a, "a", true, "")
fs.BoolVar(&b, "b", false, "")
fs.BoolVar(&a2, "a2", false, "")
fs.BoolVar(&b2, "b2", true, "")
fs.StringVar(&s, "s", "", "")
fs.StringVar(&u, "u", "foo", "")
fs.StringVar(&s2, "s2", "", "")
fs.StringVar(&u2, "u2", "foo", "")
usage := fs.Usage()
tt.MustAssert(strings.Contains(usage, " -a "))
tt.MustAssert(strings.Contains(usage, " --a2\n"))
tt.MustAssert(strings.Contains(usage, " -b "))
tt.MustAssert(strings.Contains(usage, " --b2\n"))
tt.MustAssert(strings.Contains(usage, " -s=<string>\n"))
tt.MustAssert(strings.Contains(usage, " --s2=<string>\n"))
tt.MustAssert(strings.Contains(usage, " -u=<string>\n"))
tt.MustAssert(strings.Contains(usage, " --u2=<string>\n"))
}
func TestFlagUsageCollapsing(t *testing.T) {
// Flags without descriptions should not be followed by a blank line
tt := assert.WrapTB(t)
var foo, bar int
fs := NewFlagSet()
fs.IntVar(&foo, "foo", 0, "")
fs.IntVar(&bar, "bar", 0, "")
expected := "" +
" -bar=<int>\n" +
" -foo=<int>\n"
tt.MustEqual(expected, fs.Usage())
}
type (
hintOnlyVar string
kindOnlyVar string
hintBothVar string
hintNoneVar string
)
func (h hintOnlyVar) String() string { return string(h) }
func (h hintOnlyVar) Hint() (kind, hint string) { return "", "hint" }
func (h *hintOnlyVar) Set(s string) error { *h = hintOnlyVar(s); return nil }
func (h kindOnlyVar) String() string { return string(h) }
func (h kindOnlyVar) Hint() (kind, hint string) { return "kind", "" }
func (h *kindOnlyVar) Set(s string) error { *h = kindOnlyVar(s); return nil }
func (h hintBothVar) String() string { return string(h) }
func (h hintBothVar) Hint() (kind, hint string) { return "kind", "hint" }
func (h *hintBothVar) Set(s string) error { *h = hintBothVar(s); return nil }
func (h hintNoneVar) String() string { return string(h) }
func (h hintNoneVar) Hint() (kind, hint string) { return "", "" }
func (h *hintNoneVar) Set(s string) error { *h = hintNoneVar(s); return nil }
const expectedHintableUsage = `
-hintboth=<kind> (hint)
hint both
-hintnone
hint none
-hintonly (hint)
hint only
-kindonly=<kind>
kind only
`
func TestFlagVarHintable(t *testing.T) {
tt := assert.WrapTB(t)
var hintOnly hintOnlyVar
var kindOnly kindOnlyVar
var hintBoth hintBothVar
var hintNone hintNoneVar
fs := NewFlagSet()
fs.Var(&hintOnly, "hintonly", "hint only")
fs.Var(&kindOnly, "kindonly", "kind only")
fs.Var(&hintBoth, "hintboth", "hint both")
fs.Var(&hintNone, "hintnone", "hint none")
// FIXME: brittle test, but adequate for now.
tt.MustEqual(expectedHintableUsage, "\n"+fs.Usage())
}