##// END OF EJS Templates
tests for utils.capture
MinRK -
Show More
@@ -0,0 +1,161 b''
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 from __future__ import print_function
16
17 import sys
18
19 import nose.tools as nt
20
21 from IPython.utils import capture
22
23 #-----------------------------------------------------------------------------
24 # Globals
25 #-----------------------------------------------------------------------------
26
27 _mime_map = dict(
28 _repr_png_="image/png",
29 _repr_jpeg_="image/jpeg",
30 _repr_svg_="image/svg+xml",
31 _repr_html_="text/html",
32 _repr_json_="application/json",
33 _repr_javascript_="application/javascript",
34 )
35
36 basic_data = {
37 'image/png' : b'binarydata',
38 'text/html' : "<b>bold</b>",
39 }
40 basic_metadata = {
41 'image/png' : {
42 'width' : 10,
43 'height' : 20,
44 },
45 }
46
47 full_data = {
48 'image/png' : b'binarydata',
49 'image/jpeg' : b'binarydata',
50 'image/svg+xml' : "<svg>",
51 'text/html' : "<b>bold</b>",
52 'application/javascript' : "alert();",
53 'application/json' : "{}",
54 }
55 full_metadata = {
56 'image/png' : {"png" : "exists"},
57 'image/jpeg' : {"jpeg" : "exists"},
58 'image/svg+xml' : {"svg" : "exists"},
59 'text/html' : {"html" : "exists"},
60 'application/javascript' : {"js" : "exists"},
61 'application/json' : {"json" : "exists"},
62 }
63
64 hello_stdout = "hello, stdout"
65 hello_stderr = "hello, stderr"
66
67 #-----------------------------------------------------------------------------
68 # Test Functions
69 #-----------------------------------------------------------------------------
70
71 def test_rich_output_empty():
72 """RichOutput with no args"""
73 rich = capture.RichOutput()
74 for method, mime in _mime_map.items():
75 yield nt.assert_equal, getattr(rich, method)(), None
76
77 def test_rich_output():
78 """test RichOutput basics"""
79 data = basic_data
80 metadata = basic_metadata
81 rich = capture.RichOutput(source="test", data=data, metadata=metadata)
82 yield nt.assert_equal, rich.source, "test"
83 yield nt.assert_equal, rich._repr_html_(), data['text/html']
84 yield nt.assert_equal, rich._repr_png_(), (data['image/png'], metadata['image/png'])
85 yield nt.assert_equal, rich._repr_latex_(), None
86 yield nt.assert_equal, rich._repr_javascript_(), None
87 yield nt.assert_equal, rich._repr_svg_(), None
88
89 def test_rich_output_no_metadata():
90 """test RichOutput with no metadata"""
91 data = full_data
92 rich = capture.RichOutput(source="test", data=data)
93 for method, mime in _mime_map.items():
94 yield nt.assert_equal, getattr(rich, method)(), data[mime]
95
96 def test_rich_output_metadata():
97 """test RichOutput with metadata"""
98 data = full_data
99 metadata = full_metadata
100 rich = capture.RichOutput(source="test", data=data, metadata=metadata)
101 for method, mime in _mime_map.items():
102 yield nt.assert_equal, getattr(rich, method)(), (data[mime], metadata[mime])
103
104 def test_rich_output_display():
105 """test RichOutput.display
106
107 This is a bit circular, because we are actually using the capture code we are testing
108 to test itself.
109 """
110 data = full_data
111 rich = capture.RichOutput(data=data)
112 with capture.capture_output() as cap:
113 rich.display()
114 yield nt.assert_equal, len(cap.outputs), 1
115 rich2 = cap.outputs[0]
116 yield nt.assert_equal, rich2.data, rich.data
117 yield nt.assert_equal, rich2.metadata, rich.metadata
118
119 def test_capture_output():
120 """capture_output works"""
121 rich = capture.RichOutput(data=full_data)
122 with capture.capture_output() 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, hello_stderr, cap.stderr
128
129 def test_capture_output_no_stdout():
130 """test capture_output(stdout=False)"""
131 rich = capture.RichOutput(data=full_data)
132 with capture.capture_output(stdout=False) as cap:
133 print(hello_stdout, end="")
134 print(hello_stderr, end="", file=sys.stderr)
135 rich.display()
136 yield nt.assert_equal, "", cap.stdout
137 yield nt.assert_equal, hello_stderr, cap.stderr
138 yield nt.assert_equal, len(cap.outputs), 1
139
140 def test_capture_output_no_stderr():
141 """test capture_output(stderr=False)"""
142 rich = capture.RichOutput(data=full_data)
143 # add nested capture_output so stderr doesn't make it to nose output
144 with capture.capture_output(), capture.capture_output(stderr=False) as cap:
145 print(hello_stdout, end="")
146 print(hello_stderr, end="", file=sys.stderr)
147 rich.display()
148 yield nt.assert_equal, hello_stdout, cap.stdout
149 yield nt.assert_equal, "", cap.stderr
150 yield nt.assert_equal, len(cap.outputs), 1
151
152 def test_capture_output_no_display():
153 """test capture_output(display=False)"""
154 rich = capture.RichOutput(data=full_data)
155 with capture.capture_output(display=False) as cap:
156 print(hello_stdout, end="")
157 print(hello_stderr, end="", file=sys.stderr)
158 rich.display()
159 yield nt.assert_equal, hello_stdout, cap.stdout
160 yield nt.assert_equal, hello_stderr, cap.stderr
161 yield nt.assert_equal, cap.outputs, [] No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now