| 1 |
""" |
| 2 |
Pygments RTF formatter tests |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
:copyright: Copyright 2006-present by the Pygments team, see AUTHORS. |
| 6 |
:license: BSD, see LICENSE for details. |
| 7 |
""" |
| 8 |
|
| 9 |
from io import StringIO |
| 10 |
import itertools |
| 11 |
import re |
| 12 |
import pytest |
| 13 |
|
| 14 |
from pygments.formatters import RtfFormatter |
| 15 |
from pygments.lexers import CppLexer, PythonLexer |
| 16 |
from pygments.lexers.special import TextLexer |
| 17 |
from pygments.style import _ansimap, Style |
| 18 |
from pygments.token import Name, String, Token |
| 19 |
|
| 20 |
foot = r'\par' '\n' r'}' + '\n' |
| 21 |
|
| 22 |
|
| 23 |
def _escape(string): |
| 24 |
return string.replace("\n", r"\n") |
| 25 |
|
| 26 |
|
| 27 |
def _build_message(*args, **kwargs): |
| 28 |
string = kwargs.get('string', None) |
| 29 |
t = _escape(kwargs.get('t', '')) |
| 30 |
expected = _escape(kwargs.get('expected', '')) |
| 31 |
result = _escape(kwargs.get('result', '')) |
| 32 |
|
| 33 |
if string is None: |
| 34 |
string = ("The expected output of '{t}'\n\n" |
| 35 |
"\t\tShould be '{expected}'\n\n" |
| 36 |
"\t\tActually outputs '{result}'\n\n" |
| 37 |
"\t(WARNING: Partial Output of Result!)") |
| 38 |
|
| 39 |
end = -len(_escape(foot)) |
| 40 |
start = end - len(expected) |
| 41 |
|
| 42 |
return string.format(t=t, |
| 43 |
result = result[start:end], |
| 44 |
expected = expected) |
| 45 |
|
| 46 |
|
| 47 |
def format_rtf(t, options=None, lexer=TextLexer): |
| 48 |
if options is None: |
| 49 |
options = {} |
| 50 |
tokensource = lexer().get_tokens(t) |
| 51 |
fmt = RtfFormatter(**options) |
| 52 |
buf = StringIO() |
| 53 |
fmt.format(tokensource, buf) |
| 54 |
result = buf.getvalue() |
| 55 |
buf.close() |
| 56 |
return result |
| 57 |
|
| 58 |
|
| 59 |
def extract_color_table(rtf): |
| 60 |
r""" |
| 61 |
Return af list of \redR\greenG\blueB; color definitions |
| 62 |
extracted from the input (the color table). |
| 63 |
""" |
| 64 |
return re.findall((r"\\red[0-9]{1,3}" |
| 65 |
r"\\green[0-9]{1,3}" |
| 66 |
r"\\blue[0-9]{1,3};"), |
| 67 |
rtf) |
| 68 |
|
| 69 |
|
| 70 |
def test_rtf_header(): |
| 71 |
t = '' |
| 72 |
result = format_rtf(t) |
| 73 |
expected = r'{\rtf1\ansi\uc0' |
| 74 |
msg = (f"RTF documents are expected to start with '{expected}'\n" |
| 75 |
f"\t\tStarts intead with '{result[:len(expected)]}'\n" |
| 76 |
"\t(WARNING: Partial Output of Result!)") |
| 77 |
assert result.startswith(expected), msg |
| 78 |
|
| 79 |
|
| 80 |
def test_rtf_footer(): |
| 81 |
t = '' |
| 82 |
result = format_rtf(t) |
| 83 |
expected = '' |
| 84 |
msg = (f"RTF documents are expected to end with '{_escape(expected)}'\n" |
| 85 |
f"\t\tEnds intead with '{_escape(result[-len(expected):])}'\n" |
| 86 |
"\t(WARNING: Partial Output of Result!)") |
| 87 |
assert result.endswith(expected+foot), msg |
| 88 |
|
| 89 |
|
| 90 |
def test_ascii_characters(): |
| 91 |
t = 'a b c d ~' |
| 92 |
result = format_rtf(t) |
| 93 |
expected = r'a b c d ~' |
| 94 |
msg = _build_message(t=t, result=result, expected=expected) |
| 95 |
assert result.endswith(expected+foot), msg |
| 96 |
|
| 97 |
|
| 98 |
def test_escape_characters(): |
| 99 |
t = '\\ {{' |
| 100 |
result = format_rtf(t) |
| 101 |
expected = r'\\ \{\{' |
| 102 |
msg = _build_message(t=t, result=result, expected=expected) |
| 103 |
assert result.endswith(expected+foot), msg |
| 104 |
|
| 105 |
|
| 106 |
def test_single_characters(): |
| 107 |
t = 'â € ¤ каждой' |
| 108 |
result = format_rtf(t) |
| 109 |
expected = (r'{\u226} {\u8364} {\u164} ' |
| 110 |
r'{\u1082}{\u1072}{\u1078}{\u1076}{\u1086}{\u1081}') |
| 111 |
msg = _build_message(t=t, result=result, expected=expected) |
| 112 |
assert result.endswith(expected+foot), msg |
| 113 |
|
| 114 |
|
| 115 |
def test_double_characters(): |
| 116 |
t = 'က 힣 ↕ ↕︎ 鼖' |
| 117 |
result = format_rtf(t) |
| 118 |
expected = (r'{\u4096} {\u55203} {\u8597} ' |
| 119 |
r'{\u8597}{\u65038} {\u55422}{\u56859}') |
| 120 |
msg = _build_message(t=t, result=result, expected=expected) |
| 121 |
assert result.endswith(expected+foot), msg |
| 122 |
|
| 123 |
|
| 124 |
def test_linenos_all_defaults(): |
| 125 |
t = 'line1\nline2\n' |
| 126 |
options = {} |
| 127 |
result = format_rtf(t, options) |
| 128 |
expected = (r'line1\par' + '\n' |
| 129 |
r'line2\par' + '\n' |
| 130 |
r'}' + '\n') |
| 131 |
msg = _build_message(t=t, result=result, expected=expected) |
| 132 |
assert result.endswith(expected), msg |
| 133 |
|
| 134 |
|
| 135 |
def test_linenos_text(): |
| 136 |
t = 'line1\nline2\n' |
| 137 |
options = dict(linenos=True, lineno_padding=2) |
| 138 |
result = format_rtf(t, options) |
| 139 |
expected = (r'{\cf1 1 }line1\par' + '\n' |
| 140 |
r'{\cf1 2 }line2\par' + '\n' |
| 141 |
r'}' + '\n') |
| 142 |
msg = _build_message(t=t, result=result, expected=expected) |
| 143 |
assert result.endswith(expected), msg |
| 144 |
|
| 145 |
|
| 146 |
def test_linenos_newline_characters(): |
| 147 |
t = r'line1\nline2' + '\n' |
| 148 |
options = dict(linenos=True, lineno_padding=2) |
| 149 |
result = format_rtf(t, options) |
| 150 |
expected = (r'{\cf1 1 }line1\\nline2\par' + '\n' |
| 151 |
r'}' + '\n') |
| 152 |
msg = _build_message(t=t, result=result, expected=expected) |
| 153 |
assert result.endswith(expected), msg |
| 154 |
|
| 155 |
|
| 156 |
def test_linenos_python(): |
| 157 |
class TestStyle(Style): |
| 158 |
name = 'rtf_formatter_test' |
| 159 |
line_number_color = "#ff0000" |
| 160 |
styles = {Token: '', String: '#00ff00', Name: '#0000ff'} |
| 161 |
|
| 162 |
t = r's = "line1\nline2"' + '\n' |
| 163 |
options = dict(linenos=True, lineno_padding=2, style=TestStyle) |
| 164 |
result = format_rtf(t, options, PythonLexer) |
| 165 |
expected = (r'{\rtf1\ansi\uc0\deff0{\fonttbl{\f0\fmodern\fprq1\fcharset0;}}' + '\n' |
| 166 |
r'{\colortbl;' + '\n' |
| 167 |
r'\red255\green0\blue0;' + '\n' |
| 168 |
r'\red0\green255\blue0;' + '\n' |
| 169 |
r'\red0\green0\blue255;' + '\n' |
| 170 |
r'}' + '\n' |
| 171 |
r'\f0\sa0' + '\n' |
| 172 |
r'\dntblnsbdb' + '\n' |
| 173 |
r'{\cf1 1 }{\cf3 s} = {\cf2 "}{\cf2 line1}{\cf2 \\n}{\cf2 line2}{\cf2 "}\par' + '\n' |
| 174 |
r'}' + '\n') |
| 175 |
msg = _build_message(t=t, result=result, expected=expected) |
| 176 |
assert result.endswith(expected), msg |
| 177 |
|
| 178 |
|
| 179 |
def test_linenos_left_padding(): |
| 180 |
t = '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n' |
| 181 |
options = dict(linenos=True, lineno_padding=2) |
| 182 |
result = format_rtf(t, options) |
| 183 |
expected = (r'{\cf1 9 }8\par' + '\n' |
| 184 |
r'{\cf1 10 }9\par' + '\n' |
| 185 |
r'}' + '\n') |
| 186 |
msg = _build_message(t=t, result=result, expected=expected) |
| 187 |
assert result.endswith(expected), msg |
| 188 |
|
| 189 |
|
| 190 |
def test_lineno_padding(): |
| 191 |
t = 'line1\nline2\n' |
| 192 |
options = dict(linenos=True, lineno_padding=3) |
| 193 |
result = format_rtf(t, options) |
| 194 |
expected = (r'{\cf1 1 }line1\par' + '\n' |
| 195 |
r'{\cf1 2 }line2\par' + '\n' |
| 196 |
r'}' + '\n') |
| 197 |
msg = _build_message(t=t, result=result, expected=expected) |
| 198 |
assert result.endswith(expected), msg |
| 199 |
|
| 200 |
|
| 201 |
def test_linenostep(): |
| 202 |
t = 'line1\nline2\nline3\nline4\n' |
| 203 |
options = dict(linenos=True, |
| 204 |
linenostep=2, |
| 205 |
lineno_padding=2) |
| 206 |
result = format_rtf(t, options) |
| 207 |
expected = (r'{\cf1 }line1\par' + '\n' |
| 208 |
r'{\cf1 2 }line2\par' + '\n' |
| 209 |
r'{\cf1 }line3\par' + '\n' |
| 210 |
r'{\cf1 4 }line4\par' + '\n' |
| 211 |
r'}' + '\n') |
| 212 |
msg = _build_message(t=t, result=result, expected=expected) |
| 213 |
assert result.endswith(expected), msg |
| 214 |
|
| 215 |
|
| 216 |
def test_linenostart(): |
| 217 |
t = 'line1\nline2\nline3\nline4\n' |
| 218 |
options = dict(linenos=True, |
| 219 |
linenostart=3, |
| 220 |
lineno_padding=2) |
| 221 |
result = format_rtf(t, options) |
| 222 |
expected = (r'{\cf1 3 }line1\par' + '\n' |
| 223 |
r'{\cf1 4 }line2\par' + '\n' |
| 224 |
r'{\cf1 5 }line3\par' + '\n' |
| 225 |
r'{\cf1 6 }line4\par' + '\n' |
| 226 |
r'}' + '\n') |
| 227 |
msg = _build_message(t=t, result=result, expected=expected) |
| 228 |
assert result.endswith(expected), msg |
| 229 |
|
| 230 |
|
| 231 |
def test_linenostart_left_padding(): |
| 232 |
t = 'line1\nline2\nline3\n' |
| 233 |
options = dict(linenos=True, |
| 234 |
linenostart=98, |
| 235 |
lineno_padding=2) |
| 236 |
result = format_rtf(t, options) |
| 237 |
expected = (r'{\cf1 98 }line1\par' + '\n' |
| 238 |
r'{\cf1 99 }line2\par' + '\n' |
| 239 |
r'{\cf1 100 }line3\par' + '\n' |
| 240 |
r'}' + '\n') |
| 241 |
msg = _build_message(t=t, result=result, expected=expected) |
| 242 |
assert result.endswith(expected), msg |
| 243 |
|
| 244 |
|
| 245 |
def test_linenos_hl_lines(): |
| 246 |
t = 'line1\nline2\nline3\n' |
| 247 |
options = dict(linenos=True, |
| 248 |
hl_lines="2 3", |
| 249 |
lineno_padding=2) |
| 250 |
result = format_rtf(t, options) |
| 251 |
expected = (r'{\cf1 1 }line1\par' + '\n' |
| 252 |
r'{\highlight2 {\cf1 2 }line2\par}' + '\n' |
| 253 |
r'{\highlight2 {\cf1 3 }line3\par}' + '\n' |
| 254 |
r'}' + '\n') |
| 255 |
msg = _build_message(t=t, result=result, expected=expected) |
| 256 |
assert result.endswith(expected), msg |
| 257 |
|
| 258 |
|
| 259 |
def test_linenos_off_hl_lines(): |
| 260 |
t = 'line1\nline2\nline3\n' |
| 261 |
options = dict(linenos=False, |
| 262 |
hl_lines="2 3") |
| 263 |
result = format_rtf(t, options) |
| 264 |
expected = (r'line1\par' + '\n' |
| 265 |
r'{\highlight1 line2\par}' + '\n' |
| 266 |
r'{\highlight1 line3\par}' + '\n' |
| 267 |
r'}' + '\n') |
| 268 |
msg = _build_message(t=t, result=result, expected=expected) |
| 269 |
assert result.endswith(expected), msg |
| 270 |
|
| 271 |
|
| 272 |
def test_hl_linenostart_no_lines_highlighted(): |
| 273 |
t = 'line11\nline12\nline13\n' |
| 274 |
options = dict(linenos=False, |
| 275 |
hl_lines="2 3", |
| 276 |
hl_linenostart=True, |
| 277 |
linenostart=11) |
| 278 |
result = format_rtf(t, options) |
| 279 |
expected = (r'line11\par' + '\n' |
| 280 |
r'line12\par' + '\n' |
| 281 |
r'line13\par' + '\n' |
| 282 |
r'}' + '\n') |
| 283 |
msg = _build_message(t=t, result=result, expected=expected) |
| 284 |
assert result.endswith(expected), msg |
| 285 |
|
| 286 |
|
| 287 |
def test_hl_linenostart_lines_highlighted(): |
| 288 |
t = 'line11\nline12\nline13\n' |
| 289 |
options = dict(linenos=False, |
| 290 |
hl_lines="12 13", |
| 291 |
hl_linenostart=True, |
| 292 |
linenostart=11) |
| 293 |
result = format_rtf(t, options) |
| 294 |
expected = (r'line11\par' + '\n' |
| 295 |
r'{\highlight1 line12\par}' + '\n' |
| 296 |
r'{\highlight1 line13\par}' + '\n' |
| 297 |
r'}' + '\n') |
| 298 |
msg = _build_message(t=t, result=result, expected=expected) |
| 299 |
assert result.endswith(expected), msg |
| 300 |
|
| 301 |
|
| 302 |
def test_lineno_color_style_specify_hex(): |
| 303 |
class TestStyle(Style): |
| 304 |
name = 'rtf_formatter_test' |
| 305 |
line_number_color = "#123456" |
| 306 |
|
| 307 |
t = 'line1\nline2\n' |
| 308 |
options = dict(linenos=True, |
| 309 |
style=TestStyle) |
| 310 |
result = format_rtf(t, options) |
| 311 |
rtf_color_str = RtfFormatter.hex_to_rtf_color(TestStyle.line_number_color) |
| 312 |
color_tbl = extract_color_table(result) |
| 313 |
msg = (f"Color table {color_tbl} " |
| 314 |
f"should have '{rtf_color_str}' " |
| 315 |
"as first entry") |
| 316 |
|
| 317 |
# With linenos=True the color table should contain: |
| 318 |
# 1st entry: line number color (hence \cf1) |
| 319 |
assert color_tbl[0] == rtf_color_str, msg |
| 320 |
|
| 321 |
|
| 322 |
def test_lineno_color_style_specify_inherit(): |
| 323 |
class TestStyle(Style): |
| 324 |
name = 'rtf_formatter_test' |
| 325 |
line_number_color = "inherit" # Default from pygments/style.py |
| 326 |
|
| 327 |
t = 'line1\nline2\n' |
| 328 |
options = dict(linenos=True, |
| 329 |
style=TestStyle) |
| 330 |
result = format_rtf(t, options) |
| 331 |
rtf_color_str = RtfFormatter.hex_to_rtf_color(_ansimap['ansibrightblack']) |
| 332 |
color_tbl = extract_color_table(result) |
| 333 |
msg = (f"Color table {color_tbl} " |
| 334 |
f"should have '{rtf_color_str}' " |
| 335 |
"as first entry") |
| 336 |
|
| 337 |
# With linenos=True the color table should contain: |
| 338 |
# 1st entry: line number color (hence \cf1) |
| 339 |
assert color_tbl[0] == rtf_color_str, msg |
| 340 |
|
| 341 |
|
| 342 |
def test_lineno_color_from_cli_option(): |
| 343 |
class TestStyle(Style): |
| 344 |
name = 'rtf_formatter_test' |
| 345 |
line_number_color = "#123456" # Default from pygments/style.py |
| 346 |
|
| 347 |
option_color = "112233" |
| 348 |
t = 'line1\nline2\n' |
| 349 |
options = dict(linenos=True, |
| 350 |
style=TestStyle, |
| 351 |
lineno_color=option_color) |
| 352 |
result = format_rtf(t, options) |
| 353 |
rtf_color_str = RtfFormatter.hex_to_rtf_color(option_color) |
| 354 |
color_tbl = extract_color_table(result) |
| 355 |
msg = (f"Color table {color_tbl} " |
| 356 |
f"should have '{rtf_color_str}' " |
| 357 |
"as first entry") |
| 358 |
|
| 359 |
# With linenos=True the color table should contain: |
| 360 |
# 1st entry: line number color (hence \cf1) |
| 361 |
assert color_tbl[0] == rtf_color_str, msg |
| 362 |
|
| 363 |
|
| 364 |
def test_hl_color_style(): |
| 365 |
class TestStyle(Style): |
| 366 |
name = 'rtf_formatter_test' |
| 367 |
line_number_color = "#123456" |
| 368 |
highlight_color = "#abcdef" |
| 369 |
|
| 370 |
t = 'line1\nline2\n' |
| 371 |
options = dict(linenos=True, |
| 372 |
lineno_padding=2, |
| 373 |
style=TestStyle, |
| 374 |
hl_lines="1 2") |
| 375 |
result = format_rtf(t, options) |
| 376 |
|
| 377 |
rtf_color = RtfFormatter.hex_to_rtf_color(TestStyle.highlight_color) |
| 378 |
|
| 379 |
color_tbl = extract_color_table(result) |
| 380 |
msg = (f"Color table {color_tbl} " |
| 381 |
f"should have '{rtf_color}' " |
| 382 |
"as second entry") |
| 383 |
|
| 384 |
# With linenos=True and hl_lines="1 2" the color table should contain: |
| 385 |
# 1st entry: line number color (hence \cf1) |
| 386 |
# 2nd entry: highlight color (hence \highlight2) |
| 387 |
assert color_tbl[1] == rtf_color, msg |
| 388 |
|
| 389 |
expected = (r'{\highlight2 {\cf1 1 }line1\par}' + '\n' |
| 390 |
r'{\highlight2 {\cf1 2 }line2\par}' + '\n' |
| 391 |
r'}' + '\n') |
| 392 |
msg = _build_message(t=t, result=result, expected=expected) |
| 393 |
|
| 394 |
assert result.endswith(expected), msg |
| 395 |
|
| 396 |
|
| 397 |
def test_hl_color_style_no_linenos(): |
| 398 |
class TestStyle(Style): |
| 399 |
name = 'rtf_formatter_test' |
| 400 |
line_number_color = "#123456" |
| 401 |
highlight_color = "#abcdef" |
| 402 |
|
| 403 |
t = 'line1\nline2\n' |
| 404 |
options = dict(linenos=False, |
| 405 |
style=TestStyle, |
| 406 |
hl_lines="1 2") |
| 407 |
result = format_rtf(t, options) |
| 408 |
|
| 409 |
rtf_color = RtfFormatter.hex_to_rtf_color(TestStyle.highlight_color) |
| 410 |
|
| 411 |
color_tbl = extract_color_table(result) |
| 412 |
msg = (f"Color table {color_tbl} " |
| 413 |
f"should have '{rtf_color}' " |
| 414 |
"as second entry") |
| 415 |
|
| 416 |
# With linenos=False and hl_lines="1 2" the color table should contain: |
| 417 |
# 1st entry: highlight color (hence \highlight1) |
| 418 |
assert rtf_color in color_tbl and color_tbl[0] == rtf_color, msg |
| 419 |
|
| 420 |
expected = (r'{\highlight1 line1\par}' + '\n' |
| 421 |
r'{\highlight1 line2\par}' + '\n' |
| 422 |
r'}' + '\n') |
| 423 |
msg = _build_message(t=t, result=result, expected=expected) |
| 424 |
|
| 425 |
assert result.endswith(expected), msg |
| 426 |
|
| 427 |
|
| 428 |
def test_hl_color_option(): |
| 429 |
class TestStyle(Style): |
| 430 |
name = 'rtf_formatter_test' |
| 431 |
line_number_color = "#123456" |
| 432 |
highlight_color = "#abcdef" |
| 433 |
|
| 434 |
t = 'line1\nline2\n' |
| 435 |
hl_color = "aabbcc" |
| 436 |
options = dict(linenos=False, |
| 437 |
style=TestStyle, |
| 438 |
hl_lines="1 2", |
| 439 |
hl_color=hl_color) |
| 440 |
result = format_rtf(t, options) |
| 441 |
|
| 442 |
rtf_color = RtfFormatter.hex_to_rtf_color(hl_color) |
| 443 |
|
| 444 |
color_tbl = extract_color_table(result) |
| 445 |
msg = (f"Color table {color_tbl} " |
| 446 |
f"should have '{rtf_color}' " |
| 447 |
"as second entry") |
| 448 |
|
| 449 |
# With linenos=False and hl_lines="1 2" the color table should contain: |
| 450 |
# 1st entry: highlight color (hence \highlight1) |
| 451 |
assert rtf_color in color_tbl and color_tbl[0] == rtf_color, msg |
| 452 |
|
| 453 |
expected = (r'{\highlight1 line1\par}' + '\n' |
| 454 |
r'{\highlight1 line2\par}' + '\n' |
| 455 |
r'}' + '\n') |
| 456 |
msg = _build_message(t=t, result=result, expected=expected) |
| 457 |
|
| 458 |
assert result.endswith(expected), msg |
| 459 |
|
| 460 |
|
| 461 |
def test_all_options(): |
| 462 |
# Test if all combinations of options (given values and defaults) |
| 463 |
# produce output: |
| 464 |
# |
| 465 |
# - No uncaught exceptions |
| 466 |
# - Output contains one \par control word per input line |
| 467 |
|
| 468 |
def get_option_combinations(options): |
| 469 |
for _, values in options.items(): |
| 470 |
values.append('default') |
| 471 |
# https://stackoverflow.com/a/40623158 |
| 472 |
combinations = (dict(zip(options.keys(), x)) |
| 473 |
for x in itertools.product(*options.values())) |
| 474 |
for c in combinations: |
| 475 |
yield {opt:val for opt,val in c.items() if val!='default'} |
| 476 |
|
| 477 |
options = {'linenos': [True], |
| 478 |
'lineno_fontsize': [36], |
| 479 |
'fontsize': [36], |
| 480 |
'lineno_padding': [4], |
| 481 |
'linenostart': [10], |
| 482 |
'linenostep': [3], |
| 483 |
'lineno_color': ['ff0000'], |
| 484 |
'hl_lines': ['2'], |
| 485 |
'hl_linenostart': [True], |
| 486 |
'hl_color': ['00ff00'] |
| 487 |
} |
| 488 |
|
| 489 |
t_cpp = [r'#include <iostream>', |
| 490 |
r'int main(int argc, char** argv) {', |
| 491 |
r' /* Multi-line comment', |
| 492 |
r' with \n escape sequence */' |
| 493 |
r' for (int i = 0; i < argc; i++){', |
| 494 |
r' std::cout << i << ": " << argv[i] << "\n";', |
| 495 |
r' }', |
| 496 |
r' return 0;', |
| 497 |
r'}' |
| 498 |
] |
| 499 |
|
| 500 |
t_python = [r'# Description of program', |
| 501 |
r'def add(a, b):', |
| 502 |
r' """ Add numbers a and b.', |
| 503 |
r' Newline \n in docstring."""', |
| 504 |
r' return a+b', |
| 505 |
r'if __name__ == "__main__":', |
| 506 |
r'result = add(2,2)', |
| 507 |
r'print(f"Result:\n{result}")' |
| 508 |
] |
| 509 |
|
| 510 |
t_text = [r'Header1;"Long', |
| 511 |
r'Header2";Header3', |
| 512 |
r'1,2;Single Line;20/02/2024', |
| 513 |
r'1,3;"Multiple', |
| 514 |
r'Lines";21/02/2024'] |
| 515 |
|
| 516 |
|
| 517 |
for opts in get_option_combinations(options): |
| 518 |
|
| 519 |
opt_strs = '\n'.join([f"{k}: {v}" for k,v in opts.items()]) |
| 520 |
opt_str_for_copying = "-O " + ",".join([f"{k}={v}" for k,v in opts.items()]) |
| 521 |
|
| 522 |
for t, lexer in [(t_cpp, CppLexer), |
| 523 |
(t_python, PythonLexer), |
| 524 |
(t_text, TextLexer)]: |
| 525 |
|
| 526 |
input_text = '\n'.join(t) + '\n' # Last line should end in \n |
| 527 |
|
| 528 |
try: |
| 529 |
result = format_rtf(input_text, opts,lexer) |
| 530 |
except Exception as e: |
| 531 |
msg = (f"RTF-formatting caused an exception with options\n\n" |
| 532 |
f"{opt_strs}\n\n" |
| 533 |
f"{opt_str_for_copying}\n\n" |
| 534 |
f"Lexer: {lexer.__name__}\n\n" |
| 535 |
f"Input:\n" |
| 536 |
f"{input_text}\n" |
| 537 |
f"{type(e)}: {e}\n") |
| 538 |
|
| 539 |
pytest.fail(msg) |
| 540 |
|
| 541 |
num_input_lines = len(t) |
| 542 |
num_of_pars = result.count(r'\par') |
| 543 |
|
| 544 |
msg = (f"Different of number of input lines and formatted lines:\n" |
| 545 |
f"{opt_strs}\n\n" |
| 546 |
f"{opt_str_for_copying}\n\n" |
| 547 |
f"\\par control words: {num_of_pars}\n" |
| 548 |
f"Input lines: {num_input_lines}\n\n" |
| 549 |
f"Input:\n" |
| 550 |
f"{input_text}\n") |
| 551 |
|
| 552 |
assert num_of_pars == num_input_lines, msg |