| 1 |
from friendly_traceback import token_utils |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
def check(source): |
| 8 |
tokens = token_utils.tokenize(source) |
| 9 |
new_source = token_utils.untokenize(tokens) |
| 10 |
assert source == new_source |
| 11 |
|
| 12 |
|
| 13 |
def check_lines(source): |
| 14 |
lines = token_utils.get_lines(source) |
| 15 |
tokens = [] |
| 16 |
for line in lines: |
| 17 |
tokens.extend(line) |
| 18 |
assert source == token_utils.untokenize(tokens) |
| 19 |
|
| 20 |
|
| 21 |
def test_untokenize(): |
| 22 |
check( |
| 23 |
''' |
| 24 |
|
| 25 |
def zap(): |
| 26 |
|
| 27 |
"""Hello zap. |
| 28 |
|
| 29 |
"""; 1 |
| 30 |
|
| 31 |
|
| 32 |
x \t= \t\t \t 1 |
| 33 |
|
| 34 |
|
| 35 |
''' |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def test_untokenize_with_tab_indentation(): |
| 40 |
check( |
| 41 |
""" |
| 42 |
if True: |
| 43 |
\tdef zap(): |
| 44 |
\t\tx \t= \t\t \t 1 |
| 45 |
""" |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
def test_untokenize_with_backslash_in_comment(): |
| 50 |
check( |
| 51 |
r''' |
| 52 |
def foo(): |
| 53 |
"""Hello foo.""" |
| 54 |
def zap(): bar(1) # \ |
| 55 |
''' |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def test_untokenize_with_escaped_newline(): |
| 60 |
check( |
| 61 |
r'''def foo(): |
| 62 |
"""Hello foo.""" |
| 63 |
x = \ |
| 64 |
1 |
| 65 |
''' |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
def test_cpython_bug_35107(): |
| 70 |
|
| 71 |
check("#") |
| 72 |
check("#\n") |
| 73 |
|
| 74 |
|
| 75 |
def test_last_line_empty(): |
| 76 |
"""If the last line contains only space characters with no newline |
| 77 |
Python's tokenizer drops this content. To ensure that the |
| 78 |
tokenize-untokenize returns the original value, we have introduced |
| 79 |
a fix in our utility functions""" |
| 80 |
|
| 81 |
source = "a\n " |
| 82 |
source_2 = "a\n\t" |
| 83 |
check(source) |
| 84 |
check(source_2) |
| 85 |
|
| 86 |
check_lines(source) |
| 87 |
check_lines(source_2) |
| 88 |
|
| 89 |
|
| 90 |
source1 = "a = b" |
| 91 |
source2 = "a = b # comment\n" |
| 92 |
source3 = """ |
| 93 |
if True: |
| 94 |
a = b # comment |
| 95 |
""" |
| 96 |
tokens1 = token_utils.tokenize(source1) |
| 97 |
tokens2 = token_utils.tokenize(source2) |
| 98 |
lines3 = token_utils.get_lines(source3) |
| 99 |
|
| 100 |
|
| 101 |
def test_dedent(): |
| 102 |
new_tokens = token_utils.dedent(lines3[2], 4) |
| 103 |
assert new_tokens == tokens2 |
| 104 |
|
| 105 |
|
| 106 |
def test_indent(): |
| 107 |
new_tokens = token_utils.indent(tokens2, 4) |
| 108 |
new_line_a = token_utils.untokenize(new_tokens) |
| 109 |
new_line_b = token_utils.untokenize(lines3[2]) |
| 110 |
assert new_line_a == new_line_b |
| 111 |
|
| 112 |
|
| 113 |
def test_self(): |
| 114 |
with open(__file__, "r") as f: |
| 115 |
source = f.read() |
| 116 |
check(source) |
| 117 |
|
| 118 |
|
| 119 |
def test_find_substring_index(): |
| 120 |
assert token_utils.find_substring_index(source2, source3) == -1 |
| 121 |
assert token_utils.find_substring_index(source3, source2) == 3 |
| 122 |
|
| 123 |
|
| 124 |
def test_immediately_before_and_after(): |
| 125 |
tokens = token_utils.get_significant_tokens("**/ =") |
| 126 |
assert tokens[0].immediately_before(tokens[1]) |
| 127 |
assert tokens[1].immediately_after(tokens[0]) |
| 128 |
assert not tokens[1].immediately_before(tokens[2]) |
| 129 |
assert not tokens[2].immediately_after(tokens[1]) |
| 130 |
|
| 131 |
def test_unclosed_triple_quoted_string(): |
| 132 |
with open("tests/unclosed.txt") as f: |
| 133 |
source = f.read() |
| 134 |
assert token_utils.untokenize(token_utils.tokenize(source)) == source |
| 135 |
|
| 136 |
def test_unterminated_string(): |
| 137 |
|
| 138 |
with open("tests/unterminated.txt") as f: |
| 139 |
source = f.read() |
| 140 |
assert token_utils.untokenize(token_utils.tokenize(source)) == source |
| 141 |
|
| 142 |
|
| 143 |
def test_strip_commment(): |
| 144 |
statement = "if True: # a comment" |
| 145 |
stripped = token_utils.strip_comment(statement) |
| 146 |
assert stripped.strip() == "if True:" |
| 147 |
|
| 148 |
|
| 149 |
def test_invalid_octal(): |
| 150 |
|
| 151 |
check("b = 0o1876 + 0o2") |
| 152 |
check("a = 0o23 + 0O2987") |
| 153 |
|
| 154 |
def test_non_printable_character(): |
| 155 |
check('print\x17("Hello")') |