Skip to content

Commit

Permalink
teletest: initial concept
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Aug 7, 2024
1 parent 864bef4 commit d1feb7d
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 1 deletion.
225 changes: 225 additions & 0 deletions teletest/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package teletest

import (
"time"

tele "gopkg.in/telebot.v3"
)

type Context struct {
nc tele.Context

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 {
//TODO implement me
panic("implement me")
}

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.Reply("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"

"github.com/stretchr/testify/require"

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

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.Reply(t, "Hello!")
require.Contains(t, users, int64(1))

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

expect.Reply(t, "echo")
}
31 changes: 31 additions & 0 deletions teletest/recorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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) 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

0 comments on commit d1feb7d

Please sign in to comment.