diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 798c20c..7cf8d0e 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -299,3 +299,59 @@ def test_dirops(): nt.assert_equal(curpath(), startdir) finally: os.chdir(startdir) + + +def check_cpaste(code, should_fail=False): + """Execute code via 'cpaste' and ensure it was executed, unless + should_fail is set. + """ + _ip.user_ns['code_ran'] = False + + src = StringIO() + src.write('\n') + src.write(code) + src.write('\n--\n') + src.seek(0) + + stdin_save = sys.stdin + sys.stdin = src + + try: + _ip.magic('cpaste') + except: + if not should_fail: + raise AssertionError("Failure not expected : '%s'" % + code) + else: + assert _ip.user_ns['code_ran'] + if should_fail: + raise AssertionError("Failure expected : '%s'" % code) + finally: + sys.stdin = stdin_save + + +def test_cpaste(): + """Test cpaste magic""" + + def run(): + """Marker function: sets a flag when executed. + """ + _ip.user_ns['code_ran'] = True + return 'run' # return string so '+ run()' doesn't result in success + + tests = {'pass': ["> > > run()", + ">>> > run()", + "+++ run()", + "++ run()", + " >>> run()"], + + 'fail': ["+ + run()", + " ++ run()"]} + + _ip.user_ns['run'] = run + + for code in tests['pass']: + check_cpaste(code) + + for code in tests['fail']: + check_cpaste(code, should_fail=True) diff --git a/test/test_cpaste.ipy b/test/test_cpaste.ipy deleted file mode 100644 index 259e930..0000000 --- a/test/test_cpaste.ipy +++ /dev/null @@ -1,62 +0,0 @@ -"""Test cpaste magic""" - -tests = {'pass': ["> > > run()", - ">>> > run()", - "+++ run()", - "++ run()", - " >>> run()"], - - 'fail': ["+ + run()", - " ++ run()"]} - -from StringIO import StringIO -import sys - -stdin_save = sys.stdin - -# NOTE: no blank lines allowed in function definition -def testcase(code,should_fail=False): - """Execute code via 'cpaste' and ensure it was executed, unless - should_fail is set. - - """ - _ip.user_ns['code_ran'] = False - # - src = StringIO() - src.write('\n') - src.write(code) - src.write('\n--\n') - src.seek(0) - # - sys.stdin = src - - try: - cpaste - except: - if not should_fail: - raise AssertionError("Failure not expected : '%s'" % - code) - else: - assert code_ran - if should_fail: - raise AssertionError("Failure expected : '%s'" % code) - # - finally: - sys.stdin = stdin_save - # - -def run(): - """Marker function: sets a flag when executed. - - """ - _ip.user_ns['code_ran'] = True - return 'run' # return string so '+ run()' doesn't result in success - - -### Actual testing happens here - -for code in tests['pass']: - testcase(code) - -for code in tests['fail']: - testcase(code,should_fail=True)