forked from savely-krasovsky/go-telegram-to-discord-reposter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities_test.go
206 lines (201 loc) · 4.39 KB
/
entities_test.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package tgapi
import (
"testing"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type TestCase struct {
Text string
Entities []tgbotapi.MessageEntity
Expected string
}
func TestEntities(t *testing.T) {
cases := map[string]TestCase{
"italic": {
Text: "\u3297\ufe0f Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "italic",
Offset: 9,
Length: 10,
},
},
Expected: "\u3297\ufe0f Lorem _markdownum_ temptabat",
},
"bold": {
Text: "㊗️ Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "bold",
Offset: 9,
Length: 10,
},
},
Expected: "㊗️ Lorem **markdownum** temptabat",
},
"underline": {
Text: "㊗️ Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "underline",
Offset: 9,
Length: 10,
},
},
Expected: "㊗️ Lorem __markdownum__ temptabat",
},
"strikethrough": {
Text: "㊗️ Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "strikethrough",
Offset: 9,
Length: 10,
},
},
Expected: "㊗️ Lorem ~~markdownum~~ temptabat",
},
"code": {
Text: "㊗️ Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "code",
Offset: 9,
Length: 10,
},
},
Expected: "㊗️ Lorem `markdownum` temptabat",
},
"pre": {
Text: "㊗️ Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "pre",
Offset: 9,
Length: 10,
},
},
Expected: "㊗️ Lorem ```markdownum``` temptabat",
},
"text_link": {
Text: "㊗️ Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "text_link",
Offset: 9,
Length: 10,
URL: "https://example.com/",
},
},
Expected: `㊗️ Lorem [markdownum](https://example.com/ "https://example.com/") temptabat`,
},
"hashtag": {
Text: "㊗️ Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "hashtag",
Offset: 9,
Length: 10,
},
},
Expected: "㊗️ Lorem markdownum temptabat",
},
"url": {
Text: "㊗️ Lorem https://t.me/#foo-bar temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "url",
Offset: 9,
Length: 21,
},
},
Expected: "㊗️ Lorem https://t.me/#foo-bar temptabat",
},
"escape": {
Text: "㊗️ *Lorem* _markdownum_ https://t.me/f?foo ~temptabat~ ![lorem](ipsum) __dolor__ >sit amet",
Entities: []tgbotapi.MessageEntity{
{
Type: "url",
Offset: 24,
Length: 18,
},
},
Expected: "㊗️ \\*Lorem\\* \\_markdownum\\_ https://t.me/f?foo \\~temptabat\\~ \\!\\[lorem\\]\\(ipsum\\) \\_\\_dolor\\_\\_ \\>sit amet",
},
"adjacent": {
Text: "㊗️ Lorem markdownum temptabat",
Entities: []tgbotapi.MessageEntity{
{
Type: "bold",
Offset: 0,
Length: 8,
},
{
Type: "italic",
Offset: 8,
Length: 11,
},
},
Expected: `**㊗️ Lorem**_ markdownum_ temptabat`,
},
"complex": {
Text: "Lorem markdownum _temptabat usus rapta_ superesse uno segetes reponere decens,\n#carinae ~__*quis*__~.",
Entities: []tgbotapi.MessageEntity{
{
Type: "bold",
Offset: 0,
Length: 5,
},
{
Type: "italic",
Offset: 0,
Length: 5,
},
{
Type: "italic",
Offset: 6,
Length: 10,
},
{
Type: "underline",
Offset: 28,
Length: 4,
},
{
Type: "strikethrough",
Offset: 40,
Length: 9,
},
{
Type: "code",
Offset: 50,
Length: 3,
},
{
Type: "text_link",
Offset: 54,
Length: 7,
URL: "https://example.com/",
},
{
Type: "bold",
Offset: 54,
Length: 7,
},
{
Type: "hashtag",
Offset: 81,
Length: 8,
},
},
Expected: "**_Lorem_** _markdownum_ \\_temptabat __usus__ rapta\\_ ~~superesse~~ `uno` [**segetes**](https://example.com/ \"https://example.com/\") reponere decens,\n\\#carinae \\~\\_\\_\\*quis\\*\\_\\_\\~\\.",
},
}
for casename, testcase := range cases {
t.Run(casename, func(t *testing.T) {
actual := EntitiesToDiscordMarkdown(testcase.Text, testcase.Entities)
if actual != testcase.Expected {
t.Fatalf("\nExpected:\n\"%s\"\n\nGot:\n\"%s\"", testcase.Expected, actual)
}
})
}
}