Show More
@@ -1,126 +1,154 b'' | |||
|
1 | 1 | """Tests for the line-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 for tests for token-based transformers. |
|
5 | 5 | """ |
|
6 | 6 | import nose.tools as nt |
|
7 | 7 | |
|
8 | 8 | from IPython.core import inputtransformer2 as ipt2 |
|
9 | 9 | |
|
10 | 10 | CELL_MAGIC = ("""\ |
|
11 | 11 | %%foo arg |
|
12 | 12 | body 1 |
|
13 | 13 | body 2 |
|
14 | 14 | """, """\ |
|
15 | 15 | get_ipython().run_cell_magic('foo', 'arg', 'body 1\\nbody 2\\n') |
|
16 | 16 | """) |
|
17 | 17 | |
|
18 | 18 | def test_cell_magic(): |
|
19 | 19 | for sample, expected in [CELL_MAGIC]: |
|
20 | 20 | nt.assert_equal(ipt2.cell_magic(sample.splitlines(keepends=True)), |
|
21 | 21 | expected.splitlines(keepends=True)) |
|
22 | 22 | |
|
23 | 23 | CLASSIC_PROMPT = ("""\ |
|
24 | 24 | >>> for a in range(5): |
|
25 | 25 | ... print(a) |
|
26 | 26 | """, """\ |
|
27 | 27 | for a in range(5): |
|
28 | 28 | print(a) |
|
29 | 29 | """) |
|
30 | 30 | |
|
31 | 31 | CLASSIC_PROMPT_L2 = ("""\ |
|
32 | 32 | for a in range(5): |
|
33 | 33 | ... print(a) |
|
34 | 34 | ... print(a ** 2) |
|
35 | 35 | """, """\ |
|
36 | 36 | for a in range(5): |
|
37 | 37 | print(a) |
|
38 | 38 | print(a ** 2) |
|
39 | 39 | """) |
|
40 | 40 | |
|
41 | 41 | def test_classic_prompt(): |
|
42 | 42 | for sample, expected in [CLASSIC_PROMPT, CLASSIC_PROMPT_L2]: |
|
43 | 43 | nt.assert_equal(ipt2.classic_prompt(sample.splitlines(keepends=True)), |
|
44 | 44 | expected.splitlines(keepends=True)) |
|
45 | 45 | |
|
46 | 46 | IPYTHON_PROMPT = ("""\ |
|
47 | 47 | In [1]: for a in range(5): |
|
48 | 48 | ...: print(a) |
|
49 | 49 | """, """\ |
|
50 | 50 | for a in range(5): |
|
51 | 51 | print(a) |
|
52 | 52 | """) |
|
53 | 53 | |
|
54 | 54 | IPYTHON_PROMPT_L2 = ("""\ |
|
55 | 55 | for a in range(5): |
|
56 | 56 | ...: print(a) |
|
57 | 57 | ...: print(a ** 2) |
|
58 | 58 | """, """\ |
|
59 | 59 | for a in range(5): |
|
60 | 60 | print(a) |
|
61 | 61 | print(a ** 2) |
|
62 | 62 | """) |
|
63 | 63 | |
|
64 | ||
|
65 | IPYTHON_PROMPT_VI_INS = ("""\ | |
|
66 | [ins] In [11]: def a(): | |
|
67 | ...: 123 | |
|
68 | ...: | |
|
69 | ...: 123 | |
|
70 | """, """\ | |
|
71 | def a(): | |
|
72 | 123 | |
|
73 | ||
|
74 | 123 | |
|
75 | """) | |
|
76 | ||
|
77 | IPYTHON_PROMPT_VI_NAV = ("""\ | |
|
78 | [nav] In [11]: def a(): | |
|
79 | ...: 123 | |
|
80 | ...: | |
|
81 | ...: 123 | |
|
82 | """, """\ | |
|
83 | def a(): | |
|
84 | 123 | |
|
85 | ||
|
86 | 123 | |
|
87 | """) | |
|
88 | ||
|
89 | ||
|
64 | 90 | def test_ipython_prompt(): |
|
65 |
for sample, expected in [IPYTHON_PROMPT, IPYTHON_PROMPT_L2 |
|
|
91 | for sample, expected in [IPYTHON_PROMPT, IPYTHON_PROMPT_L2, | |
|
92 | IPYTHON_PROMPT_VI_INS, IPYTHON_PROMPT_VI_NAV]: | |
|
66 | 93 | nt.assert_equal(ipt2.ipython_prompt(sample.splitlines(keepends=True)), |
|
67 | 94 | expected.splitlines(keepends=True)) |
|
68 | 95 | |
|
96 | ||
|
69 | 97 | INDENT_SPACES = ("""\ |
|
70 | 98 | if True: |
|
71 | 99 | a = 3 |
|
72 | 100 | """, """\ |
|
73 | 101 | if True: |
|
74 | 102 | a = 3 |
|
75 | 103 | """) |
|
76 | 104 | |
|
77 | 105 | INDENT_TABS = ("""\ |
|
78 | 106 | \tif True: |
|
79 | 107 | \t\tb = 4 |
|
80 | 108 | """, """\ |
|
81 | 109 | if True: |
|
82 | 110 | \tb = 4 |
|
83 | 111 | """) |
|
84 | 112 | |
|
85 | 113 | def test_leading_indent(): |
|
86 | 114 | for sample, expected in [INDENT_SPACES, INDENT_TABS]: |
|
87 | 115 | nt.assert_equal(ipt2.leading_indent(sample.splitlines(keepends=True)), |
|
88 | 116 | expected.splitlines(keepends=True)) |
|
89 | 117 | |
|
90 | 118 | LEADING_EMPTY_LINES = ("""\ |
|
91 | 119 | \t |
|
92 | 120 | |
|
93 | 121 | if True: |
|
94 | 122 | a = 3 |
|
95 | 123 | |
|
96 | 124 | b = 4 |
|
97 | 125 | """, """\ |
|
98 | 126 | if True: |
|
99 | 127 | a = 3 |
|
100 | 128 | |
|
101 | 129 | b = 4 |
|
102 | 130 | """) |
|
103 | 131 | |
|
104 | 132 | ONLY_EMPTY_LINES = ("""\ |
|
105 | 133 | \t |
|
106 | 134 | |
|
107 | 135 | """, """\ |
|
108 | 136 | \t |
|
109 | 137 | |
|
110 | 138 | """) |
|
111 | 139 | |
|
112 | 140 | def test_leading_empty_lines(): |
|
113 | 141 | for sample, expected in [LEADING_EMPTY_LINES, ONLY_EMPTY_LINES]: |
|
114 | 142 | nt.assert_equal( |
|
115 | 143 | ipt2.leading_empty_lines(sample.splitlines(keepends=True)), |
|
116 | 144 | expected.splitlines(keepends=True)) |
|
117 | 145 | |
|
118 | 146 | CRLF_MAGIC = ([ |
|
119 | 147 | "%%ls\r\n" |
|
120 | 148 | ], [ |
|
121 | 149 | "get_ipython().run_cell_magic('ls', '', '')\n" |
|
122 | 150 | ]) |
|
123 | 151 | |
|
124 | 152 | def test_crlf_magic(): |
|
125 | 153 | for sample, expected in [CRLF_MAGIC]: |
|
126 | nt.assert_equal(ipt2.cell_magic(sample), expected) No newline at end of file | |
|
154 | nt.assert_equal(ipt2.cell_magic(sample), expected) |
General Comments 0
You need to be logged in to leave comments.
Login now