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