Skip to content

Commit

Permalink
tests: implement options test
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Jun 9, 2020
1 parent e1811e1 commit ddb943d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
21 changes: 12 additions & 9 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (og *SendOptions) copy() *SendOptions {
if cp.ReplyMarkup != nil {
cp.ReplyMarkup = cp.ReplyMarkup.copy()
}

return &cp
}

Expand Down Expand Up @@ -110,16 +109,20 @@ type ReplyMarkup struct {
func (r *ReplyMarkup) copy() *ReplyMarkup {
cp := *r

cp.ReplyKeyboard = make([][]ReplyButton, len(r.ReplyKeyboard))
for i, row := range r.ReplyKeyboard {
cp.ReplyKeyboard[i] = make([]ReplyButton, len(row))
copy(cp.ReplyKeyboard[i], row)
if len(r.ReplyKeyboard) > 0 {
cp.ReplyKeyboard = make([][]ReplyButton, len(r.ReplyKeyboard))
for i, row := range r.ReplyKeyboard {
cp.ReplyKeyboard[i] = make([]ReplyButton, len(row))
copy(cp.ReplyKeyboard[i], row)
}
}

cp.InlineKeyboard = make([][]InlineButton, len(r.InlineKeyboard))
for i, row := range r.InlineKeyboard {
cp.InlineKeyboard[i] = make([]InlineButton, len(row))
copy(cp.InlineKeyboard[i], row)
if len(r.InlineKeyboard) > 0 {
cp.InlineKeyboard = make([][]InlineButton, len(r.InlineKeyboard))
for i, row := range r.InlineKeyboard {
cp.InlineKeyboard[i] = make([]InlineButton, len(row))
copy(cp.InlineKeyboard[i], row)
}
}

return &cp
Expand Down
23 changes: 19 additions & 4 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestBtn(t *testing.T) {
assert.Equal(t, &InlineButton{Text: "T", Login: &Login{Text: "T"}}, r.Login("T", &Login{Text: "T"}).Inline())
}

func TestReplyInline(t *testing.T) {
func TestOptions(t *testing.T) {
r := &ReplyMarkup{}
r.Reply(
r.Row(r.Text("Menu")),
Expand All @@ -36,13 +36,28 @@ func TestReplyInline(t *testing.T) {
}, r.ReplyKeyboard)

i := &ReplyMarkup{}
i.Inline(r.Row(
r.Data("Previous", "prev"),
r.Data("Next", "next"),
i.Inline(i.Row(
i.Data("Previous", "prev"),
i.Data("Next", "next"),
))

assert.Equal(t, [][]InlineButton{{
{Unique: "prev", Text: "Previous"},
{Unique: "next", Text: "Next"},
}}, i.InlineKeyboard)

assert.Panics(t, func() {
r.Reply(r.Row(r.Data("T", "u")))
i.Inline(i.Row(i.Text("T")))
})

assert.Equal(t, r.copy(), r)
assert.Equal(t, i.copy(), i)

o := &SendOptions{ReplyMarkup: r}
assert.Equal(t, o.copy(), o)

data, err := PollQuiz.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, []byte(`{"type":"quiz"}`), data)
}

0 comments on commit ddb943d

Please sign in to comment.