add GetCommands() test

pull/184/head
Vincent 8 years ago
parent ab0aca70ed
commit d0ecb3c9f3
  1. 2
      bot_test.go
  2. 3
      helpers_test.go
  3. 9
      types.go
  4. 35
      types_test.go

@ -8,7 +8,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/TheMysteriousVincent/telegram-bot-api"
) )
const ( const (

@ -1,8 +1,9 @@
package tgbotapi_test package tgbotapi_test
import ( import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"testing" "testing"
"github.com/TheMysteriousVincent/telegram-bot-api"
) )
func TestNewInlineQueryResultArticle(t *testing.T) { func TestNewInlineQueryResultArticle(t *testing.T) {

@ -204,15 +204,16 @@ func (m *Message) GetCommands() (*Commands, error) {
var cmds Commands var cmds Commands
text := []rune(m.Text) text := []rune(m.Text)
textLen := len(text)
for i := 0; i < len(botCmdsEntries); i++ { for i := 0; i < len(botCmdsEntries); i++ {
e := botCmdsEntries[i] e := botCmdsEntries[i]
nOff := len(text) nOff := textLen
if i+1 >= len(botCmdsEntries) { if i+1 < len(botCmdsEntries) {
nOff = (botCmdsEntries[i+1]).Offset nOff = (botCmdsEntries[i+1]).Offset
} }
cmd := Command{ cmd := Command{
Name: string(text[e.Offset : e.Offset+e.Length]), Name: string(text[e.Offset+1 : e.Offset+e.Length]),
Arguments: strings.Split(string(text[e.Offset+e.Length:nOff]), " "), Arguments: strings.Fields(string(text[e.Offset+e.Length : nOff])),
} }
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
} }

@ -1,10 +1,11 @@
package tgbotapi_test package tgbotapi_test
import ( import (
"fmt"
"testing" "testing"
"time" "time"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/TheMysteriousVincent/telegram-bot-api"
) )
func TestUserStringWith(t *testing.T) { func TestUserStringWith(t *testing.T) {
@ -45,6 +46,38 @@ func TestMessageTime(t *testing.T) {
} }
} }
func TestGetCommands(t *testing.T) {
message := tgbotapi.Message{Text: "/test t\n/testCommandsWith2Arguments TestArgument1 TestArgument2"}
message.Entities = &[]tgbotapi.MessageEntity{
{
Type: "bot_command",
Offset: 0,
Length: 5,
},
{
Type: "bot_command",
Offset: 8,
Length: 27,
},
}
cmds, _ := message.GetCommands()
fmt.Println(*cmds)
if len(*cmds) != 2 {
t.Fatal("there have to be exactly two entries")
}
cmd := (*cmds)[0]
if cmd.Name != "test" {
t.Fatal("name of cmd 1 is '", cmd.Name, "' (test)")
}
cmd = (*cmds)[1]
if cmd.Name != "testCommandsWith2Arguments" {
t.Fatal("name of cmd 1 is '", cmd.Name, "' (testCommandsWith2Arguments)")
}
}
func TestMessageIsCommandWithCommand(t *testing.T) { func TestMessageIsCommandWithCommand(t *testing.T) {
message := tgbotapi.Message{Text: "/command"} message := tgbotapi.Message{Text: "/command"}
message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}} message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}