aboutsummaryrefslogtreecommitdiff
path: root/telegram/formatter/formatter_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'telegram/formatter/formatter_test.go')
-rw-r--r--telegram/formatter/formatter_test.go198
1 files changed, 153 insertions, 45 deletions
diff --git a/telegram/formatter/formatter_test.go b/telegram/formatter/formatter_test.go
index e4bdd23..187d486 100644
--- a/telegram/formatter/formatter_test.go
+++ b/telegram/formatter/formatter_test.go
@@ -7,7 +7,7 @@ import (
)
func TestNoFormatting(t *testing.T) {
- markup := Format("abc\ndef", []*client.TextEntity{}, EntityToMarkdown)
+ markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeMarkdown)
if markup != "abc\ndef" {
t.Errorf("No formatting expected, but: %v", markup)
}
@@ -20,7 +20,7 @@ func TestFormattingSimple(t *testing.T) {
Length: 4,
Type: &client.TextEntityTypeBold{},
},
- }, EntityToMarkdown)
+ }, MarkupModeMarkdown)
if markup != "👙**🐧🐖**" {
t.Errorf("Wrong simple formatting: %v", markup)
}
@@ -40,7 +40,7 @@ func TestFormattingAdjacent(t *testing.T) {
Url: "https://narayana.im/",
},
},
- }, EntityToMarkdown)
+ }, MarkupModeMarkdown)
if markup != "a👙_🐧_[🐖](https://narayana.im/)" {
t.Errorf("Wrong adjacent formatting: %v", markup)
}
@@ -63,18 +63,18 @@ func TestFormattingAdjacentAndNested(t *testing.T) {
Length: 2,
Type: &client.TextEntityTypeItalic{},
},
- }, EntityToMarkdown)
+ }, MarkupModeMarkdown)
if markup != "```\n**👙**🐧\n```_🐖_" {
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
}
}
func TestRebalanceTwoZero(t *testing.T) {
- s1 := InsertionStack{
- &Insertion{Offset: 7},
- &Insertion{Offset: 8},
+ s1 := insertionStack{
+ &insertion{Offset: 7},
+ &insertion{Offset: 8},
}
- s2 := InsertionStack{}
+ s2 := insertionStack{}
s1, s2 = s1.rebalance(s2, 7)
if !(len(s1) == 2 && len(s2) == 0 && s1[0].Offset == 7 && s1[1].Offset == 8) {
t.Errorf("Wrong rebalance 2–0: %#v %#v", s1, s2)
@@ -82,13 +82,13 @@ func TestRebalanceTwoZero(t *testing.T) {
}
func TestRebalanceNeeded(t *testing.T) {
- s1 := InsertionStack{
- &Insertion{Offset: 7},
- &Insertion{Offset: 8},
+ s1 := insertionStack{
+ &insertion{Offset: 7},
+ &insertion{Offset: 8},
}
- s2 := InsertionStack{
- &Insertion{Offset: 10},
- &Insertion{Offset: 9},
+ s2 := insertionStack{
+ &insertion{Offset: 10},
+ &insertion{Offset: 9},
}
s1, s2 = s1.rebalance(s2, 9)
if !(len(s1) == 3 && len(s2) == 1 &&
@@ -99,13 +99,13 @@ func TestRebalanceNeeded(t *testing.T) {
}
func TestRebalanceNotNeeded(t *testing.T) {
- s1 := InsertionStack{
- &Insertion{Offset: 7},
- &Insertion{Offset: 8},
+ s1 := insertionStack{
+ &insertion{Offset: 7},
+ &insertion{Offset: 8},
}
- s2 := InsertionStack{
- &Insertion{Offset: 10},
- &Insertion{Offset: 9},
+ s2 := insertionStack{
+ &insertion{Offset: 10},
+ &insertion{Offset: 9},
}
s1, s2 = s1.rebalance(s2, 8)
if !(len(s1) == 2 && len(s2) == 2 &&
@@ -116,13 +116,13 @@ func TestRebalanceNotNeeded(t *testing.T) {
}
func TestRebalanceLate(t *testing.T) {
- s1 := InsertionStack{
- &Insertion{Offset: 7},
- &Insertion{Offset: 8},
+ s1 := insertionStack{
+ &insertion{Offset: 7},
+ &insertion{Offset: 8},
}
- s2 := InsertionStack{
- &Insertion{Offset: 10},
- &Insertion{Offset: 9},
+ s2 := insertionStack{
+ &insertion{Offset: 10},
+ &insertion{Offset: 9},
}
s1, s2 = s1.rebalance(s2, 10)
if !(len(s1) == 4 && len(s2) == 0 &&
@@ -133,7 +133,7 @@ func TestRebalanceLate(t *testing.T) {
}
func TestIteratorEmpty(t *testing.T) {
- s := InsertionStack{}
+ s := insertionStack{}
g := s.NewIterator()
v := g()
if v != nil {
@@ -142,9 +142,9 @@ func TestIteratorEmpty(t *testing.T) {
}
func TestIterator(t *testing.T) {
- s := InsertionStack{
- &Insertion{Offset: 7},
- &Insertion{Offset: 8},
+ s := insertionStack{
+ &insertion{Offset: 7},
+ &insertion{Offset: 8},
}
g := s.NewIterator()
v := g()
@@ -208,7 +208,7 @@ func TestSortEmpty(t *testing.T) {
}
func TestNoFormattingXEP0393(t *testing.T) {
- markup := Format("abc\ndef", []*client.TextEntity{}, EntityToXEP0393)
+ markup := Format("abc\ndef", []*client.TextEntity{}, MarkupModeXEP0393)
if markup != "abc\ndef" {
t.Errorf("No formatting expected, but: %v", markup)
}
@@ -221,7 +221,7 @@ func TestFormattingXEP0393Simple(t *testing.T) {
Length: 4,
Type: &client.TextEntityTypeBold{},
},
- }, EntityToXEP0393)
+ }, MarkupModeXEP0393)
if markup != "👙*🐧🐖*" {
t.Errorf("Wrong simple formatting: %v", markup)
}
@@ -241,7 +241,7 @@ func TestFormattingXEP0393Adjacent(t *testing.T) {
Url: "https://narayana.im/",
},
},
- }, EntityToXEP0393)
+ }, MarkupModeXEP0393)
if markup != "a👙_🐧_🐖 <https://narayana.im/>" {
t.Errorf("Wrong adjacent formatting: %v", markup)
}
@@ -264,7 +264,7 @@ func TestFormattingXEP0393AdjacentAndNested(t *testing.T) {
Length: 2,
Type: &client.TextEntityTypeItalic{},
},
- }, EntityToXEP0393)
+ }, MarkupModeXEP0393)
if markup != "```\n*👙*🐧\n```_🐖_" {
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
}
@@ -287,7 +287,7 @@ func TestFormattingXEP0393AdjacentItalicBoldItalic(t *testing.T) {
Length: 69,
Type: &client.TextEntityTypeItalic{},
},
- }, EntityToXEP0393)
+ }, MarkupModeXEP0393)
if markup != "_раса двуногих крысолюдей, *которую так редко замечают, что многие отрицают само их существование*_" {
t.Errorf("Wrong adjacent italic/bold-italic formatting: %v", markup)
}
@@ -315,7 +315,7 @@ func TestFormattingXEP0393MultipleAdjacent(t *testing.T) {
Length: 1,
Type: &client.TextEntityTypeItalic{},
},
- }, EntityToXEP0393)
+ }, MarkupModeXEP0393)
if markup != "a*bcd*_e_" {
t.Errorf("Wrong multiple adjacent formatting: %v", markup)
}
@@ -343,7 +343,7 @@ func TestFormattingXEP0393Intersecting(t *testing.T) {
Length: 1,
Type: &client.TextEntityTypeBold{},
},
- }, EntityToXEP0393)
+ }, MarkupModeXEP0393)
if markup != "a*b*_*cd*e_" {
t.Errorf("Wrong intersecting formatting: %v", markup)
}
@@ -361,7 +361,7 @@ func TestFormattingXEP0393InlineCode(t *testing.T) {
Length: 25,
Type: &client.TextEntityTypePre{},
},
- }, EntityToXEP0393)
+ }, MarkupModeXEP0393)
if markup != "Is `Gajim` a thing?\n\n```\necho 'Hello'\necho 'world'\n```\n\nhruck(" {
t.Errorf("Wrong intersecting formatting: %v", markup)
}
@@ -374,7 +374,7 @@ func TestFormattingMarkdownStrikethrough(t *testing.T) {
Length: 3,
Type: &client.TextEntityTypeStrikethrough{},
},
- }, EntityToMarkdown)
+ }, MarkupModeMarkdown)
if markup != "Everyone ~~dis~~likes cake." {
t.Errorf("Wrong strikethrough formatting: %v", markup)
}
@@ -387,14 +387,14 @@ func TestFormattingXEP0393Strikethrough(t *testing.T) {
Length: 3,
Type: &client.TextEntityTypeStrikethrough{},
},
- }, EntityToXEP0393)
+ }, MarkupModeXEP0393)
if markup != "Everyone ~dis~likes cake." {
t.Errorf("Wrong strikethrough formatting: %v", markup)
}
}
func TestClaspLeft(t *testing.T) {
- text := "a b c"
+ text := textToDoubledRunes("a b c")
entities := []*client.TextEntity{
&client.TextEntity{
Offset: 1,
@@ -409,7 +409,7 @@ func TestClaspLeft(t *testing.T) {
}
func TestClaspBoth(t *testing.T) {
- text := "a b c"
+ text := textToDoubledRunes("a b c")
entities := []*client.TextEntity{
&client.TextEntity{
Offset: 1,
@@ -424,7 +424,7 @@ func TestClaspBoth(t *testing.T) {
}
func TestClaspNotNeeded(t *testing.T) {
- text := " abc "
+ text := textToDoubledRunes(" abc ")
entities := []*client.TextEntity{
&client.TextEntity{
Offset: 1,
@@ -439,7 +439,7 @@ func TestClaspNotNeeded(t *testing.T) {
}
func TestClaspNested(t *testing.T) {
- text := "a b c"
+ text := textToDoubledRunes("a b c")
entities := []*client.TextEntity{
&client.TextEntity{
Offset: 1,
@@ -459,7 +459,7 @@ func TestClaspNested(t *testing.T) {
}
func TestClaspEmoji(t *testing.T) {
- text := "a 🐖 c"
+ text := textToDoubledRunes("a 🐖 c")
entities := []*client.TextEntity{
&client.TextEntity{
Offset: 1,
@@ -472,3 +472,111 @@ func TestClaspEmoji(t *testing.T) {
t.Errorf("Wrong claspemoji: %#v", entities)
}
}
+
+func TestNoNewlineBlockquoteXEP0393(t *testing.T) {
+ markup := Format("yes it can i think", []*client.TextEntity{
+ &client.TextEntity{
+ Offset: 4,
+ Length: 6,
+ Type: &client.TextEntityTypeBlockQuote{},
+ },
+ }, MarkupModeXEP0393)
+ if markup != "yes \n> it can\n i think" {
+ t.Errorf("Wrong blockquote formatting: %v", markup)
+ }
+}
+
+func TestNoNewlineBlockquoteMarkdown(t *testing.T) {
+ markup := Format("yes it can i think", []*client.TextEntity{
+ &client.TextEntity{
+ Offset: 4,
+ Length: 6,
+ Type: &client.TextEntityTypeBlockQuote{},
+ },
+ }, MarkupModeMarkdown)
+ if markup != "yes \n> it can\n\n i think" {
+ t.Errorf("Wrong blockquote formatting: %v", markup)
+ }
+}
+
+func TestMultilineBlockquoteXEP0393(t *testing.T) {
+ markup := Format("hruck\npuck\n\nshuck\ntext", []*client.TextEntity{
+ &client.TextEntity{
+ Offset: 0,
+ Length: 17,
+ Type: &client.TextEntityTypeBlockQuote{},
+ },
+ }, MarkupModeXEP0393)
+ if markup != "> hruck\n> puck\n> \n> shuck\ntext" {
+ t.Errorf("Wrong blockquote formatting: %v", markup)
+ }
+}
+
+func TestMultilineBlockquoteMarkdown(t *testing.T) {
+ markup := Format("hruck\npuck\n\nshuck\ntext", []*client.TextEntity{
+ &client.TextEntity{
+ Offset: 0,
+ Length: 17,
+ Type: &client.TextEntityTypeBlockQuote{},
+ },
+ }, MarkupModeMarkdown)
+ if markup != "> hruck\npuck\n\n> shuck\n\ntext" {
+ t.Errorf("Wrong blockquote formatting: %v", markup)
+ }
+}
+
+func TestMixedBlockquoteXEP0393(t *testing.T) {
+ markup := Format("hruck\npuck\nshuck\ntext", []*client.TextEntity{
+ &client.TextEntity{
+ Offset: 0,
+ Length: 16,
+ Type: &client.TextEntityTypeBlockQuote{},
+ },
+ &client.TextEntity{
+ Offset: 0,
+ Length: 16,
+ Type: &client.TextEntityTypeBold{},
+ },
+ &client.TextEntity{
+ Offset: 0,
+ Length: 10,
+ Type: &client.TextEntityTypeItalic{},
+ },
+ &client.TextEntity{
+ Offset: 7,
+ Length: 2,
+ Type: &client.TextEntityTypeStrikethrough{},
+ },
+ }, MarkupModeXEP0393)
+ if markup != "> *_hruck\n> p~uc~k_\n> shuck*\ntext" {
+ t.Errorf("Wrong blockquote formatting: %v", markup)
+ }
+}
+
+func TestMixedBlockquoteMarkdown(t *testing.T) {
+ markup := Format("hruck\npuck\nshuck\ntext", []*client.TextEntity{
+ &client.TextEntity{
+ Offset: 0,
+ Length: 16,
+ Type: &client.TextEntityTypeBlockQuote{},
+ },
+ &client.TextEntity{
+ Offset: 0,
+ Length: 16,
+ Type: &client.TextEntityTypeBold{},
+ },
+ &client.TextEntity{
+ Offset: 0,
+ Length: 10,
+ Type: &client.TextEntityTypeItalic{},
+ },
+ &client.TextEntity{
+ Offset: 7,
+ Length: 2,
+ Type: &client.TextEntityTypeStrikethrough{},
+ },
+ }, MarkupModeMarkdown)
+ if markup != "> **_hruck\np~~uc~~k_\nshuck**\n\ntext" {
+ t.Errorf("Wrong blockquote formatting: %v", markup)
+ }
+}