-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_descriptor_test.go
236 lines (191 loc) · 7.09 KB
/
command_descriptor_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package console_test
import (
"strings"
"testing"
"github.com/eidolon/console"
"github.com/eidolon/console/parameters"
"github.com/seeruk/assert"
)
func TestDescribeCommand(t *testing.T) {
t.Run("should return command usage information", func(t *testing.T) {
application := console.NewApplication("eidolon/console", "1.2.3+testing")
command := console.Command{
Name: "test",
}
result := console.DescribeCommand(application, &command, []string{command.Name})
assert.True(t, strings.Contains(result, "USAGE:"), "Expected usage information.")
})
t.Run("should include the command name", func(t *testing.T) {
application := console.NewApplication("eidolon/console", "1.2.3+testing")
command := console.Command{
Name: "test-command-name",
}
result := console.DescribeCommand(application, &command, []string{command.Name})
assert.True(t, strings.Contains(result, "test-command-name"), "Expected command name.")
})
t.Run("should show the command description", func(t *testing.T) {
description := "This is the test-command-name description."
application := console.NewApplication("eidolon/console", "1.2.3+testing")
command := console.Command{
Name: "test-command-name",
Description: description,
}
result := console.DescribeCommand(application, &command, []string{command.Name})
assert.True(t, strings.Contains(result, description), "Expected command description.")
})
t.Run("should return command help if there is some", func(t *testing.T) {
help := "This is some help for the test-command-name command."
application := console.NewApplication("eidolon/console", "1.2.3+testing")
command := console.Command{
Name: "test-command-name",
Help: help,
}
result := console.DescribeCommand(application, &command, []string{command.Name})
assert.True(t, strings.Contains(result, "HELP:"), "Expected command help header.")
assert.True(t, strings.Contains(result, help), "Expected command help.")
})
t.Run("should show arguments if there are any", func(t *testing.T) {
var s1 string
var s2 string
application := console.NewApplication("eidolon/console", "1.2.3+testing")
command := console.Command{
Name: "test-command-name",
Configure: func(definition *console.Definition) {
definition.AddArgument(console.ArgumentDefinition{
Value: parameters.NewStringValue(&s1),
Spec: "STRING_ARG_S1",
})
definition.AddArgument(console.ArgumentDefinition{
Value: parameters.NewStringValue(&s2),
Spec: "STRING_ARG_S2",
})
},
}
result := console.DescribeCommand(application, &command, []string{command.Name})
assert.True(t, strings.Contains(result, "STRING_ARG_S1"), "Expected argument name.")
assert.True(t, strings.Contains(result, "STRING_ARG_S2"), "Expected argument name.")
})
t.Run("should show optional arguments wrapped in brackets", func(t *testing.T) {
var s1 string
var s2 string
application := console.NewApplication("eidolon/console", "1.2.3+testing")
command := console.Command{
Name: "test-command-name",
Configure: func(definition *console.Definition) {
definition.AddArgument(console.ArgumentDefinition{
Value: parameters.NewStringValue(&s1),
Spec: "[STRING_ARG_S1]",
})
definition.AddArgument(console.ArgumentDefinition{
Value: parameters.NewStringValue(&s2),
Spec: "[STRING_ARG_S2]",
})
},
}
result := console.DescribeCommand(application, &command, []string{command.Name})
assert.True(t, strings.Contains(result, "[STRING_ARG_S1]"), "Expected argument name.")
assert.True(t, strings.Contains(result, "[STRING_ARG_S2]"), "Expected argument name.")
})
t.Run("should show that there are options if there are any", func(t *testing.T) {
// @TODO: Update with global options implementation.
//var s1 string
//var s2 string
//
//application := console.NewApplication("eidolon/console", "1.2.3+testing")
//application.Configure = func(definition *console.Definition) {
// definition.AddOption(console.OptionDefinition{
// Value: parameters.NewStringValue(&s1),
// Spec: "--s1=VALUE",
// })
//}
//
//command := console.Command{
// Name: "test-command-name",
// Configure: func(definition *console.Definition) {
// definition.AddOption(console.OptionDefinition{
// Value: parameters.NewStringValue(&s2),
// Spec: "--s2=VALUE",
// })
// },
//}
//
//result := console.DescribeCommand(application, &command, []string{command.Name})
//
//assert.True(t, strings.Contains(result, "[OPTIONS...]"), "Expected options.")
})
t.Run("should show that there are sub-commands if there are any", func(t *testing.T) {
application := console.NewApplication("eidolon/console", "1.2.3+testing")
command := console.Command{
Name: "test-command-name",
}
subCommand := console.Command{
Name: "test-subCommand-name",
Description: "Test sub-command description.",
}
command.AddCommand(&subCommand)
result := console.DescribeCommand(application, &command, []string{command.Name})
assert.True(t, strings.Contains(result, "COMMANDS:"), "Expected commands")
assert.True(t, strings.Contains(result, subCommand.Name), "Expected sub-command name")
assert.True(t, strings.Contains(result, subCommand.Description), "Expected sub-command desc")
})
}
func TestDescribeCommands(t *testing.T) {
t.Run("should include a title", func(t *testing.T) {
result := console.DescribeCommands([]*console.Command{})
assert.True(t, strings.Contains(result, "COMMANDS:"), "Expected title.")
})
t.Run("should show all command names", func(t *testing.T) {
result := console.DescribeCommands([]*console.Command{
{
Name: "foo-cmd",
},
{
Name: "bar-cmd",
},
})
assert.True(t, strings.Contains(result, "foo-cmd"), "Expected command name.")
assert.True(t, strings.Contains(result, "bar-cmd"), "Expected command name.")
})
t.Run("should show all command descriptions", func(t *testing.T) {
fooCmdDesc := "The foo-cmd description is this."
barCmdDesc := "An alternative command description for bar-cmd."
result := console.DescribeCommands([]*console.Command{
{
Name: "foo-cmd",
Description: fooCmdDesc,
},
{
Name: "bar-cmd",
Description: barCmdDesc,
},
})
assert.True(t, strings.Contains(result, fooCmdDesc), "Expected command description.")
assert.True(t, strings.Contains(result, barCmdDesc), "Expected command description.")
})
t.Run("should show the commands in alphabetical order", func(t *testing.T) {
result := console.DescribeCommands([]*console.Command{
{
Name: "foo-cmd",
},
{
Name: "bar-cmd",
},
})
fooIdx := strings.Index(result, "foo-cmd")
barIdx := strings.Index(result, "bar-cmd")
assert.True(t, fooIdx > barIdx, "Expected foo-cmd to come after bar-cmd.")
})
t.Run("should show command aliases", func(t *testing.T) {
result := console.DescribeCommands([]*console.Command{
{
Name: "foo-cmd",
Alias: "f",
},
{
Name: "bar-cmd",
},
})
assert.True(t, strings.Contains(result, "(Alias: f)"), "Expected command description.")
assert.False(t, strings.Contains(result, "(Alias: b"), "Expected no command description.")
})
}