##// END OF EJS Templates
[core][tests][magic_terminal] Remove nose
Samuel Gaist -
Show More
@@ -1,192 +1,193 b''
1 """Tests for various magic functions specific to the terminal frontend.
1 """Tests for various magic functions specific to the terminal frontend.
2
2
3 Needs to be run by nose (to make ipython session available).
3 Needs to be run by nose (to make ipython session available).
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Imports
7 # Imports
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9
9
10 import sys
10 import sys
11 from io import StringIO
11 from io import StringIO
12 from unittest import TestCase
12 from unittest import TestCase
13
13
14 import nose.tools as nt
15
16 from IPython.testing import tools as tt
14 from IPython.testing import tools as tt
17
15
18 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
19 # Test functions begin
17 # Test functions begin
20 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
21
19
22 def check_cpaste(code, should_fail=False):
20 def check_cpaste(code, should_fail=False):
23 """Execute code via 'cpaste' and ensure it was executed, unless
21 """Execute code via 'cpaste' and ensure it was executed, unless
24 should_fail is set.
22 should_fail is set.
25 """
23 """
26 ip.user_ns['code_ran'] = False
24 ip.user_ns['code_ran'] = False
27
25
28 src = StringIO()
26 src = StringIO()
29 if not hasattr(src, 'encoding'):
27 if not hasattr(src, 'encoding'):
30 # IPython expects stdin to have an encoding attribute
28 # IPython expects stdin to have an encoding attribute
31 src.encoding = None
29 src.encoding = None
32 src.write(code)
30 src.write(code)
33 src.write('\n--\n')
31 src.write('\n--\n')
34 src.seek(0)
32 src.seek(0)
35
33
36 stdin_save = sys.stdin
34 stdin_save = sys.stdin
37 sys.stdin = src
35 sys.stdin = src
38
36
39 try:
37 try:
40 context = tt.AssertPrints if should_fail else tt.AssertNotPrints
38 context = tt.AssertPrints if should_fail else tt.AssertNotPrints
41 with context("Traceback (most recent call last)"):
39 with context("Traceback (most recent call last)"):
42 ip.magic('cpaste')
40 ip.magic('cpaste')
43
41
44 if not should_fail:
42 if not should_fail:
45 assert ip.user_ns['code_ran'], "%r failed" % code
43 assert ip.user_ns['code_ran'], "%r failed" % code
46 finally:
44 finally:
47 sys.stdin = stdin_save
45 sys.stdin = stdin_save
48
46
49 def test_cpaste():
47 def test_cpaste():
50 """Test cpaste magic"""
48 """Test cpaste magic"""
51
49
52 def runf():
50 def runf():
53 """Marker function: sets a flag when executed.
51 """Marker function: sets a flag when executed.
54 """
52 """
55 ip.user_ns['code_ran'] = True
53 ip.user_ns['code_ran'] = True
56 return 'runf' # return string so '+ runf()' doesn't result in success
54 return 'runf' # return string so '+ runf()' doesn't result in success
57
55
58 tests = {'pass': ["runf()",
56 tests = {'pass': ["runf()",
59 "In [1]: runf()",
57 "In [1]: runf()",
60 "In [1]: if 1:\n ...: runf()",
58 "In [1]: if 1:\n ...: runf()",
61 "> > > runf()",
59 "> > > runf()",
62 ">>> runf()",
60 ">>> runf()",
63 " >>> runf()",
61 " >>> runf()",
64 ],
62 ],
65
63
66 'fail': ["1 + runf()",
64 'fail': ["1 + runf()",
67 "++ runf()",
65 "++ runf()",
68 ]}
66 ]}
69
67
70 ip.user_ns['runf'] = runf
68 ip.user_ns['runf'] = runf
71
69
72 for code in tests['pass']:
70 for code in tests['pass']:
73 check_cpaste(code)
71 check_cpaste(code)
74
72
75 for code in tests['fail']:
73 for code in tests['fail']:
76 check_cpaste(code, should_fail=True)
74 check_cpaste(code, should_fail=True)
77
75
78
76
79 class PasteTestCase(TestCase):
77 class PasteTestCase(TestCase):
80 """Multiple tests for clipboard pasting"""
78 """Multiple tests for clipboard pasting"""
81
79
82 def paste(self, txt, flags='-q'):
80 def paste(self, txt, flags='-q'):
83 """Paste input text, by default in quiet mode"""
81 """Paste input text, by default in quiet mode"""
84 ip.hooks.clipboard_get = lambda : txt
82 ip.hooks.clipboard_get = lambda : txt
85 ip.magic('paste '+flags)
83 ip.magic('paste '+flags)
86
84
87 def setUp(self):
85 def setUp(self):
88 # Inject fake clipboard hook but save original so we can restore it later
86 # Inject fake clipboard hook but save original so we can restore it later
89 self.original_clip = ip.hooks.clipboard_get
87 self.original_clip = ip.hooks.clipboard_get
90
88
91 def tearDown(self):
89 def tearDown(self):
92 # Restore original hook
90 # Restore original hook
93 ip.hooks.clipboard_get = self.original_clip
91 ip.hooks.clipboard_get = self.original_clip
94
92
95 def test_paste(self):
93 def test_paste(self):
96 ip.user_ns.pop('x', None)
94 ip.user_ns.pop("x", None)
97 self.paste('x = 1')
95 self.paste("x = 1")
98 nt.assert_equal(ip.user_ns['x'], 1)
96 self.assertEqual(ip.user_ns["x"], 1)
99 ip.user_ns.pop('x')
97 ip.user_ns.pop("x")
100
98
101 def test_paste_pyprompt(self):
99 def test_paste_pyprompt(self):
102 ip.user_ns.pop('x', None)
100 ip.user_ns.pop("x", None)
103 self.paste('>>> x=2')
101 self.paste(">>> x=2")
104 nt.assert_equal(ip.user_ns['x'], 2)
102 self.assertEqual(ip.user_ns["x"], 2)
105 ip.user_ns.pop('x')
103 ip.user_ns.pop("x")
106
104
107 def test_paste_py_multi(self):
105 def test_paste_py_multi(self):
108 self.paste("""
106 self.paste("""
109 >>> x = [1,2,3]
107 >>> x = [1,2,3]
110 >>> y = []
108 >>> y = []
111 >>> for i in x:
109 >>> for i in x:
112 ... y.append(i**2)
110 ... y.append(i**2)
113 ...
111 ...
114 """)
112 """
115 nt.assert_equal(ip.user_ns['x'], [1,2,3])
113 )
116 nt.assert_equal(ip.user_ns['y'], [1,4,9])
114 self.assertEqual(ip.user_ns["x"], [1, 2, 3])
115 self.assertEqual(ip.user_ns["y"], [1, 4, 9])
117
116
118 def test_paste_py_multi_r(self):
117 def test_paste_py_multi_r(self):
119 "Now, test that self.paste -r works"
118 "Now, test that self.paste -r works"
120 self.test_paste_py_multi()
119 self.test_paste_py_multi()
121 nt.assert_equal(ip.user_ns.pop('x'), [1,2,3])
120 self.assertEqual(ip.user_ns.pop("x"), [1, 2, 3])
122 nt.assert_equal(ip.user_ns.pop('y'), [1,4,9])
121 self.assertEqual(ip.user_ns.pop("y"), [1, 4, 9])
123 nt.assert_false('x' in ip.user_ns)
122 self.assertFalse("x" in ip.user_ns)
124 ip.magic('paste -r')
123 ip.magic("paste -r")
125 nt.assert_equal(ip.user_ns['x'], [1,2,3])
124 self.assertEqual(ip.user_ns["x"], [1, 2, 3])
126 nt.assert_equal(ip.user_ns['y'], [1,4,9])
125 self.assertEqual(ip.user_ns["y"], [1, 4, 9])
127
126
128 def test_paste_email(self):
127 def test_paste_email(self):
129 "Test pasting of email-quoted contents"
128 "Test pasting of email-quoted contents"
130 self.paste("""\
129 self.paste("""\
131 >> def foo(x):
130 >> def foo(x):
132 >> return x + 1
131 >> return x + 1
133 >> xx = foo(1.1)""")
132 >> xx = foo(1.1)"""
134 nt.assert_equal(ip.user_ns['xx'], 2.1)
133 )
134 self.assertEqual(ip.user_ns["xx"], 2.1)
135
135
136 def test_paste_email2(self):
136 def test_paste_email2(self):
137 "Email again; some programs add a space also at each quoting level"
137 "Email again; some programs add a space also at each quoting level"
138 self.paste("""\
138 self.paste("""\
139 > > def foo(x):
139 > > def foo(x):
140 > > return x + 1
140 > > return x + 1
141 > > yy = foo(2.1) """)
141 > > yy = foo(2.1) """
142 nt.assert_equal(ip.user_ns['yy'], 3.1)
142 )
143 self.assertEqual(ip.user_ns["yy"], 3.1)
143
144
144 def test_paste_email_py(self):
145 def test_paste_email_py(self):
145 "Email quoting of interactive input"
146 "Email quoting of interactive input"
146 self.paste("""\
147 self.paste("""\
147 >> >>> def f(x):
148 >> >>> def f(x):
148 >> ... return x+1
149 >> ... return x+1
149 >> ...
150 >> ...
150 >> >>> zz = f(2.5) """)
151 >> >>> zz = f(2.5) """
151 nt.assert_equal(ip.user_ns['zz'], 3.5)
152 )
153 self.assertEqual(ip.user_ns["zz"], 3.5)
152
154
153 def test_paste_echo(self):
155 def test_paste_echo(self):
154 "Also test self.paste echoing, by temporarily faking the writer"
156 "Also test self.paste echoing, by temporarily faking the writer"
155 w = StringIO()
157 w = StringIO()
156 writer = ip.write
158 writer = ip.write
157 ip.write = w.write
159 ip.write = w.write
158 code = """
160 code = """
159 a = 100
161 a = 100
160 b = 200"""
162 b = 200"""
161 try:
163 try:
162 self.paste(code,'')
164 self.paste(code,'')
163 out = w.getvalue()
165 out = w.getvalue()
164 finally:
166 finally:
165 ip.write = writer
167 ip.write = writer
166 nt.assert_equal(ip.user_ns['a'], 100)
168 self.assertEqual(ip.user_ns["a"], 100)
167 nt.assert_equal(ip.user_ns['b'], 200)
169 self.assertEqual(ip.user_ns["b"], 200)
168 assert out == code+"\n## -- End pasted text --\n"
170 assert out == code + "\n## -- End pasted text --\n"
169
171
170 def test_paste_leading_commas(self):
172 def test_paste_leading_commas(self):
171 "Test multiline strings with leading commas"
173 "Test multiline strings with leading commas"
172 tm = ip.magics_manager.registry['TerminalMagics']
174 tm = ip.magics_manager.registry['TerminalMagics']
173 s = '''\
175 s = '''\
174 a = """
176 a = """
175 ,1,2,3
177 ,1,2,3
176 """'''
178 """'''
177 ip.user_ns.pop('foo', None)
179 ip.user_ns.pop("foo", None)
178 tm.store_or_execute(s, 'foo')
180 tm.store_or_execute(s, "foo")
179 nt.assert_in('foo', ip.user_ns)
181 self.assertIn("foo", ip.user_ns)
180
181
182
182 def test_paste_trailing_question(self):
183 def test_paste_trailing_question(self):
183 "Test pasting sources with trailing question marks"
184 "Test pasting sources with trailing question marks"
184 tm = ip.magics_manager.registry['TerminalMagics']
185 tm = ip.magics_manager.registry['TerminalMagics']
185 s = '''\
186 s = '''\
186 def funcfoo():
187 def funcfoo():
187 if True: #am i true?
188 if True: #am i true?
188 return 'fooresult'
189 return 'fooresult'
189 '''
190 '''
190 ip.user_ns.pop('funcfoo', None)
191 ip.user_ns.pop('funcfoo', None)
191 self.paste(s)
192 self.paste(s)
192 nt.assert_equal(ip.user_ns['funcfoo'](), 'fooresult')
193 self.assertEqual(ip.user_ns["funcfoo"](), "fooresult")
General Comments 0
You need to be logged in to leave comments. Login now