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