diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2020-01-10 00:16:40 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2020-01-10 00:16:40 +0300 |
commit | 70383bee128b11451211e514200b23f47bf272db (patch) | |
tree | bfc56f26264e206e66e4a6c150f70cf8625e8d66 /telegram/formatter/formatter_test.go | |
parent | b8fcac6ae24dd5e07f366741f0f282f33b18b503 (diff) |
Convert formatting entities to Markdown
Diffstat (limited to 'telegram/formatter/formatter_test.go')
-rw-r--r-- | telegram/formatter/formatter_test.go | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/telegram/formatter/formatter_test.go b/telegram/formatter/formatter_test.go new file mode 100644 index 0000000..63337d6 --- /dev/null +++ b/telegram/formatter/formatter_test.go @@ -0,0 +1,208 @@ +package formatter + +import ( + "testing" + + "github.com/zelenin/go-tdlib/client" +) + +func TestNoFormatting(t *testing.T) { + markup := Format("abc\ndef", []*client.TextEntity{}, EntityToMarkdown) + if markup != "abc\ndef" { + t.Errorf("No formatting expected, but: %v", markup) + } +} + +func TestFormattingSimple(t *testing.T) { + markup := Format("👙🐧🐖", []*client.TextEntity{ + &client.TextEntity{ + Offset: 2, + Length: 4, + Type: &client.TextEntityTypeBold{}, + }, + }, EntityToMarkdown) + if markup != "👙**🐧🐖**" { + t.Errorf("Wrong simple formatting: %v", markup) + } +} + +func TestFormattingAdjacent(t *testing.T) { + markup := Format("a👙🐧🐖", []*client.TextEntity{ + &client.TextEntity{ + Offset: 3, + Length: 2, + Type: &client.TextEntityTypeItalic{}, + }, + &client.TextEntity{ + Offset: 5, + Length: 2, + Type: &client.TextEntityTypeTextUrl{ + Url: "https://narayana.im/", + }, + }, + }, EntityToMarkdown) + if markup != "a👙_🐧_[🐖](https://narayana.im/)" { + t.Errorf("Wrong adjacent formatting: %v", markup) + } +} + +func TestFormattingAdjacentAndNested(t *testing.T) { + markup := Format("👙🐧🐖", []*client.TextEntity{ + &client.TextEntity{ + Offset: 0, + Length: 4, + Type: &client.TextEntityTypePre{}, + }, + &client.TextEntity{ + Offset: 0, + Length: 2, + Type: &client.TextEntityTypeBold{}, + }, + &client.TextEntity{ + Offset: 4, + Length: 2, + Type: &client.TextEntityTypeItalic{}, + }, + }, EntityToMarkdown) + if markup != "\n```\n**👙**🐧\n```\n_🐖_" { + t.Errorf("Wrong adjacent&nested formatting: %v", markup) + } +} + +func TestRebalanceTwoZero(t *testing.T) { + s1 := InsertionStack{ + &Insertion{Offset: 7}, + &Insertion{Offset: 8}, + } + 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) + } +} + +func TestRebalanceNeeded(t *testing.T) { + s1 := InsertionStack{ + &Insertion{Offset: 7}, + &Insertion{Offset: 8}, + } + s2 := InsertionStack{ + &Insertion{Offset: 10}, + &Insertion{Offset: 9}, + } + s1, s2 = s1.rebalance(s2, 9) + if !(len(s1) == 3 && len(s2) == 1 && + s1[0].Offset == 7 && s1[1].Offset == 8 && s1[2].Offset == 9 && + s2[0].Offset == 10) { + t.Errorf("Wrong rebalance when needed: %#v %#v", s1, s2) + } +} + +func TestRebalanceNotNeeded(t *testing.T) { + s1 := InsertionStack{ + &Insertion{Offset: 7}, + &Insertion{Offset: 8}, + } + s2 := InsertionStack{ + &Insertion{Offset: 10}, + &Insertion{Offset: 9}, + } + s1, s2 = s1.rebalance(s2, 8) + if !(len(s1) == 2 && len(s2) == 2 && + s1[0].Offset == 7 && s1[1].Offset == 8 && + s2[0].Offset == 10 && s2[1].Offset == 9) { + t.Errorf("Wrong rebalance when not needed: %#v %#v", s1, s2) + } +} + +func TestRebalanceLate(t *testing.T) { + s1 := InsertionStack{ + &Insertion{Offset: 7}, + &Insertion{Offset: 8}, + } + s2 := InsertionStack{ + &Insertion{Offset: 10}, + &Insertion{Offset: 9}, + } + s1, s2 = s1.rebalance(s2, 10) + if !(len(s1) == 4 && len(s2) == 0 && + s1[0].Offset == 7 && s1[1].Offset == 8 && + s1[2].Offset == 9 && s1[3].Offset == 10) { + t.Errorf("Wrong rebalance when late: %#v %#v", s1, s2) + } +} + +func TestIteratorEmpty(t *testing.T) { + s := InsertionStack{} + g := s.NewIterator() + v := g() + if v != nil { + t.Errorf("Empty iterator should return nil but returned %#v", v) + } +} + +func TestIterator(t *testing.T) { + s := InsertionStack{ + &Insertion{Offset: 7}, + &Insertion{Offset: 8}, + } + g := s.NewIterator() + v := g() + if v == nil || v.Offset != 7 { + t.Errorf("Wrong insertion instead of 7: %#v", v) + } + v = g() + if v == nil || v.Offset != 8 { + t.Errorf("Wrong insertion instead of 8: %#v", v) + } + v = g() + if v != nil { + t.Errorf("nil should be returned after end, %#v instead", v) + } + v = g() + if v != nil { + t.Errorf("Further attempts should return nil too, %#v instead", v) + } +} + +func TestSortEntities(t *testing.T) { + entities := []*client.TextEntity{ + &client.TextEntity{ + Offset: 3, + Length: 2, + }, + &client.TextEntity{ + Offset: 5, + Length: 2, + }, + &client.TextEntity{ + Offset: 7, + Length: 2, + }, + &client.TextEntity{ + Offset: 6, + Length: 1, + }, + &client.TextEntity{ + Offset: 5, + Length: 1, + }, + } + entities = SortEntities(entities) + if !(len(entities) == 5 && + entities[0].Offset == 3 && entities[0].Length == 2 && + entities[1].Offset == 5 && entities[1].Length == 2 && + entities[2].Offset == 5 && entities[2].Length == 1 && + entities[3].Offset == 6 && entities[3].Length == 1 && + entities[4].Offset == 7 && entities[4].Length == 2) { + t.Errorf("Wrong sorting order: %#v", entities) + } +} + +func TestSortEmpty(t *testing.T) { + entities := []*client.TextEntity{} + entities = SortEntities(entities) + if len(entities) != 0 { + t.Errorf("Empty entities set sorting error: %#v", entities) + } +} |