Show More
@@ -1,92 +1,92 b'' | |||
|
1 | 1 | #----------------------------------------------------------------------------- |
|
2 | 2 | # Copyright (C) 2012 The IPython Development Team |
|
3 | 3 | # |
|
4 | 4 | # Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | # the file COPYING, distributed as part of this software. |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | import os |
|
9 | 9 | import unittest |
|
10 | 10 | import base64 |
|
11 | 11 | |
|
12 | 12 | from IPython.zmq.kernelmanager import KernelManager |
|
13 | 13 | from IPython.frontend.terminal.console.interactiveshell \ |
|
14 | 14 | import ZMQTerminalInteractiveShell |
|
15 | 15 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
16 | 16 | from IPython.testing.tools import monkeypatch |
|
17 | 17 | from IPython.testing.decorators import skip_without |
|
18 | 18 | from IPython.utils.ipstruct import Struct |
|
19 | 19 | from IPython.utils.process import find_cmd |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | SCRIPT_PATH = os.path.join( |
|
23 | 23 | os.path.abspath(os.path.dirname(__file__)), 'writetofile.py') |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | class ZMQTerminalInteractiveShellTestCase(unittest.TestCase): |
|
27 | 27 | |
|
28 | 28 | def setUp(self): |
|
29 | 29 | km = KernelManager() |
|
30 | 30 | self.shell = ZMQTerminalInteractiveShell(kernel_manager=km) |
|
31 | 31 | self.raw = b'dummy data' |
|
32 | 32 | self.mime = 'image/png' |
|
33 | 33 | self.data = {self.mime: base64.encodestring(self.raw).decode('ascii')} |
|
34 | 34 | |
|
35 | 35 | def test_no_call_by_default(self): |
|
36 | 36 | def raise_if_called(*args, **kwds): |
|
37 | 37 | assert False |
|
38 | 38 | |
|
39 | 39 | shell = self.shell |
|
40 | shell.handle_image_PIL | |
|
41 | shell.handle_image_stream | |
|
42 | shell.handle_image_tempfile | |
|
43 | shell.handle_image_callable | |
|
40 | shell.handle_image_PIL = raise_if_called | |
|
41 | shell.handle_image_stream = raise_if_called | |
|
42 | shell.handle_image_tempfile = raise_if_called | |
|
43 | shell.handle_image_callable = raise_if_called | |
|
44 | 44 | |
|
45 | 45 | shell.handle_image(None, None) # arguments are dummy |
|
46 | 46 | |
|
47 | 47 | @skip_without('PIL') |
|
48 | 48 | def test_handle_image_PIL(self): |
|
49 | 49 | import PIL.Image |
|
50 | 50 | |
|
51 | 51 | open_called_with = [] |
|
52 | 52 | show_called_with = [] |
|
53 | 53 | |
|
54 | 54 | def fake_open(arg): |
|
55 | 55 | open_called_with.append(arg) |
|
56 | 56 | return Struct(show=lambda: show_called_with.append(None)) |
|
57 | 57 | |
|
58 | 58 | with monkeypatch(PIL.Image, 'open', fake_open): |
|
59 | 59 | self.shell.handle_image_PIL(self.data, self.mime) |
|
60 | 60 | |
|
61 | 61 | assert len(open_called_with) == 1 |
|
62 | 62 | assert len(show_called_with) == 1 |
|
63 | 63 | assert open_called_with[0].getvalue() == self.raw |
|
64 | 64 | |
|
65 | 65 | def check_handler_with_file(self, inpath, handler): |
|
66 | 66 | shell = self.shell |
|
67 | 67 | configname = '{0}_image_handler'.format(handler) |
|
68 | 68 | funcname = 'handle_image_{0}'.format(handler) |
|
69 | 69 | |
|
70 | 70 | assert hasattr(shell, configname) |
|
71 | 71 | assert hasattr(shell, funcname) |
|
72 | 72 | |
|
73 | 73 | with NamedFileInTemporaryDirectory('data') as file: |
|
74 | 74 | cmd = [find_cmd('python'), SCRIPT_PATH, inpath, file.name] |
|
75 | 75 | setattr(shell, configname, cmd) |
|
76 | 76 | getattr(shell, funcname)(self.data, self.mime) |
|
77 | 77 | transferred = file.read() |
|
78 | 78 | |
|
79 | 79 | assert transferred == self.raw |
|
80 | 80 | |
|
81 | 81 | def test_handle_image_stream(self): |
|
82 | 82 | self.check_handler_with_file('-', 'stream') |
|
83 | 83 | |
|
84 | 84 | def test_handle_image_tempfile(self): |
|
85 | 85 | self.check_handler_with_file('{file}', 'tempfile') |
|
86 | 86 | |
|
87 | 87 | def test_handle_image_callable(self): |
|
88 | 88 | called_with = [] |
|
89 | 89 | self.shell.callable_image_handler = called_with.append |
|
90 | 90 | self.shell.handle_image_callable(self.data, self.mime) |
|
91 | 91 | assert len(called_with) == 1 |
|
92 | 92 | assert called_with[0] is self.data |
General Comments 0
You need to be logged in to leave comments.
Login now