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
/
events_test.go
83 lines (68 loc) · 2.4 KB
/
events_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
//+build ignore
package drouter_test
import (
"github.com/NyanKiyoshi/disgord-plugin-router"
"github.com/NyanKiyoshi/disgord-plugin-router/mocks/mocked_disgord"
"github.com/andersfylling/disgord"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"testing"
)
func mockMySelf(
t *testing.T, senderID disgord.Snowflake,
testFunc func(*testing.T, *mocked_disgord.MockrouterSession)) {
// Start a new mocking controller
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
// Mock session and MySelf()
mockedSession := mocked_disgord.NewMockrouterSession(mockCtrl)
mockedSession.
EXPECT().
Myself().Return(&disgord.User{ID: senderID}, nil)
// Run test
testFunc(t, mockedSession)
}
// It ensures:
// - That it's ignoring messages that are the bot itself (returning nil).
// - And that it returns nil when the command is not found.
func TestRouterDefinition_OnMessageReceived_HandlesErrors(t *testing.T) {
router := createTestRouter()
t.Run("ignores self messages", func(t *testing.T) {
mockMySelf(t, selfUserID, func(t *testing.T, session *mocked_disgord.MockrouterSession) {
// It should return nil
assert.Nil(t, router.OnMessageReceived(session, &disgord.MessageCreate{
Message: createDummyMessage(),
}))
})
})
t.Run("returns nil when invalid command", func(t *testing.T) {
mockMySelf(t, disgord.Snowflake(555), func(t *testing.T, session *mocked_disgord.MockrouterSession) {
// It should return nil
assert.Nil(t, router.OnMessageReceived(session, &disgord.MessageCreate{
Message: createDummyMessage(),
}))
})
})
t.Run("dispatches command when valid request", func(t *testing.T) {
// Will contain whether the ping command was invoked or not
var pingHandlerWasCalled bool
// Create a !ping module
router.Plugin(_myModuleInternalType{}, "ping").Handler(func(ctx *drouter.Context) error {
pingHandlerWasCalled = true
return nil
}).SetPrefix("!").Activate()
// Create a valid command message
message := createDummyMessage()
message.Content = "!ping"
mockMySelf(t, disgord.Snowflake(555), func(t *testing.T, session *mocked_disgord.MockrouterSession) {
// Dispatch the event
successChannel := router.OnMessageReceived(session, &disgord.MessageCreate{
Message: message,
})
// Ensure it was a success
assert.True(t, <-successChannel)
})
// Ensure the command was invoked
assert.True(t, pingHandlerWasCalled)
})
}