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