diff --git a/IPython/core/tests/test_magic_terminal.py b/IPython/core/tests/test_magic_terminal.py index fbea5f7..19d75bd 100644 --- a/IPython/core/tests/test_magic_terminal.py +++ b/IPython/core/tests/test_magic_terminal.py @@ -10,12 +10,18 @@ from __future__ import absolute_import import sys from StringIO import StringIO +from unittest import TestCase import nose.tools as nt from IPython.testing import tools as tt #----------------------------------------------------------------------------- +# Globals +#----------------------------------------------------------------------------- +ip = get_ipython() + +#----------------------------------------------------------------------------- # Test functions begin #----------------------------------------------------------------------------- @@ -84,87 +90,92 @@ def test_cpaste(): check_cpaste(code, should_fail=True) -# Multiple tests for clipboard pasting -def test_paste(): - _ip = get_ipython() +class PasteTestCase(TestCase): + """Multiple tests for clipboard pasting""" - def paste(txt, flags='-q'): + def paste(self, txt, flags='-q'): """Paste input text, by default in quiet mode""" - hooks.clipboard_get = lambda : txt - _ip.magic('paste '+flags) - - # Inject fake clipboard hook but save original so we can restore it later - hooks = _ip.hooks - user_ns = _ip.user_ns - original_clip = hooks.clipboard_get - - try: - # Run tests with fake clipboard function - user_ns.pop('x', None) - paste('x=1') - nt.assert_equal(user_ns['x'], 1) + ip.hooks.clipboard_get = lambda : txt + ip.magic('paste '+flags) - user_ns.pop('x', None) - paste('>>> x=2') - nt.assert_equal(user_ns['x'], 2) + def setUp(self): + # Inject fake clipboard hook but save original so we can restore it later + self.original_clip = ip.hooks.clipboard_get - paste(""" + def tearDown(self): + # Restore original hook + ip.hooks.clipboard_get = self.original_clip + + def test_paste(self): + ip.user_ns.pop('x', None) + self.paste('x=1') + nt.assert_equal(ip.user_ns['x'], 1) + + def test_paste_pyprompt(self): + ip.user_ns.pop('x', None) + self.paste('>>> x=2') + nt.assert_equal(ip.user_ns['x'], 2) + + def test_paste_py_multi(self): + self.paste(""" >>> x = [1,2,3] >>> y = [] >>> for i in x: ... y.append(i**2) ... """) - nt.assert_equal(user_ns['x'], [1,2,3]) - nt.assert_equal(user_ns['y'], [1,4,9]) - - # Now, test that paste -r works - user_ns.pop('x', None) - nt.assert_false('x' in user_ns) - _ip.magic('paste -r') - nt.assert_equal(user_ns['x'], [1,2,3]) - - # Test pasting of email-quoted contents - paste(""" + nt.assert_equal(ip.user_ns['x'], [1,2,3]) + nt.assert_equal(ip.user_ns['y'], [1,4,9]) + + def test_paste_py_multi_r(self): + "Now, test that self.paste -r works" + ip.user_ns.pop('x', None) + nt.assert_false('x' in ip.user_ns) + ip.magic('paste -r') + nt.assert_equal(ip.user_ns['x'], [1,2,3]) + + def test_paste_email(self): + "Test pasting of email-quoted contents" + self.paste(""" >> def foo(x): >> return x + 1 >> x = foo(1.1) """) - nt.assert_equal(user_ns['x'], 2.1) + nt.assert_equal(ip.user_ns['x'], 2.1) - # Email again; some programs add a space also at each quoting level - paste(""" + def test_paste_email2(self): + "Email again; some programs add a space also at each quoting level" + self.paste(""" > > def foo(x): > > return x + 1 > > x = foo(2.1) """) - nt.assert_equal(user_ns['x'], 3.1) + nt.assert_equal(ip.user_ns['x'], 3.1) - # Email quoting of interactive input - paste(""" + def test_paste_email_py(self): + "Email quoting of interactive input" + self.paste(""" >> >>> def f(x): >> ... return x+1 >> ... >> >>> x = f(2.5) """) - nt.assert_equal(user_ns['x'], 3.5) + nt.assert_equal(ip.user_ns['x'], 3.5) - # Also test paste echoing, by temporarily faking the writer + def test_paste_echo(self): + "Also test self.paste echoing, by temporarily faking the writer" w = StringIO() - writer = _ip.write - _ip.write = w.write + writer = ip.write + ip.write = w.write code = """ a = 100 b = 200""" try: - paste(code,'') + self.paste(code,'') out = w.getvalue() finally: - _ip.write = writer - nt.assert_equal(user_ns['a'], 100) - nt.assert_equal(user_ns['b'], 200) + ip.write = writer + nt.assert_equal(ip.user_ns['a'], 100) + nt.assert_equal(ip.user_ns['b'], 200) nt.assert_equal(out, code+"\n## -- End pasted text --\n") - finally: - # Restore original hook - hooks.clipboard_get = original_clip