From 7c779e95cc68cff9599a251e8cb9325c8e6df7ad 2013-09-19 17:56:11 From: Thomas Kluyver Date: 2013-09-19 17:56:11 Subject: [PATCH] Add test for alias creation, use and destruction --- diff --git a/IPython/core/tests/test_alias.py b/IPython/core/tests/test_alias.py new file mode 100644 index 0000000..8ae57f6 --- /dev/null +++ b/IPython/core/tests/test_alias.py @@ -0,0 +1,41 @@ +from IPython.utils.capture import capture_output + +import nose.tools as nt + +def test_alias_lifecycle(): + name = 'test_alias1' + cmd = 'echo "Hello"' + am = _ip.alias_manager + am.clear_aliases() + am.define_alias(name, cmd) + assert am.is_alias(name) + nt.assert_equal(am.retrieve_alias(name), cmd) + nt.assert_in((name, cmd), am.aliases) + + # Test running the alias + orig_system = _ip.system + result = [] + _ip.system = result.append + try: + _ip.run_cell('%{}'.format(name)) + result = [c.strip() for c in result] + nt.assert_equal(result, [cmd]) + finally: + _ip.system = orig_system + + # Test removing the alias + am.undefine_alias(name) + assert not am.is_alias(name) + with nt.assert_raises(ValueError): + am.retrieve_alias(name) + nt.assert_not_in((name, cmd), am.aliases) + + +def test_alias_args_error(): + """Error expanding with wrong number of arguments""" + _ip.alias_manager.define_alias('parts', 'echo first %s second %s') + # capture stderr: + with capture_output() as cap: + _ip.run_cell('parts 1') + + nt.assert_equal(cap.stderr.split(':')[0], 'UsageError') \ No newline at end of file diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index 954f60c..ee6569c 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -106,17 +106,6 @@ class InteractiveShellTestCase(unittest.TestCase): ip.run_cell('a = """\n%exit\n"""') self.assertEqual(ip.user_ns['a'], '\n%exit\n') - def test_alias_crash(self): - """Errors in prefilter can't crash IPython""" - ip.run_cell('%alias parts echo first %s second %s') - # capture stderr: - save_err = io.stderr - io.stderr = StringIO() - ip.run_cell('parts 1') - err = io.stderr.getvalue() - io.stderr = save_err - self.assertEqual(err.split(':')[0], 'ERROR') - def test_trailing_newline(self): """test that running !(command) does not raise a SyntaxError""" ip.run_cell('!(true)\n', False)