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