-
Notifications
You must be signed in to change notification settings - Fork 632
/
test_rtf_formatter.py
552 lines (445 loc) · 18 KB
/
test_rtf_formatter.py
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
"""
Pygments RTF formatter tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from io import StringIO
import itertools
import re
import pytest
from pygments.formatters import RtfFormatter
from pygments.lexers import CppLexer, PythonLexer
from pygments.lexers.special import TextLexer
from pygments.style import _ansimap, Style
from pygments.token import Name, String, Token
foot = r'\par' '\n' r'}' + '\n'
def _escape(string):
return string.replace("\n", r"\n")
def _build_message(*args, **kwargs):
string = kwargs.get('string', None)
t = _escape(kwargs.get('t', ''))
expected = _escape(kwargs.get('expected', ''))
result = _escape(kwargs.get('result', ''))
if string is None:
string = ("The expected output of '{t}'\n\n"
"\t\tShould be '{expected}'\n\n"
"\t\tActually outputs '{result}'\n\n"
"\t(WARNING: Partial Output of Result!)")
end = -len(_escape(foot))
start = end - len(expected)
return string.format(t=t,
result = result[start:end],
expected = expected)
def format_rtf(t, options=None, lexer=TextLexer):
if options is None:
options = {}
tokensource = lexer().get_tokens(t)
fmt = RtfFormatter(**options)
buf = StringIO()
fmt.format(tokensource, buf)
result = buf.getvalue()
buf.close()
return result
def extract_color_table(rtf):
r"""
Return af list of \redR\greenG\blueB; color definitions
extracted from the input (the color table).
"""
return re.findall((r"\\red[0-9]{1,3}"
r"\\green[0-9]{1,3}"
r"\\blue[0-9]{1,3};"),
rtf)
def test_rtf_header():
t = ''
result = format_rtf(t)
expected = r'{\rtf1\ansi\uc0'
msg = (f"RTF documents are expected to start with '{expected}'\n"
f"\t\tStarts intead with '{result[:len(expected)]}'\n"
"\t(WARNING: Partial Output of Result!)")
assert result.startswith(expected), msg
def test_rtf_footer():
t = ''
result = format_rtf(t)
expected = ''
msg = (f"RTF documents are expected to end with '{_escape(expected)}'\n"
f"\t\tEnds intead with '{_escape(result[-len(expected):])}'\n"
"\t(WARNING: Partial Output of Result!)")
assert result.endswith(expected+foot), msg
def test_ascii_characters():
t = 'a b c d ~'
result = format_rtf(t)
expected = r'a b c d ~'
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected+foot), msg
def test_escape_characters():
t = '\\ {{'
result = format_rtf(t)
expected = r'\\ \{\{'
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected+foot), msg
def test_single_characters():
t = 'â € ¤ каждой'
result = format_rtf(t)
expected = (r'{\u226} {\u8364} {\u164} '
r'{\u1082}{\u1072}{\u1078}{\u1076}{\u1086}{\u1081}')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected+foot), msg
def test_double_characters():
t = 'က 힣 ↕ ↕︎ 鼖'
result = format_rtf(t)
expected = (r'{\u4096} {\u55203} {\u8597} '
r'{\u8597}{\u65038} {\u55422}{\u56859}')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected+foot), msg
def test_linenos_all_defaults():
t = 'line1\nline2\n'
options = {}
result = format_rtf(t, options)
expected = (r'line1\par' + '\n'
r'line2\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenos_text():
t = 'line1\nline2\n'
options = dict(linenos=True, lineno_padding=2)
result = format_rtf(t, options)
expected = (r'{\cf1 1 }line1\par' + '\n'
r'{\cf1 2 }line2\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenos_newline_characters():
t = r'line1\nline2' + '\n'
options = dict(linenos=True, lineno_padding=2)
result = format_rtf(t, options)
expected = (r'{\cf1 1 }line1\\nline2\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenos_python():
class TestStyle(Style):
name = 'rtf_formatter_test'
line_number_color = "#ff0000"
styles = {Token: '', String: '#00ff00', Name: '#0000ff'}
t = r's = "line1\nline2"' + '\n'
options = dict(linenos=True, lineno_padding=2, style=TestStyle)
result = format_rtf(t, options, PythonLexer)
expected = (r'{\rtf1\ansi\uc0\deff0{\fonttbl{\f0\fmodern\fprq1\fcharset0;}}' + '\n'
r'{\colortbl;' + '\n'
r'\red255\green0\blue0;' + '\n'
r'\red0\green255\blue0;' + '\n'
r'\red0\green0\blue255;' + '\n'
r'}' + '\n'
r'\f0\sa0' + '\n'
r'\dntblnsbdb' + '\n'
r'{\cf1 1 }{\cf3 s} = {\cf2 "}{\cf2 line1}{\cf2 \\n}{\cf2 line2}{\cf2 "}\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenos_left_padding():
t = '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n'
options = dict(linenos=True, lineno_padding=2)
result = format_rtf(t, options)
expected = (r'{\cf1 9 }8\par' + '\n'
r'{\cf1 10 }9\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_lineno_padding():
t = 'line1\nline2\n'
options = dict(linenos=True, lineno_padding=3)
result = format_rtf(t, options)
expected = (r'{\cf1 1 }line1\par' + '\n'
r'{\cf1 2 }line2\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenostep():
t = 'line1\nline2\nline3\nline4\n'
options = dict(linenos=True,
linenostep=2,
lineno_padding=2)
result = format_rtf(t, options)
expected = (r'{\cf1 }line1\par' + '\n'
r'{\cf1 2 }line2\par' + '\n'
r'{\cf1 }line3\par' + '\n'
r'{\cf1 4 }line4\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenostart():
t = 'line1\nline2\nline3\nline4\n'
options = dict(linenos=True,
linenostart=3,
lineno_padding=2)
result = format_rtf(t, options)
expected = (r'{\cf1 3 }line1\par' + '\n'
r'{\cf1 4 }line2\par' + '\n'
r'{\cf1 5 }line3\par' + '\n'
r'{\cf1 6 }line4\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenostart_left_padding():
t = 'line1\nline2\nline3\n'
options = dict(linenos=True,
linenostart=98,
lineno_padding=2)
result = format_rtf(t, options)
expected = (r'{\cf1 98 }line1\par' + '\n'
r'{\cf1 99 }line2\par' + '\n'
r'{\cf1 100 }line3\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenos_hl_lines():
t = 'line1\nline2\nline3\n'
options = dict(linenos=True,
hl_lines="2 3",
lineno_padding=2)
result = format_rtf(t, options)
expected = (r'{\cf1 1 }line1\par' + '\n'
r'{\highlight2 {\cf1 2 }line2\par}' + '\n'
r'{\highlight2 {\cf1 3 }line3\par}' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_linenos_off_hl_lines():
t = 'line1\nline2\nline3\n'
options = dict(linenos=False,
hl_lines="2 3")
result = format_rtf(t, options)
expected = (r'line1\par' + '\n'
r'{\highlight1 line2\par}' + '\n'
r'{\highlight1 line3\par}' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_hl_linenostart_no_lines_highlighted():
t = 'line11\nline12\nline13\n'
options = dict(linenos=False,
hl_lines="2 3",
hl_linenostart=True,
linenostart=11)
result = format_rtf(t, options)
expected = (r'line11\par' + '\n'
r'line12\par' + '\n'
r'line13\par' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_hl_linenostart_lines_highlighted():
t = 'line11\nline12\nline13\n'
options = dict(linenos=False,
hl_lines="12 13",
hl_linenostart=True,
linenostart=11)
result = format_rtf(t, options)
expected = (r'line11\par' + '\n'
r'{\highlight1 line12\par}' + '\n'
r'{\highlight1 line13\par}' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_lineno_color_style_specify_hex():
class TestStyle(Style):
name = 'rtf_formatter_test'
line_number_color = "#123456"
t = 'line1\nline2\n'
options = dict(linenos=True,
style=TestStyle)
result = format_rtf(t, options)
rtf_color_str = RtfFormatter.hex_to_rtf_color(TestStyle.line_number_color)
color_tbl = extract_color_table(result)
msg = (f"Color table {color_tbl} "
f"should have '{rtf_color_str}' "
"as first entry")
# With linenos=True the color table should contain:
# 1st entry: line number color (hence \cf1)
assert color_tbl[0] == rtf_color_str, msg
def test_lineno_color_style_specify_inherit():
class TestStyle(Style):
name = 'rtf_formatter_test'
line_number_color = "inherit" # Default from pygments/style.py
t = 'line1\nline2\n'
options = dict(linenos=True,
style=TestStyle)
result = format_rtf(t, options)
rtf_color_str = RtfFormatter.hex_to_rtf_color(_ansimap['ansibrightblack'])
color_tbl = extract_color_table(result)
msg = (f"Color table {color_tbl} "
f"should have '{rtf_color_str}' "
"as first entry")
# With linenos=True the color table should contain:
# 1st entry: line number color (hence \cf1)
assert color_tbl[0] == rtf_color_str, msg
def test_lineno_color_from_cli_option():
class TestStyle(Style):
name = 'rtf_formatter_test'
line_number_color = "#123456" # Default from pygments/style.py
option_color = "112233"
t = 'line1\nline2\n'
options = dict(linenos=True,
style=TestStyle,
lineno_color=option_color)
result = format_rtf(t, options)
rtf_color_str = RtfFormatter.hex_to_rtf_color(option_color)
color_tbl = extract_color_table(result)
msg = (f"Color table {color_tbl} "
f"should have '{rtf_color_str}' "
"as first entry")
# With linenos=True the color table should contain:
# 1st entry: line number color (hence \cf1)
assert color_tbl[0] == rtf_color_str, msg
def test_hl_color_style():
class TestStyle(Style):
name = 'rtf_formatter_test'
line_number_color = "#123456"
highlight_color = "#abcdef"
t = 'line1\nline2\n'
options = dict(linenos=True,
lineno_padding=2,
style=TestStyle,
hl_lines="1 2")
result = format_rtf(t, options)
rtf_color = RtfFormatter.hex_to_rtf_color(TestStyle.highlight_color)
color_tbl = extract_color_table(result)
msg = (f"Color table {color_tbl} "
f"should have '{rtf_color}' "
"as second entry")
# With linenos=True and hl_lines="1 2" the color table should contain:
# 1st entry: line number color (hence \cf1)
# 2nd entry: highlight color (hence \highlight2)
assert color_tbl[1] == rtf_color, msg
expected = (r'{\highlight2 {\cf1 1 }line1\par}' + '\n'
r'{\highlight2 {\cf1 2 }line2\par}' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_hl_color_style_no_linenos():
class TestStyle(Style):
name = 'rtf_formatter_test'
line_number_color = "#123456"
highlight_color = "#abcdef"
t = 'line1\nline2\n'
options = dict(linenos=False,
style=TestStyle,
hl_lines="1 2")
result = format_rtf(t, options)
rtf_color = RtfFormatter.hex_to_rtf_color(TestStyle.highlight_color)
color_tbl = extract_color_table(result)
msg = (f"Color table {color_tbl} "
f"should have '{rtf_color}' "
"as second entry")
# With linenos=False and hl_lines="1 2" the color table should contain:
# 1st entry: highlight color (hence \highlight1)
assert rtf_color in color_tbl and color_tbl[0] == rtf_color, msg
expected = (r'{\highlight1 line1\par}' + '\n'
r'{\highlight1 line2\par}' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_hl_color_option():
class TestStyle(Style):
name = 'rtf_formatter_test'
line_number_color = "#123456"
highlight_color = "#abcdef"
t = 'line1\nline2\n'
hl_color = "aabbcc"
options = dict(linenos=False,
style=TestStyle,
hl_lines="1 2",
hl_color=hl_color)
result = format_rtf(t, options)
rtf_color = RtfFormatter.hex_to_rtf_color(hl_color)
color_tbl = extract_color_table(result)
msg = (f"Color table {color_tbl} "
f"should have '{rtf_color}' "
"as second entry")
# With linenos=False and hl_lines="1 2" the color table should contain:
# 1st entry: highlight color (hence \highlight1)
assert rtf_color in color_tbl and color_tbl[0] == rtf_color, msg
expected = (r'{\highlight1 line1\par}' + '\n'
r'{\highlight1 line2\par}' + '\n'
r'}' + '\n')
msg = _build_message(t=t, result=result, expected=expected)
assert result.endswith(expected), msg
def test_all_options():
# Test if all combinations of options (given values and defaults)
# produce output:
#
# - No uncaught exceptions
# - Output contains one \par control word per input line
def get_option_combinations(options):
for _, values in options.items():
values.append('default')
# https://stackoverflow.com/a/40623158
combinations = (dict(zip(options.keys(), x))
for x in itertools.product(*options.values()))
for c in combinations:
yield {opt:val for opt,val in c.items() if val!='default'}
options = {'linenos': [True],
'lineno_fontsize': [36],
'fontsize': [36],
'lineno_padding': [4],
'linenostart': [10],
'linenostep': [3],
'lineno_color': ['ff0000'],
'hl_lines': ['2'],
'hl_linenostart': [True],
'hl_color': ['00ff00']
}
t_cpp = [r'#include <iostream>',
r'int main(int argc, char** argv) {',
r' /* Multi-line comment',
r' with \n escape sequence */'
r' for (int i = 0; i < argc; i++){',
r' std::cout << i << ": " << argv[i] << "\n";',
r' }',
r' return 0;',
r'}'
]
t_python = [r'# Description of program',
r'def add(a, b):',
r' """ Add numbers a and b.',
r' Newline \n in docstring."""',
r' return a+b',
r'if __name__ == "__main__":',
r'result = add(2,2)',
r'print(f"Result:\n{result}")'
]
t_text = [r'Header1;"Long',
r'Header2";Header3',
r'1,2;Single Line;20/02/2024',
r'1,3;"Multiple',
r'Lines";21/02/2024']
for opts in get_option_combinations(options):
opt_strs = '\n'.join([f"{k}: {v}" for k,v in opts.items()])
opt_str_for_copying = "-O " + ",".join([f"{k}={v}" for k,v in opts.items()])
for t, lexer in [(t_cpp, CppLexer),
(t_python, PythonLexer),
(t_text, TextLexer)]:
input_text = '\n'.join(t) + '\n' # Last line should end in \n
try:
result = format_rtf(input_text, opts,lexer)
except Exception as e:
msg = (f"RTF-formatting caused an exception with options\n\n"
f"{opt_strs}\n\n"
f"{opt_str_for_copying}\n\n"
f"Lexer: {lexer.__name__}\n\n"
f"Input:\n"
f"{input_text}\n"
f"{type(e)}: {e}\n")
pytest.fail(msg)
num_input_lines = len(t)
num_of_pars = result.count(r'\par')
msg = (f"Different of number of input lines and formatted lines:\n"
f"{opt_strs}\n\n"
f"{opt_str_for_copying}\n\n"
f"\\par control words: {num_of_pars}\n"
f"Input lines: {num_input_lines}\n\n"
f"Input:\n"
f"{input_text}\n")
assert num_of_pars == num_input_lines, msg