test_tokenutil.py
90 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
MinRK
|
r16578 | """Tests for tokenutil""" | ||
# Copyright (c) IPython Development Team. | ||||
# Distributed under the terms of the Modified BSD License. | ||||
import nose.tools as nt | ||||
Doug Blank
|
r18879 | from IPython.utils.tokenutil import token_at_cursor, line_at_cursor | ||
MinRK
|
r16578 | |||
MinRK
|
r16580 | def expect_token(expected, cell, cursor_pos): | ||
token = token_at_cursor(cell, cursor_pos) | ||||
offset = 0 | ||||
for line in cell.splitlines(): | ||||
if offset + len(line) >= cursor_pos: | ||||
break | ||||
else: | ||||
offset += len(line) | ||||
column = cursor_pos - offset | ||||
line_with_cursor = '%s|%s' % (line[:column], line[column:]) | ||||
MinRK
|
r16578 | nt.assert_equal(token, expected, | ||
MinRK
|
r18453 | "Expected %r, got %r in: %r (pos %i)" % ( | ||
expected, token, line_with_cursor, cursor_pos) | ||||
MinRK
|
r16578 | ) | ||
def test_simple(): | ||||
cell = "foo" | ||||
for i in range(len(cell)): | ||||
expect_token("foo", cell, i) | ||||
def test_function(): | ||||
cell = "foo(a=5, b='10')" | ||||
expected = 'foo' | ||||
MinRK
|
r18453 | # up to `foo(|a=` | ||
for i in range(cell.find('a=') + 1): | ||||
expect_token("foo", cell, i) | ||||
# find foo after `=` | ||||
for i in [cell.find('=') + 1, cell.rfind('=') + 1]: | ||||
expect_token("foo", cell, i) | ||||
# in between `5,|` and `|b=` | ||||
for i in range(cell.find(','), cell.find('b=')): | ||||
MinRK
|
r16578 | expect_token("foo", cell, i) | ||
def test_multiline(): | ||||
cell = '\n'.join([ | ||||
'a = 5', | ||||
'b = hello("string", there)' | ||||
]) | ||||
expected = 'hello' | ||||
MinRK
|
r18453 | start = cell.index(expected) + 1 | ||
MinRK
|
r16580 | for i in range(start, start + len(expected)): | ||
expect_token(expected, cell, i) | ||||
Min RK
|
r20471 | expected = 'hello' | ||
MinRK
|
r18453 | start = cell.index(expected) + 1 | ||
MinRK
|
r16580 | for i in range(start, start + len(expected)): | ||
expect_token(expected, cell, i) | ||||
MinRK
|
r16578 | |||
Min RK
|
r20471 | def test_nested_call(): | ||
cell = "foo(bar(a=5), b=10)" | ||||
expected = 'foo' | ||||
start = cell.index('bar') + 1 | ||||
for i in range(start, start + 3): | ||||
expect_token(expected, cell, i) | ||||
expected = 'bar' | ||||
start = cell.index('a=') | ||||
for i in range(start, start + 3): | ||||
expect_token(expected, cell, i) | ||||
expected = 'foo' | ||||
start = cell.index(')') + 1 | ||||
for i in range(start, len(cell)-1): | ||||
expect_token(expected, cell, i) | ||||
MinRK
|
r16578 | def test_attrs(): | ||
Min RK
|
r20471 | cell = "a = obj.attr.subattr" | ||
MinRK
|
r16578 | expected = 'obj' | ||
MinRK
|
r18453 | idx = cell.find('obj') + 1 | ||
MinRK
|
r16578 | for i in range(idx, idx + 3): | ||
expect_token(expected, cell, i) | ||||
MinRK
|
r18453 | idx = cell.find('.attr') + 2 | ||
MinRK
|
r16578 | expected = 'obj.attr' | ||
for i in range(idx, idx + 4): | ||||
expect_token(expected, cell, i) | ||||
MinRK
|
r18453 | idx = cell.find('.subattr') + 2 | ||
MinRK
|
r16578 | expected = 'obj.attr.subattr' | ||
for i in range(idx, len(cell)): | ||||
expect_token(expected, cell, i) | ||||
Doug Blank
|
r18879 | |||
def test_line_at_cursor(): | ||||
cell = "" | ||||
(line, offset) = line_at_cursor(cell, cursor_pos=11) | ||||
assert line == "", ("Expected '', got %r" % line) | ||||
assert offset == 0, ("Expected '', got %r" % line) | ||||