forked from mkideal/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_test.go
168 lines (159 loc) · 5.13 KB
/
tag_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
package cli
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTag(t *testing.T) {
type argT struct {
Cli string `cli:"cli"`
Pw string `pw:"pw"`
Edit string `edit:"edit"`
Usage string `cli:"usage" usage:"hello,usage"`
Dft string `cli:"dft" dft:"hello,dft"`
Name string `cli:"name" name:"hello-name"`
Prompt string `cli:"prompt" prompt:"hello,prompt"`
Parser string `cli:"parser" parser:"json"`
Sep string `cli:"sep" sep:":"`
Multi string `cli:"multi"
usage:"multi usage"
dft:"dft-value"`
StillPrintsDft string `cli:"stillDft" usage:"multi usage" dft:"dft-value" ignoreDft:"false"`
Required string `cli:"*r"`
RequiredNotEnforced string `cli:"*re" ignoreRequired:"true"`
Hidden string `cli:"hide-me" hidden:"true"`
Force string `cli:"!f"`
EditFile string `edit:"Filename:file"`
ShortAndLong string `cli:"x,y,z,xy,yz,xyz"`
Empty string `cli:"-"`
// errors
Err string `cli:"e" pw:"e" edit:"e" usage:"cli,pw,edit both are cli-like flag"`
}
argv := new(argT)
typ := reflect.TypeOf(argv).Elem()
val := reflect.ValueOf(argv).Elem()
for i, size := 0, val.NumField(); i < size; i++ {
typField, _ := typ.Field(i), val.Field(i)
tag, isEmpty, err := parseTag(typField.Name, typField.Tag)
if err != nil {
assert.Equal(t, typField.Name, "Err")
assert.Equal(t, err, errCliTagTooMany)
continue
}
if isEmpty {
assert.Equal(t, typField.Name, "Empty")
continue
}
switch typField.Name {
case "Cli":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--cli"})
case "Pw":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.True(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--pw"})
case "Edit":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.True(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--edit"})
case "Usage":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--usage"})
assert.Equal(t, tag.usage, "hello,usage")
case "Dft":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--dft"})
assert.Equal(t, tag.dft, "hello,dft")
case "Name":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--name"})
assert.Equal(t, tag.name, "hello-name")
case "Prompt":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--prompt"})
assert.Equal(t, tag.prompt, "hello,prompt")
case "Parser":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--parser"})
assert.Equal(t, typField.Tag.Get(tagParser), "json")
case "Sep":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--sep"})
assert.Equal(t, tag.sep, ":")
case "Required", "RequiredNotEnforced":
assert.True(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
case "Hidden":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.True(t, tag.hidden)
case "Force":
assert.False(t, tag.isRequired)
assert.True(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
case "EditFile":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.True(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.editFile, "Filename")
case "ShortAndLong":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.shortNames, []string{"-x", "-y", "-z"})
assert.Equal(t, tag.longNames, []string{"--xy", "--yz", "--xyz"})
case "Multi", "StillPrintsDft":
assert.Equal(t, tag.usage, "multi usage")
assert.Equal(t, tag.dft, "dft-value")
case "Empty":
assert.Equal(t, tag, (*tagProperty)(nil))
default:
panic("uncovered case " + typField.Name)
}
}
}