##// END OF EJS Templates
[utils][tests][tokenutil] Remove nose
Samuel Gaist -
Show More
@@ -1,141 +1,143 b''
1 1 """Tests for tokenutil"""
2 2 # Copyright (c) IPython Development Team.
3 3 # Distributed under the terms of the Modified BSD License.
4 4
5 import nose.tools as nt
6 5 import pytest
7 6 from IPython.testing.decorators import skip_iptest_but_not_pytest
8 7
9 8 from IPython.utils.tokenutil import token_at_cursor, line_at_cursor
10 9
11 10 def expect_token(expected, cell, cursor_pos):
12 11 token = token_at_cursor(cell, cursor_pos)
13 12 offset = 0
14 13 for line in cell.splitlines():
15 14 if offset + len(line) >= cursor_pos:
16 15 break
17 16 else:
18 17 offset += len(line)+1
19 18 column = cursor_pos - offset
20 line_with_cursor = '%s|%s' % (line[:column], line[column:])
21 nt.assert_equal(token, expected,
22 "Expected %r, got %r in: %r (pos %i)" % (
23 expected, token, line_with_cursor, cursor_pos)
19 line_with_cursor = "%s|%s" % (line[:column], line[column:])
20 assert token == expected, "Expected %r, got %r in: %r (pos %i)" % (
21 expected,
22 token,
23 line_with_cursor,
24 cursor_pos,
24 25 )
25 26
27
26 28 def test_simple():
27 29 cell = "foo"
28 30 for i in range(len(cell)):
29 31 expect_token("foo", cell, i)
30 32
31 33 def test_function():
32 34 cell = "foo(a=5, b='10')"
33 35 expected = 'foo'
34 36 # up to `foo(|a=`
35 37 for i in range(cell.find('a=') + 1):
36 38 expect_token("foo", cell, i)
37 39 # find foo after `=`
38 40 for i in [cell.find('=') + 1, cell.rfind('=') + 1]:
39 41 expect_token("foo", cell, i)
40 42 # in between `5,|` and `|b=`
41 43 for i in range(cell.find(','), cell.find('b=')):
42 44 expect_token("foo", cell, i)
43 45
44 46 def test_multiline():
45 47 cell = '\n'.join([
46 48 'a = 5',
47 49 'b = hello("string", there)'
48 50 ])
49 51 expected = 'hello'
50 52 start = cell.index(expected) + 1
51 53 for i in range(start, start + len(expected)):
52 54 expect_token(expected, cell, i)
53 55 expected = 'hello'
54 56 start = cell.index(expected) + 1
55 57 for i in range(start, start + len(expected)):
56 58 expect_token(expected, cell, i)
57 59
58 60 def test_multiline_token():
59 61 cell = '\n'.join([
60 62 '"""\n\nxxxxxxxxxx\n\n"""',
61 63 '5, """',
62 64 'docstring',
63 65 'multiline token',
64 66 '""", [',
65 67 '2, 3, "complicated"]',
66 68 'b = hello("string", there)'
67 69 ])
68 70 expected = 'hello'
69 71 start = cell.index(expected) + 1
70 72 for i in range(start, start + len(expected)):
71 73 expect_token(expected, cell, i)
72 74 expected = 'hello'
73 75 start = cell.index(expected) + 1
74 76 for i in range(start, start + len(expected)):
75 77 expect_token(expected, cell, i)
76 78
77 79 def test_nested_call():
78 80 cell = "foo(bar(a=5), b=10)"
79 81 expected = 'foo'
80 82 start = cell.index('bar') + 1
81 83 for i in range(start, start + 3):
82 84 expect_token(expected, cell, i)
83 85 expected = 'bar'
84 86 start = cell.index('a=')
85 87 for i in range(start, start + 3):
86 88 expect_token(expected, cell, i)
87 89 expected = 'foo'
88 90 start = cell.index(')') + 1
89 91 for i in range(start, len(cell)-1):
90 92 expect_token(expected, cell, i)
91 93
92 94 def test_attrs():
93 95 cell = "a = obj.attr.subattr"
94 96 expected = 'obj'
95 97 idx = cell.find('obj') + 1
96 98 for i in range(idx, idx + 3):
97 99 expect_token(expected, cell, i)
98 100 idx = cell.find('.attr') + 2
99 101 expected = 'obj.attr'
100 102 for i in range(idx, idx + 4):
101 103 expect_token(expected, cell, i)
102 104 idx = cell.find('.subattr') + 2
103 105 expected = 'obj.attr.subattr'
104 106 for i in range(idx, len(cell)):
105 107 expect_token(expected, cell, i)
106 108
107 109 def test_line_at_cursor():
108 110 cell = ""
109 111 (line, offset) = line_at_cursor(cell, cursor_pos=11)
110 nt.assert_equal(line, "")
111 nt.assert_equal(offset, 0)
112 assert line == ""
113 assert offset == 0
112 114
113 115 # The position after a newline should be the start of the following line.
114 116 cell = "One\nTwo\n"
115 117 (line, offset) = line_at_cursor(cell, cursor_pos=4)
116 nt.assert_equal(line, "Two\n")
117 nt.assert_equal(offset, 4)
118 assert line == "Two\n"
119 assert offset == 4
118 120
119 121 # The end of a cell should be on the last line
120 122 cell = "pri\npri"
121 123 (line, offset) = line_at_cursor(cell, cursor_pos=7)
122 nt.assert_equal(line, "pri")
123 nt.assert_equal(offset, 4)
124 assert line == "pri"
125 assert offset == 4
124 126
125 127
126 128 @pytest.mark.parametrize(
127 129 "c, token",
128 130 zip(
129 131 list(range(16, 22)) + list(range(22, 28)),
130 132 ["int"] * (22 - 16) + ["map"] * (28 - 22),
131 133 ),
132 134 )
133 135 @skip_iptest_but_not_pytest
134 136 def test_multiline_statement(c, token):
135 137 cell = """a = (1,
136 138 3)
137 139
138 140 int()
139 141 map()
140 142 """
141 143 expect_token(token, cell, c)
General Comments 0
You need to be logged in to leave comments. Login now