-
Notifications
You must be signed in to change notification settings - Fork 3
/
brigodier_test.go
326 lines (271 loc) · 9.51 KB
/
brigodier_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package brigodier
import (
"context"
"errors"
"fmt"
"github.com/stretchr/testify/require"
"testing"
)
func ExampleDispatcher_Do() {
var d Dispatcher
d.Register(
Literal("foo").Then(
Argument("bar", Int).
Executes(CommandFunc(func(c *CommandContext) error {
fmt.Println("Bar is", c.Int("bar"))
return nil
})),
).Executes(CommandFunc(func(c *CommandContext) error {
fmt.Println("Called foo with no arguments")
return nil
})),
)
}
func TestDispatcher_ParseExecute(t *testing.T) {
const cmd = `base`
var (
d Dispatcher
input string
)
d.Register(Literal(cmd).Executes(CommandFunc(func(c *CommandContext) error {
input = c.Input
return nil
})))
require.NoError(t, d.Do(context.TODO(), cmd))
require.Equal(t, cmd, input)
}
func TestDispatcher_MergeCommands(t *testing.T) {
var (
d Dispatcher
times int
)
cmdFn := CommandFunc(func(c *CommandContext) error { times++; return nil })
d.Register(Literal("base").Then(Literal("foo").Executes(cmdFn)))
d.Register(Literal("base").Then(Literal("bar").Executes(cmdFn)))
require.NoError(t, d.Do(context.TODO(), "base foo"))
require.NoError(t, d.Do(context.TODO(), "base bar"))
require.Equal(t, 2, times)
}
func TestDispatcher_Execute_UnknownCommand(t *testing.T) {
var d Dispatcher
d.Register(Literal("bar"))
d.Register(Literal("baz"))
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), "foo"), &err))
require.ErrorIs(t, err, ErrDispatcherUnknownCommand)
require.Equal(t, 0, err.Reader.Cursor)
}
func TestDispatcher_Execute_UnknownSubCommand(t *testing.T) {
var (
d Dispatcher
times int
)
cmdFn := CommandFunc(func(c *CommandContext) error { times++; return nil })
d.Register(Literal("foo").Executes(cmdFn))
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), "foo bar"), &err))
require.ErrorIs(t, err, ErrDispatcherUnknownArgument)
require.Equal(t, 0, times)
require.Equal(t, 4, err.Reader.Cursor)
}
func TestDispatcher_Execute_ImpermissibleCommand(t *testing.T) {
var d Dispatcher
d.Register(Literal("foo").Requires(func(context.Context) bool { return false }))
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), "foo"), &err))
require.ErrorIs(t, err, ErrDispatcherUnknownCommand)
require.Equal(t, 0, err.Reader.Cursor)
}
func TestDispatcher_Execute_EmptyCommand(t *testing.T) {
var d Dispatcher
d.Register(Literal(""))
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), ""), &err))
require.ErrorIs(t, err, ErrDispatcherUnknownCommand)
require.Equal(t, 0, err.Reader.Cursor)
}
func TestDispatcher_Execute_IncorrectLiteral(t *testing.T) {
var (
d Dispatcher
times int
)
cmdFn := CommandFunc(func(c *CommandContext) error { times++; return nil })
d.Register(Literal("foo").Executes(cmdFn).Then(Literal("bar")))
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), "foo baz"), &err))
require.ErrorIs(t, err, ErrDispatcherUnknownArgument)
require.Equal(t, 0, times)
require.Equal(t, 4, err.Reader.Cursor)
}
func TestDispatcher_Execute_AmbiguousIncorrectArgument(t *testing.T) {
var d Dispatcher
cmdFn := CommandFunc(func(c *CommandContext) error { return nil })
d.Register(Literal("foo").Executes(cmdFn).
Then(Literal("bar")).
Then(Literal("baz")),
)
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), "foo unknown"), &err))
require.ErrorIs(t, err, ErrDispatcherUnknownArgument)
require.Equal(t, 4, err.Reader.Cursor)
}
func TestDispatcher_Execute_Subcommand(t *testing.T) {
var d Dispatcher
var input string
cmdFn := CommandFunc(func(c *CommandContext) error { input = c.Input; return nil })
d.Register(Literal("foo").Then(
Literal("a"),
).Then(
Literal("=").Executes(cmdFn),
).Then(
Literal("c"),
).Executes(cmdFn))
require.NoError(t, d.Do(context.TODO(), "foo ="))
require.Equal(t, "foo =", input)
}
func TestDispatcher_ParseIncompleteLiteral(t *testing.T) {
var d Dispatcher
d.Register(Literal("foo").Then(Literal("bar")))
parse := d.Parse(context.TODO(), "foo ")
require.Equal(t, " ", parse.Reader.Remaining())
require.Len(t, parse.Context.Nodes, 1)
}
func TestDispatcher_ParseIncompleteArgument(t *testing.T) {
var d Dispatcher
d.Register(Literal("foo").Then(Argument("bar", Int)))
parse := d.Parse(context.TODO(), "foo ")
require.Equal(t, " ", parse.Reader.Remaining())
require.Len(t, parse.Context.Nodes, 1)
}
func TestDispatcher_Execute_AmbiguousParentSubcommandViaRedirect(t *testing.T) {
var d Dispatcher
var c1, c2 bool
cmdFn := CommandFunc(func(c *CommandContext) error { c1 = true; return nil })
subCmdFn := CommandFunc(func(c *CommandContext) error {
c2 = true
require.Equal(t, 1, c.Int("right"))
require.Equal(t, 2, c.Int("sub"))
return nil
})
r := d.Register(Literal("test").
Then(
Argument("incorrect", Int).Executes(cmdFn)).
Then(
Argument("right", Int).Then(
Argument("sub", Int).Executes(subCmdFn),
)),
)
d.Register(Literal("redirect").Redirect(r))
require.NoError(t, d.Do(context.TODO(), "redirect 1 2"))
require.False(t, c1)
require.True(t, c2)
}
func TestDispatcher_Execute_RedirectMultipleTimes(t *testing.T) {
var d Dispatcher
var cmdInput string
cmd := CommandFunc(func(c *CommandContext) error { cmdInput += c.Input; return nil })
concreteNode := d.Register(Literal("actual").Executes(cmd))
redirectNode := d.Register(Literal("redirected").Redirect(&d.Root))
const input = "redirected redirected actual"
parse := d.Parse(context.TODO(), input)
require.Equal(t, parse.Context.Range.Get(input), "redirected")
require.Len(t, parse.Context.Nodes, 1)
require.Equal(t, parse.Context.RootNode, &d.Root)
require.Equal(t, parse.Context.Nodes[0].Node, redirectNode)
child1 := parse.Context.Child
require.NotNil(t, child1)
require.Equal(t, child1.Range.Get(input), "redirected")
require.Len(t, child1.Nodes, 1)
require.Equal(t, &d.Root, child1.RootNode)
require.Equal(t, child1.Range, *child1.Nodes[0].Range)
require.Equal(t, redirectNode, child1.Nodes[0].Node)
child2 := child1.Child
require.NotNil(t, child2)
require.Equal(t, "actual", child2.Range.Get(input))
require.Len(t, child2.Nodes, 1)
require.Equal(t, &d.Root, child2.RootNode)
require.Equal(t, *child2.Nodes[0].Range, child2.Range)
require.Equal(t, child2.Nodes[0].Node, concreteNode)
require.NoError(t, d.Execute(parse))
require.Equal(t, input, cmdInput)
}
func TestDispatcher_Execute_Redirected(t *testing.T) {
var d Dispatcher
var cmdInput string
cmd := CommandFunc(func(c *CommandContext) error { cmdInput += c.Input; return nil })
mod := ModifierFunc(func(c *CommandContext) (context.Context, error) {
// another context
return context.Background(), nil
})
concreteNode := d.Register(Literal("actual").Executes(cmd))
redirectNode := d.Register(Literal("redirected").Fork(&d.Root, mod))
const input = "redirected actual"
parse := d.Parse(context.TODO(), input)
require.Equal(t, "redirected", parse.Context.Range.Get(input))
require.Len(t, parse.Context.Nodes, 1)
require.Equal(t, &d.Root, parse.Context.RootNode)
require.Equal(t, parse.Context.Range, *parse.Context.Nodes[0].Range)
require.Equal(t, redirectNode, parse.Context.Nodes[0].Node)
parent := parse.Context.Child
require.NotNil(t, parent)
require.Equal(t, "actual", parent.Range.Get(input))
require.Len(t, parse.Context.Nodes, 1)
require.Equal(t, &d.Root, parse.Context.RootNode)
require.Equal(t, parent.Range, *parent.Nodes[0].Range)
require.Equal(t, concreteNode, parent.Nodes[0].Node)
require.NoError(t, d.Execute(parse))
require.Equal(t, input, cmdInput)
}
func TestDispatcher_Execute_OrphanedSubcommand(t *testing.T) {
var d Dispatcher
cmd := CommandFunc(func(c *CommandContext) error { return nil })
d.Register(Literal("foo").Then(Argument("bar", Int)).Executes(cmd))
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), "foo 5"), &err))
require.ErrorIs(t, err, ErrDispatcherUnknownCommand)
require.Equal(t, 5, err.Reader.Cursor)
}
func TestDispatcher_Execute_invalidOther(t *testing.T) {
var d Dispatcher
var i int
cmd := CommandFunc(func(c *CommandContext) error { i += 1; return nil })
wrongCmd := CommandFunc(func(c *CommandContext) error { i -= 100; return nil })
d.Register(Literal("w").Executes(wrongCmd))
d.Register(Literal("world").Executes(cmd))
require.NoError(t, d.Do(context.TODO(), "world"))
require.Equal(t, 1, i)
}
func TestDispatcher_Execute_noSpaceSeparator(t *testing.T) {
var d Dispatcher
cmd := CommandFunc(func(c *CommandContext) error { return nil })
d.Register(Literal("foo").Then(Argument("bar", Int)).Executes(cmd))
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), "foo$"), &err))
require.ErrorIs(t, err, ErrDispatcherUnknownCommand)
require.Equal(t, 0, err.Reader.Cursor)
}
func TestDispatcher_Execute_InvalidSubcommand(t *testing.T) {
var d Dispatcher
cmd := CommandFunc(func(c *CommandContext) error { return nil })
d.Register(Literal("foo").Then(Argument("bar", Int)).Executes(cmd))
var err *ReaderError
require.True(t, errors.As(d.Do(context.TODO(), "foo bar"), &err))
require.ErrorIs(t, err, ErrReaderExpectedInt)
require.Equal(t, 4, err.Reader.Cursor)
}
func TestDispatcher_Path(t *testing.T) {
var d Dispatcher
bar := Literal("bar").BuildLiteral()
d.Register(Literal("foo").Then(bar))
require.Equal(t, []string{"foo", "bar"}, d.Path(bar))
}
func TestDispatcher_FindNode(t *testing.T) {
var d Dispatcher
bar := Literal("bar").BuildLiteral()
d.Register(Literal("foo").Then(bar))
require.Equal(t, bar, d.FindNode("foo", "bar"))
}
func TestDispatcher_FindNode_DoesntExist(t *testing.T) {
var d Dispatcher
require.Nil(t, d.FindNode("foo", "bar"))
}