This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slash_command_test.go
159 lines (132 loc) · 3.14 KB
/
slash_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
package mdqi
import (
"errors"
"reflect"
"testing"
)
var ErrTest = errors.New("error for test")
type TestDefFoo struct{}
func (d TestDefFoo) Category() string { return "test" }
func (d TestDefFoo) Name() string { return "foo" }
func (d TestDefFoo) Example() string { return "/test foo" }
func (d TestDefFoo) Help() string { return "I'm foo." }
func (d TestDefFoo) Handle(app *App, cmd *SlashCommand) error {
return ErrTest
}
func TestParseSlashCommand(t *testing.T) {
type Expected struct {
Result *SlashCommand
Error error
}
type Pattern struct {
Title string
Query string
Expected Expected
}
patterns := []Pattern{
Pattern{
Title: "command only category",
Query: "/quit",
Expected: Expected{
Result: &SlashCommand{
Category: "quit",
Name: "",
Args: []string{},
},
Error: nil,
},
},
Pattern{
Title: "command with name",
Query: "/tag show",
Expected: Expected{
Result: &SlashCommand{
Category: "tag",
Name: "show",
Args: []string{},
},
Error: nil,
},
},
Pattern{
Title: "command with options",
Query: "/tag set db1",
Expected: Expected{
Result: &SlashCommand{
Category: "tag",
Name: "set",
Args: []string{"db1"},
},
Error: nil,
},
},
Pattern{
Title: "not a command",
Query: "select * from item",
Expected: Expected{
Result: nil,
Error: ErrNotASlashCommand,
},
},
}
for _, pattern := range patterns {
t.Log("test:", pattern.Title)
r, err := ParseSlashCommand(pattern.Query)
if !reflect.DeepEqual(r, pattern.Expected.Result) {
t.Errorf("unexpected result: %#v", r)
}
if err != pattern.Expected.Error {
t.Error("unexpected error:", err)
}
}
}
func TestRegisterSlashCommandDefinition(t *testing.T) {
app, _ := NewApp(Conf{})
if err := app.RegisterSlashCommandDefinition(TestDefFoo{}); err != nil {
t.Fatal("unexpected error:", err)
}
if app.slashCommandDefinition["test"] == nil {
t.Fatal("there are no category map")
}
def := app.slashCommandDefinition["test"]["foo"]
if def == nil {
t.Fatal("there are no definition")
}
if c := def.Category(); c != "test" {
t.Fatal("unexpected category:", c)
}
if n := def.Name(); n != "foo" {
t.Fatal("unexpected name:", n)
}
if err := def.Handle(app, nil); err != ErrTest {
t.Fatalf("unexpected handler returns error: %s", err)
}
}
func TestFindSlashCommandDefinition(t *testing.T) {
app, _ := NewApp(Conf{})
app.slashCommandDefinition["test"] = map[string]SlashCommandDefinition{}
app.slashCommandDefinition["test"]["foo"] = TestDefFoo{}
// registered definition
{
def, err := app.FindSlashCommandDefinition("test", "foo")
if err != nil {
t.Error("unexpected error:", err)
}
if def == nil {
t.Error("definition must be returned.")
}
if def.Category() != "test" || def.Name() != "foo" {
t.Errorf("unexpected definition: %#v", def)
}
}
// unknown definition
{
def, err := app.FindSlashCommandDefinition("test", "bar")
if err != ErrSlashCommandNotFound {
t.Error("expected error")
}
if def != nil {
t.Errorf("unexpected definition: %#v", def)
}
}
}