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