Debian Codegraph @

tests/test_capture.py

raw
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 # -----------------------------------------------------------------------------
68 # Test Functions
69 # -----------------------------------------------------------------------------
70 @pytest.mark.parametrize("method_mime", _mime_map.items())
71 def test_rich_output_empty(method_mime):
72 """RichOutput with no args"""
73 rich = capture.RichOutput()
74 method, mime = method_mime
75 assert getattr(rich, method)() is None
76
77
78 def test_rich_output():
79 """test RichOutput basics"""
80 data = basic_data
81 metadata = basic_metadata
82 rich = capture.RichOutput(data=data, metadata=metadata)
83 assert rich._repr_html_() == data["text/html"]
84 assert rich._repr_png_() == (data["image/png"], metadata["image/png"])
85 assert rich._repr_latex_() is None
86 assert rich._repr_javascript_() is None
87 assert rich._repr_svg_() is None
88
89
90 @pytest.mark.parametrize("method_mime", _mime_map.items())
91 def test_rich_output_no_metadata(method_mime):
92 """test RichOutput with no metadata"""
93 data = full_data
94 rich = capture.RichOutput(data=data)
95 method, mime = method_mime
96 assert getattr(rich, method)() == data[mime]
97
98
99 @pytest.mark.parametrize("method_mime", _mime_map.items())
100 def test_rich_output_metadata(method_mime):
101 """test RichOutput with metadata"""
102 data = full_data
103 metadata = full_metadata
104 rich = capture.RichOutput(data=data, metadata=metadata)
105 method, mime = method_mime
106 assert getattr(rich, method)() == (data[mime], metadata[mime])
107
108
109 def test_rich_output_display():
110 """test RichOutput.display
111
112 This is a bit circular, because we are actually using the capture code we are testing
113 to test itself.
114 """
115 data = full_data
116 rich = capture.RichOutput(data=data)
117 with capture.capture_output() as cap:
118 rich.display()
119 assert len(cap.outputs) == 1
120 rich2 = cap.outputs[0]
121 assert rich2.data == rich.data
122 assert rich2.metadata == rich.metadata
123
124
125 def test_capture_output():
126 """capture_output works"""
127 rich = capture.RichOutput(data=full_data)
128 with capture.capture_output() as cap:
129 print(hello_stdout, end="")
130 print(hello_stderr, end="", file=sys.stderr)
131 rich.display()
132 assert hello_stdout == cap.stdout
133 assert hello_stderr == cap.stderr
134
135
136 def test_capture_output_no_stdout():
137 """test capture_output(stdout=False)"""
138 rich = capture.RichOutput(data=full_data)
139 with capture.capture_output(stdout=False) as cap:
140 print(hello_stdout, end="")
141 print(hello_stderr, end="", file=sys.stderr)
142 rich.display()
143 assert "" == cap.stdout
144 assert hello_stderr == cap.stderr
145 assert len(cap.outputs) == 1
146
147
148 def test_capture_output_no_stderr():
149 """test capture_output(stderr=False)"""
150 rich = capture.RichOutput(data=full_data)
151 # add nested capture_output so stderr doesn't make it to nose output
152 with capture.capture_output(), capture.capture_output(stderr=False) as cap:
153 print(hello_stdout, end="")
154 print(hello_stderr, end="", file=sys.stderr)
155 rich.display()
156 assert hello_stdout == cap.stdout
157 assert "" == cap.stderr
158 assert len(cap.outputs) == 1
159
160
161 def test_capture_output_no_display():
162 """test capture_output(display=False)"""
163 rich = capture.RichOutput(data=full_data)
164 with capture.capture_output(display=False) as cap:
165 print(hello_stdout, end="")
166 print(hello_stderr, end="", file=sys.stderr)
167 rich.display()
168 assert hello_stdout == cap.stdout
169 assert hello_stderr == cap.stderr
170 assert cap.outputs == []