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
|
import textwrap
import pytest
import pyparsing as pp
ppt = pp.testing
TAB = chr(9)
@pytest.mark.parametrize(
"source, options, expected",
[
# simple call to with_line_numbers
("abcd", {},
textwrap.dedent(
"""\
1
1234567890
1:abcd|
"""),
),
# simple call to with_line_numbers with empty string
("", {}, ""),
# simple call to with_line_numbers with single blank line
("\n", {}, ' \n \n1:|\n'),
# simple call to with_line_numbers with line longer than 99 chars
("abcdefghij" * 11, {},
textwrap.dedent(
"""\
1
1 2 3 4 5 6 7 8 9 0 1
12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
1:abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij|
"""),
),
# add indent = "...."
("abcd", {"indent": "...."},
textwrap.dedent(
"""\
.... 1
.... 1234567890
....1:abcd|
"""),
),
# show control characters as ?
("ab\tc\ad", {"mark_control": "?"},
textwrap.dedent(
f"""\
1 2
12345678901234567890
1:ab c?d|
""")
),
# show control characters as ?
("ab\tc\ad", {"mark_control": "?", "expand_tabs": False},
textwrap.dedent(
f"""\
1
1234567890
1:ab?c?d|
""")
),
# show control characters as unicode
("ab\tc\ad", {"mark_control": "unicode"},
textwrap.dedent(
f"""\
1 2
12345678901234567890
1:ab␠␠␠␠␠␠c␇d␊
""")
),
# show space characters as "`"
("ab\tc d", {"mark_spaces": "`", "expand_tabs": False},
textwrap.dedent(
f"""\
1
1234567890
1:ab\tc``d|
""")
),
# show space characters as unicode
("ab\tc\ad", {"mark_spaces": "unicode", "expand_tabs": False},
textwrap.dedent(
f"""\
1
1234567890
1:ab␉c\ad|
""")
),
]
)
def test_with_line_numbers(source: str, options: dict, expected: str):
observed = ppt.with_line_numbers(source, **options)
print()
print(observed)
assert observed == expected
|