Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

teletest: implement #723

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 227 additions & 0 deletions teletest/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package teletest

import (
"time"

tele "gopkg.in/telebot.v3"
)

type Context struct {
nc tele.Context

send Response
reply Response
}

func (c *Context) Bot() *tele.Bot {
//TODO implement me
panic("implement me")
}

func (c *Context) Update() tele.Update {
return c.nc.Update()
}

func (c *Context) Message() *tele.Message {
//TODO implement me
panic("implement me")
}

func (c *Context) Callback() *tele.Callback {
//TODO implement me
panic("implement me")
}

func (c *Context) Query() *tele.Query {
//TODO implement me
panic("implement me")
}

func (c *Context) InlineResult() *tele.InlineResult {
//TODO implement me
panic("implement me")
}

func (c *Context) ShippingQuery() *tele.ShippingQuery {
//TODO implement me
panic("implement me")
}

func (c *Context) PreCheckoutQuery() *tele.PreCheckoutQuery {
//TODO implement me
panic("implement me")
}

func (c *Context) Poll() *tele.Poll {
//TODO implement me
panic("implement me")
}

func (c *Context) PollAnswer() *tele.PollAnswer {
//TODO implement me
panic("implement me")
}

func (c *Context) ChatMember() *tele.ChatMemberUpdate {
//TODO implement me
panic("implement me")
}

func (c *Context) ChatJoinRequest() *tele.ChatJoinRequest {
//TODO implement me
panic("implement me")
}

func (c *Context) Migration() (int64, int64) {
//TODO implement me
panic("implement me")
}

func (c *Context) Topic() *tele.Topic {
//TODO implement me
panic("implement me")
}

func (c *Context) Boost() *tele.BoostUpdated {
//TODO implement me
panic("implement me")
}

func (c *Context) BoostRemoved() *tele.BoostRemoved {
//TODO implement me
panic("implement me")
}

func (c *Context) Sender() *tele.User {
return c.nc.Sender()
}

func (c *Context) Chat() *tele.Chat {
//TODO implement me
panic("implement me")
}

func (c *Context) Recipient() tele.Recipient {
//TODO implement me
panic("implement me")
}

func (c *Context) Text() string {
return c.nc.Text()
}

func (c *Context) Entities() tele.Entities {
//TODO implement me
panic("implement me")
}

func (c *Context) Data() string {
//TODO implement me
panic("implement me")
}

func (c *Context) Args() []string {
//TODO implement me
panic("implement me")
}

func (c *Context) Send(what interface{}, opts ...interface{}) error {
c.send.what = what
c.send.opts = opts
return nil
}

func (c *Context) SendAlbum(a tele.Album, opts ...interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Reply(what interface{}, opts ...interface{}) error {
c.reply = Response{what, opts}
return nil
}

func (c *Context) Forward(msg tele.Editable, opts ...interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) ForwardTo(to tele.Recipient, opts ...interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Edit(what interface{}, opts ...interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) EditCaption(caption string, opts ...interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) EditOrSend(what interface{}, opts ...interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) EditOrReply(what interface{}, opts ...interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Delete() error {
//TODO implement me
panic("implement me")
}

func (c *Context) DeleteAfter(d time.Duration) *time.Timer {
//TODO implement me
panic("implement me")
}

func (c *Context) Notify(action tele.ChatAction) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Ship(what ...interface{}) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Accept(errorMessage ...string) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Answer(resp *tele.QueryResponse) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Respond(resp ...*tele.CallbackResponse) error {
//TODO implement me
panic("implement me")
}

func (c *Context) RespondText(text string) error {
//TODO implement me
panic("implement me")
}

func (c *Context) RespondAlert(text string) error {
//TODO implement me
panic("implement me")
}

func (c *Context) Get(key string) interface{} {
//TODO implement me
panic("implement me")
}

func (c *Context) Set(key string, val interface{}) {
//TODO implement me
panic("implement me")
}
32 changes: 32 additions & 0 deletions teletest/example/bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package example

import (
tele "gopkg.in/telebot.v3"
)

var users = make(map[int64]bool)

func NewBot() (*tele.Bot, error) {
pref := tele.Settings{
Synchronous: true,
Offline: true,
}

b, err := tele.NewBot(pref)
if err != nil {
return nil, err
}

b.Handle("/start", func(c tele.Context) error {
id := c.Sender().ID
if !users[id] {
users[id] = true
}
return c.Send("Hello!")
})
b.Handle(tele.OnText, func(c tele.Context) error {
return c.Reply(c.Text())
})

return b, nil
}
35 changes: 35 additions & 0 deletions teletest/example/bot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package example

import (
"testing"

tele "gopkg.in/telebot.v3"
"gopkg.in/telebot.v3/teletest"

"github.com/stretchr/testify/assert"
)

var b, _ = NewBot()

func TestBot(t *testing.T) {
r, expect := teletest.New(b)

r.Trigger(tele.Update{
Message: &tele.Message{
Sender: &tele.User{ID: 1},
Text: "/start",
},
})

expect.Send(t, "Hello!")
assert.Contains(t, users, int64(1))

r.Trigger(tele.Update{
Message: &tele.Message{
Sender: &tele.User{ID: 1},
Text: "echo",
},
})

expect.Reply(t, "echo")
}
35 changes: 35 additions & 0 deletions teletest/recorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package teletest

import (
"testing"

tele "gopkg.in/telebot.v3"
)

type Recorder struct {
b *tele.Bot
e *Expect
}

func New(b *tele.Bot) (*Recorder, *Expect) {
r := &Recorder{b: b, e: &Expect{}}
return r, r.e
}

func (r *Recorder) Trigger(u tele.Update) {
c := &Context{nc: r.b.NewContext(u)}
r.b.ProcessContext(c)
*r.e = Expect{c: c}
}

type Expect struct {
c *Context
}

func (e Expect) Send(t *testing.T, what interface{}, opts ...interface{}) {
e.c.send.Expect(t, what, opts)
}

func (e Expect) Reply(t *testing.T, what interface{}, opts ...interface{}) {
e.c.reply.Expect(t, what, opts)
}
17 changes: 17 additions & 0 deletions teletest/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package teletest

import (
"testing"

"github.com/stretchr/testify/assert"
)

type Response struct {
what interface{}
opts []interface{}
}

func (r Response) Expect(t *testing.T, what interface{}, opts []interface{}) {
assert.Equal(t, what, r.what)
assert.Equal(t, opts, r.opts)
}
8 changes: 7 additions & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ type Update struct {
// ProcessUpdate processes a single incoming update.
// A started bot calls this function automatically.
func (b *Bot) ProcessUpdate(u Update) {
c := b.NewContext(u)
b.ProcessContext(b.NewContext(u))
}

// ProcessContext processes the given context.
// A started bot calls this function automatically.
func (b *Bot) ProcessContext(c Context) {
u := c.Update()

if u.Message != nil {
m := u.Message
Expand Down
Loading