-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage_test.go
169 lines (160 loc) · 3.16 KB
/
usage_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
package scli
import (
"strings"
"testing"
)
var (
usageCase = []struct {
about string
work func() (usage string)
expectSubStr string
}{{
"no argument",
func() string {
var s0 struct {
A0 bool `flag:"a" default:"0" usage:"ua"`
}
return BuildParser(&s0).Help()
},
`
Options:
-help, --help print this message
--a, --/a set [ua] to true / false [default: false]
`,
}, {
"only argument",
func() string {
var s0 struct {
A0 string `arg:"a0"`
}
return BuildParser(&s0).Help()
},
`
Arguments:
<a0>
Options:
-help, --help print this message
`,
}, {
"test arg align",
func() string {
var s0 struct {
A0 string `arg:"a0" usage:"arg 0"`
A1 string `arg:"a1111" usage:"arg 1"`
A2 string `arg:"a2" usage:"arg 2"`
}
return BuildParser(&s0).Help()
},
`
Arguments:
<a0> arg 0
<a1111> arg 1
<a2> arg 2
Options:
-help, --help print this message
`,
}, {
"test optional argument",
func() string {
var s0 struct {
A0 string `arg:"a0" usage:"arg 0"`
A1 string `arg:"a1111" default:"11" usage:"arg 1"`
A2 string `arg:"a2" default:"22" usage:"arg 2"`
A3 bool `arg:"a3" default:"false" usage:"arg 3"`
}
return BuildParser(&s0).Help()
},
`
Arguments:
<a0> arg 0
[a1111] arg 1 [default: "11"]
[a2] arg 2 [default: "22"]
[a3] arg 3 [default: false]
Options:
-help, --help print this message
`,
}, {
"test option align",
func() string {
var s0 struct {
A0 string `flag:"a0" usage:"arg 0"`
A1 string `flag:"a1111" usage:"arg 111" default:"11"`
A2 string `flag:"a2" usage:"arg 2"`
}
return BuildParser(&s0).Help()
},
`
Options:
-help, --help print this message
--a0 <arg 0>
--a1111 <arg 111> [default: "11"]
--a2 <arg 2>
`,
}, {
"test subcmd",
func() string {
var s0 struct {
C1 *struct{} `flag:"c1" usage:"cmd1"`
C2 *struct{} `flag:"c2" usage:"cmd2"`
}
return BuildParser(&s0).Help()
},
`
Commands:
c1 cmd1
c2 cmd2
`,
}, {
"test arg array",
func() string {
var s0 struct {
A0 int
A1 []string `usage:"file"`
}
return BuildParser(&s0).Help()
},
`[OPTIONS] <A0> [A1]..
Arguments:
<A0>
[A1] file [example: "str"]
`,
}, {
"test custom type arg array",
func() string {
var s0 struct {
A0 int
A1 []addr `usage:"ip:port"`
}
return BuildParser(&s0).Help()
},
`[OPTIONS] <A0> [A1]..
Arguments:
<A0>
[A1] ip:port [example: "127.0.0.1:8000"]
`,
}}
)
func TestUsage(t *testing.T) {
for _, c := range usageCase {
t.Run(c.about, func(t *testing.T) {
helpText := c.work()
realTrimmed, expTrimmed := trimEveryLine(helpText), trimEveryLine(c.expectSubStr)
if !strings.Contains(realTrimmed, expTrimmed) {
t.Fatalf(
"error: does not contain expected substr\n>>>real>>>\n%s\n===\n%s\n<<<expect<<<\n"+
">>>real.trimmed>>>\n%s\n===\n%s\n<<<expect.trimmed<<<\n",
helpText, c.expectSubStr,
realTrimmed, expTrimmed,
)
}
})
}
}
func trimEveryLine(s string) string {
ret := []string{}
lines := strings.Split(s, "\n")
for _, l := range lines {
ret = append(ret, strings.TrimSpace(l))
}
return strings.Join(ret, "\n")
}