Format: text/x-python
⬇ Download File (5.2 KB)
   1  # encoding: utf-8
   2  """Tests for IPython.utils.capture"""
   3  
   4  #-----------------------------------------------------------------------------
   5  #  Copyright (C) 2013 The IPython Development Team
   6  #
   7  #  Distributed under the terms of the BSD License.  The full license is in
   8  #  the file COPYING, distributed as part of this software.
   9  #-----------------------------------------------------------------------------
  10  
  11  #-----------------------------------------------------------------------------
  12  # Imports
  13  #-----------------------------------------------------------------------------
  14  
  15  
  16  import sys
  17  
  18  import pytest
  19  
  20  from IPython.utils import capture
  21  
  22  #-----------------------------------------------------------------------------
  23  # Globals
  24  #-----------------------------------------------------------------------------
  25  
  26  _mime_map = dict(
  27      _repr_png_="image/png",
  28      _repr_jpeg_="image/jpeg",
  29      _repr_svg_="image/svg+xml",
  30      _repr_html_="text/html",
  31      _repr_json_="application/json",
  32      _repr_javascript_="application/javascript",
  33  )
  34  
  35  basic_data = {
  36      'image/png' : b'binarydata',
  37      'text/html' : "<b>bold</b>",
  38  }
  39  basic_metadata = {
  40      'image/png' : {
  41          'width' : 10,
  42          'height' : 20,
  43      },
  44  }
  45  
  46  full_data = {
  47      'image/png' : b'binarydata',
  48      'image/jpeg' : b'binarydata',
  49      'image/svg+xml' : "<svg>",
  50      'text/html' : "<b>bold</b>",
  51      'application/javascript' : "alert();",
  52      'application/json' : "{}",
  53  }
  54  full_metadata = {
  55      'image/png' : {"png" : "exists"},
  56      'image/jpeg' : {"jpeg" : "exists"},
  57      'image/svg+xml' : {"svg" : "exists"},
  58      'text/html' : {"html" : "exists"},
  59      'application/javascript' : {"js" : "exists"},
  60      'application/json' : {"json" : "exists"},
  61  }
  62  
  63  hello_stdout = "hello, stdout"
  64  hello_stderr = "hello, stderr"
  65  
  66  #-----------------------------------------------------------------------------
  67  # Test Functions
  68  #-----------------------------------------------------------------------------
  69  @pytest.mark.parametrize("method_mime", _mime_map.items())
  70  def test_rich_output_empty(method_mime):
  71      """RichOutput with no args"""
  72      rich = capture.RichOutput()
  73      method, mime = method_mime
  74      assert getattr(rich, method)() is None
  75      
  76  def test_rich_output():
  77      """test RichOutput basics"""
  78      data = basic_data
  79      metadata = basic_metadata
  80      rich = capture.RichOutput(data=data, metadata=metadata)
  81      assert rich._repr_html_() == data["text/html"]
  82      assert rich._repr_png_() == (data["image/png"], metadata["image/png"])
  83      assert rich._repr_latex_() is None
  84      assert rich._repr_javascript_() is None
  85      assert rich._repr_svg_() is None
  86  
  87  
  88  @pytest.mark.parametrize("method_mime", _mime_map.items())
  89  def test_rich_output_no_metadata(method_mime):
  90      """test RichOutput with no metadata"""
  91      data = full_data
  92      rich = capture.RichOutput(data=data)
  93      method, mime = method_mime
  94      assert getattr(rich, method)() == data[mime]
  95  
  96  
  97  @pytest.mark.parametrize("method_mime", _mime_map.items())
  98  def test_rich_output_metadata(method_mime):
  99      """test RichOutput with metadata"""
 100      data = full_data
 101      metadata = full_metadata
 102      rich = capture.RichOutput(data=data, metadata=metadata)
 103      method, mime = method_mime
 104      assert getattr(rich, method)() == (data[mime], metadata[mime])
 105  
 106  def test_rich_output_display():
 107      """test RichOutput.display
 108  
 109      This is a bit circular, because we are actually using the capture code we are testing
 110      to test itself.
 111      """
 112      data = full_data
 113      rich = capture.RichOutput(data=data)
 114      with capture.capture_output() as cap:
 115          rich.display()
 116      assert len(cap.outputs) == 1
 117      rich2 = cap.outputs[0]
 118      assert rich2.data == rich.data
 119      assert rich2.metadata == rich.metadata
 120  
 121  def test_capture_output():
 122      """capture_output works"""
 123      rich = capture.RichOutput(data=full_data)
 124      with capture.capture_output() as cap:
 125          print(hello_stdout, end="")
 126          print(hello_stderr, end="", file=sys.stderr)
 127          rich.display()
 128      assert hello_stdout == cap.stdout
 129      assert hello_stderr == cap.stderr
 130  
 131  
 132  def test_capture_output_no_stdout():
 133      """test capture_output(stdout=False)"""
 134      rich = capture.RichOutput(data=full_data)
 135      with capture.capture_output(stdout=False) as cap:
 136          print(hello_stdout, end="")
 137          print(hello_stderr, end="", file=sys.stderr)
 138          rich.display()
 139      assert "" == cap.stdout
 140      assert hello_stderr == cap.stderr
 141      assert len(cap.outputs) == 1
 142  
 143  
 144  def test_capture_output_no_stderr():
 145      """test capture_output(stderr=False)"""
 146      rich = capture.RichOutput(data=full_data)
 147      # add nested capture_output so stderr doesn't make it to nose output
 148      with capture.capture_output(), capture.capture_output(stderr=False) as cap:
 149          print(hello_stdout, end="")
 150          print(hello_stderr, end="", file=sys.stderr)
 151          rich.display()
 152      assert hello_stdout == cap.stdout
 153      assert "" == cap.stderr
 154      assert len(cap.outputs) == 1
 155  
 156  
 157  def test_capture_output_no_display():
 158      """test capture_output(display=False)"""
 159      rich = capture.RichOutput(data=full_data)
 160      with capture.capture_output(display=False) as cap:
 161          print(hello_stdout, end="")
 162          print(hello_stderr, end="", file=sys.stderr)
 163          rich.display()
 164      assert hello_stdout == cap.stdout
 165      assert hello_stderr == cap.stderr
 166      assert cap.outputs == []
 167