Skip to content

Commit

Permalink
Add TestEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukinix committed Jul 24, 2020
1 parent 92bd96d commit 69031ac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions troll_shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func reply(bot TrollShieldBot, update *telegram.Update, text string) {
}
}

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

// kickTroll ban the troll and send a message about where we can found the trolls
func kickTroll(bot *telegram.BotAPI, update *telegram.Update, user telegram.User, trollHouse string) {
func kickTroll(bot TrollShieldBot, update *telegram.Update, user telegram.User, trollHouse string) {
chatMember := telegram.ChatMemberConfig{
ChatID: update.Message.Chat.ID,
UserID: user.ID,
Expand Down
44 changes: 40 additions & 4 deletions troll_shield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ package main
import (
"testing"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
)

func TestGetUserName(t *testing.T) {
user1 := tgbotapi.User{
user1 := telegram.User{
FirstName: "Rolisvaldo",
}
if got := getUserName(user1); got != "Rolisvaldo" {
t.Errorf("getUserName when only FirstName is available should return it, not %v", got)
}

user2 := tgbotapi.User{
user2 := telegram.User{
FirstName: "Rolisvaldo",
LastName: "Da Silva",
}
if got := getUserName(user2); got != "Rolisvaldo Da Silva" {
t.Errorf("getUserName when FirstName and LastName are available should return it, not %v", got)
}

user3 := tgbotapi.User{
user3 := telegram.User{
FirstName: "Rolisvaldo",
LastName: "Da Silva",
UserName: "rolisvaldo",
Expand All @@ -31,3 +31,39 @@ func TestGetUserName(t *testing.T) {
t.Errorf("getUserName when only UserName is available should return it with @, not %v", got)
}
}

func TestEvents(t *testing.T) {
// messageEvent
update := telegram.Update{}
if got := messageEvent(&update); got != false {
t.Errorf("messageEvent should return false when there is no message, got: %v", got)
}
message := telegram.Message{}
update.Message = &message
if got := messageEvent(&update); got != true {
t.Errorf("messageEvent should return true when there is a message, got: %v", got)
}

// newChatMemberEvent
if got := newChatMemberEvent(&update); got != false {
t.Errorf("newChatMemberEvent should return false when there is no new members, got: %v", got)
}
newChatMembers := []telegram.User{}
update.Message.NewChatMembers = &newChatMembers
if got := newChatMemberEvent(&update); got != true {
t.Errorf("newChatMemberEvent should return true when there is new members, got: %v", got)
}

if got := fromChatEvent(&update, "commonlispbr"); got != false {
t.Errorf("fromChatEvent should return false when there is no new members, got: %v", got)
}
chat := telegram.Chat{UserName: "commonlispbr", Title: "CommonLispBR HQ"}
update.Message.Chat = &chat
if got := fromChatEvent(&update, "commonlispbr"); got != true {
t.Errorf("fromChatEvent should return true when there is a chat with UserName, got: %v", got)
}
if got := fromChatEvent(&update, "CommonLispBR HQ"); got != true {
t.Errorf("fromChatEvent should return true when there is new members, got: %v", got)
}

}

0 comments on commit 69031ac

Please sign in to comment.