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