Show More
@@ -1,88 +1,93 b'' | |||||
1 | """Tests for the key interactiveshell module. |
|
1 | """Tests for the key interactiveshell module. | |
2 |
|
2 | |||
3 | Historically the main classes in interactiveshell have been under-tested. This |
|
3 | Historically the main classes in interactiveshell have been under-tested. This | |
4 | module should grow as many single-method tests as possible to trap many of the |
|
4 | module should grow as many single-method tests as possible to trap many of the | |
5 | recurring bugs we seem to encounter with high-level interaction. |
|
5 | recurring bugs we seem to encounter with high-level interaction. | |
6 |
|
6 | |||
7 | Authors |
|
7 | Authors | |
8 | ------- |
|
8 | ------- | |
9 | * Fernando Perez |
|
9 | * Fernando Perez | |
10 | """ |
|
10 | """ | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Copyright (C) 2011 The IPython Development Team |
|
12 | # Copyright (C) 2011 The IPython Development Team | |
13 | # |
|
13 | # | |
14 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | # Distributed under the terms of the BSD License. The full license is in | |
15 | # the file COPYING, distributed as part of this software. |
|
15 | # the file COPYING, distributed as part of this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # stdlib |
|
21 | # stdlib | |
22 | import unittest |
|
22 | import unittest | |
23 | from IPython.testing import decorators as dec |
|
23 | from IPython.testing import decorators as dec | |
24 |
|
24 | |||
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | # Tests |
|
26 | # Tests | |
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 |
|
28 | |||
29 | class InteractiveShellTestCase(unittest.TestCase): |
|
29 | class InteractiveShellTestCase(unittest.TestCase): | |
30 | def test_naked_string_cells(self): |
|
30 | def test_naked_string_cells(self): | |
31 | """Test that cells with only naked strings are fully executed""" |
|
31 | """Test that cells with only naked strings are fully executed""" | |
32 | ip = get_ipython() |
|
32 | ip = get_ipython() | |
33 | # First, single-line inputs |
|
33 | # First, single-line inputs | |
34 | ip.run_cell('"a"\n') |
|
34 | ip.run_cell('"a"\n') | |
35 | self.assertEquals(ip.user_ns['_'], 'a') |
|
35 | self.assertEquals(ip.user_ns['_'], 'a') | |
36 | # And also multi-line cells |
|
36 | # And also multi-line cells | |
37 | ip.run_cell('"""a\nb"""\n') |
|
37 | ip.run_cell('"""a\nb"""\n') | |
38 | self.assertEquals(ip.user_ns['_'], 'a\nb') |
|
38 | self.assertEquals(ip.user_ns['_'], 'a\nb') | |
39 |
|
39 | |||
40 | def test_run_empty_cell(self): |
|
40 | def test_run_empty_cell(self): | |
41 | """Just make sure we don't get a horrible error with a blank |
|
41 | """Just make sure we don't get a horrible error with a blank | |
42 | cell of input. Yes, I did overlook that.""" |
|
42 | cell of input. Yes, I did overlook that.""" | |
43 | ip = get_ipython() |
|
43 | ip = get_ipython() | |
44 | old_xc = ip.execution_count |
|
44 | old_xc = ip.execution_count | |
45 | ip.run_cell('') |
|
45 | ip.run_cell('') | |
46 | self.assertEquals(ip.execution_count, old_xc) |
|
46 | self.assertEquals(ip.execution_count, old_xc) | |
47 |
|
47 | |||
48 | def test_run_cell_multiline(self): |
|
48 | def test_run_cell_multiline(self): | |
49 | """Multi-block, multi-line cells must execute correctly. |
|
49 | """Multi-block, multi-line cells must execute correctly. | |
50 | """ |
|
50 | """ | |
51 | ip = get_ipython() |
|
51 | ip = get_ipython() | |
52 | src = '\n'.join(["x=1", |
|
52 | src = '\n'.join(["x=1", | |
53 | "y=2", |
|
53 | "y=2", | |
54 | "if 1:", |
|
54 | "if 1:", | |
55 | " x += 1", |
|
55 | " x += 1", | |
56 | " y += 1",]) |
|
56 | " y += 1",]) | |
57 | ip.run_cell(src) |
|
57 | ip.run_cell(src) | |
58 | self.assertEquals(ip.user_ns['x'], 2) |
|
58 | self.assertEquals(ip.user_ns['x'], 2) | |
59 | self.assertEquals(ip.user_ns['y'], 3) |
|
59 | self.assertEquals(ip.user_ns['y'], 3) | |
60 |
|
60 | |||
61 | def test_multiline_string_cells(self): |
|
61 | def test_multiline_string_cells(self): | |
62 | "Code sprinkled with multiline strings should execute (GH-306)" |
|
62 | "Code sprinkled with multiline strings should execute (GH-306)" | |
63 | ip = get_ipython() |
|
63 | ip = get_ipython() | |
64 | ip.run_cell('tmp=0') |
|
64 | ip.run_cell('tmp=0') | |
65 | self.assertEquals(ip.user_ns['tmp'], 0) |
|
65 | self.assertEquals(ip.user_ns['tmp'], 0) | |
66 | ip.run_cell('tmp=1;"""a\nb"""\n') |
|
66 | ip.run_cell('tmp=1;"""a\nb"""\n') | |
67 | self.assertEquals(ip.user_ns['tmp'], 1) |
|
67 | self.assertEquals(ip.user_ns['tmp'], 1) | |
68 |
|
68 | |||
69 | def test_dont_cache_with_semicolon(self): |
|
69 | def test_dont_cache_with_semicolon(self): | |
70 | "Ending a line with semicolon should not cache the returned object (GH-307)" |
|
70 | "Ending a line with semicolon should not cache the returned object (GH-307)" | |
71 | ip = get_ipython() |
|
71 | ip = get_ipython() | |
72 | oldlen = len(ip.user_ns['Out']) |
|
72 | oldlen = len(ip.user_ns['Out']) | |
73 | a = ip.run_cell('1;') |
|
73 | a = ip.run_cell('1;') | |
74 | newlen = len(ip.user_ns['Out']) |
|
74 | newlen = len(ip.user_ns['Out']) | |
75 | self.assertEquals(oldlen, newlen) |
|
75 | self.assertEquals(oldlen, newlen) | |
76 | #also test the default caching behavior |
|
76 | #also test the default caching behavior | |
77 | ip.run_cell('1') |
|
77 | ip.run_cell('1') | |
78 | newlen = len(ip.user_ns['Out']) |
|
78 | newlen = len(ip.user_ns['Out']) | |
79 | self.assertEquals(oldlen+1, newlen) |
|
79 | self.assertEquals(oldlen+1, newlen) | |
80 |
|
80 | |||
81 | def test_In_variable(self): |
|
81 | def test_In_variable(self): | |
82 | "Verify that In variable grows with user input (GH-284)" |
|
82 | "Verify that In variable grows with user input (GH-284)" | |
83 | ip = get_ipython() |
|
83 | ip = get_ipython() | |
84 | oldlen = len(ip.user_ns['In']) |
|
84 | oldlen = len(ip.user_ns['In']) | |
85 | ip.run_cell('1;') |
|
85 | ip.run_cell('1;') | |
86 | newlen = len(ip.user_ns['In']) |
|
86 | newlen = len(ip.user_ns['In']) | |
87 | self.assertEquals(oldlen+1, newlen) |
|
87 | self.assertEquals(oldlen+1, newlen) | |
88 | self.assertEquals(ip.user_ns['In'][-1],'1;') |
|
88 | self.assertEquals(ip.user_ns['In'][-1],'1;') | |
|
89 | ||||
|
90 | def test_magic_names_in_string(self): | |||
|
91 | ip = get_ipython() | |||
|
92 | ip.run_cell('"""\n%exit\n"""') | |||
|
93 | self.assertEquals(ip.user_ns['In'][-1], '\n%exit\n') |
General Comments 0
You need to be logged in to leave comments.
Login now