-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutgoing_test.go
113 lines (90 loc) · 1.76 KB
/
outgoing_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
package bearychat
import (
"errors"
"testing"
"github.com/go-akka/configuration"
)
type Greeter struct {
name string
word string
}
func init() {
RegisterTriggerDriver("test-greeter", NewGreeter)
}
func NewGreeter(word string, config *configuration.Config) (Trigger, error) {
return &Greeter{
word: word,
name: config.GetString("name"),
}, nil
}
func (p *Greeter) Handle(req *Request, resp *Response) error {
switch req.TriggerWord {
case "!hello":
{
resp.Text = "Hello " + req.UserName + " I am " + p.name
return nil
}
case "!morning":
{
resp.Text = "Morning " + req.UserName + " I am " + p.name
return nil
}
}
return errors.New("Unknown TriggerWord")
}
func TestBasicOutgoingRequest(t *testing.T) {
confStr := `
{
hello = {
word = "!hello"
drivers = [test-greeter]
test-greeter = {
name = "robot A"
}
}
morning = {
word = "!morning"
drivers = [test-greeter]
test-greeter = {
name = "robot B"
}
}
}
`
config := configuration.ParseString(confStr)
outgoing, err := NewOutgoing(config)
if outgoing == nil {
t.Error(err)
return
}
req1 := &Request{
Text: "!hello my name is zeal",
UserName: "zeal",
TriggerWord: "!hello",
}
req2 := &Request{
Text: "!hello my name is gogap",
UserName: "gogap",
TriggerWord: "!morning",
}
resp1 := Response{}
err = outgoing.Handle(req1, &resp1)
if err != nil {
t.Error(err)
return
}
if resp1.Text != "Hello zeal I am robot A" {
t.Error("bad !hello trigger response: " + resp1.Text)
return
}
resp2 := Response{}
err = outgoing.Handle(req2, &resp2)
if err != nil {
t.Error(err)
return
}
if resp2.Text != "Morning gogap I am robot B" {
t.Error("bad !morning trigger response: " + resp2.Text)
return
}
}