package tgfmt
import (
"testing"
"time"
)
func TestRichInline(t *testing.T) {
cases := []struct {
name string
got Rich
want string
}{
{"bold", NewRich("bold text").Bold(), "bold text"},
{"spoiler", NewRich("spoiler").Spoiler(), "spoiler"},
{"escape", NewRich(`a & "c"`), "a<b> & "c""},
{"link", NewRich("inline URL").Link("https://t.me/"), `inline URL`},
{"link attr escape", NewRich("x").Link(`https://e/?q=">`), `x`},
{"mention", NewRich("user").Mention(123456789), `user`},
{"anchor", Rich("").Anchor("chapter-1"), ``},
{"anchor link", NewRich("in-document link").AnchorLink("chapter-1"), `in-document link`},
{"reference", NewRich("Referenced text").Ref("note-1"), `Referenced text`},
{"emoji", Emoji("5368324170671202286", "👍"), `👍`},
{"emoji alt escape", Emoji("1", ``), `<x>`},
{"time format", NewRich("22:45 tomorrow").TimeFormat(time.Unix(1647531900, 0), "wDT"),
`22:45 tomorrow`},
{"math", NewRich("x^2 + y^2").Math(), "x^2 + y^2"},
}
for _, c := range cases {
if string(c.got) != c.want {
t.Errorf("%s:\n got %s\n want %s", c.name, c.got, c.want)
}
}
}
func TestRichBlocks(t *testing.T) {
cases := []struct {
name string
got RichBlock
want string
}{
{"h1", H1(NewRich("Heading 1")), "Heading 1
"},
{"p", P(NewRich("Paragraph text")), "Paragraph text
"},
{"pre code", PreCode("python", NewRich("print('x')")),
`print('x')
`},
{"footer", Footer(NewRich("Footer text")), ""},
{"hr", Hr(), "
"},
{"anchor block", AnchorBlock("chapter-2"), ``},
{"math block", MathBlock("E = mc^2"), "E = mc^2"},
}
for _, c := range cases {
if string(c.got) != c.want {
t.Errorf("%s:\n got %s\n want %s", c.name, c.got, c.want)
}
}
}
func TestRichLists(t *testing.T) {
cases := []struct {
name string
got RichBlock
want string
}{
{"ul", Ul(Li(NewRich("unordered list item"))),
""},
{"ol plain", Ol(OlOpts{}, Li(NewRich("ordered list item"))),
"- ordered list item
"},
{"ol attrs", Ol(OlOpts{Start: 3, Type: "a", Reversed: true}, Li(NewRich("ordered list item"))),
`- ordered list item
`},
{"li value type", Ol(OlOpts{}, Li(NewRich("item")).SetValue(7).SetType("i")),
`- item
`},
{"checkboxes", Ul(
LiCheckbox(true, NewRich("Checked checkbox")),
LiCheckbox(false, NewRich("Unchecked checkbox")),
), ``},
}
for _, c := range cases {
if string(c.got) != c.want {
t.Errorf("%s:\n got %s\n want %s", c.name, c.got, c.want)
}
}
}
func TestRichQuotes(t *testing.T) {
got := Blockquote(NewRich("The Author"),
NewRich("Block quotation started"),
NewRich("Block quotation continued"),
NewRich("The last line of the block quotation"),
)
want := "Block quotation started
Block quotation continued
" +
"The last line of the block quotationThe Author
"
if string(got) != want {
t.Errorf("blockquote:\n got %s\n want %s", got, want)
}
got = Aside(NewRich("The Author"), NewRich("Pull quote"))
want = ""
if string(got) != want {
t.Errorf("aside:\n got %s\n want %s", got, want)
}
got = Blockquote("", NewRich("no credit"))
want = "no credit
"
if string(got) != want {
t.Errorf("blockquote without credit:\n got %s\n want %s", got, want)
}
}
func TestRichMedia(t *testing.T) {
cases := []struct {
name string
got RichBlock
want string
}{
{"photo", Photo("https://telegram.org/example/photo.jpg").Block(),
`
`},
{"video", Video("https://telegram.org/example/video.mp4").Block(),
``},
{"audio", Audio("https://telegram.org/example/audio.mp3").Block(),
``},
{"photo spoiler caption", Photo("https://telegram.org/example/photo.jpg").SetSpoiler().
Caption(NewRich("Photo credit"), NewRich("Photo caption")),
`
` +
`Photo captionPhoto credit`},
{"video caption no credit", Video("https://telegram.org/example/video.mp4").
Caption("", NewRich("Video caption")),
`` +
`Video caption`},
{"map", Map(41.9, 12.5, 14), ``},
{"map caption", MapCaption(41.9, 12.5, 14, "", NewRich("Map caption")),
`Map caption`},
{"collage", Collage(
Photo("https://telegram.org/example/photo.jpg"),
Video("https://telegram.org/example/video.mp4"),
), `
` +
``},
{"slideshow caption", SlideshowCaption("", NewRich("Slideshow caption"),
Photo("https://telegram.org/example/photo.jpg"),
), `
` +
`Slideshow caption`},
}
for _, c := range cases {
if string(c.got) != c.want {
t.Errorf("%s:\n got %s\n want %s", c.name, c.got, c.want)
}
}
}
func TestRichTable(t *testing.T) {
got := Table(false, false, "",
Row(true, Cell(NewRich("Header 1")), Cell(NewRich("Header 2"))),
Row(false, Cell(NewRich("Value 1")), Cell(NewRich("Value 2"))),
)
want := "| Header 1 | Header 2 |
" +
"| Value 1 | Value 2 |
"
if string(got) != want {
t.Errorf("plain table:\n got %s\n want %s", got, want)
}
got = Table(true, true, NewRich("Table caption"),
Row(false,
Cell(NewRich("Value")).SetSpan(2, 2).SetAlign("left"),
Cell(NewRich("Value2")).SetAlign("center"),
),
)
want = `Table caption` +
`| Value | Value2 |
`
if string(got) != want {
t.Errorf("table attrs:\n got %s\n want %s", got, want)
}
}
func TestRichDetails(t *testing.T) {
got := Details(true, NewRich("Title"), P(NewRich("Content")))
want := "Title
Content
"
if string(got) != want {
t.Errorf("details:\n got %s\n want %s", got, want)
}
}