Show More
@@ -0,0 +1,64 b'' | |||
|
1 | """Tests for Octave magics extension.""" | |
|
2 | ||
|
3 | import nose.tools as nt | |
|
4 | ||
|
5 | try: | |
|
6 | import oct2py | |
|
7 | import numpy as np | |
|
8 | import numpy.testing as npt | |
|
9 | ||
|
10 | from IPython.extensions import octavemagic | |
|
11 | except Exception, e: | |
|
12 | __test__ = False | |
|
13 | ||
|
14 | global octave | |
|
15 | ||
|
16 | def setup(): | |
|
17 | ip = get_ipython() | |
|
18 | global octave | |
|
19 | ||
|
20 | octave = octavemagic.OctaveMagics(ip) | |
|
21 | ip.register_magics(octave) | |
|
22 | ||
|
23 | ip.ex('import numpy as np') | |
|
24 | ||
|
25 | def test_octave_inline(): | |
|
26 | ip = get_ipython() | |
|
27 | result = ip.run_line_magic('octave', '[1, 2, 3] + 1;') | |
|
28 | npt.assert_array_equal(result, [[2, 3, 4]]) | |
|
29 | ||
|
30 | def test_octave_roundtrip(): | |
|
31 | ip = get_ipython() | |
|
32 | ip.ex('x = np.arange(3); y = 4.5') | |
|
33 | ip.run_line_magic('octave_push', 'x y') | |
|
34 | ip.run_line_magic('octave', 'x = x + 1; y = y + 1;') | |
|
35 | ip.run_line_magic('octave_pull', 'x y') | |
|
36 | ||
|
37 | npt.assert_array_equal(ip.user_ns['x'], [[1, 2, 3]]) | |
|
38 | nt.assert_equal(ip.user_ns['y'], 5.5) | |
|
39 | ||
|
40 | def test_octave_cell_magic(): | |
|
41 | ip = get_ipython() | |
|
42 | ip.ex('x = 3; y = [1, 2]') | |
|
43 | ip.run_cell_magic('octave', '-f png -s 400,400 -i x,y -o z', | |
|
44 | 'z = x + y;') | |
|
45 | npt.assert_array_equal(ip.user_ns['z'], [[4, 5]]) | |
|
46 | ||
|
47 | ||
|
48 | def verify_publish_data(source, data): | |
|
49 | if 'image/svg+xml' in data: | |
|
50 | svg = data['image/svg+xml'] | |
|
51 | assert 'height="500px"' in svg | |
|
52 | assert 'width="400px"' in svg | |
|
53 | ||
|
54 | test_octave_plot.svgs_generated += 1 | |
|
55 | ||
|
56 | def test_octave_plot(): | |
|
57 | octave._publish_display_data = verify_publish_data | |
|
58 | test_octave_plot.svgs_generated = 0 | |
|
59 | ||
|
60 | ip = get_ipython() | |
|
61 | ip.run_cell_magic('octave', '-f svg -s 400,500', | |
|
62 | 'plot([1, 2, 3]); figure; plot([4, 5, 6]);') | |
|
63 | ||
|
64 | nt.assert_equal(test_octave_plot.svgs_generated, 2) |
@@ -70,6 +70,10 b' class OctaveMagics(Magics):' | |||
|
70 | 70 | self._oct = oct2py.Oct2Py() |
|
71 | 71 | self._plot_format = 'png' |
|
72 | 72 | |
|
73 | # Allow publish_display_data to be overridden for | |
|
74 | # testing purposes. | |
|
75 | self._publish_display_data = publish_display_data | |
|
76 | ||
|
73 | 77 | |
|
74 | 78 | def _fix_gnuplot_svg_size(self, image, size=None): |
|
75 | 79 | """ |
@@ -320,8 +324,8 b' class OctaveMagics(Magics):' | |||
|
320 | 324 | output = unicode_to_str(output) |
|
321 | 325 | self.shell.push({output: self._oct.get(output)}) |
|
322 | 326 | |
|
323 |
for |
|
|
324 |
publish_display_data( |
|
|
327 | for source, data in display_data: | |
|
328 | self._publish_display_data(source, data) | |
|
325 | 329 | |
|
326 | 330 | if return_output: |
|
327 | 331 | ans = self._oct.get('_') |
General Comments 0
You need to be logged in to leave comments.
Login now