aboutsummaryrefslogtreecommitdiff
path: root/telegram/formatter/formatter_test.go
blob: c988b10c6861676c50e9d542cd9c7df3c9f50601 (plain) (blame)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
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```_🐖_" {
		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)
	}
}

func TestNoFormattingXEP0393(t *testing.T) {
	markup := Format("abc\ndef", []*client.TextEntity{}, EntityToXEP0393)
	if markup != "abc\ndef" {
		t.Errorf("No formatting expected, but: %v", markup)
	}
}

func TestFormattingXEP0393Simple(t *testing.T) {
	markup := Format("👙🐧🐖", []*client.TextEntity{
		&client.TextEntity{
			Offset: 2,
			Length: 4,
			Type:   &client.TextEntityTypeBold{},
		},
	}, EntityToXEP0393)
	if markup != "👙*🐧🐖*" {
		t.Errorf("Wrong simple formatting: %v", markup)
	}
}

func TestFormattingXEP0393Adjacent(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/",
			},
		},
	}, EntityToXEP0393)
	if markup != "a👙_🐧_🐖 <https://narayana.im/>" {
		t.Errorf("Wrong adjacent formatting: %v", markup)
	}
}

func TestFormattingXEP0393AdjacentAndNested(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{},
		},
	}, EntityToXEP0393)
	if markup != "```\n*👙*🐧\n```_🐖_" {
		t.Errorf("Wrong adjacent&nested formatting: %v", markup)
	}
}

func TestFormattingXEP0393AdjacentItalicBoldItalic(t *testing.T) {
	markup := Format("раса двуногих крысолюдей, которую так редко замечают, что многие отрицают само их существование", []*client.TextEntity{
		&client.TextEntity{
			Offset: 0,
			Length: 26,
			Type:   &client.TextEntityTypeItalic{},
		},
		&client.TextEntity{
			Offset: 26,
			Length: 69,
			Type:   &client.TextEntityTypeBold{},
		},
		&client.TextEntity{
			Offset: 26,
			Length: 69,
			Type:   &client.TextEntityTypeItalic{},
		},
	}, EntityToXEP0393)
	if markup != "_раса двуногих крысолюдей, *которую так редко замечают, что многие отрицают само их существование*_" {
		t.Errorf("Wrong adjacent italic/bold-italic formatting: %v", markup)
	}
}

func TestFormattingXEP0393MultipleAdjacent(t *testing.T) {
	markup := Format("abcde", []*client.TextEntity{
		&client.TextEntity{
			Offset: 1,
			Length: 1,
			Type:   &client.TextEntityTypeBold{},
		},
		&client.TextEntity{
			Offset: 2,
			Length: 1,
			Type:   &client.TextEntityTypeBold{},
		},
		&client.TextEntity{
			Offset: 3,
			Length: 1,
			Type:   &client.TextEntityTypeBold{},
		},
		&client.TextEntity{
			Offset: 4,
			Length: 1,
			Type:   &client.TextEntityTypeItalic{},
		},
	}, EntityToXEP0393)
	if markup != "a*bcd*_e_" {
		t.Errorf("Wrong multiple adjacent formatting: %v", markup)
	}
}

func TestFormattingXEP0393Intersecting(t *testing.T) {
	markup := Format("abcde", []*client.TextEntity{
		&client.TextEntity{
			Offset: 1,
			Length: 1,
			Type:   &client.TextEntityTypeBold{},
		},
		&client.TextEntity{
			Offset: 2,
			Length: 3,
			Type:   &client.TextEntityTypeItalic{},
		},
		&client.TextEntity{
			Offset: 2,
			Length: 1,
			Type:   &client.TextEntityTypeBold{},
		},
		&client.TextEntity{
			Offset: 3,
			Length: 1,
			Type:   &client.TextEntityTypeBold{},
		},
	}, EntityToXEP0393)
	if markup != "a*b*_*cd*e_" {
		t.Errorf("Wrong intersecting formatting: %v", markup)
	}
}

func TestFormattingXEP0393InlineCode(t *testing.T) {
	markup := Format("Is Gajim a thing?\n\necho 'Hello'\necho 'world'\n\nhruck(", []*client.TextEntity{
		&client.TextEntity{
			Offset: 3,
			Length: 5,
			Type:   &client.TextEntityTypeCode{},
		},
		&client.TextEntity{
			Offset: 19,
			Length: 25,
			Type:   &client.TextEntityTypePre{},
		},
	}, EntityToXEP0393)
	if markup != "Is `Gajim` a thing?\n\n```\necho 'Hello'\necho 'world'\n```\n\nhruck(" {
		t.Errorf("Wrong intersecting formatting: %v", markup)
	}
}

func TestFormattingMarkdownStrikethrough(t *testing.T) {
	markup := Format("Everyone dislikes cake.", []*client.TextEntity{
		&client.TextEntity{
			Offset: 9,
			Length: 3,
			Type:   &client.TextEntityTypeStrikethrough{},
		},
	}, EntityToMarkdown)
	if markup != "Everyone ~~dis~~likes cake." {
		t.Errorf("Wrong strikethrough formatting: %v", markup)
	}
}

func TestFormattingXEP0393Strikethrough(t *testing.T) {
	markup := Format("Everyone dislikes cake.", []*client.TextEntity{
		&client.TextEntity{
			Offset: 9,
			Length: 3,
			Type:   &client.TextEntityTypeStrikethrough{},
		},
	}, EntityToXEP0393)
	if markup != "Everyone ~dis~likes cake." {
		t.Errorf("Wrong strikethrough formatting: %v", markup)
	}
}