Show More
@@ -1,112 +1,112 | |||
|
1 | 1 | import sys |
|
2 | 2 | from IPython.testing.tools import AssertPrints, AssertNotPrints |
|
3 | 3 | from IPython.core.displayhook import CapturingDisplayHook |
|
4 | 4 | from IPython.utils.capture import CapturedIO |
|
5 | 5 | |
|
6 | 6 | def test_output_displayed(): |
|
7 | 7 | """Checking to make sure that output is displayed""" |
|
8 | 8 | |
|
9 | 9 | with AssertPrints('2'): |
|
10 | 10 | ip.run_cell('1+1', store_history=True) |
|
11 | 11 | |
|
12 | 12 | with AssertPrints('2'): |
|
13 | 13 | ip.run_cell('1+1 # comment with a semicolon;', store_history=True) |
|
14 | 14 | |
|
15 | 15 | with AssertPrints('2'): |
|
16 | 16 | ip.run_cell('1+1\n#commented_out_function();', store_history=True) |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | def test_output_quiet(): |
|
20 | 20 | """Checking to make sure that output is quiet""" |
|
21 | 21 | |
|
22 | 22 | with AssertNotPrints('2'): |
|
23 | 23 | ip.run_cell('1+1;', store_history=True) |
|
24 | 24 | |
|
25 | 25 | with AssertNotPrints('2'): |
|
26 | 26 | ip.run_cell('1+1; # comment with a semicolon', store_history=True) |
|
27 | 27 | |
|
28 | 28 | with AssertNotPrints('2'): |
|
29 | 29 | ip.run_cell('1+1;\n#commented_out_function()', store_history=True) |
|
30 | 30 | |
|
31 | def test_underscore_no_overrite_user(): | |
|
31 | def test_underscore_no_overwrite_user(): | |
|
32 | 32 | ip.run_cell('_ = 42', store_history=True) |
|
33 | 33 | ip.run_cell('1+1', store_history=True) |
|
34 | 34 | |
|
35 | 35 | with AssertPrints('42'): |
|
36 | 36 | ip.run_cell('print(_)', store_history=True) |
|
37 | 37 | |
|
38 | 38 | ip.run_cell('del _', store_history=True) |
|
39 | 39 | ip.run_cell('6+6', store_history=True) |
|
40 | 40 | with AssertPrints('12'): |
|
41 | 41 | ip.run_cell('_', store_history=True) |
|
42 | 42 | |
|
43 | 43 | |
|
44 | def test_underscore_no_overrite_builtins(): | |
|
44 | def test_underscore_no_overwrite_builtins(): | |
|
45 | 45 | ip.run_cell("import gettext ; gettext.install('foo')", store_history=True) |
|
46 | 46 | ip.run_cell('3+3', store_history=True) |
|
47 | 47 | |
|
48 | 48 | with AssertPrints('gettext'): |
|
49 | 49 | ip.run_cell('print(_)', store_history=True) |
|
50 | 50 | |
|
51 | 51 | ip.run_cell('_ = "userset"', store_history=True) |
|
52 | 52 | |
|
53 | 53 | with AssertPrints('userset'): |
|
54 | 54 | ip.run_cell('print(_)', store_history=True) |
|
55 | 55 | ip.run_cell('import builtins; del builtins._') |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def test_interactivehooks_ast_modes(): |
|
59 | 59 | """ |
|
60 | 60 | Test that ast nodes can be triggered with different modes |
|
61 | 61 | """ |
|
62 | 62 | saved_mode = ip.ast_node_interactivity |
|
63 | 63 | ip.ast_node_interactivity = 'last_expr_or_assign' |
|
64 | 64 | |
|
65 | 65 | try: |
|
66 | 66 | with AssertPrints('2'): |
|
67 | 67 | ip.run_cell('a = 1+1', store_history=True) |
|
68 | 68 | |
|
69 | 69 | with AssertPrints('9'): |
|
70 | 70 | ip.run_cell('b = 1+8 # comment with a semicolon;', store_history=False) |
|
71 | 71 | |
|
72 | 72 | with AssertPrints('7'): |
|
73 | 73 | ip.run_cell('c = 1+6\n#commented_out_function();', store_history=True) |
|
74 | 74 | |
|
75 | 75 | ip.run_cell('d = 11', store_history=True) |
|
76 | 76 | with AssertPrints('12'): |
|
77 | 77 | ip.run_cell('d += 1', store_history=True) |
|
78 | 78 | |
|
79 | 79 | with AssertNotPrints('42'): |
|
80 | 80 | ip.run_cell('(u,v) = (41+1, 43-1)') |
|
81 | 81 | |
|
82 | 82 | finally: |
|
83 | 83 | ip.ast_node_interactivity = saved_mode |
|
84 | 84 | |
|
85 | 85 | def test_interactivehooks_ast_modes_semi_suppress(): |
|
86 | 86 | """ |
|
87 | 87 | Test that ast nodes can be triggered with different modes and suppressed |
|
88 | 88 | by semicolon |
|
89 | 89 | """ |
|
90 | 90 | saved_mode = ip.ast_node_interactivity |
|
91 | 91 | ip.ast_node_interactivity = 'last_expr_or_assign' |
|
92 | 92 | |
|
93 | 93 | try: |
|
94 | 94 | with AssertNotPrints('2'): |
|
95 | 95 | ip.run_cell('x = 1+1;', store_history=True) |
|
96 | 96 | |
|
97 | 97 | with AssertNotPrints('7'): |
|
98 | 98 | ip.run_cell('y = 1+6; # comment with a semicolon', store_history=True) |
|
99 | 99 | |
|
100 | 100 | with AssertNotPrints('9'): |
|
101 | 101 | ip.run_cell('z = 1+8;\n#commented_out_function()', store_history=True) |
|
102 | 102 | |
|
103 | 103 | finally: |
|
104 | 104 | ip.ast_node_interactivity = saved_mode |
|
105 | 105 | |
|
106 | 106 | def test_capture_display_hook_format(): |
|
107 | 107 | """Tests that the capture display hook conforms to the CapturedIO output format""" |
|
108 | 108 | hook = CapturingDisplayHook(ip) |
|
109 | 109 | hook({"foo": "bar"}) |
|
110 | 110 | captured = CapturedIO(sys.stdout, sys.stderr, hook.outputs) |
|
111 | 111 | # Should not raise with RichOutput transformation error |
|
112 | 112 | captured.outputs |
General Comments 0
You need to be logged in to leave comments.
Login now