Skip to content

Commit 69031ac

Browse files
committed
Add TestEvents
1 parent 92bd96d commit 69031ac

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

troll_shield.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func reply(bot TrollShieldBot, update *telegram.Update, text string) {
8989
}
9090
}
9191

92-
func welcomeMessage(bot *telegram.BotAPI, update *telegram.Update, member telegram.User) {
92+
func welcomeMessage(bot TrollShieldBot, update *telegram.Update, member telegram.User) {
9393
username := getUserName(member)
9494
text := fmt.Sprintf(
9595
`Olá %s! Seja bem-vindo ao grupo oficial de Common Lisp do Brasil.
@@ -136,7 +136,7 @@ func findTrollHouses(bot TrollShieldBot, userID int) string {
136136
}
137137

138138
// kickTroll ban the troll and send a message about where we can found the trolls
139-
func kickTroll(bot *telegram.BotAPI, update *telegram.Update, user telegram.User, trollHouse string) {
139+
func kickTroll(bot TrollShieldBot, update *telegram.Update, user telegram.User, trollHouse string) {
140140
chatMember := telegram.ChatMemberConfig{
141141
ChatID: update.Message.Chat.ID,
142142
UserID: user.ID,

troll_shield_test.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ package main
33
import (
44
"testing"
55

6-
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
6+
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
77
)
88

99
func TestGetUserName(t *testing.T) {
10-
user1 := tgbotapi.User{
10+
user1 := telegram.User{
1111
FirstName: "Rolisvaldo",
1212
}
1313
if got := getUserName(user1); got != "Rolisvaldo" {
1414
t.Errorf("getUserName when only FirstName is available should return it, not %v", got)
1515
}
1616

17-
user2 := tgbotapi.User{
17+
user2 := telegram.User{
1818
FirstName: "Rolisvaldo",
1919
LastName: "Da Silva",
2020
}
2121
if got := getUserName(user2); got != "Rolisvaldo Da Silva" {
2222
t.Errorf("getUserName when FirstName and LastName are available should return it, not %v", got)
2323
}
2424

25-
user3 := tgbotapi.User{
25+
user3 := telegram.User{
2626
FirstName: "Rolisvaldo",
2727
LastName: "Da Silva",
2828
UserName: "rolisvaldo",
@@ -31,3 +31,39 @@ func TestGetUserName(t *testing.T) {
3131
t.Errorf("getUserName when only UserName is available should return it with @, not %v", got)
3232
}
3333
}
34+
35+
func TestEvents(t *testing.T) {
36+
// messageEvent
37+
update := telegram.Update{}
38+
if got := messageEvent(&update); got != false {
39+
t.Errorf("messageEvent should return false when there is no message, got: %v", got)
40+
}
41+
message := telegram.Message{}
42+
update.Message = &message
43+
if got := messageEvent(&update); got != true {
44+
t.Errorf("messageEvent should return true when there is a message, got: %v", got)
45+
}
46+
47+
// newChatMemberEvent
48+
if got := newChatMemberEvent(&update); got != false {
49+
t.Errorf("newChatMemberEvent should return false when there is no new members, got: %v", got)
50+
}
51+
newChatMembers := []telegram.User{}
52+
update.Message.NewChatMembers = &newChatMembers
53+
if got := newChatMemberEvent(&update); got != true {
54+
t.Errorf("newChatMemberEvent should return true when there is new members, got: %v", got)
55+
}
56+
57+
if got := fromChatEvent(&update, "commonlispbr"); got != false {
58+
t.Errorf("fromChatEvent should return false when there is no new members, got: %v", got)
59+
}
60+
chat := telegram.Chat{UserName: "commonlispbr", Title: "CommonLispBR HQ"}
61+
update.Message.Chat = &chat
62+
if got := fromChatEvent(&update, "commonlispbr"); got != true {
63+
t.Errorf("fromChatEvent should return true when there is a chat with UserName, got: %v", got)
64+
}
65+
if got := fromChatEvent(&update, "CommonLispBR HQ"); got != true {
66+
t.Errorf("fromChatEvent should return true when there is new members, got: %v", got)
67+
}
68+
69+
}

0 commit comments

Comments
 (0)