Repository navigation
Expand file tree
/
Copy pathformatting_edge_test.go
More file actions
84 lines (79 loc) · 3.95 KB
/
Copy pathformatting_edge_test.go
File metadata and controls
84 lines (79 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package gotdbot
import (
"reflect"
"strings"
"testing"
"unicode/utf16"
"unicode/utf8"
)
func TestUnparseUnicodeAndNestedEntities(t *testing.T) {
for _, tc := range []struct {
name, text, mode, want string
entities []TextEntity
}{
{"UTF16 emoji", "A😀B", "html", "A<b>😀</b>B", []TextEntity{{Offset: 1, Length: 2, Type: &TextEntityTypeBold{}}}},
{"combining mark", "e\u0301!", "html", "<i>e\u0301</i>!", []TextEntity{{Offset: 0, Length: 2, Type: &TextEntityTypeItalic{}}}},
{"nested", "hello", "html", "<b>h<i>ell</i>o</b>", []TextEntity{{Offset: 0, Length: 5, Type: &TextEntityTypeBold{}}, {Offset: 1, Length: 3, Type: &TextEntityTypeItalic{}}}},
{"same span", "hello", "html", "<b><i>hello</i></b>", []TextEntity{{Offset: 0, Length: 5, Type: &TextEntityTypeBold{}}, {Offset: 0, Length: 5, Type: &TextEntityTypeItalic{}}}},
{"adjacent", "abcd", "html", "<b>ab</b><i>cd</i>", []TextEntity{{Offset: 0, Length: 2, Type: &TextEntityTypeBold{}}, {Offset: 2, Length: 2, Type: &TextEntityTypeItalic{}}}},
{"code escapes", "a`b\\c", "markdownv2", "`a\\`b\\\\c`", []TextEntity{{Offset: 0, Length: 5, Type: &TextEntityTypeCode{}}}},
{"HTML escaping", "<&>", "html", "<b><&></b>", []TextEntity{{Offset: 0, Length: 3, Type: &TextEntityTypeBold{}}}},
} {
t.Run(tc.name, func(t *testing.T) {
before := append([]TextEntity(nil), tc.entities...)
if got := (&FormattedText{Text: tc.text, Entities: tc.entities}).Format(tc.mode); got != tc.want {
t.Fatalf("got %q, want %q", got, tc.want)
}
if !reflect.DeepEqual(before, tc.entities) {
t.Fatal("formatting mutated entities")
}
})
}
}
func TestFormattingConvenienceHelpers(t *testing.T) {
if got := (&FormattedText{Text: "_*[]()!\\"}).Format("MarkdownV2"); got != "\\_\\*\\[\\]\\(\\)\\!\\\\" {
t.Fatalf("Markdown escaping: %q", got)
}
if got := (&User{Id: 42, FirstName: "<name>"}).Mention("HTML"); got != `<a href="tg://user?id=42"><name></a>` {
t.Fatal(got)
}
if got := (&User{Id: 42, FirstName: "a*b"}).Mention("MarkdownV2"); got != `[a\*b](tg://user?id=42)` {
t.Fatal(got)
}
m := &Message{Content: &MessageText{Text: &FormattedText{Text: "hi", Entities: []TextEntity{{Offset: 0, Length: 2, Type: &TextEntityTypeBold{}}}}}}
if m.OriginalHTML() != "<b>hi</b>" || m.OriginalMD() != "*hi*" || m.OriginalMDV2() != "*hi*" {
t.Fatal("original text formatting failed")
}
m.Content = &MessagePhoto{Caption: &FormattedText{Text: "hi", Entities: []TextEntity{{Offset: 0, Length: 2, Type: &TextEntityTypeBold{}}}}}
if m.OriginalCaptionHTML() != "<b>hi</b>" || m.OriginalCaptionMD() != "*hi*" || m.OriginalCaptionMDV2() != "*hi*" {
t.Fatal("original caption formatting failed")
}
}
func TestFormattedTextExplicitEntitiesAndParseMode(t *testing.T) {
c := newTestClient(t)
entities := []TextEntity{{Offset: 0, Length: 2, Type: &TextEntityTypeBold{}}}
c.sendRequest = func(int, []byte) { t.Error("explicit entities/plain text unexpectedly reached TDLib") }
formatted, err := c.GetFormattedText("hi", entities, "invalid mode")
if err != nil || formatted.Text != "hi" || !reflect.DeepEqual(formatted.Entities, entities) {
t.Fatalf("explicit entities = (%v,%v)", formatted, err)
}
formatted, err = c.GetFormattedText("plain", nil, "")
if err != nil || formatted.Text != "plain" {
t.Fatalf("plain = (%v,%v)", formatted, err)
}
}
func FuzzUnparseEntitiesUnicode(f *testing.F) {
for _, text := range []string{"hello", "😀 привет & <world>", "e\u0301", "日本語", "", "a`b\\c"} {
f.Add(text)
}
f.Fuzz(func(t *testing.T, text string) {
if !utf8.ValidString(text) || len(text) > 4096 || text == "" {
return
}
entities := []TextEntity{{Offset: 0, Length: int32(len(utf16.Encode([]rune(text)))), Type: &TextEntityTypeBold{}}}
want := "<b>" + strings.NewReplacer("&", "&", "<", "<", ">", ">").Replace(text) + "</b>"
if got := (&FormattedText{Text: text, Entities: entities}).Format("html"); got != want {
t.Fatalf("Unicode formatting = %q, want %q", got, want)
}
})
}