Show More
@@ -1,54 +1,51 b'' | |||||
1 | import unittest |
|
1 | import unittest | |
2 | try: # Python 3.3 + |
|
2 | from unittest.mock import Mock | |
3 | from unittest.mock import Mock |
|
|||
4 | except ImportError: |
|
|||
5 | from mock import Mock |
|
|||
6 |
|
3 | |||
7 | from IPython.core import events |
|
4 | from IPython.core import events | |
8 | import IPython.testing.tools as tt |
|
5 | import IPython.testing.tools as tt | |
9 |
|
6 | |||
10 | def ping_received(): |
|
7 | def ping_received(): | |
11 | pass |
|
8 | pass | |
12 |
|
9 | |||
13 | class CallbackTests(unittest.TestCase): |
|
10 | class CallbackTests(unittest.TestCase): | |
14 | def setUp(self): |
|
11 | def setUp(self): | |
15 | self.em = events.EventManager(get_ipython(), {'ping_received': ping_received}) |
|
12 | self.em = events.EventManager(get_ipython(), {'ping_received': ping_received}) | |
16 |
|
13 | |||
17 | def test_register_unregister(self): |
|
14 | def test_register_unregister(self): | |
18 | cb = Mock() |
|
15 | cb = Mock() | |
19 |
|
16 | |||
20 | self.em.register('ping_received', cb) |
|
17 | self.em.register('ping_received', cb) | |
21 | self.em.trigger('ping_received') |
|
18 | self.em.trigger('ping_received') | |
22 | self.assertEqual(cb.call_count, 1) |
|
19 | self.assertEqual(cb.call_count, 1) | |
23 |
|
20 | |||
24 | self.em.unregister('ping_received', cb) |
|
21 | self.em.unregister('ping_received', cb) | |
25 | self.em.trigger('ping_received') |
|
22 | self.em.trigger('ping_received') | |
26 | self.assertEqual(cb.call_count, 1) |
|
23 | self.assertEqual(cb.call_count, 1) | |
27 |
|
24 | |||
28 | def test_cb_error(self): |
|
25 | def test_cb_error(self): | |
29 | cb = Mock(side_effect=ValueError) |
|
26 | cb = Mock(side_effect=ValueError) | |
30 | self.em.register('ping_received', cb) |
|
27 | self.em.register('ping_received', cb) | |
31 | with tt.AssertPrints("Error in callback"): |
|
28 | with tt.AssertPrints("Error in callback"): | |
32 | self.em.trigger('ping_received') |
|
29 | self.em.trigger('ping_received') | |
33 |
|
30 | |||
34 | def test_unregister_during_callback(self): |
|
31 | def test_unregister_during_callback(self): | |
35 | invoked = [False] * 3 |
|
32 | invoked = [False] * 3 | |
36 |
|
33 | |||
37 | def func1(*_): |
|
34 | def func1(*_): | |
38 | invoked[0] = True |
|
35 | invoked[0] = True | |
39 | self.em.unregister('ping_received', func1) |
|
36 | self.em.unregister('ping_received', func1) | |
40 | self.em.register('ping_received', func3) |
|
37 | self.em.register('ping_received', func3) | |
41 |
|
38 | |||
42 | def func2(*_): |
|
39 | def func2(*_): | |
43 | invoked[1] = True |
|
40 | invoked[1] = True | |
44 | self.em.unregister('ping_received', func2) |
|
41 | self.em.unregister('ping_received', func2) | |
45 |
|
42 | |||
46 | def func3(*_): |
|
43 | def func3(*_): | |
47 | invoked[2] = True |
|
44 | invoked[2] = True | |
48 |
|
45 | |||
49 | self.em.register('ping_received', func1) |
|
46 | self.em.register('ping_received', func1) | |
50 | self.em.register('ping_received', func2) |
|
47 | self.em.register('ping_received', func2) | |
51 |
|
48 | |||
52 | self.em.trigger('ping_received') |
|
49 | self.em.trigger('ping_received') | |
53 | self.assertEqual([True, True, False], invoked) |
|
50 | self.assertEqual([True, True, False], invoked) | |
54 | self.assertEqual([func3], self.em.callbacks['ping_received']) |
|
51 | self.assertEqual([func3], self.em.callbacks['ping_received']) |
@@ -1,205 +1,201 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """Tools for handling LaTeX.""" |
|
2 | """Tools for handling LaTeX.""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | |||
7 | from io import BytesIO, open |
|
7 | from io import BytesIO, open | |
8 | import os |
|
8 | import os | |
9 | import tempfile |
|
9 | import tempfile | |
10 | import shutil |
|
10 | import shutil | |
11 | import subprocess |
|
11 | import subprocess | |
|
12 | from base64 import encodebytes | |||
12 |
|
13 | |||
13 | from IPython.utils.process import find_cmd, FindCmdError |
|
14 | from IPython.utils.process import find_cmd, FindCmdError | |
14 | from traitlets.config import get_config |
|
15 | from traitlets.config import get_config | |
15 | from traitlets.config.configurable import SingletonConfigurable |
|
16 | from traitlets.config.configurable import SingletonConfigurable | |
16 | from traitlets import List, Bool, Unicode |
|
17 | from traitlets import List, Bool, Unicode | |
17 | from IPython.utils.py3compat import cast_unicode, cast_unicode_py2 as u, PY3 |
|
18 | from IPython.utils.py3compat import cast_unicode, cast_unicode_py2 as u, PY3 | |
18 |
|
19 | |||
19 | try: # Py3 |
|
|||
20 | from base64 import encodebytes |
|
|||
21 | except ImportError: # Py2 |
|
|||
22 | from base64 import encodestring as encodebytes |
|
|||
23 |
|
||||
24 |
|
20 | |||
25 | class LaTeXTool(SingletonConfigurable): |
|
21 | class LaTeXTool(SingletonConfigurable): | |
26 | """An object to store configuration of the LaTeX tool.""" |
|
22 | """An object to store configuration of the LaTeX tool.""" | |
27 | def _config_default(self): |
|
23 | def _config_default(self): | |
28 | return get_config() |
|
24 | return get_config() | |
29 |
|
25 | |||
30 | backends = List( |
|
26 | backends = List( | |
31 | Unicode(), ["matplotlib", "dvipng"], |
|
27 | Unicode(), ["matplotlib", "dvipng"], | |
32 | help="Preferred backend to draw LaTeX math equations. " |
|
28 | help="Preferred backend to draw LaTeX math equations. " | |
33 | "Backends in the list are checked one by one and the first " |
|
29 | "Backends in the list are checked one by one and the first " | |
34 | "usable one is used. Note that `matplotlib` backend " |
|
30 | "usable one is used. Note that `matplotlib` backend " | |
35 | "is usable only for inline style equations. To draw " |
|
31 | "is usable only for inline style equations. To draw " | |
36 | "display style equations, `dvipng` backend must be specified. ", |
|
32 | "display style equations, `dvipng` backend must be specified. ", | |
37 | # It is a List instead of Enum, to make configuration more |
|
33 | # It is a List instead of Enum, to make configuration more | |
38 | # flexible. For example, to use matplotlib mainly but dvipng |
|
34 | # flexible. For example, to use matplotlib mainly but dvipng | |
39 | # for display style, the default ["matplotlib", "dvipng"] can |
|
35 | # for display style, the default ["matplotlib", "dvipng"] can | |
40 | # be used. To NOT use dvipng so that other repr such as |
|
36 | # be used. To NOT use dvipng so that other repr such as | |
41 | # unicode pretty printing is used, you can use ["matplotlib"]. |
|
37 | # unicode pretty printing is used, you can use ["matplotlib"]. | |
42 | ).tag(config=True) |
|
38 | ).tag(config=True) | |
43 |
|
39 | |||
44 | use_breqn = Bool( |
|
40 | use_breqn = Bool( | |
45 | True, |
|
41 | True, | |
46 | help="Use breqn.sty to automatically break long equations. " |
|
42 | help="Use breqn.sty to automatically break long equations. " | |
47 | "This configuration takes effect only for dvipng backend.", |
|
43 | "This configuration takes effect only for dvipng backend.", | |
48 | ).tag(config=True) |
|
44 | ).tag(config=True) | |
49 |
|
45 | |||
50 | packages = List( |
|
46 | packages = List( | |
51 | ['amsmath', 'amsthm', 'amssymb', 'bm'], |
|
47 | ['amsmath', 'amsthm', 'amssymb', 'bm'], | |
52 | help="A list of packages to use for dvipng backend. " |
|
48 | help="A list of packages to use for dvipng backend. " | |
53 | "'breqn' will be automatically appended when use_breqn=True.", |
|
49 | "'breqn' will be automatically appended when use_breqn=True.", | |
54 | ).tag(config=True) |
|
50 | ).tag(config=True) | |
55 |
|
51 | |||
56 | preamble = Unicode( |
|
52 | preamble = Unicode( | |
57 | help="Additional preamble to use when generating LaTeX source " |
|
53 | help="Additional preamble to use when generating LaTeX source " | |
58 | "for dvipng backend.", |
|
54 | "for dvipng backend.", | |
59 | ).tag(config=True) |
|
55 | ).tag(config=True) | |
60 |
|
56 | |||
61 |
|
57 | |||
62 | def latex_to_png(s, encode=False, backend=None, wrap=False): |
|
58 | def latex_to_png(s, encode=False, backend=None, wrap=False): | |
63 | """Render a LaTeX string to PNG. |
|
59 | """Render a LaTeX string to PNG. | |
64 |
|
60 | |||
65 | Parameters |
|
61 | Parameters | |
66 | ---------- |
|
62 | ---------- | |
67 | s : str |
|
63 | s : str | |
68 | The raw string containing valid inline LaTeX. |
|
64 | The raw string containing valid inline LaTeX. | |
69 | encode : bool, optional |
|
65 | encode : bool, optional | |
70 | Should the PNG data base64 encoded to make it JSON'able. |
|
66 | Should the PNG data base64 encoded to make it JSON'able. | |
71 | backend : {matplotlib, dvipng} |
|
67 | backend : {matplotlib, dvipng} | |
72 | Backend for producing PNG data. |
|
68 | Backend for producing PNG data. | |
73 | wrap : bool |
|
69 | wrap : bool | |
74 | If true, Automatically wrap `s` as a LaTeX equation. |
|
70 | If true, Automatically wrap `s` as a LaTeX equation. | |
75 |
|
71 | |||
76 | None is returned when the backend cannot be used. |
|
72 | None is returned when the backend cannot be used. | |
77 |
|
73 | |||
78 | """ |
|
74 | """ | |
79 | s = cast_unicode(s) |
|
75 | s = cast_unicode(s) | |
80 | allowed_backends = LaTeXTool.instance().backends |
|
76 | allowed_backends = LaTeXTool.instance().backends | |
81 | if backend is None: |
|
77 | if backend is None: | |
82 | backend = allowed_backends[0] |
|
78 | backend = allowed_backends[0] | |
83 | if backend not in allowed_backends: |
|
79 | if backend not in allowed_backends: | |
84 | return None |
|
80 | return None | |
85 | if backend == 'matplotlib': |
|
81 | if backend == 'matplotlib': | |
86 | f = latex_to_png_mpl |
|
82 | f = latex_to_png_mpl | |
87 | elif backend == 'dvipng': |
|
83 | elif backend == 'dvipng': | |
88 | f = latex_to_png_dvipng |
|
84 | f = latex_to_png_dvipng | |
89 | else: |
|
85 | else: | |
90 | raise ValueError('No such backend {0}'.format(backend)) |
|
86 | raise ValueError('No such backend {0}'.format(backend)) | |
91 | bin_data = f(s, wrap) |
|
87 | bin_data = f(s, wrap) | |
92 | if encode and bin_data: |
|
88 | if encode and bin_data: | |
93 | bin_data = encodebytes(bin_data) |
|
89 | bin_data = encodebytes(bin_data) | |
94 | return bin_data |
|
90 | return bin_data | |
95 |
|
91 | |||
96 |
|
92 | |||
97 | def latex_to_png_mpl(s, wrap): |
|
93 | def latex_to_png_mpl(s, wrap): | |
98 | try: |
|
94 | try: | |
99 | from matplotlib import mathtext |
|
95 | from matplotlib import mathtext | |
100 | from pyparsing import ParseFatalException |
|
96 | from pyparsing import ParseFatalException | |
101 | except ImportError: |
|
97 | except ImportError: | |
102 | return None |
|
98 | return None | |
103 |
|
99 | |||
104 | # mpl mathtext doesn't support display math, force inline |
|
100 | # mpl mathtext doesn't support display math, force inline | |
105 | s = s.replace('$$', '$') |
|
101 | s = s.replace('$$', '$') | |
106 | if wrap: |
|
102 | if wrap: | |
107 | s = u'${0}$'.format(s) |
|
103 | s = u'${0}$'.format(s) | |
108 |
|
104 | |||
109 | try: |
|
105 | try: | |
110 | mt = mathtext.MathTextParser('bitmap') |
|
106 | mt = mathtext.MathTextParser('bitmap') | |
111 | f = BytesIO() |
|
107 | f = BytesIO() | |
112 | mt.to_png(f, s, fontsize=12) |
|
108 | mt.to_png(f, s, fontsize=12) | |
113 | return f.getvalue() |
|
109 | return f.getvalue() | |
114 | except (ValueError, RuntimeError, ParseFatalException): |
|
110 | except (ValueError, RuntimeError, ParseFatalException): | |
115 | return None |
|
111 | return None | |
116 |
|
112 | |||
117 |
|
113 | |||
118 | def latex_to_png_dvipng(s, wrap): |
|
114 | def latex_to_png_dvipng(s, wrap): | |
119 | try: |
|
115 | try: | |
120 | find_cmd('latex') |
|
116 | find_cmd('latex') | |
121 | find_cmd('dvipng') |
|
117 | find_cmd('dvipng') | |
122 | except FindCmdError: |
|
118 | except FindCmdError: | |
123 | return None |
|
119 | return None | |
124 | try: |
|
120 | try: | |
125 | workdir = tempfile.mkdtemp() |
|
121 | workdir = tempfile.mkdtemp() | |
126 | tmpfile = os.path.join(workdir, "tmp.tex") |
|
122 | tmpfile = os.path.join(workdir, "tmp.tex") | |
127 | dvifile = os.path.join(workdir, "tmp.dvi") |
|
123 | dvifile = os.path.join(workdir, "tmp.dvi") | |
128 | outfile = os.path.join(workdir, "tmp.png") |
|
124 | outfile = os.path.join(workdir, "tmp.png") | |
129 |
|
125 | |||
130 | with open(tmpfile, "w", encoding='utf8') as f: |
|
126 | with open(tmpfile, "w", encoding='utf8') as f: | |
131 | f.writelines(genelatex(s, wrap)) |
|
127 | f.writelines(genelatex(s, wrap)) | |
132 |
|
128 | |||
133 | with open(os.devnull, 'wb') as devnull: |
|
129 | with open(os.devnull, 'wb') as devnull: | |
134 | subprocess.check_call( |
|
130 | subprocess.check_call( | |
135 | ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile], |
|
131 | ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile], | |
136 | cwd=workdir, stdout=devnull, stderr=devnull) |
|
132 | cwd=workdir, stdout=devnull, stderr=devnull) | |
137 |
|
133 | |||
138 | subprocess.check_call( |
|
134 | subprocess.check_call( | |
139 | ["dvipng", "-T", "tight", "-x", "1500", "-z", "9", |
|
135 | ["dvipng", "-T", "tight", "-x", "1500", "-z", "9", | |
140 | "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir, |
|
136 | "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir, | |
141 | stdout=devnull, stderr=devnull) |
|
137 | stdout=devnull, stderr=devnull) | |
142 |
|
138 | |||
143 | with open(outfile, "rb") as f: |
|
139 | with open(outfile, "rb") as f: | |
144 | return f.read() |
|
140 | return f.read() | |
145 | except subprocess.CalledProcessError: |
|
141 | except subprocess.CalledProcessError: | |
146 | return None |
|
142 | return None | |
147 | finally: |
|
143 | finally: | |
148 | shutil.rmtree(workdir) |
|
144 | shutil.rmtree(workdir) | |
149 |
|
145 | |||
150 |
|
146 | |||
151 | def kpsewhich(filename): |
|
147 | def kpsewhich(filename): | |
152 | """Invoke kpsewhich command with an argument `filename`.""" |
|
148 | """Invoke kpsewhich command with an argument `filename`.""" | |
153 | try: |
|
149 | try: | |
154 | find_cmd("kpsewhich") |
|
150 | find_cmd("kpsewhich") | |
155 | proc = subprocess.Popen( |
|
151 | proc = subprocess.Popen( | |
156 | ["kpsewhich", filename], |
|
152 | ["kpsewhich", filename], | |
157 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
153 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
158 | (stdout, stderr) = proc.communicate() |
|
154 | (stdout, stderr) = proc.communicate() | |
159 | return stdout.strip().decode('utf8', 'replace') |
|
155 | return stdout.strip().decode('utf8', 'replace') | |
160 | except FindCmdError: |
|
156 | except FindCmdError: | |
161 | pass |
|
157 | pass | |
162 |
|
158 | |||
163 |
|
159 | |||
164 | def genelatex(body, wrap): |
|
160 | def genelatex(body, wrap): | |
165 | """Generate LaTeX document for dvipng backend.""" |
|
161 | """Generate LaTeX document for dvipng backend.""" | |
166 | lt = LaTeXTool.instance() |
|
162 | lt = LaTeXTool.instance() | |
167 | breqn = wrap and lt.use_breqn and kpsewhich("breqn.sty") |
|
163 | breqn = wrap and lt.use_breqn and kpsewhich("breqn.sty") | |
168 | yield u(r'\documentclass{article}') |
|
164 | yield u(r'\documentclass{article}') | |
169 | packages = lt.packages |
|
165 | packages = lt.packages | |
170 | if breqn: |
|
166 | if breqn: | |
171 | packages = packages + ['breqn'] |
|
167 | packages = packages + ['breqn'] | |
172 | for pack in packages: |
|
168 | for pack in packages: | |
173 | yield u(r'\usepackage{{{0}}}'.format(pack)) |
|
169 | yield u(r'\usepackage{{{0}}}'.format(pack)) | |
174 | yield u(r'\pagestyle{empty}') |
|
170 | yield u(r'\pagestyle{empty}') | |
175 | if lt.preamble: |
|
171 | if lt.preamble: | |
176 | yield lt.preamble |
|
172 | yield lt.preamble | |
177 | yield u(r'\begin{document}') |
|
173 | yield u(r'\begin{document}') | |
178 | if breqn: |
|
174 | if breqn: | |
179 | yield u(r'\begin{dmath*}') |
|
175 | yield u(r'\begin{dmath*}') | |
180 | yield body |
|
176 | yield body | |
181 | yield u(r'\end{dmath*}') |
|
177 | yield u(r'\end{dmath*}') | |
182 | elif wrap: |
|
178 | elif wrap: | |
183 | yield u'$${0}$$'.format(body) |
|
179 | yield u'$${0}$$'.format(body) | |
184 | else: |
|
180 | else: | |
185 | yield body |
|
181 | yield body | |
186 | yield u'\end{document}' |
|
182 | yield u'\end{document}' | |
187 |
|
183 | |||
188 |
|
184 | |||
189 | _data_uri_template_png = u"""<img src="data:image/png;base64,%s" alt=%s />""" |
|
185 | _data_uri_template_png = u"""<img src="data:image/png;base64,%s" alt=%s />""" | |
190 |
|
186 | |||
191 | def latex_to_html(s, alt='image'): |
|
187 | def latex_to_html(s, alt='image'): | |
192 | """Render LaTeX to HTML with embedded PNG data using data URIs. |
|
188 | """Render LaTeX to HTML with embedded PNG data using data URIs. | |
193 |
|
189 | |||
194 | Parameters |
|
190 | Parameters | |
195 | ---------- |
|
191 | ---------- | |
196 | s : str |
|
192 | s : str | |
197 | The raw string containing valid inline LateX. |
|
193 | The raw string containing valid inline LateX. | |
198 | alt : str |
|
194 | alt : str | |
199 | The alt text to use for the HTML. |
|
195 | The alt text to use for the HTML. | |
200 | """ |
|
196 | """ | |
201 | base64_data = latex_to_png(s, encode=True).decode('ascii') |
|
197 | base64_data = latex_to_png(s, encode=True).decode('ascii') | |
202 | if base64_data: |
|
198 | if base64_data: | |
203 | return _data_uri_template_png % (base64_data, alt) |
|
199 | return _data_uri_template_png % (base64_data, alt) | |
204 |
|
200 | |||
205 |
|
201 |
@@ -1,139 +1,134 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Tests for IPython.utils.path.py""" |
|
2 | """Tests for IPython.utils.path.py""" | |
3 |
|
3 | |||
4 | # Copyright (c) IPython Development Team. |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # Distributed under the terms of the Modified BSD License. |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 |
|
6 | from unittest.mock import patch | ||
7 | try: |
|
|||
8 | from unittest.mock import patch |
|
|||
9 | except ImportError: |
|
|||
10 | from mock import patch |
|
|||
11 |
|
||||
12 | import nose.tools as nt |
|
7 | import nose.tools as nt | |
13 |
|
8 | |||
14 | from IPython.lib import latextools |
|
9 | from IPython.lib import latextools | |
15 | from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib |
|
10 | from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib | |
16 | from IPython.utils.process import FindCmdError |
|
11 | from IPython.utils.process import FindCmdError | |
17 |
|
12 | |||
18 |
|
13 | |||
19 | def test_latex_to_png_dvipng_fails_when_no_cmd(): |
|
14 | def test_latex_to_png_dvipng_fails_when_no_cmd(): | |
20 | """ |
|
15 | """ | |
21 | `latex_to_png_dvipng` should return None when there is no required command |
|
16 | `latex_to_png_dvipng` should return None when there is no required command | |
22 | """ |
|
17 | """ | |
23 | for command in ['latex', 'dvipng']: |
|
18 | for command in ['latex', 'dvipng']: | |
24 | yield (check_latex_to_png_dvipng_fails_when_no_cmd, command) |
|
19 | yield (check_latex_to_png_dvipng_fails_when_no_cmd, command) | |
25 |
|
20 | |||
26 |
|
21 | |||
27 | def check_latex_to_png_dvipng_fails_when_no_cmd(command): |
|
22 | def check_latex_to_png_dvipng_fails_when_no_cmd(command): | |
28 | def mock_find_cmd(arg): |
|
23 | def mock_find_cmd(arg): | |
29 | if arg == command: |
|
24 | if arg == command: | |
30 | raise FindCmdError |
|
25 | raise FindCmdError | |
31 |
|
26 | |||
32 | with patch.object(latextools, "find_cmd", mock_find_cmd): |
|
27 | with patch.object(latextools, "find_cmd", mock_find_cmd): | |
33 | nt.assert_equal(latextools.latex_to_png_dvipng("whatever", True), |
|
28 | nt.assert_equal(latextools.latex_to_png_dvipng("whatever", True), | |
34 | None) |
|
29 | None) | |
35 |
|
30 | |||
36 |
|
31 | |||
37 | @onlyif_cmds_exist('latex', 'dvipng') |
|
32 | @onlyif_cmds_exist('latex', 'dvipng') | |
38 | def test_latex_to_png_dvipng_runs(): |
|
33 | def test_latex_to_png_dvipng_runs(): | |
39 | """ |
|
34 | """ | |
40 | Test that latex_to_png_dvipng just runs without error. |
|
35 | Test that latex_to_png_dvipng just runs without error. | |
41 | """ |
|
36 | """ | |
42 | def mock_kpsewhich(filename): |
|
37 | def mock_kpsewhich(filename): | |
43 | nt.assert_equal(filename, "breqn.sty") |
|
38 | nt.assert_equal(filename, "breqn.sty") | |
44 | return None |
|
39 | return None | |
45 |
|
40 | |||
46 | for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]: |
|
41 | for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]: | |
47 | yield (latextools.latex_to_png_dvipng, s, wrap) |
|
42 | yield (latextools.latex_to_png_dvipng, s, wrap) | |
48 |
|
43 | |||
49 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): |
|
44 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): | |
50 | yield (latextools.latex_to_png_dvipng, s, wrap) |
|
45 | yield (latextools.latex_to_png_dvipng, s, wrap) | |
51 |
|
46 | |||
52 | @skipif_not_matplotlib |
|
47 | @skipif_not_matplotlib | |
53 | def test_latex_to_png_mpl_runs(): |
|
48 | def test_latex_to_png_mpl_runs(): | |
54 | """ |
|
49 | """ | |
55 | Test that latex_to_png_mpl just runs without error. |
|
50 | Test that latex_to_png_mpl just runs without error. | |
56 | """ |
|
51 | """ | |
57 | def mock_kpsewhich(filename): |
|
52 | def mock_kpsewhich(filename): | |
58 | nt.assert_equal(filename, "breqn.sty") |
|
53 | nt.assert_equal(filename, "breqn.sty") | |
59 | return None |
|
54 | return None | |
60 |
|
55 | |||
61 | for (s, wrap) in [("$x^2$", False), ("x^2", True)]: |
|
56 | for (s, wrap) in [("$x^2$", False), ("x^2", True)]: | |
62 | yield (latextools.latex_to_png_mpl, s, wrap) |
|
57 | yield (latextools.latex_to_png_mpl, s, wrap) | |
63 |
|
58 | |||
64 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): |
|
59 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): | |
65 | yield (latextools.latex_to_png_mpl, s, wrap) |
|
60 | yield (latextools.latex_to_png_mpl, s, wrap) | |
66 |
|
61 | |||
67 | @skipif_not_matplotlib |
|
62 | @skipif_not_matplotlib | |
68 | def test_latex_to_html(): |
|
63 | def test_latex_to_html(): | |
69 | img = latextools.latex_to_html("$x^2$") |
|
64 | img = latextools.latex_to_html("$x^2$") | |
70 | nt.assert_in("data:image/png;base64,iVBOR", img) |
|
65 | nt.assert_in("data:image/png;base64,iVBOR", img) | |
71 |
|
66 | |||
72 |
|
67 | |||
73 | def test_genelatex_no_wrap(): |
|
68 | def test_genelatex_no_wrap(): | |
74 | """ |
|
69 | """ | |
75 | Test genelatex with wrap=False. |
|
70 | Test genelatex with wrap=False. | |
76 | """ |
|
71 | """ | |
77 | def mock_kpsewhich(filename): |
|
72 | def mock_kpsewhich(filename): | |
78 | assert False, ("kpsewhich should not be called " |
|
73 | assert False, ("kpsewhich should not be called " | |
79 | "(called with {0})".format(filename)) |
|
74 | "(called with {0})".format(filename)) | |
80 |
|
75 | |||
81 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): |
|
76 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): | |
82 | nt.assert_equal( |
|
77 | nt.assert_equal( | |
83 | '\n'.join(latextools.genelatex("body text", False)), |
|
78 | '\n'.join(latextools.genelatex("body text", False)), | |
84 | r'''\documentclass{article} |
|
79 | r'''\documentclass{article} | |
85 | \usepackage{amsmath} |
|
80 | \usepackage{amsmath} | |
86 | \usepackage{amsthm} |
|
81 | \usepackage{amsthm} | |
87 | \usepackage{amssymb} |
|
82 | \usepackage{amssymb} | |
88 | \usepackage{bm} |
|
83 | \usepackage{bm} | |
89 | \pagestyle{empty} |
|
84 | \pagestyle{empty} | |
90 | \begin{document} |
|
85 | \begin{document} | |
91 | body text |
|
86 | body text | |
92 | \end{document}''') |
|
87 | \end{document}''') | |
93 |
|
88 | |||
94 |
|
89 | |||
95 | def test_genelatex_wrap_with_breqn(): |
|
90 | def test_genelatex_wrap_with_breqn(): | |
96 | """ |
|
91 | """ | |
97 | Test genelatex with wrap=True for the case breqn.sty is installed. |
|
92 | Test genelatex with wrap=True for the case breqn.sty is installed. | |
98 | """ |
|
93 | """ | |
99 | def mock_kpsewhich(filename): |
|
94 | def mock_kpsewhich(filename): | |
100 | nt.assert_equal(filename, "breqn.sty") |
|
95 | nt.assert_equal(filename, "breqn.sty") | |
101 | return "path/to/breqn.sty" |
|
96 | return "path/to/breqn.sty" | |
102 |
|
97 | |||
103 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): |
|
98 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): | |
104 | nt.assert_equal( |
|
99 | nt.assert_equal( | |
105 | '\n'.join(latextools.genelatex("x^2", True)), |
|
100 | '\n'.join(latextools.genelatex("x^2", True)), | |
106 | r'''\documentclass{article} |
|
101 | r'''\documentclass{article} | |
107 | \usepackage{amsmath} |
|
102 | \usepackage{amsmath} | |
108 | \usepackage{amsthm} |
|
103 | \usepackage{amsthm} | |
109 | \usepackage{amssymb} |
|
104 | \usepackage{amssymb} | |
110 | \usepackage{bm} |
|
105 | \usepackage{bm} | |
111 | \usepackage{breqn} |
|
106 | \usepackage{breqn} | |
112 | \pagestyle{empty} |
|
107 | \pagestyle{empty} | |
113 | \begin{document} |
|
108 | \begin{document} | |
114 | \begin{dmath*} |
|
109 | \begin{dmath*} | |
115 | x^2 |
|
110 | x^2 | |
116 | \end{dmath*} |
|
111 | \end{dmath*} | |
117 | \end{document}''') |
|
112 | \end{document}''') | |
118 |
|
113 | |||
119 |
|
114 | |||
120 | def test_genelatex_wrap_without_breqn(): |
|
115 | def test_genelatex_wrap_without_breqn(): | |
121 | """ |
|
116 | """ | |
122 | Test genelatex with wrap=True for the case breqn.sty is not installed. |
|
117 | Test genelatex with wrap=True for the case breqn.sty is not installed. | |
123 | """ |
|
118 | """ | |
124 | def mock_kpsewhich(filename): |
|
119 | def mock_kpsewhich(filename): | |
125 | nt.assert_equal(filename, "breqn.sty") |
|
120 | nt.assert_equal(filename, "breqn.sty") | |
126 | return None |
|
121 | return None | |
127 |
|
122 | |||
128 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): |
|
123 | with patch.object(latextools, "kpsewhich", mock_kpsewhich): | |
129 | nt.assert_equal( |
|
124 | nt.assert_equal( | |
130 | '\n'.join(latextools.genelatex("x^2", True)), |
|
125 | '\n'.join(latextools.genelatex("x^2", True)), | |
131 | r'''\documentclass{article} |
|
126 | r'''\documentclass{article} | |
132 | \usepackage{amsmath} |
|
127 | \usepackage{amsmath} | |
133 | \usepackage{amsthm} |
|
128 | \usepackage{amsthm} | |
134 | \usepackage{amssymb} |
|
129 | \usepackage{amssymb} | |
135 | \usepackage{bm} |
|
130 | \usepackage{bm} | |
136 | \pagestyle{empty} |
|
131 | \pagestyle{empty} | |
137 | \begin{document} |
|
132 | \begin{document} | |
138 | $$x^2$$ |
|
133 | $$x^2$$ | |
139 | \end{document}''') |
|
134 | \end{document}''') |
General Comments 0
You need to be logged in to leave comments.
Login now