Show More
@@ -1,387 +1,388 b'' | |||||
1 | """Tests for the token-based transformers in IPython.core.inputtransformer2 |
|
1 | """Tests for the token-based transformers in IPython.core.inputtransformer2 | |
2 |
|
2 | |||
3 | Line-based transformers are the simpler ones; token-based transformers are |
|
3 | Line-based transformers are the simpler ones; token-based transformers are | |
4 | more complex. See test_inputtransformer2_line for tests for line-based |
|
4 | more complex. See test_inputtransformer2_line for tests for line-based | |
5 | transformations. |
|
5 | transformations. | |
6 | """ |
|
6 | """ | |
7 | import string |
|
7 | import string | |
8 | import sys |
|
8 | import sys | |
9 | from textwrap import dedent |
|
9 | from textwrap import dedent | |
10 |
|
10 | |||
11 | import pytest |
|
11 | import pytest | |
12 |
|
12 | |||
13 | from IPython.core import inputtransformer2 as ipt2 |
|
13 | from IPython.core import inputtransformer2 as ipt2 | |
14 | from IPython.core.inputtransformer2 import _find_assign_op, make_tokens_by_line |
|
14 | from IPython.core.inputtransformer2 import _find_assign_op, make_tokens_by_line | |
15 | from IPython.testing.decorators import skip_iptest_but_not_pytest |
|
15 | from IPython.testing.decorators import skip_iptest_but_not_pytest | |
16 |
|
16 | |||
17 | MULTILINE_MAGIC = ("""\ |
|
17 | MULTILINE_MAGIC = ("""\ | |
18 | a = f() |
|
18 | a = f() | |
19 | %foo \\ |
|
19 | %foo \\ | |
20 | bar |
|
20 | bar | |
21 | g() |
|
21 | g() | |
22 | """.splitlines(keepends=True), (2, 0), """\ |
|
22 | """.splitlines(keepends=True), (2, 0), """\ | |
23 | a = f() |
|
23 | a = f() | |
24 | get_ipython().run_line_magic('foo', ' bar') |
|
24 | get_ipython().run_line_magic('foo', ' bar') | |
25 | g() |
|
25 | g() | |
26 | """.splitlines(keepends=True)) |
|
26 | """.splitlines(keepends=True)) | |
27 |
|
27 | |||
28 | INDENTED_MAGIC = ("""\ |
|
28 | INDENTED_MAGIC = ("""\ | |
29 | for a in range(5): |
|
29 | for a in range(5): | |
30 | %ls |
|
30 | %ls | |
31 | """.splitlines(keepends=True), (2, 4), """\ |
|
31 | """.splitlines(keepends=True), (2, 4), """\ | |
32 | for a in range(5): |
|
32 | for a in range(5): | |
33 | get_ipython().run_line_magic('ls', '') |
|
33 | get_ipython().run_line_magic('ls', '') | |
34 | """.splitlines(keepends=True)) |
|
34 | """.splitlines(keepends=True)) | |
35 |
|
35 | |||
36 | CRLF_MAGIC = ([ |
|
36 | CRLF_MAGIC = ([ | |
37 | "a = f()\n", |
|
37 | "a = f()\n", | |
38 | "%ls\r\n", |
|
38 | "%ls\r\n", | |
39 | "g()\n" |
|
39 | "g()\n" | |
40 | ], (2, 0), [ |
|
40 | ], (2, 0), [ | |
41 | "a = f()\n", |
|
41 | "a = f()\n", | |
42 | "get_ipython().run_line_magic('ls', '')\n", |
|
42 | "get_ipython().run_line_magic('ls', '')\n", | |
43 | "g()\n" |
|
43 | "g()\n" | |
44 | ]) |
|
44 | ]) | |
45 |
|
45 | |||
46 | MULTILINE_MAGIC_ASSIGN = ("""\ |
|
46 | MULTILINE_MAGIC_ASSIGN = ("""\ | |
47 | a = f() |
|
47 | a = f() | |
48 | b = %foo \\ |
|
48 | b = %foo \\ | |
49 | bar |
|
49 | bar | |
50 | g() |
|
50 | g() | |
51 | """.splitlines(keepends=True), (2, 4), """\ |
|
51 | """.splitlines(keepends=True), (2, 4), """\ | |
52 | a = f() |
|
52 | a = f() | |
53 | b = get_ipython().run_line_magic('foo', ' bar') |
|
53 | b = get_ipython().run_line_magic('foo', ' bar') | |
54 | g() |
|
54 | g() | |
55 | """.splitlines(keepends=True)) |
|
55 | """.splitlines(keepends=True)) | |
56 |
|
56 | |||
57 | MULTILINE_SYSTEM_ASSIGN = ("""\ |
|
57 | MULTILINE_SYSTEM_ASSIGN = ("""\ | |
58 | a = f() |
|
58 | a = f() | |
59 | b = !foo \\ |
|
59 | b = !foo \\ | |
60 | bar |
|
60 | bar | |
61 | g() |
|
61 | g() | |
62 | """.splitlines(keepends=True), (2, 4), """\ |
|
62 | """.splitlines(keepends=True), (2, 4), """\ | |
63 | a = f() |
|
63 | a = f() | |
64 | b = get_ipython().getoutput('foo bar') |
|
64 | b = get_ipython().getoutput('foo bar') | |
65 | g() |
|
65 | g() | |
66 | """.splitlines(keepends=True)) |
|
66 | """.splitlines(keepends=True)) | |
67 |
|
67 | |||
68 | ##### |
|
68 | ##### | |
69 |
|
69 | |||
70 | MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT = ("""\ |
|
70 | MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT = ("""\ | |
71 | def test(): |
|
71 | def test(): | |
72 | for i in range(1): |
|
72 | for i in range(1): | |
73 | print(i) |
|
73 | print(i) | |
74 | res =! ls |
|
74 | res =! ls | |
75 | """.splitlines(keepends=True), (4, 7), '''\ |
|
75 | """.splitlines(keepends=True), (4, 7), '''\ | |
76 | def test(): |
|
76 | def test(): | |
77 | for i in range(1): |
|
77 | for i in range(1): | |
78 | print(i) |
|
78 | print(i) | |
79 | res =get_ipython().getoutput(\' ls\') |
|
79 | res =get_ipython().getoutput(\' ls\') | |
80 | '''.splitlines(keepends=True)) |
|
80 | '''.splitlines(keepends=True)) | |
81 |
|
81 | |||
82 | ###### |
|
82 | ###### | |
83 |
|
83 | |||
84 | AUTOCALL_QUOTE = ( |
|
84 | AUTOCALL_QUOTE = ( | |
85 | [",f 1 2 3\n"], (1, 0), |
|
85 | [",f 1 2 3\n"], (1, 0), | |
86 | ['f("1", "2", "3")\n'] |
|
86 | ['f("1", "2", "3")\n'] | |
87 | ) |
|
87 | ) | |
88 |
|
88 | |||
89 | AUTOCALL_QUOTE2 = ( |
|
89 | AUTOCALL_QUOTE2 = ( | |
90 | [";f 1 2 3\n"], (1, 0), |
|
90 | [";f 1 2 3\n"], (1, 0), | |
91 | ['f("1 2 3")\n'] |
|
91 | ['f("1 2 3")\n'] | |
92 | ) |
|
92 | ) | |
93 |
|
93 | |||
94 | AUTOCALL_PAREN = ( |
|
94 | AUTOCALL_PAREN = ( | |
95 | ["/f 1 2 3\n"], (1, 0), |
|
95 | ["/f 1 2 3\n"], (1, 0), | |
96 | ['f(1, 2, 3)\n'] |
|
96 | ['f(1, 2, 3)\n'] | |
97 | ) |
|
97 | ) | |
98 |
|
98 | |||
99 | SIMPLE_HELP = ( |
|
99 | SIMPLE_HELP = ( | |
100 | ["foo?\n"], (1, 0), |
|
100 | ["foo?\n"], (1, 0), | |
101 | ["get_ipython().run_line_magic('pinfo', 'foo')\n"] |
|
101 | ["get_ipython().run_line_magic('pinfo', 'foo')\n"] | |
102 | ) |
|
102 | ) | |
103 |
|
103 | |||
104 | DETAILED_HELP = ( |
|
104 | DETAILED_HELP = ( | |
105 | ["foo??\n"], (1, 0), |
|
105 | ["foo??\n"], (1, 0), | |
106 | ["get_ipython().run_line_magic('pinfo2', 'foo')\n"] |
|
106 | ["get_ipython().run_line_magic('pinfo2', 'foo')\n"] | |
107 | ) |
|
107 | ) | |
108 |
|
108 | |||
109 | MAGIC_HELP = ( |
|
109 | MAGIC_HELP = ( | |
110 | ["%foo?\n"], (1, 0), |
|
110 | ["%foo?\n"], (1, 0), | |
111 | ["get_ipython().run_line_magic('pinfo', '%foo')\n"] |
|
111 | ["get_ipython().run_line_magic('pinfo', '%foo')\n"] | |
112 | ) |
|
112 | ) | |
113 |
|
113 | |||
114 | HELP_IN_EXPR = ( |
|
114 | HELP_IN_EXPR = ( | |
115 | ["a = b + c?\n"], (1, 0), |
|
115 | ["a = b + c?\n"], (1, 0), | |
116 | ["get_ipython().set_next_input('a = b + c');" |
|
116 | ["get_ipython().set_next_input('a = b + c');" | |
117 | "get_ipython().run_line_magic('pinfo', 'c')\n"] |
|
117 | "get_ipython().run_line_magic('pinfo', 'c')\n"] | |
118 | ) |
|
118 | ) | |
119 |
|
119 | |||
120 | HELP_CONTINUED_LINE = ("""\ |
|
120 | HELP_CONTINUED_LINE = ("""\ | |
121 | a = \\ |
|
121 | a = \\ | |
122 | zip? |
|
122 | zip? | |
123 | """.splitlines(keepends=True), (1, 0), |
|
123 | """.splitlines(keepends=True), (1, 0), | |
124 | [r"get_ipython().set_next_input('a = \\\nzip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"] |
|
124 | [r"get_ipython().set_next_input('a = \\\nzip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"] | |
125 | ) |
|
125 | ) | |
126 |
|
126 | |||
127 | HELP_MULTILINE = ("""\ |
|
127 | HELP_MULTILINE = ("""\ | |
128 | (a, |
|
128 | (a, | |
129 | b) = zip? |
|
129 | b) = zip? | |
130 | """.splitlines(keepends=True), (1, 0), |
|
130 | """.splitlines(keepends=True), (1, 0), | |
131 | [r"get_ipython().set_next_input('(a,\nb) = zip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"] |
|
131 | [r"get_ipython().set_next_input('(a,\nb) = zip');get_ipython().run_line_magic('pinfo', 'zip')" + "\n"] | |
132 | ) |
|
132 | ) | |
133 |
|
133 | |||
134 | HELP_UNICODE = ( |
|
134 | HELP_UNICODE = ( | |
135 | ["Ο.foo?\n"], (1, 0), |
|
135 | ["Ο.foo?\n"], (1, 0), | |
136 | ["get_ipython().run_line_magic('pinfo', 'Ο.foo')\n"] |
|
136 | ["get_ipython().run_line_magic('pinfo', 'Ο.foo')\n"] | |
137 | ) |
|
137 | ) | |
138 |
|
138 | |||
139 |
|
139 | |||
140 | def null_cleanup_transformer(lines): |
|
140 | def null_cleanup_transformer(lines): | |
141 | """ |
|
141 | """ | |
142 | A cleanup transform that returns an empty list. |
|
142 | A cleanup transform that returns an empty list. | |
143 | """ |
|
143 | """ | |
144 | return [] |
|
144 | return [] | |
145 |
|
145 | |||
146 | def check_make_token_by_line_never_ends_empty(): |
|
146 | ||
|
147 | def test_check_make_token_by_line_never_ends_empty(): | |||
147 | """ |
|
148 | """ | |
148 | Check that not sequence of single or double characters ends up leading to en empty list of tokens |
|
149 | Check that not sequence of single or double characters ends up leading to en empty list of tokens | |
149 | """ |
|
150 | """ | |
150 | from string import printable |
|
151 | from string import printable | |
151 | for c in printable: |
|
152 | for c in printable: | |
152 | assert make_tokens_by_line(c)[-1] != [] |
|
153 | assert make_tokens_by_line(c)[-1] != [] | |
153 | for k in printable: |
|
154 | for k in printable: | |
154 | assert make_tokens_by_line(c + k)[-1] != [] |
|
155 | assert make_tokens_by_line(c + k)[-1] != [] | |
155 |
|
156 | |||
156 |
|
157 | |||
157 | def check_find(transformer, case, match=True): |
|
158 | def check_find(transformer, case, match=True): | |
158 | sample, expected_start, _ = case |
|
159 | sample, expected_start, _ = case | |
159 | tbl = make_tokens_by_line(sample) |
|
160 | tbl = make_tokens_by_line(sample) | |
160 | res = transformer.find(tbl) |
|
161 | res = transformer.find(tbl) | |
161 | if match: |
|
162 | if match: | |
162 | # start_line is stored 0-indexed, expected values are 1-indexed |
|
163 | # start_line is stored 0-indexed, expected values are 1-indexed | |
163 | assert (res.start_line + 1, res.start_col) == expected_start |
|
164 | assert (res.start_line + 1, res.start_col) == expected_start | |
164 | return res |
|
165 | return res | |
165 | else: |
|
166 | else: | |
166 | assert res is None |
|
167 | assert res is None | |
167 |
|
168 | |||
168 | def check_transform(transformer_cls, case): |
|
169 | def check_transform(transformer_cls, case): | |
169 | lines, start, expected = case |
|
170 | lines, start, expected = case | |
170 | transformer = transformer_cls(start) |
|
171 | transformer = transformer_cls(start) | |
171 | assert transformer.transform(lines) == expected |
|
172 | assert transformer.transform(lines) == expected | |
172 |
|
173 | |||
173 | def test_continued_line(): |
|
174 | def test_continued_line(): | |
174 | lines = MULTILINE_MAGIC_ASSIGN[0] |
|
175 | lines = MULTILINE_MAGIC_ASSIGN[0] | |
175 | assert ipt2.find_end_of_continued_line(lines, 1) == 2 |
|
176 | assert ipt2.find_end_of_continued_line(lines, 1) == 2 | |
176 |
|
177 | |||
177 | assert ipt2.assemble_continued_line(lines, (1, 5), 2) == "foo bar" |
|
178 | assert ipt2.assemble_continued_line(lines, (1, 5), 2) == "foo bar" | |
178 |
|
179 | |||
179 | def test_find_assign_magic(): |
|
180 | def test_find_assign_magic(): | |
180 | check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) |
|
181 | check_find(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) | |
181 | check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN, match=False) |
|
182 | check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN, match=False) | |
182 | check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT, match=False) |
|
183 | check_find(ipt2.MagicAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT, match=False) | |
183 |
|
184 | |||
184 | def test_transform_assign_magic(): |
|
185 | def test_transform_assign_magic(): | |
185 | check_transform(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) |
|
186 | check_transform(ipt2.MagicAssign, MULTILINE_MAGIC_ASSIGN) | |
186 |
|
187 | |||
187 | def test_find_assign_system(): |
|
188 | def test_find_assign_system(): | |
188 | check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN) |
|
189 | check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN) | |
189 | check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT) |
|
190 | check_find(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT) | |
190 | check_find(ipt2.SystemAssign, (["a = !ls\n"], (1, 5), None)) |
|
191 | check_find(ipt2.SystemAssign, (["a = !ls\n"], (1, 5), None)) | |
191 | check_find(ipt2.SystemAssign, (["a=!ls\n"], (1, 2), None)) |
|
192 | check_find(ipt2.SystemAssign, (["a=!ls\n"], (1, 2), None)) | |
192 | check_find(ipt2.SystemAssign, MULTILINE_MAGIC_ASSIGN, match=False) |
|
193 | check_find(ipt2.SystemAssign, MULTILINE_MAGIC_ASSIGN, match=False) | |
193 |
|
194 | |||
194 | def test_transform_assign_system(): |
|
195 | def test_transform_assign_system(): | |
195 | check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN) |
|
196 | check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN) | |
196 | check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT) |
|
197 | check_transform(ipt2.SystemAssign, MULTILINE_SYSTEM_ASSIGN_AFTER_DEDENT) | |
197 |
|
198 | |||
198 | def test_find_magic_escape(): |
|
199 | def test_find_magic_escape(): | |
199 | check_find(ipt2.EscapedCommand, MULTILINE_MAGIC) |
|
200 | check_find(ipt2.EscapedCommand, MULTILINE_MAGIC) | |
200 | check_find(ipt2.EscapedCommand, INDENTED_MAGIC) |
|
201 | check_find(ipt2.EscapedCommand, INDENTED_MAGIC) | |
201 | check_find(ipt2.EscapedCommand, MULTILINE_MAGIC_ASSIGN, match=False) |
|
202 | check_find(ipt2.EscapedCommand, MULTILINE_MAGIC_ASSIGN, match=False) | |
202 |
|
203 | |||
203 | def test_transform_magic_escape(): |
|
204 | def test_transform_magic_escape(): | |
204 | check_transform(ipt2.EscapedCommand, MULTILINE_MAGIC) |
|
205 | check_transform(ipt2.EscapedCommand, MULTILINE_MAGIC) | |
205 | check_transform(ipt2.EscapedCommand, INDENTED_MAGIC) |
|
206 | check_transform(ipt2.EscapedCommand, INDENTED_MAGIC) | |
206 | check_transform(ipt2.EscapedCommand, CRLF_MAGIC) |
|
207 | check_transform(ipt2.EscapedCommand, CRLF_MAGIC) | |
207 |
|
208 | |||
208 | def test_find_autocalls(): |
|
209 | def test_find_autocalls(): | |
209 | for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: |
|
210 | for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: | |
210 | print("Testing %r" % case[0]) |
|
211 | print("Testing %r" % case[0]) | |
211 | check_find(ipt2.EscapedCommand, case) |
|
212 | check_find(ipt2.EscapedCommand, case) | |
212 |
|
213 | |||
213 | def test_transform_autocall(): |
|
214 | def test_transform_autocall(): | |
214 | for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: |
|
215 | for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: | |
215 | print("Testing %r" % case[0]) |
|
216 | print("Testing %r" % case[0]) | |
216 | check_transform(ipt2.EscapedCommand, case) |
|
217 | check_transform(ipt2.EscapedCommand, case) | |
217 |
|
218 | |||
218 | def test_find_help(): |
|
219 | def test_find_help(): | |
219 | for case in [SIMPLE_HELP, DETAILED_HELP, MAGIC_HELP, HELP_IN_EXPR]: |
|
220 | for case in [SIMPLE_HELP, DETAILED_HELP, MAGIC_HELP, HELP_IN_EXPR]: | |
220 | check_find(ipt2.HelpEnd, case) |
|
221 | check_find(ipt2.HelpEnd, case) | |
221 |
|
222 | |||
222 | tf = check_find(ipt2.HelpEnd, HELP_CONTINUED_LINE) |
|
223 | tf = check_find(ipt2.HelpEnd, HELP_CONTINUED_LINE) | |
223 | assert tf.q_line == 1 |
|
224 | assert tf.q_line == 1 | |
224 | assert tf.q_col == 3 |
|
225 | assert tf.q_col == 3 | |
225 |
|
226 | |||
226 | tf = check_find(ipt2.HelpEnd, HELP_MULTILINE) |
|
227 | tf = check_find(ipt2.HelpEnd, HELP_MULTILINE) | |
227 | assert tf.q_line == 1 |
|
228 | assert tf.q_line == 1 | |
228 | assert tf.q_col == 8 |
|
229 | assert tf.q_col == 8 | |
229 |
|
230 | |||
230 | # ? in a comment does not trigger help |
|
231 | # ? in a comment does not trigger help | |
231 | check_find(ipt2.HelpEnd, (["foo # bar?\n"], None, None), match=False) |
|
232 | check_find(ipt2.HelpEnd, (["foo # bar?\n"], None, None), match=False) | |
232 | # Nor in a string |
|
233 | # Nor in a string | |
233 | check_find(ipt2.HelpEnd, (["foo = '''bar?\n"], None, None), match=False) |
|
234 | check_find(ipt2.HelpEnd, (["foo = '''bar?\n"], None, None), match=False) | |
234 |
|
235 | |||
235 | def test_transform_help(): |
|
236 | def test_transform_help(): | |
236 | tf = ipt2.HelpEnd((1, 0), (1, 9)) |
|
237 | tf = ipt2.HelpEnd((1, 0), (1, 9)) | |
237 | assert tf.transform(HELP_IN_EXPR[0]) == HELP_IN_EXPR[2] |
|
238 | assert tf.transform(HELP_IN_EXPR[0]) == HELP_IN_EXPR[2] | |
238 |
|
239 | |||
239 | tf = ipt2.HelpEnd((1, 0), (2, 3)) |
|
240 | tf = ipt2.HelpEnd((1, 0), (2, 3)) | |
240 | assert tf.transform(HELP_CONTINUED_LINE[0]) == HELP_CONTINUED_LINE[2] |
|
241 | assert tf.transform(HELP_CONTINUED_LINE[0]) == HELP_CONTINUED_LINE[2] | |
241 |
|
242 | |||
242 | tf = ipt2.HelpEnd((1, 0), (2, 8)) |
|
243 | tf = ipt2.HelpEnd((1, 0), (2, 8)) | |
243 | assert tf.transform(HELP_MULTILINE[0]) == HELP_MULTILINE[2] |
|
244 | assert tf.transform(HELP_MULTILINE[0]) == HELP_MULTILINE[2] | |
244 |
|
245 | |||
245 | tf = ipt2.HelpEnd((1, 0), (1, 0)) |
|
246 | tf = ipt2.HelpEnd((1, 0), (1, 0)) | |
246 | assert tf.transform(HELP_UNICODE[0]) == HELP_UNICODE[2] |
|
247 | assert tf.transform(HELP_UNICODE[0]) == HELP_UNICODE[2] | |
247 |
|
248 | |||
248 | def test_find_assign_op_dedent(): |
|
249 | def test_find_assign_op_dedent(): | |
249 | """ |
|
250 | """ | |
250 | be careful that empty token like dedent are not counted as parens |
|
251 | be careful that empty token like dedent are not counted as parens | |
251 | """ |
|
252 | """ | |
252 | class Tk: |
|
253 | class Tk: | |
253 | def __init__(self, s): |
|
254 | def __init__(self, s): | |
254 | self.string = s |
|
255 | self.string = s | |
255 |
|
256 | |||
256 | assert _find_assign_op([Tk(s) for s in ("", "a", "=", "b")]) == 2 |
|
257 | assert _find_assign_op([Tk(s) for s in ("", "a", "=", "b")]) == 2 | |
257 | assert ( |
|
258 | assert ( | |
258 | _find_assign_op([Tk(s) for s in ("", "(", "a", "=", "b", ")", "=", "5")]) == 6 |
|
259 | _find_assign_op([Tk(s) for s in ("", "(", "a", "=", "b", ")", "=", "5")]) == 6 | |
259 | ) |
|
260 | ) | |
260 |
|
261 | |||
261 |
|
262 | |||
262 | examples = [ |
|
263 | examples = [ | |
263 | pytest.param("a = 1", "complete", None), |
|
264 | pytest.param("a = 1", "complete", None), | |
264 | pytest.param("for a in range(5):", "incomplete", 4), |
|
265 | pytest.param("for a in range(5):", "incomplete", 4), | |
265 | pytest.param("for a in range(5):\n if a > 0:", "incomplete", 8), |
|
266 | pytest.param("for a in range(5):\n if a > 0:", "incomplete", 8), | |
266 | pytest.param("raise = 2", "invalid", None), |
|
267 | pytest.param("raise = 2", "invalid", None), | |
267 | pytest.param("a = [1,\n2,", "incomplete", 0), |
|
268 | pytest.param("a = [1,\n2,", "incomplete", 0), | |
268 | pytest.param("(\n))", "incomplete", 0), |
|
269 | pytest.param("(\n))", "incomplete", 0), | |
269 | pytest.param("\\\r\n", "incomplete", 0), |
|
270 | pytest.param("\\\r\n", "incomplete", 0), | |
270 | pytest.param("a = '''\n hi", "incomplete", 3), |
|
271 | pytest.param("a = '''\n hi", "incomplete", 3), | |
271 | pytest.param("def a():\n x=1\n global x", "invalid", None), |
|
272 | pytest.param("def a():\n x=1\n global x", "invalid", None), | |
272 | pytest.param( |
|
273 | pytest.param( | |
273 | "a \\ ", |
|
274 | "a \\ ", | |
274 | "invalid", |
|
275 | "invalid", | |
275 | None, |
|
276 | None, | |
276 | marks=pytest.mark.xfail( |
|
277 | marks=pytest.mark.xfail( | |
277 | reason="Bug in python 3.9.8 βΒ bpo 45738", |
|
278 | reason="Bug in python 3.9.8 βΒ bpo 45738", | |
278 | condition=sys.version_info[:3] == (3, 9, 8), |
|
279 | condition=sys.version_info[:3] == (3, 9, 8), | |
279 | raises=SystemError, |
|
280 | raises=SystemError, | |
280 | strict=True, |
|
281 | strict=True, | |
281 | ), |
|
282 | ), | |
282 | ), # Nothing allowed after backslash, |
|
283 | ), # Nothing allowed after backslash, | |
283 | pytest.param("1\\\n+2", "complete", None), |
|
284 | pytest.param("1\\\n+2", "complete", None), | |
284 | ] |
|
285 | ] | |
285 |
|
286 | |||
286 |
|
287 | |||
287 | @skip_iptest_but_not_pytest |
|
288 | @skip_iptest_but_not_pytest | |
288 | @pytest.mark.parametrize("code, expected, number", examples) |
|
289 | @pytest.mark.parametrize("code, expected, number", examples) | |
289 | def test_check_complete_param(code, expected, number): |
|
290 | def test_check_complete_param(code, expected, number): | |
290 | cc = ipt2.TransformerManager().check_complete |
|
291 | cc = ipt2.TransformerManager().check_complete | |
291 | assert cc(code) == (expected, number) |
|
292 | assert cc(code) == (expected, number) | |
292 |
|
293 | |||
293 |
|
294 | |||
294 | @skip_iptest_but_not_pytest |
|
295 | @skip_iptest_but_not_pytest | |
295 | @pytest.mark.xfail( |
|
296 | @pytest.mark.xfail( | |
296 | reason="Bug in python 3.9.8 βΒ bpo 45738", |
|
297 | reason="Bug in python 3.9.8 βΒ bpo 45738", | |
297 | condition=sys.version_info[:3] == (3, 9, 8), |
|
298 | condition=sys.version_info[:3] == (3, 9, 8), | |
298 | ) |
|
299 | ) | |
299 | def test_check_complete(): |
|
300 | def test_check_complete(): | |
300 | cc = ipt2.TransformerManager().check_complete |
|
301 | cc = ipt2.TransformerManager().check_complete | |
301 |
|
302 | |||
302 | example = dedent(""" |
|
303 | example = dedent(""" | |
303 | if True: |
|
304 | if True: | |
304 | a=1""" ) |
|
305 | a=1""" ) | |
305 |
|
306 | |||
306 | assert cc(example) == ("incomplete", 4) |
|
307 | assert cc(example) == ("incomplete", 4) | |
307 | assert cc(example + "\n") == ("complete", None) |
|
308 | assert cc(example + "\n") == ("complete", None) | |
308 | assert cc(example + "\n ") == ("complete", None) |
|
309 | assert cc(example + "\n ") == ("complete", None) | |
309 |
|
310 | |||
310 | # no need to loop on all the letters/numbers. |
|
311 | # no need to loop on all the letters/numbers. | |
311 | short = '12abAB'+string.printable[62:] |
|
312 | short = '12abAB'+string.printable[62:] | |
312 | for c in short: |
|
313 | for c in short: | |
313 | # test does not raise: |
|
314 | # test does not raise: | |
314 | cc(c) |
|
315 | cc(c) | |
315 | for k in short: |
|
316 | for k in short: | |
316 | cc(c+k) |
|
317 | cc(c+k) | |
317 |
|
318 | |||
318 | assert cc("def f():\n x=0\n \\\n ") == ("incomplete", 2) |
|
319 | assert cc("def f():\n x=0\n \\\n ") == ("incomplete", 2) | |
319 |
|
320 | |||
320 |
|
321 | |||
321 | def test_check_complete_II(): |
|
322 | def test_check_complete_II(): | |
322 | """ |
|
323 | """ | |
323 | Test that multiple line strings are properly handled. |
|
324 | Test that multiple line strings are properly handled. | |
324 |
|
325 | |||
325 | Separate test function for convenience |
|
326 | Separate test function for convenience | |
326 |
|
327 | |||
327 | """ |
|
328 | """ | |
328 | cc = ipt2.TransformerManager().check_complete |
|
329 | cc = ipt2.TransformerManager().check_complete | |
329 | assert cc('''def foo():\n """''') == ("incomplete", 4) |
|
330 | assert cc('''def foo():\n """''') == ("incomplete", 4) | |
330 |
|
331 | |||
331 |
|
332 | |||
332 | def test_check_complete_invalidates_sunken_brackets(): |
|
333 | def test_check_complete_invalidates_sunken_brackets(): | |
333 | """ |
|
334 | """ | |
334 | Test that a single line with more closing brackets than the opening ones is |
|
335 | Test that a single line with more closing brackets than the opening ones is | |
335 | interpreted as invalid |
|
336 | interpreted as invalid | |
336 | """ |
|
337 | """ | |
337 | cc = ipt2.TransformerManager().check_complete |
|
338 | cc = ipt2.TransformerManager().check_complete | |
338 | assert cc(")") == ("invalid", None) |
|
339 | assert cc(")") == ("invalid", None) | |
339 | assert cc("]") == ("invalid", None) |
|
340 | assert cc("]") == ("invalid", None) | |
340 | assert cc("}") == ("invalid", None) |
|
341 | assert cc("}") == ("invalid", None) | |
341 | assert cc(")(") == ("invalid", None) |
|
342 | assert cc(")(") == ("invalid", None) | |
342 | assert cc("][") == ("invalid", None) |
|
343 | assert cc("][") == ("invalid", None) | |
343 | assert cc("}{") == ("invalid", None) |
|
344 | assert cc("}{") == ("invalid", None) | |
344 | assert cc("]()(") == ("invalid", None) |
|
345 | assert cc("]()(") == ("invalid", None) | |
345 | assert cc("())(") == ("invalid", None) |
|
346 | assert cc("())(") == ("invalid", None) | |
346 | assert cc(")[](") == ("invalid", None) |
|
347 | assert cc(")[](") == ("invalid", None) | |
347 | assert cc("()](") == ("invalid", None) |
|
348 | assert cc("()](") == ("invalid", None) | |
348 |
|
349 | |||
349 |
|
350 | |||
350 | def test_null_cleanup_transformer(): |
|
351 | def test_null_cleanup_transformer(): | |
351 | manager = ipt2.TransformerManager() |
|
352 | manager = ipt2.TransformerManager() | |
352 | manager.cleanup_transforms.insert(0, null_cleanup_transformer) |
|
353 | manager.cleanup_transforms.insert(0, null_cleanup_transformer) | |
353 | assert manager.transform_cell("") == "" |
|
354 | assert manager.transform_cell("") == "" | |
354 |
|
355 | |||
355 |
|
356 | |||
356 |
|
357 | |||
357 |
|
358 | |||
358 | def test_side_effects_I(): |
|
359 | def test_side_effects_I(): | |
359 | count = 0 |
|
360 | count = 0 | |
360 | def counter(lines): |
|
361 | def counter(lines): | |
361 | nonlocal count |
|
362 | nonlocal count | |
362 | count += 1 |
|
363 | count += 1 | |
363 | return lines |
|
364 | return lines | |
364 |
|
365 | |||
365 | counter.has_side_effects = True |
|
366 | counter.has_side_effects = True | |
366 |
|
367 | |||
367 | manager = ipt2.TransformerManager() |
|
368 | manager = ipt2.TransformerManager() | |
368 | manager.cleanup_transforms.insert(0, counter) |
|
369 | manager.cleanup_transforms.insert(0, counter) | |
369 | assert manager.check_complete("a=1\n") == ('complete', None) |
|
370 | assert manager.check_complete("a=1\n") == ('complete', None) | |
370 | assert count == 0 |
|
371 | assert count == 0 | |
371 |
|
372 | |||
372 |
|
373 | |||
373 |
|
374 | |||
374 |
|
375 | |||
375 | def test_side_effects_II(): |
|
376 | def test_side_effects_II(): | |
376 | count = 0 |
|
377 | count = 0 | |
377 | def counter(lines): |
|
378 | def counter(lines): | |
378 | nonlocal count |
|
379 | nonlocal count | |
379 | count += 1 |
|
380 | count += 1 | |
380 | return lines |
|
381 | return lines | |
381 |
|
382 | |||
382 | counter.has_side_effects = True |
|
383 | counter.has_side_effects = True | |
383 |
|
384 | |||
384 | manager = ipt2.TransformerManager() |
|
385 | manager = ipt2.TransformerManager() | |
385 | manager.line_transforms.insert(0, counter) |
|
386 | manager.line_transforms.insert(0, counter) | |
386 | assert manager.check_complete("b=1\n") == ('complete', None) |
|
387 | assert manager.check_complete("b=1\n") == ('complete', None) | |
387 | assert count == 0 |
|
388 | assert count == 0 |
General Comments 0
You need to be logged in to leave comments.
Login now