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