Show More
@@ -0,0 +1,41 b'' | |||||
|
1 | from IPython.utils.capture import capture_output | |||
|
2 | ||||
|
3 | import nose.tools as nt | |||
|
4 | ||||
|
5 | def test_alias_lifecycle(): | |||
|
6 | name = 'test_alias1' | |||
|
7 | cmd = 'echo "Hello"' | |||
|
8 | am = _ip.alias_manager | |||
|
9 | am.clear_aliases() | |||
|
10 | am.define_alias(name, cmd) | |||
|
11 | assert am.is_alias(name) | |||
|
12 | nt.assert_equal(am.retrieve_alias(name), cmd) | |||
|
13 | nt.assert_in((name, cmd), am.aliases) | |||
|
14 | ||||
|
15 | # Test running the alias | |||
|
16 | orig_system = _ip.system | |||
|
17 | result = [] | |||
|
18 | _ip.system = result.append | |||
|
19 | try: | |||
|
20 | _ip.run_cell('%{}'.format(name)) | |||
|
21 | result = [c.strip() for c in result] | |||
|
22 | nt.assert_equal(result, [cmd]) | |||
|
23 | finally: | |||
|
24 | _ip.system = orig_system | |||
|
25 | ||||
|
26 | # Test removing the alias | |||
|
27 | am.undefine_alias(name) | |||
|
28 | assert not am.is_alias(name) | |||
|
29 | with nt.assert_raises(ValueError): | |||
|
30 | am.retrieve_alias(name) | |||
|
31 | nt.assert_not_in((name, cmd), am.aliases) | |||
|
32 | ||||
|
33 | ||||
|
34 | def test_alias_args_error(): | |||
|
35 | """Error expanding with wrong number of arguments""" | |||
|
36 | _ip.alias_manager.define_alias('parts', 'echo first %s second %s') | |||
|
37 | # capture stderr: | |||
|
38 | with capture_output() as cap: | |||
|
39 | _ip.run_cell('parts 1') | |||
|
40 | ||||
|
41 | nt.assert_equal(cap.stderr.split(':')[0], 'UsageError') No newline at end of file |
@@ -106,17 +106,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
106 | ip.run_cell('a = """\n%exit\n"""') |
|
106 | ip.run_cell('a = """\n%exit\n"""') | |
107 | self.assertEqual(ip.user_ns['a'], '\n%exit\n') |
|
107 | self.assertEqual(ip.user_ns['a'], '\n%exit\n') | |
108 |
|
108 | |||
109 | def test_alias_crash(self): |
|
|||
110 | """Errors in prefilter can't crash IPython""" |
|
|||
111 | ip.run_cell('%alias parts echo first %s second %s') |
|
|||
112 | # capture stderr: |
|
|||
113 | save_err = io.stderr |
|
|||
114 | io.stderr = StringIO() |
|
|||
115 | ip.run_cell('parts 1') |
|
|||
116 | err = io.stderr.getvalue() |
|
|||
117 | io.stderr = save_err |
|
|||
118 | self.assertEqual(err.split(':')[0], 'ERROR') |
|
|||
119 |
|
||||
120 | def test_trailing_newline(self): |
|
109 | def test_trailing_newline(self): | |
121 | """test that running !(command) does not raise a SyntaxError""" |
|
110 | """test that running !(command) does not raise a SyntaxError""" | |
122 | ip.run_cell('!(true)\n', False) |
|
111 | ip.run_cell('!(true)\n', False) |
General Comments 0
You need to be logged in to leave comments.
Login now