diff --git a/telegram/formatter/formatter_test.go b/telegram/formatter/formatter_test.go index 187d486..aaaca89 100644 --- a/telegram/formatter/formatter_test.go +++ b/telegram/formatter/formatter_test.go @@ -1,13 +1,28 @@ package formatter import ( + "errors" + "strings" "testing" "github.com/zelenin/go-tdlib/client" ) +type MentionRetrieverMock struct {} + +func (m *MentionRetrieverMock) GetMUCNicknameByUsername(username string) (string, error) { + if strings.HasPrefix(username, "@") { + return username[1:], nil + } + return "", errors.New("Я@ТЫ@Я@ТЫ@Я@ТЫ@Я@ТЫ@Я@ТЫ@") +} + +func (m *MentionRetrieverMock) GetMUCNickname(id int64) (string) { + return "42" +} + func TestNoFormatting(t *testing.T) { - markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeMarkdown) + markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeMarkdown, &MentionRetrieverMock{}) if markup != "abc\ndef" { t.Errorf("No formatting expected, but: %v", markup) } @@ -20,7 +35,7 @@ func TestFormattingSimple(t *testing.T) { Length: 4, Type: &client.TextEntityTypeBold{}, }, - }, MarkupModeMarkdown) + }, MarkupModeMarkdown, &MentionRetrieverMock{}) if markup != "👙**🐧🐖**" { t.Errorf("Wrong simple formatting: %v", markup) } @@ -40,7 +55,7 @@ func TestFormattingAdjacent(t *testing.T) { Url: "https://narayana.im/", }, }, - }, MarkupModeMarkdown) + }, MarkupModeMarkdown, &MentionRetrieverMock{}) if markup != "a👙_🐧_[🐖](https://narayana.im/)" { t.Errorf("Wrong adjacent formatting: %v", markup) } @@ -63,7 +78,7 @@ func TestFormattingAdjacentAndNested(t *testing.T) { Length: 2, Type: &client.TextEntityTypeItalic{}, }, - }, MarkupModeMarkdown) + }, MarkupModeMarkdown, &MentionRetrieverMock{}) if markup != "```\n**👙**🐧\n```_🐖_" { t.Errorf("Wrong adjacent&nested formatting: %v", markup) } @@ -208,7 +223,7 @@ func TestSortEmpty(t *testing.T) { } func TestNoFormattingXEP0393(t *testing.T) { - markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeXEP0393) + markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "abc\ndef" { t.Errorf("No formatting expected, but: %v", markup) } @@ -221,7 +236,7 @@ func TestFormattingXEP0393Simple(t *testing.T) { Length: 4, Type: &client.TextEntityTypeBold{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "👙*🐧🐖*" { t.Errorf("Wrong simple formatting: %v", markup) } @@ -241,7 +256,7 @@ func TestFormattingXEP0393Adjacent(t *testing.T) { Url: "https://narayana.im/", }, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "a👙_🐧_🐖 " { t.Errorf("Wrong adjacent formatting: %v", markup) } @@ -264,7 +279,7 @@ func TestFormattingXEP0393AdjacentAndNested(t *testing.T) { Length: 2, Type: &client.TextEntityTypeItalic{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "```\n*👙*🐧\n```_🐖_" { t.Errorf("Wrong adjacent&nested formatting: %v", markup) } @@ -287,7 +302,7 @@ func TestFormattingXEP0393AdjacentItalicBoldItalic(t *testing.T) { Length: 69, Type: &client.TextEntityTypeItalic{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "_раса двуногих крысолюдей, *которую так редко замечают, что многие отрицают само их существование*_" { t.Errorf("Wrong adjacent italic/bold-italic formatting: %v", markup) } @@ -315,7 +330,7 @@ func TestFormattingXEP0393MultipleAdjacent(t *testing.T) { Length: 1, Type: &client.TextEntityTypeItalic{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "a*bcd*_e_" { t.Errorf("Wrong multiple adjacent formatting: %v", markup) } @@ -343,7 +358,7 @@ func TestFormattingXEP0393Intersecting(t *testing.T) { Length: 1, Type: &client.TextEntityTypeBold{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "a*b*_*cd*e_" { t.Errorf("Wrong intersecting formatting: %v", markup) } @@ -361,7 +376,7 @@ func TestFormattingXEP0393InlineCode(t *testing.T) { Length: 25, Type: &client.TextEntityTypePre{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "Is `Gajim` a thing?\n\n```\necho 'Hello'\necho 'world'\n```\n\nhruck(" { t.Errorf("Wrong intersecting formatting: %v", markup) } @@ -374,7 +389,7 @@ func TestFormattingMarkdownStrikethrough(t *testing.T) { Length: 3, Type: &client.TextEntityTypeStrikethrough{}, }, - }, MarkupModeMarkdown) + }, MarkupModeMarkdown, &MentionRetrieverMock{}) if markup != "Everyone ~~dis~~likes cake." { t.Errorf("Wrong strikethrough formatting: %v", markup) } @@ -387,7 +402,7 @@ func TestFormattingXEP0393Strikethrough(t *testing.T) { Length: 3, Type: &client.TextEntityTypeStrikethrough{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "Everyone ~dis~likes cake." { t.Errorf("Wrong strikethrough formatting: %v", markup) } @@ -480,7 +495,7 @@ func TestNoNewlineBlockquoteXEP0393(t *testing.T) { Length: 6, Type: &client.TextEntityTypeBlockQuote{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "yes \n> it can\n i think" { t.Errorf("Wrong blockquote formatting: %v", markup) } @@ -493,7 +508,7 @@ func TestNoNewlineBlockquoteMarkdown(t *testing.T) { Length: 6, Type: &client.TextEntityTypeBlockQuote{}, }, - }, MarkupModeMarkdown) + }, MarkupModeMarkdown, &MentionRetrieverMock{}) if markup != "yes \n> it can\n\n i think" { t.Errorf("Wrong blockquote formatting: %v", markup) } @@ -506,7 +521,7 @@ func TestMultilineBlockquoteXEP0393(t *testing.T) { Length: 17, Type: &client.TextEntityTypeBlockQuote{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "> hruck\n> puck\n> \n> shuck\ntext" { t.Errorf("Wrong blockquote formatting: %v", markup) } @@ -519,7 +534,7 @@ func TestMultilineBlockquoteMarkdown(t *testing.T) { Length: 17, Type: &client.TextEntityTypeBlockQuote{}, }, - }, MarkupModeMarkdown) + }, MarkupModeMarkdown, &MentionRetrieverMock{}) if markup != "> hruck\npuck\n\n> shuck\n\ntext" { t.Errorf("Wrong blockquote formatting: %v", markup) } @@ -547,7 +562,7 @@ func TestMixedBlockquoteXEP0393(t *testing.T) { Length: 2, Type: &client.TextEntityTypeStrikethrough{}, }, - }, MarkupModeXEP0393) + }, MarkupModeXEP0393, &MentionRetrieverMock{}) if markup != "> *_hruck\n> p~uc~k_\n> shuck*\ntext" { t.Errorf("Wrong blockquote formatting: %v", markup) } @@ -575,8 +590,83 @@ func TestMixedBlockquoteMarkdown(t *testing.T) { Length: 2, Type: &client.TextEntityTypeStrikethrough{}, }, - }, MarkupModeMarkdown) + }, MarkupModeMarkdown, &MentionRetrieverMock{}) if markup != "> **_hruck\np~~uc~~k_\nshuck**\n\ntext" { t.Errorf("Wrong blockquote formatting: %v", markup) } } + +func TestUsernameMention(t *testing.T) { + markup := Format("a @b c", []*client.TextEntity{ + &client.TextEntity{ + Offset: 2, + Length: 2, + Type: &client.TextEntityTypeMention{}, + }, + }, MarkupModeXEP0393, &MentionRetrieverMock{}) + if markup != "a b c" { + t.Errorf("Wrong mention formatting: %v", markup) + } +} + +func TestUsernameMentionName(t *testing.T) { + markup := Format("a bb c", []*client.TextEntity{ + &client.TextEntity{ + Offset: 2, + Length: 2, + Type: &client.TextEntityTypeMentionName{UserId: 100500}, + }, + }, MarkupModeXEP0393, &MentionRetrieverMock{}) + if markup != "a 42 c" { + t.Errorf("Wrong mention name formatting: %v", markup) + } +} + +func TestUsernameMentionNested(t *testing.T) { + markup := Format("a @b c", []*client.TextEntity{ + &client.TextEntity{ + Offset: 2, + Length: 2, + Type: &client.TextEntityTypeMention{}, + }, + &client.TextEntity{ + Offset: 2, + Length: 1, + Type: &client.TextEntityTypeBold{}, + }, + }, MarkupModeXEP0393, &MentionRetrieverMock{}) + if markup != "a b c" { + t.Errorf("Wrong formatting of mention with nested entity: %v", markup) + } +} + +func TestUsernameMentionNestedEven(t *testing.T) { + markup := Format("a @b c", []*client.TextEntity{ + &client.TextEntity{ + Offset: 2, + Length: 2, + Type: &client.TextEntityTypeMention{}, + }, + &client.TextEntity{ + Offset: 2, + Length: 2, + Type: &client.TextEntityTypeBold{}, + }, + }, MarkupModeXEP0393, &MentionRetrieverMock{}) + if markup != "a *b* c" { + t.Errorf("Wrong formatting of mention with even nested entity: %v", markup) + } +} + +func TestUsernameMentionError(t *testing.T) { + markup := Format("a bb c", []*client.TextEntity{ + &client.TextEntity{ + Offset: 2, + Length: 2, + Type: &client.TextEntityTypeMention{}, + }, + }, MarkupModeXEP0393, &MentionRetrieverMock{}) + if markup != "a bb c" { + t.Errorf("Wrong formatting of erroneous mention: %v", markup) + } +}