| 1 |
"""Tests for tokenutil""" |
| 2 |
|
| 3 |
# Copyright (c) IPython Development Team. |
| 4 |
# Distributed under the terms of the Modified BSD License. |
| 5 |
|
| 6 |
import pytest |
| 7 |
import textwrap |
| 8 |
|
| 9 |
from IPython.utils.tokenutil import token_at_cursor, line_at_cursor |
| 10 |
|
| 11 |
|
| 12 |
def expect_token(expected: str, cell: str, cursor_pos: int | None = None) -> None: |
| 13 |
""" |
| 14 |
If cursor_pos is `None`, look for `|` as the cursor position. |
| 15 |
Assert that the token at the cursor position is `expected`. |
| 16 |
""" |
| 17 |
if cursor_pos is None: |
| 18 |
assert ( |
| 19 |
cursor_count := cell.count("|") |
| 20 |
) == 1, ( |
| 21 |
f"Cursor position not specified and found {cursor_count} instance(s) of '|'" |
| 22 |
) |
| 23 |
cursor_pos = cell.index("|") |
| 24 |
cell = cell.replace("|", "") |
| 25 |
token = token_at_cursor(cell, cursor_pos) |
| 26 |
offset = 0 |
| 27 |
for line in cell.splitlines(): |
| 28 |
if offset + len(line) >= cursor_pos: |
| 29 |
break |
| 30 |
else: |
| 31 |
offset += len(line) + 1 |
| 32 |
column = cursor_pos - offset |
| 33 |
line_with_cursor = "%s|%s" % (line[:column], line[column:]) |
| 34 |
assert token == expected, "Expected %r, got %r in: %r (pos %i)" % ( |
| 35 |
expected, |
| 36 |
token, |
| 37 |
line_with_cursor, |
| 38 |
cursor_pos, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def test_simple(): |
| 43 |
cell = "foo" |
| 44 |
for i in range(len(cell)): |
| 45 |
expect_token("foo", cell, i) |
| 46 |
|
| 47 |
|
| 48 |
def test_function(): |
| 49 |
cell = "foo(a=5, b='10')" |
| 50 |
for i in range(len(cell)): |
| 51 |
expect_token("foo", cell, i) |
| 52 |
expect_token("a", "foo(|a, b)") |
| 53 |
expect_token("foo", "foo(a|, b)") |
| 54 |
expect_token("foo", "foo(a,| b)") |
| 55 |
expect_token("b", "foo(a, |b)") |
| 56 |
expect_token("foo", "foo(a, b|)") |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize( |
| 60 |
"cell", |
| 61 |
[ |
| 62 |
"\n".join(["a = 5", "b = hello('string', there)"]), |
| 63 |
textwrap.dedent( |
| 64 |
''' |
| 65 |
"""\n\nxxxxxxxxxx\n\n""" |
| 66 |
5, """ |
| 67 |
"""\n\nxxxxxxxxxx\n\n""" |
| 68 |
docstring |
| 69 |
multiline token |
| 70 |
""", [ |
| 71 |
2, 3, "complicated"] |
| 72 |
b = hello("string", there) |
| 73 |
''' |
| 74 |
), |
| 75 |
], |
| 76 |
) |
| 77 |
def test_multiline(cell): |
| 78 |
expected = "hello" |
| 79 |
start = cell.index(expected) + 1 |
| 80 |
for i in range(start, start + len(expected)): |
| 81 |
expect_token(expected, cell, i) |
| 82 |
start = cell.index(expected) + 1 |
| 83 |
for i in range(start, start + len(expected)): |
| 84 |
expect_token(expected, cell, i) |
| 85 |
|
| 86 |
|
| 87 |
def test_nested_call_kwargs(): |
| 88 |
cell = "foo(bar(a=5), b=10)" |
| 89 |
start = cell.index("bar") |
| 90 |
last = start + len("bar") |
| 91 |
for i in range(start, last + 1): |
| 92 |
expect_token("bar", cell, i) |
| 93 |
start = cell.index("a=") |
| 94 |
for i in range(start, start + 3): |
| 95 |
expect_token("bar", cell, i) |
| 96 |
expect_token("bar", "foo(bar(a=|5), b=10)") |
| 97 |
expect_token("bar", "foo(bar(a=5|), b=10)") |
| 98 |
for i in range(cell.index(")") + 1, cell.index("b=") + 2): |
| 99 |
expect_token("foo", cell, i) |
| 100 |
expect_token("foo", cell, len(cell) - 1) |
| 101 |
|
| 102 |
|
| 103 |
def test_nested_call_args(): |
| 104 |
expect_token("bar", "foo(bar(x,| ))") |
| 105 |
expect_token("bar", "foo(bar(x, |))") |
| 106 |
expect_token("bar", "foo(ba|r(x, y))") |
| 107 |
expect_token("x", "foo(bar(|x, y))") |
| 108 |
expect_token("bar", "foo(bar(x|, y))") |
| 109 |
expect_token("bar", "foo(bar(x,| y))") |
| 110 |
expect_token("y", "foo(bar(x, |y))") |
| 111 |
expect_token("bar", "foo(bar(x, y|))") |
| 112 |
expect_token("foo", "foo(bar(x, y)|)") |
| 113 |
expect_token("foo", "foo(bar(, y)|)") |
| 114 |
|
| 115 |
|
| 116 |
def test_outer_name(): |
| 117 |
expect_token("x", "|x + foo(a, b) + y + z") |
| 118 |
expect_token("x", "x| + foo(a, b) + y + z") |
| 119 |
expect_token("x", "x |+ foo(a, b) + y + z") |
| 120 |
expect_token("x", "x +| foo(a, b) + y + z") |
| 121 |
expect_token("foo", "x + |foo(a, b) + y + z") |
| 122 |
expect_token("a", "x + foo(|a, b) + y + z") |
| 123 |
expect_token("foo", "x + foo(a|, b) + y + z") |
| 124 |
expect_token("foo", "x + foo(a, b)| + y + z") |
| 125 |
expect_token("foo", "x + foo(a, b) |+ y + z") |
| 126 |
expect_token("foo", "x + foo(a, b) +| y + z") |
| 127 |
expect_token("y", "x + foo(a, b) + |y + z") |
| 128 |
expect_token("y", "x + foo(a, b) + y| + z") |
| 129 |
expect_token("y", "x + foo(a, b) + y |+ z") |
| 130 |
expect_token("y", "x + foo(a, b) + y +| z") |
| 131 |
expect_token("z", "x + foo(a, b) + y + |z") |
| 132 |
|
| 133 |
|
| 134 |
def test_attrs(): |
| 135 |
cell = "a = obj.attr.subattr" |
| 136 |
expected = "obj" |
| 137 |
idx = cell.find("obj") + 1 |
| 138 |
for i in range(idx, idx + 3): |
| 139 |
expect_token(expected, cell, i) |
| 140 |
idx = cell.find(".attr") + 2 |
| 141 |
expected = "obj.attr" |
| 142 |
for i in range(idx, idx + 4): |
| 143 |
expect_token(expected, cell, i) |
| 144 |
idx = cell.find(".subattr") + 2 |
| 145 |
expected = "obj.attr.subattr" |
| 146 |
for i in range(idx, len(cell)): |
| 147 |
expect_token(expected, cell, i) |
| 148 |
|
| 149 |
|
| 150 |
def test_line_at_cursor(): |
| 151 |
cell = "" |
| 152 |
(line, offset) = line_at_cursor(cell, cursor_pos=11) |
| 153 |
assert line == "" |
| 154 |
assert offset == 0 |
| 155 |
|
| 156 |
# The position after a newline should be the start of the following line. |
| 157 |
cell = "One\nTwo\n" |
| 158 |
(line, offset) = line_at_cursor(cell, cursor_pos=4) |
| 159 |
assert line == "Two\n" |
| 160 |
assert offset == 4 |
| 161 |
|
| 162 |
# The end of a cell should be on the last line |
| 163 |
cell = "pri\npri" |
| 164 |
(line, offset) = line_at_cursor(cell, cursor_pos=7) |
| 165 |
assert line == "pri" |
| 166 |
assert offset == 4 |
| 167 |
|
| 168 |
|
| 169 |
@pytest.mark.parametrize( |
| 170 |
"c, token", |
| 171 |
zip( |
| 172 |
list(range(16, 22)) + list(range(22, 28)), |
| 173 |
["int"] * (22 - 16) + ["map"] * (28 - 22), |
| 174 |
), |
| 175 |
) |
| 176 |
def test_multiline_statement(c, token): |
| 177 |
cell = """a = (1, |
| 178 |
3) |
| 179 |
|
| 180 |
int() |
| 181 |
map() |
| 182 |
""" |
| 183 |
expect_token(token, cell, c) |