This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
178 lines (149 loc) · 4.04 KB
/
command_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
package drouter_test
import (
"github.com/NyanKiyoshi/disgord-plugin-router"
"github.com/stretchr/testify/assert"
"testing"
)
func createDummyCommand(names ...string) *drouter.Command {
if len(names) < 1 {
names = []string{"test"}
}
cmd := &drouter.Command{
Names: drouter.NewStringSet(names...),
}
return cmd
}
func TestCommand_Match(t *testing.T) {
// Create a testing command and a dummy handler
cmd := createDummyCommand()
// Ensure the default matching function is nil
assert.Nil(t, cmd.MatchFunc)
// Register the custom matcher
cmd.Match(func(input string) bool {
return true
})
// Check the custom matcher was set
assert.NotNil(t, cmd.MatchFunc)
assert.Equal(t, true, cmd.MatchFunc(""))
}
func TestCommand_MatchRE(t *testing.T) {
// Create a testing command and a dummy handler
cmd := createDummyCommand()
// Ensure the default matching function is nil
assert.Nil(t, cmd.MatchFunc)
// Register the custom matcher
cmd.MatchRE("^p([a-z]+)ch$")
// Check the custom matcher was set
assert.NotNil(t, cmd.MatchFunc)
// Check the matcher regex is working as expected
assert.Equal(t, true, cmd.MatchFunc("punch"))
assert.Equal(t, true, cmd.MatchFunc("peach"))
assert.Equal(t, false, cmd.MatchFunc("hello"))
}
func TestCommand_Handler(t *testing.T) {
// Create a testing command and a dummy handler
cmd := createDummyCommand()
handler := func(ctx *drouter.Context) error {
return successError
}
// Ensure no handler is registered by default
assert.Nil(t, cmd.HandlerFunc)
// Register an handler to the plugin
cmd.Handler(handler)
// Check it was correctly set
assert.NotNil(t, cmd.HandlerFunc)
assert.Equal(t, successError, cmd.HandlerFunc(nil))
}
func TestCommand_Use(t *testing.T) {
// Create a testing plugin
cmd := createDummyCommand()
// Create the dummy callback
callback := func(ctx *drouter.Context) error {
return successError
}
// Ensure there are not wrappers on an empty plugin
assert.Empty(t, cmd.Wrappers)
// Add the callback to the module
cmd.Use(callback)
// Ensure it was added
assert.Len(t, cmd.Wrappers, 1)
assert.Equal(t, successError, cmd.Wrappers[0](nil))
}
var helpCommandTests = []struct {
in string
expectedShortText string
}{
{in: "Two\nLines:)", expectedShortText: "Two"},
{in: "Only one line", expectedShortText: "Only one line"},
{in: "", expectedShortText: ""},
{in: " ", expectedShortText: " "},
}
func TestCommand_Help(t *testing.T) {
// Create a testing plugin
cmd := createDummyCommand()
// Ensure the help texts are empty by default
assert.Empty(t, cmd.ShortHelp)
assert.Empty(t, cmd.LongHelp)
for _, tt := range helpCommandTests {
t.Run(tt.in, func(t *testing.T) {
// Set the new help
cmd.Help(tt.in)
// Check the results
assert.Equal(t, tt.expectedShortText, cmd.ShortHelp)
assert.Equal(t, tt.in, cmd.LongHelp)
})
}
}
// isMatchingTests table driven tests against the 'ping' command
var isMatchingTests = []struct {
testName string
commandNames []string
matchFunc func(input string) bool
shouldFind bool
}{
{
testName: "valid existing command",
commandNames: []string{"ping"},
matchFunc: nil,
shouldFind: true,
},
{
testName: "inexisting command",
commandNames: []string{"pong"},
matchFunc: nil,
shouldFind: false,
},
{
testName: "inexisting command, but matching func",
commandNames: []string{"pong"},
matchFunc: func(input string) bool {
return true
},
shouldFind: true,
},
{
testName: "no commands set, but matching func",
commandNames: []string{},
matchFunc: func(input string) bool {
return true
},
shouldFind: true,
},
{
testName: "nothing set",
commandNames: []string{},
matchFunc: nil,
shouldFind: false,
},
}
func TestCommand_IsMatching(t *testing.T) {
for _, tt := range isMatchingTests {
t.Run(tt.testName, func(t *testing.T) {
// Setup the test command
cmd := createDummyCommand(tt.commandNames...)
cmd.MatchFunc = tt.matchFunc
// Attempt matching again 'ping'
assert.Equal(t, tt.shouldFind, cmd.IsMatching("ping"))
})
}
}