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