Debian Codegraph @

tests/test_inputtransformer2_line.py

raw
1 """Tests for the line-based transformers in IPython.core.inputtransformer2
2
3 Line-based transformers are the simpler ones; token-based transformers are
4 more complex. See test_inputtransformer2 for tests for token-based transformers.
5 """
6
7 import pytest
8
9 from IPython.core import inputtransformer2 as ipt2
10
11 CELL_MAGIC = (
12 """\
13 %%foo arg
14 body 1
15 body 2
16 """,
17 """\
18 get_ipython().run_cell_magic('foo', 'arg', 'body 1\\nbody 2\\n')
19 """,
20 )
21
22
23 def test_cell_magic():
24 for sample, expected in [CELL_MAGIC]:
25 assert ipt2.cell_magic(sample.splitlines(keepends=True)) == expected.splitlines(
26 keepends=True
27 )
28
29
30 CLASSIC_PROMPT = (
31 """\
32 >>> for a in range(5):
33 ... print(a)
34 """,
35 """\
36 for a in range(5):
37 print(a)
38 """,
39 )
40
41 CLASSIC_PROMPT_L2 = (
42 """\
43 for a in range(5):
44 ... print(a)
45 ... print(a ** 2)
46 """,
47 """\
48 for a in range(5):
49 ... print(a)
50 ... print(a ** 2)
51 """,
52 )
53
54 CLASSIC_PROMPT_L3 = (
55 """\
56 >>> \"\"\"
57 ... This code is inside a triple-quoted string.
58 ... >>> for a in range(5):
59 ... ... print(a)
60 ... \"\"\"
61 >>> for a in range(5):
62 ... print(a)
63 """,
64 """\
65 >>> \"\"\"
66 ... This code is inside a triple-quoted string.
67 ... >>> for a in range(5):
68 ... ... print(a)
69 ... \"\"\"
70 for a in range(5):
71 print(a)
72 """,
73 )
74
75 CLASSIC_PROMPT_DEDENT_SINGLE_LINE = (
76 ">>> print(1)\n",
77 "print(1)\n",
78 )
79
80 CLASSIC_PROMPT_DEDENT_LEADING_WS = (
81 " >>> print(1)\n",
82 "print(1)\n",
83 )
84
85 CLASSIC_PROMPT_MULTILINE_DOCTEST = (
86 """\
87 >>> for i in range(2):
88 ... print(i)
89 """,
90 """\
91 for i in range(2):
92 print(i)
93 """,
94 )
95
96 CLASSIC_PROMPT_STANDALONE_CONTINUATION = (
97 "... print(1)\n",
98 "... print(1)\n",
99 )
100
101
102 def test_classic_prompt():
103 for sample, expected in [
104 CLASSIC_PROMPT,
105 CLASSIC_PROMPT_L2,
106 CLASSIC_PROMPT_L3,
107 CLASSIC_PROMPT_DEDENT_SINGLE_LINE,
108 CLASSIC_PROMPT_DEDENT_LEADING_WS,
109 CLASSIC_PROMPT_MULTILINE_DOCTEST,
110 CLASSIC_PROMPT_STANDALONE_CONTINUATION,
111 ]:
112 assert ipt2.classic_prompt(
113 sample.splitlines(keepends=True)
114 ) == expected.splitlines(keepends=True)
115
116
117 IPYTHON_PROMPT = (
118 """\
119 In [1]: for a in range(5):
120 ...: print(a)
121 """,
122 """\
123 for a in range(5):
124 print(a)
125 """,
126 )
127
128 IPYTHON_PROMPT_L2 = (
129 """\
130 for a in range(5):
131 ...: print(a)
132 ...: print(a ** 2)
133 """,
134 """\
135 for a in range(5):
136 print(a)
137 print(a ** 2)
138 """,
139 )
140
141
142 IPYTHON_PROMPT_VI_INS = (
143 """\
144 [ins] In [11]: def a():
145 ...: 123
146 ...:
147 ...: 123
148 """,
149 """\
150 def a():
151 123
152
153 123
154 """,
155 )
156
157 IPYTHON_PROMPT_VI_NAV = (
158 """\
159 [nav] In [11]: def a():
160 ...: 123
161 ...:
162 ...: 123
163 """,
164 """\
165 def a():
166 123
167
168 123
169 """,
170 )
171
172
173 def test_ipython_prompt():
174 for sample, expected in [
175 IPYTHON_PROMPT,
176 IPYTHON_PROMPT_L2,
177 IPYTHON_PROMPT_VI_INS,
178 IPYTHON_PROMPT_VI_NAV,
179 ]:
180 assert ipt2.ipython_prompt(
181 sample.splitlines(keepends=True)
182 ) == expected.splitlines(keepends=True)
183
184
185 INDENT_SPACES = (
186 """\
187 if True:
188 a = 3
189 """,
190 """\
191 if True:
192 a = 3
193 """,
194 )
195
196 INDENT_TABS = (
197 """\
198 \tif True:
199 \t\tb = 4
200 """,
201 """\
202 if True:
203 \tb = 4
204 """,
205 )
206
207
208 def test_leading_indent():
209 for sample, expected in [INDENT_SPACES, INDENT_TABS]:
210 assert ipt2.leading_indent(
211 sample.splitlines(keepends=True)
212 ) == expected.splitlines(keepends=True)
213
214
215 INDENT_SPACES_COMMENT = (
216 """\
217 # comment
218 if True:
219 a = 3
220 """,
221 """\
222 # comment
223 if True:
224 a = 3
225 """,
226 )
227
228 INDENT_TABS_COMMENT = (
229 """\
230 \t# comment
231 if True:
232 \tb = 4
233 """,
234 """\
235 \t# comment
236 if True:
237 \tb = 4
238 """,
239 )
240
241
242 INDENTED_CODE_WITH_ALIGNED_COMMENT = (
243 """\
244 # comment
245 x = 1
246 print(x)
247 """,
248 """\
249 # comment
250 x = 1
251 print(x)
252 """,
253 )
254
255
256 @pytest.mark.parametrize(
257 "sample, expected",
258 [INDENT_SPACES_COMMENT, INDENT_TABS_COMMENT, INDENTED_CODE_WITH_ALIGNED_COMMENT],
259 )
260 def test_leading_indent_comment(sample, expected):
261 assert ipt2.leading_indent(sample.splitlines(keepends=True)) == expected.splitlines(
262 keepends=True
263 )
264
265
266 LEADING_EMPTY_LINES = (
267 """\
268 \t
269
270 if True:
271 a = 3
272
273 b = 4
274 """,
275 """\
276 if True:
277 a = 3
278
279 b = 4
280 """,
281 )
282
283 ONLY_EMPTY_LINES = (
284 """\
285 \t
286
287 """,
288 """\
289 \t
290
291 """,
292 )
293
294
295 def test_leading_empty_lines():
296 for sample, expected in [LEADING_EMPTY_LINES, ONLY_EMPTY_LINES]:
297 assert ipt2.leading_empty_lines(
298 sample.splitlines(keepends=True)
299 ) == expected.splitlines(keepends=True)
300
301
302 CRLF_MAGIC = (["%%ls\r\n"], ["get_ipython().run_cell_magic('ls', '', '')\n"])
303
304
305 def test_crlf_magic():
306 for sample, expected in [CRLF_MAGIC]:
307 assert ipt2.cell_magic(sample) == expected