-
Notifications
You must be signed in to change notification settings - Fork 3
/
usage_test.go
177 lines (161 loc) · 4.11 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
170
171
172
173
174
175
176
177
package brigodier
import (
"context"
"github.com/stretchr/testify/require"
"testing"
)
func setupUsage(d *Dispatcher) {
cmd := CommandFunc(func(c *CommandContext) error { return nil })
requireFalse := func(ctx context.Context) bool { return false }
d.Register(Literal("a").Then(
Literal("1").Then(
Literal("i").Executes(cmd),
Literal("ii").Executes(cmd),
),
Literal("2").Then(
Literal("i").Executes(cmd),
Literal("ii").Executes(cmd),
),
))
d.Register(Literal("b").Then(Literal("1").Executes(cmd)))
d.Register(Literal("c").Executes(cmd))
d.Register(Literal("d").Requires(requireFalse).Executes(cmd))
d.Register(Literal("e").Executes(cmd).Then(
Literal("1").Executes(cmd).Then(
Literal("i").Executes(cmd),
Literal("ii").Executes(cmd),
),
))
d.Register(Literal("f").Then(
Literal("1").Then(
Literal("i").Executes(cmd),
Literal("ii").Executes(cmd).Requires(requireFalse),
),
Literal("2").Then(
Literal("i").Executes(cmd).Requires(requireFalse),
Literal("ii").Executes(cmd),
),
))
d.Register(Literal("g").Executes(cmd).Then(
Literal("1").Then(Literal("i").Executes(cmd)),
))
d.Register(Literal("h").Executes(cmd).Then(
Literal("1").Then(Literal("i").Executes(cmd)),
Literal("2").Then(Literal("i").Then(Literal("ii").Executes(cmd))),
Literal("3").Executes(cmd),
))
d.Register(Literal("i").Executes(cmd).Then(
Literal("1").Executes(cmd),
Literal("2").Executes(cmd),
))
d.Register(Literal("j").Redirect(&d.Root))
d.Register(Literal("k").Redirect(get(d, "h")))
}
func get(d *Dispatcher, command string) CommandNode {
for _, n := range d.Parse(context.TODO(), command).Context.Nodes {
return n.Node
}
return nil
}
func getR(d *Dispatcher, command *StringReader) CommandNode {
for _, n := range d.ParseReader(context.TODO(), command).Context.Nodes {
return n.Node
}
return nil
}
func TestDispatcher_AllUsage_NoCommands(t *testing.T) {
var d Dispatcher
results := d.AllUsage(context.TODO(), &d.Root, true)
require.Empty(t, results)
}
func TestDispatcher_SmartUsage_NoCommands(t *testing.T) {
var d Dispatcher
results := d.SmartUsage(context.TODO(), &d.Root)
require.True(t, results.Empty())
}
func TestDispatcher_AllUsage_Root(t *testing.T) {
var d Dispatcher
setupUsage(&d)
results := d.AllUsage(context.TODO(), &d.Root, true)
usages := []string{
"a 1 i",
"a 1 ii",
"a 2 i",
"a 2 ii",
"b 1",
"c",
"e",
"e 1",
"e 1 i",
"e 1 ii",
"f 1 i",
"f 2 ii",
"g",
"g 1 i",
"h",
"h 1 i",
"h 2 i ii",
"h 3",
"i",
"i 1",
"i 2",
"j ...",
"k -> h",
}
for _, s := range usages {
require.Contains(t, results, s)
}
require.Equal(t, len(usages), len(results))
}
type expectedSmartUsage struct {
node CommandNode
usage string
}
func testSmartUsage(t testing.TB, results CommandNodeStringMap, expected ...expectedSmartUsage) {
t.Helper()
require.Equal(t, len(expected), results.Size())
var i int
results.Range(func(key CommandNode, value string) bool {
require.Equal(t, expected[i].usage, value, "%s is not %s", expected[i].usage, value)
require.NotNil(t, expected[i].node)
i++
return true
})
}
func TestDispatcher_SmartUsage_Root(t *testing.T) {
d := new(Dispatcher)
setupUsage(d)
results := d.SmartUsage(context.TODO(), &d.Root)
testSmartUsage(t, results, []expectedSmartUsage{
{get(d, "a"), "a (1|2)"},
{get(d, "b"), "b 1"},
{get(d, "c"), "c"},
{get(d, "e"), "e [1]"},
{get(d, "f"), "f (1|2)"},
{get(d, "g"), "g [1]"},
{get(d, "h"), "h [1|2|3]"},
{get(d, "i"), "i [1|2]"},
{get(d, "j"), "j ..."},
{get(d, "k"), "k -> h"},
}...)
}
func TestDispatcher_SmartUsage_H(t *testing.T) {
d := new(Dispatcher)
setupUsage(d)
results := d.SmartUsage(context.TODO(), get(d, "h"))
testSmartUsage(t, results, []expectedSmartUsage{
{get(d, "h 1"), "[1] i"},
{get(d, "h 2"), "[2] i ii"},
{get(d, "h 3"), "[3]"},
}...)
}
func TestDispatcher_SmartUsage_OffsetH(t *testing.T) {
d := new(Dispatcher)
setupUsage(d)
results := d.SmartUsage(context.TODO(), getR(d, inputWithOffset("/|/|/h", 5)))
testSmartUsage(t, results, []expectedSmartUsage{
{get(d, "h 1"), "[1] i"},
{get(d, "h 2"), "[2] i ii"},
{get(d, "h 3"), "[3]"},
}...)
}