media: implement SetCaption in Album

This commit is contained in:
Nikita 2024-03-01 21:58:56 +02:00 committed by demget
parent cb88a11861
commit 260a67b811
2 changed files with 61 additions and 0 deletions

View File

@ -45,6 +45,24 @@ type Inputtable interface {
// Album lets you group multiple media into a single message.
type Album []Inputtable
func (a Album) SetCaption(caption string) {
if len(a) < 1 {
return
}
switch a[0].MediaType() {
case "audio":
a[0].(*Audio).Caption = caption
case "video":
a[0].(*Video).Caption = caption
case "document":
a[0].(*Document).Caption = caption
case "photo":
a[0].(*Photo).Caption = caption
case "animation":
a[0].(*Animation).Caption = caption
}
}
// Photo object represents a single photo file.
type Photo struct {
File

43
media_test.go Normal file
View File

@ -0,0 +1,43 @@
package telebot
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestAlbumSetCaption(t *testing.T) {
var a Album
a = append(a, &Photo{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)
a = Album{}
a = append(a, &Animation{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)
a = Album{}
a = append(a, &Audio{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)
a = Album{}
a = append(a, &Document{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)
a = Album{}
a = append(a, &Video{Caption: "wrong_caption"})
a = append(a, &Photo{Caption: "t"})
a.SetCaption("correct_caption")
assert.Equal(t, "correct_caption", a[0].InputMedia().Caption)
assert.Equal(t, "t", a[1].InputMedia().Caption)
}