Show More
@@ -1,218 +1,228 | |||
|
1 | 1 | """Tests for the token-based transformers in IPython.core.inputtransformer2 |
|
2 | 2 | |
|
3 | 3 | Line-based transformers are the simpler ones; token-based transformers are |
|
4 | 4 | more complex. See test_inputtransformer2_line for tests for line-based |
|
5 | 5 | transformations. |
|
6 | 6 | """ |
|
7 | 7 | import nose.tools as nt |
|
8 | 8 | import string |
|
9 | 9 | |
|
10 | 10 | from IPython.core import inputtransformer2 as ipt2 |
|
11 | 11 | from IPython.core.inputtransformer2 import make_tokens_by_line |
|
12 | 12 | |
|
13 | from textwrap import dedent | |
|
14 | ||
|
13 | 15 | MULTILINE_MAGIC = ("""\ |
|
14 | 16 | a = f() |
|
15 | 17 | %foo \\ |
|
16 | 18 | bar |
|
17 | 19 | g() |
|
18 | 20 | """.splitlines(keepends=True), (2, 0), """\ |
|
19 | 21 | a = f() |
|
20 | 22 | get_ipython().run_line_magic('foo', ' bar') |
|
21 | 23 | g() |
|
22 | 24 | """.splitlines(keepends=True)) |
|
23 | 25 | |
|
24 | 26 | INDENTED_MAGIC = ("""\ |
|
25 | 27 | for a in range(5): |
|
26 | 28 | %ls |
|
27 | 29 | """.splitlines(keepends=True), (2, 4), """\ |
|
28 | 30 | for a in range(5): |
|
29 | 31 | get_ipython().run_line_magic('ls', '') |
|
30 | 32 | """.splitlines(keepends=True)) |
|
31 | 33 | |
|
32 | 34 | MULTILINE_MAGIC_ASSIGN = ("""\ |
|
33 | 35 | a = f() |
|
34 | 36 | b = %foo \\ |
|
35 | 37 | bar |
|
36 | 38 | g() |
|
37 | 39 | """.splitlines(keepends=True), (2, 4), """\ |
|
38 | 40 | a = f() |
|
39 | 41 | b = get_ipython().run_line_magic('foo', ' bar') |
|
40 | 42 | g() |
|
41 | 43 | """.splitlines(keepends=True)) |
|
42 | 44 | |
|
43 | 45 | MULTILINE_SYSTEM_ASSIGN = ("""\ |
|
44 | 46 | a = f() |
|
45 | 47 | b = !foo \\ |
|
46 | 48 | bar |
|
47 | 49 | g() |
|
48 | 50 | """.splitlines(keepends=True), (2, 4), """\ |
|
49 | 51 | a = f() |
|
50 | 52 | b = get_ipython().getoutput('foo bar') |
|
51 | 53 | g() |
|
52 | 54 | """.splitlines(keepends=True)) |
|
53 | 55 | |
|
54 | 56 | AUTOCALL_QUOTE = ( |
|
55 | 57 | [",f 1 2 3\n"], (1, 0), |
|
56 | 58 | ['f("1", "2", "3")\n'] |
|
57 | 59 | ) |
|
58 | 60 | |
|
59 | 61 | AUTOCALL_QUOTE2 = ( |
|
60 | 62 | [";f 1 2 3\n"], (1, 0), |
|
61 | 63 | ['f("1 2 3")\n'] |
|
62 | 64 | ) |
|
63 | 65 | |
|
64 | 66 | AUTOCALL_PAREN = ( |
|
65 | 67 | ["/f 1 2 3\n"], (1, 0), |
|
66 | 68 | ['f(1, 2, 3)\n'] |
|
67 | 69 | ) |
|
68 | 70 | |
|
69 | 71 | SIMPLE_HELP = ( |
|
70 | 72 | ["foo?\n"], (1, 0), |
|
71 | 73 | ["get_ipython().run_line_magic('pinfo', 'foo')\n"] |
|
72 | 74 | ) |
|
73 | 75 | |
|
74 | 76 | DETAILED_HELP = ( |
|
75 | 77 | ["foo??\n"], (1, 0), |
|
76 | 78 | ["get_ipython().run_line_magic('pinfo2', 'foo')\n"] |
|
77 | 79 | ) |
|
78 | 80 | |
|
79 | 81 | MAGIC_HELP = ( |
|
80 | 82 | ["%foo?\n"], (1, 0), |
|
81 | 83 | ["get_ipython().run_line_magic('pinfo', '%foo')\n"] |
|
82 | 84 | ) |
|
83 | 85 | |
|
84 | 86 | HELP_IN_EXPR = ( |
|
85 | 87 | ["a = b + c?\n"], (1, 0), |
|
86 | 88 | ["get_ipython().set_next_input('a = b + c');" |
|
87 | 89 | "get_ipython().run_line_magic('pinfo', 'c')\n"] |
|
88 | 90 | ) |
|
89 | 91 | |
|
90 | 92 | HELP_CONTINUED_LINE = ("""\ |
|
91 | 93 | a = \\ |
|
92 | 94 | zip? |
|
93 | 95 | """.splitlines(keepends=True), (1, 0), |
|
94 | 96 | [r"get_ipython().set_next_input('a = \\\nzip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"] |
|
95 | 97 | ) |
|
96 | 98 | |
|
97 | 99 | HELP_MULTILINE = ("""\ |
|
98 | 100 | (a, |
|
99 | 101 | b) = zip? |
|
100 | 102 | """.splitlines(keepends=True), (1, 0), |
|
101 | 103 | [r"get_ipython().set_next_input('(a,\nb) = zip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"] |
|
102 | 104 | ) |
|
103 | 105 | |
|
104 | 106 | def check_make_token_by_line_never_ends_empty(): |
|
105 | 107 | """ |
|
106 | 108 | Check that not sequence of single or double characters ends up leading to en empty list of tokens |
|
107 | 109 | """ |
|
108 | 110 | from string import printable |
|
109 | 111 | for c in printable: |
|
110 | 112 | nt.assert_not_equal(make_tokens_by_line(c)[-1], []) |
|
111 | 113 | for k in printable: |
|
112 | 114 | nt.assert_not_equal(make_tokens_by_line(c+k)[-1], []) |
|
113 | 115 | |
|
114 | 116 | def check_find(transformer, case, match=True): |
|
115 | 117 | sample, expected_start, _ = case |
|
116 | 118 | tbl = make_tokens_by_line(sample) |
|
117 | 119 | res = transformer.find(tbl) |
|
118 | 120 | if match: |
|
119 | 121 | # start_line is stored 0-indexed, expected values are 1-indexed |
|
120 | 122 | nt.assert_equal((res.start_line+1, res.start_col), expected_start) |
|
121 | 123 | return res |
|
122 | 124 | else: |
|
123 | 125 | nt.assert_is(res, None) |
|
124 | 126 | |
|
125 | 127 | def check_transform(transformer_cls, case): |
|
126 | 128 | lines, start, expected = case |
|
127 | 129 | transformer = transformer_cls(start) |
|
128 | 130 | nt.assert_equal(transformer.transform(lines), expected) |
|
129 | 131 | |
|
130 | 132 | def test_continued_line(): |
|
131 | 133 | lines = MULTILINE_MAGIC_ASSIGN[0] |
|
132 | 134 | nt.assert_equal(ipt2.find_end_of_continued_line(lines, 1), 2) |
|
133 | 135 | |
|
134 | 136 | nt.assert_equal(ipt2.assemble_continued_line(lines, (1, 5), 2), "foo bar") |
|
135 | 137 | |
|
136 | 138 | def test_find_assign_magic(): |
|
137 | 139 | check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) |
|
138 | 140 | check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN, match=False) |
|
139 | 141 | |
|
140 | 142 | def test_transform_assign_magic(): |
|
141 | 143 | check_transform(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) |
|
142 | 144 | |
|
143 | 145 | def test_find_assign_system(): |
|
144 | 146 | check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN) |
|
145 | 147 | check_find(ipt2.SystemAssign, (["a = !ls\n"], (1, 5), None)) |
|
146 | 148 | check_find(ipt2.SystemAssign, (["a=!ls\n"], (1, 2), None)) |
|
147 | 149 | check_find(ipt2.SystemAssign, MULTILINE_MAGIC_ASSIGN, match=False) |
|
148 | 150 | |
|
149 | 151 | def test_transform_assign_system(): |
|
150 | 152 | check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN) |
|
151 | 153 | |
|
152 | 154 | def test_find_magic_escape(): |
|
153 | 155 | check_find(ipt2.EscapedCommand, MULTILINE_MAGIC) |
|
154 | 156 | check_find(ipt2.EscapedCommand, INDENTED_MAGIC) |
|
155 | 157 | check_find(ipt2.EscapedCommand, MULTILINE_MAGIC_ASSIGN, match=False) |
|
156 | 158 | |
|
157 | 159 | def test_transform_magic_escape(): |
|
158 | 160 | check_transform(ipt2.EscapedCommand, MULTILINE_MAGIC) |
|
159 | 161 | check_transform(ipt2.EscapedCommand, INDENTED_MAGIC) |
|
160 | 162 | |
|
161 | 163 | def test_find_autocalls(): |
|
162 | 164 | for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: |
|
163 | 165 | print("Testing %r" % case[0]) |
|
164 | 166 | check_find(ipt2.EscapedCommand, case) |
|
165 | 167 | |
|
166 | 168 | def test_transform_autocall(): |
|
167 | 169 | for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: |
|
168 | 170 | print("Testing %r" % case[0]) |
|
169 | 171 | check_transform(ipt2.EscapedCommand, case) |
|
170 | 172 | |
|
171 | 173 | def test_find_help(): |
|
172 | 174 | for case in [SIMPLE_HELP, DETAILED_HELP, MAGIC_HELP, HELP_IN_EXPR]: |
|
173 | 175 | check_find(ipt2.HelpEnd, case) |
|
174 | 176 | |
|
175 | 177 | tf = check_find(ipt2.HelpEnd, HELP_CONTINUED_LINE) |
|
176 | 178 | nt.assert_equal(tf.q_line, 1) |
|
177 | 179 | nt.assert_equal(tf.q_col, 3) |
|
178 | 180 | |
|
179 | 181 | tf = check_find(ipt2.HelpEnd, HELP_MULTILINE) |
|
180 | 182 | nt.assert_equal(tf.q_line, 1) |
|
181 | 183 | nt.assert_equal(tf.q_col, 8) |
|
182 | 184 | |
|
183 | 185 | # ? in a comment does not trigger help |
|
184 | 186 | check_find(ipt2.HelpEnd, (["foo # bar?\n"], None, None), match=False) |
|
185 | 187 | # Nor in a string |
|
186 | 188 | check_find(ipt2.HelpEnd, (["foo = '''bar?\n"], None, None), match=False) |
|
187 | 189 | |
|
188 | 190 | def test_transform_help(): |
|
189 | 191 | tf = ipt2.HelpEnd((1, 0), (1, 9)) |
|
190 | 192 | nt.assert_equal(tf.transform(HELP_IN_EXPR[0]), HELP_IN_EXPR[2]) |
|
191 | 193 | |
|
192 | 194 | tf = ipt2.HelpEnd((1, 0), (2, 3)) |
|
193 | 195 | nt.assert_equal(tf.transform(HELP_CONTINUED_LINE[0]), HELP_CONTINUED_LINE[2]) |
|
194 | 196 | |
|
195 | 197 | tf = ipt2.HelpEnd((1, 0), (2, 8)) |
|
196 | 198 | nt.assert_equal(tf.transform(HELP_MULTILINE[0]), HELP_MULTILINE[2]) |
|
197 | 199 | |
|
198 | 200 | def test_check_complete(): |
|
199 | 201 | cc = ipt2.TransformerManager().check_complete |
|
200 | 202 | nt.assert_equal(cc("a = 1"), ('complete', None)) |
|
201 | 203 | nt.assert_equal(cc("for a in range(5):"), ('incomplete', 4)) |
|
202 | 204 | nt.assert_equal(cc("raise = 2"), ('invalid', None)) |
|
203 | 205 | nt.assert_equal(cc("a = [1,\n2,"), ('incomplete', 0)) |
|
204 | 206 | nt.assert_equal(cc(")"), ('incomplete', 0)) |
|
205 | 207 | nt.assert_equal(cc("\\\r\n"), ('incomplete', 0)) |
|
206 | 208 | nt.assert_equal(cc("a = '''\n hi"), ('incomplete', 3)) |
|
207 | 209 | nt.assert_equal(cc("def a():\n x=1\n global x"), ('invalid', None)) |
|
208 | 210 | nt.assert_equal(cc("a \\ "), ('invalid', None)) # Nothing allowed after backslash |
|
209 | 211 | nt.assert_equal(cc("1\\\n+2"), ('complete', None)) |
|
210 | 212 | |
|
213 | example = dedent(""" | |
|
214 | if True: | |
|
215 | a=1""" ) | |
|
216 | ||
|
217 | nt.assert_equal(cc(example), ('incomplete', 4)) | |
|
218 | nt.assert_equal(cc(example+'\n'), ('complete', None)) | |
|
219 | nt.assert_equal(cc(example+'\n '), ('complete', None)) | |
|
220 | ||
|
211 | 221 | # no need to loop on all the letters/numbers. |
|
212 | 222 | short = '12abAB'+string.printable[62:] |
|
213 | 223 | for c in short: |
|
214 | 224 | # test does not raise: |
|
215 | 225 | cc(c) |
|
216 | 226 | for k in short: |
|
217 | 227 | cc(c+k) |
|
218 | 228 |
General Comments 0
You need to be logged in to leave comments.
Login now