Skip to content

Commit

Permalink
Turn a possibly empty field into a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed Sep 25, 2024
1 parent dd7351b commit 956e7cd
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Bot struct {
Token string

// The bot's User info, as returned by Bot.GetMe. Populated when created through the NewBot method.
User
*User
// The bot client to use to make requests
BotClient
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewBot(token string, opts *BotOpts) (*Bot, error) {
if err != nil {
return nil, fmt.Errorf("failed to check bot token: %w", err)
}
b.User = *botUser
b.User = botUser
}

return &b, nil
Expand Down
14 changes: 2 additions & 12 deletions ext/botmapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ package ext

import (
"testing"

"github.com/PaulSonOfLars/gotgbot/v2"
)

func Test_botMapping(t *testing.T) {
bm := botMapping{}
b := &gotgbot.Bot{
User: gotgbot.User{},
Token: "SOME_TOKEN",
BotClient: &gotgbot.BaseBotClient{},
}
b := NewTestBot()

var origBdata *botData
t.Run("addBot", func(t *testing.T) {
Expand Down Expand Up @@ -78,11 +72,7 @@ func Test_botMapping(t *testing.T) {

func Test_botData_isUpdateChannelStopped(t *testing.T) {
bm := botMapping{}
b := &gotgbot.Bot{
User: gotgbot.User{},
Token: "SOME_TOKEN",
BotClient: &gotgbot.BaseBotClient{},
}
b := NewTestBot()

bData, err := bm.addBot(b, "", "")
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions ext/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package ext

import (
"errors"
"math/rand"
"net/http"
"net/http/httptest"

"github.com/PaulSonOfLars/gotgbot/v2"
)
Expand Down Expand Up @@ -37,3 +40,24 @@ func (u *Updater) InjectUpdate(token string, upd gotgbot.Update) error {
}
return d.ProcessUpdate(bData.bot, &upd, nil)
}

func NewTestBot() *gotgbot.Bot {
server := httptest.NewServer(nil)
return &gotgbot.Bot{
Token: "use-me",
User: &gotgbot.User{
Id: rand.Int63(),
IsBot: false,
FirstName: "gobot",
LastName: "",
Username: "gotgbot",
},
BotClient: &gotgbot.BaseBotClient{
Client: http.Client{},
DefaultRequestOpts: &gotgbot.RequestOpts{
Timeout: 0,
APIURL: server.URL,
},
},
}
}
2 changes: 1 addition & 1 deletion ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Context struct {
// We store the info of the Bot that received this update so we can keep track of update ownership.
// We do NOT store full gotgbot.Bot struct, as that would leak the bot token and bot client information in what
// should be a data-only struct.
Bot gotgbot.User
Bot *gotgbot.User
// Data represents update-local storage.
// This can be used to pass data across handlers - for example, to cache operations relevant to the current update,
// such as admin checks.
Expand Down
4 changes: 2 additions & 2 deletions ext/handlers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func NewTestBot() *gotgbot.Bot {
server := httptest.NewServer(nil)
return &gotgbot.Bot{
Token: "use-me",
User: gotgbot.User{
Id: 0,
User: &gotgbot.User{
Id: rand.Int63(),
IsBot: false,
FirstName: "gobot",
LastName: "",
Expand Down
17 changes: 4 additions & 13 deletions ext/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ import (
)

func TestUpdaterThrowsErrorWhenSameWebhookAddedTwice(t *testing.T) {
b := &gotgbot.Bot{
User: gotgbot.User{},
Token: "SOME_TOKEN",
BotClient: &gotgbot.BaseBotClient{},
}
b := ext.NewTestBot()

d := ext.NewDispatcher(&ext.DispatcherOpts{})
u := ext.NewUpdater(d, nil)
Expand All @@ -45,12 +41,7 @@ func TestUpdaterThrowsErrorWhenSameWebhookAddedTwice(t *testing.T) {
}

func TestUpdaterSupportsWebhookReAdding(t *testing.T) {
b := &gotgbot.Bot{
User: gotgbot.User{},
Token: "SOME_TOKEN",
BotClient: &gotgbot.BaseBotClient{},
}

b := ext.NewTestBot()
d := ext.NewDispatcher(&ext.DispatcherOpts{})
u := ext.NewUpdater(d, nil)

Expand Down Expand Up @@ -117,7 +108,7 @@ func concurrentTest(t *testing.T) {
}

b := &gotgbot.Bot{
User: gotgbot.User{},
User: &gotgbot.User{},
Token: "SOME_TOKEN",
BotClient: &gotgbot.BaseBotClient{},
}
Expand Down Expand Up @@ -449,7 +440,7 @@ func TestUpdaterSupportsLongPollReAdding(t *testing.T) {
}

b := &gotgbot.Bot{
User: gotgbot.User{},
User: &gotgbot.User{},
Token: "SOME_TOKEN",
BotClient: &gotgbot.BaseBotClient{
DefaultRequestOpts: reqOpts,
Expand Down

0 comments on commit 956e7cd

Please sign in to comment.