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