test_capture.py 5.4 KB

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