-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels_test.go
219 lines (199 loc) · 5.04 KB
/
levels_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package egcmd
import (
"reflect"
"testing"
)
func TestLevelExample(t *testing.T) {
level := Level{name: "root"}
testCases := []struct {
name string
arguments string
description string
want Example
}{
{
"NoArguments",
"",
"Default behaviour",
Example{Description: "Default behaviour"},
},
{
"WithArguments",
"--verbose",
"verbose output",
Example{Arguments: "--verbose", Description: "verbose output"},
},
}
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := level.Example(tc.arguments, tc.description)
count := i + 1
if got := len(level.examples); count != got {
t.Errorf("Expected level example count to be %d, got %d", count, got)
}
if got := *actual; tc.want != got {
t.Errorf("Expected returned example to be %v, got %v", tc.want, got)
}
if got := *level.examples[i]; tc.want != got {
t.Errorf("Expected level example %d to be %v, got %v", i, tc.want, got)
}
})
}
}
func TestLevelEnvs(t *testing.T) {
level := Level{name: "root"}
testCases := []struct {
name string
envVars string
arguments string
description string
want Example
}{
{
"NoEnvs",
"",
"",
"Default behaviour",
Example{Description: "Default behaviour"},
},
{
"WithEnvs",
"LOGGING=verbose",
"",
"verbose output",
Example{EnvVars: "LOGGING=verbose", Description: "verbose output"},
},
}
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := level.Envs(tc.envVars, tc.arguments, tc.description)
count := i + 1
if got := len(level.examples); count != got {
t.Errorf("Expected level example count to be %d, got %d", count, got)
}
if got := *actual; tc.want != got {
t.Errorf("Expected returned example to be %v, got %v", tc.want, got)
}
if got := *level.examples[i]; tc.want != got {
t.Errorf("Expected level example %d to be %v, got %v", i, tc.want, got)
}
})
}
}
func TestAppCommand(t *testing.T) {
app := App{Level: Level{name: "root"}, commands: make(map[string]*Command)}
testCases := []struct {
name string
command string
exampleToAppend *Example
duplicateCommand bool
}{
{
"FirstCommand",
"init",
&Example{Description: "first"},
false,
},
{
"DuplicateCommand",
"init",
&Example{Description: "second"},
true,
},
{
"NextCommand",
"LOGGING=verbose",
&Example{Description: "next"},
false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := app.Command(tc.command)
isDuplicate := len(actual.examples) > 0
appCommand, found := app.commands[tc.command]
actual.examples = append(actual.examples, tc.exampleToAppend)
if got := actual.name; tc.command != got {
t.Errorf("Expected returned command to be %q, got %q", tc.command, got)
}
if !found {
t.Errorf("Expected command in app to be present, but was not found")
return
}
if got := appCommand.name; tc.command != got {
t.Errorf("Expected command in app to have the name %q, got %q", tc.command, got)
}
if got := isDuplicate; tc.duplicateCommand != got {
t.Errorf("Expected command to have an example count of %t, got %t", tc.duplicateCommand, got)
}
})
}
}
func TestAppFind(t *testing.T) {
appExamples := []*Example{
{"", "Action: default", ""},
{"simple", "Action: simple", ""},
{"complex sub-action", "Action: complex sub-action", ""},
}
simpleExamples := []*Example{
{"", "Default for simple", ""},
{"--debug", "Simple with debug", ""},
}
complexExamples := []*Example{
{"--json", "Default for complex with json arg", ""},
{"sub-action", "complex action with sub-action", ""},
}
subActionExamples := []*Example{
{"", "Subaction", "APP_KEY=1234"},
}
app := App{
Level: Level{"root", appExamples},
commands: map[string]*Command{
"simple": {Level: Level{"simple", simpleExamples}},
"complex": {Level: Level{"complex", complexExamples}},
"complex sub-action": {Level: Level{"complex sub-action", subActionExamples}},
},
}
testCases := []struct {
testName string
command string
want ExamplesFound
}{
{
"Root",
"",
ExamplesFound{Context: "root", Examples: appExamples},
},
{
"SimpleCommand",
"simple",
ExamplesFound{Context: "root simple", Examples: simpleExamples},
},
{
"ComplexCommand",
"complex",
ExamplesFound{Context: "root complex", Examples: complexExamples},
},
{
"ComplexSubCommand",
"complex sub-action",
ExamplesFound{Context: "root complex sub-action", Examples: subActionExamples},
},
{
"CommandNotFound",
"no-match",
ExamplesFound{Context: "root no-match"},
},
}
for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
got := app.Find(tc.command)
if tc.want.Context != got.Context {
t.Errorf("Expected context to be %#v, got %#v", tc.want, got)
}
if !reflect.DeepEqual(tc.want.Examples, got.Examples) {
t.Errorf("Expected the examples found to be list %#v, got %#v", tc.want, got)
}
})
}
}