Show More
@@ -1,139 +1,139 | |||||
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 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2008-2011 The IPython Development Team |
|
5 | # Copyright (C) 2008-2011 The IPython Development Team | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | # Distributed under the terms of the BSD License. The full license is in | |
8 | # the file COPYING, distributed as part of this software. |
|
8 | # the file COPYING, distributed as part of this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | import nose.tools as nt |
|
11 | import nose.tools as nt | |
12 |
|
12 | |||
13 | from IPython.lib import latextools |
|
13 | from IPython.lib import latextools | |
14 | from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib |
|
14 | from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib | |
15 | from IPython.testing.tools import monkeypatch |
|
15 | from IPython.testing.tools import monkeypatch | |
16 | from IPython.utils.process import FindCmdError |
|
16 | from IPython.utils.process import FindCmdError | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | def test_latex_to_png_dvipng_fails_when_no_cmd(): |
|
19 | def test_latex_to_png_dvipng_fails_when_no_cmd(): | |
20 | """ |
|
20 | """ | |
21 | `latex_to_png_dvipng` should return None when there is no required command |
|
21 | `latex_to_png_dvipng` should return None when there is no required command | |
22 | """ |
|
22 | """ | |
23 | for command in ['latex', 'dvipng']: |
|
23 | for command in ['latex', 'dvipng']: | |
24 | yield (check_latex_to_png_dvipng_fails_when_no_cmd, command) |
|
24 | yield (check_latex_to_png_dvipng_fails_when_no_cmd, command) | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | def check_latex_to_png_dvipng_fails_when_no_cmd(command): |
|
27 | def check_latex_to_png_dvipng_fails_when_no_cmd(command): | |
28 | def mock_find_cmd(arg): |
|
28 | def mock_find_cmd(arg): | |
29 | if arg == command: |
|
29 | if arg == command: | |
30 | raise FindCmdError |
|
30 | raise FindCmdError | |
31 |
|
31 | |||
32 | with monkeypatch(latextools, "find_cmd", mock_find_cmd): |
|
32 | with monkeypatch(latextools, "find_cmd", mock_find_cmd): | |
33 | nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True), |
|
33 | nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True), | |
34 | None) |
|
34 | None) | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | @onlyif_cmds_exist('latex', 'dvipng') |
|
37 | @onlyif_cmds_exist('latex', 'dvipng') | |
38 | def test_latex_to_png_dvipng_runs(): |
|
38 | def test_latex_to_png_dvipng_runs(): | |
39 | """ |
|
39 | """ | |
40 | Test that latex_to_png_dvipng just runs without error. |
|
40 | Test that latex_to_png_dvipng just runs without error. | |
41 | """ |
|
41 | """ | |
42 | def mock_kpsewhich(filename): |
|
42 | def mock_kpsewhich(filename): | |
43 | nt.assert_equals(filename, "breqn.sty") |
|
43 | nt.assert_equals(filename, "breqn.sty") | |
44 | return None |
|
44 | return None | |
45 |
|
45 | |||
46 | for (s, wrap) in [("$$x^2$$", False), ("x^2", True)]: |
|
46 | for (s, wrap) in [(u"$$x^2$$", False), (u"x^2", True)]: | |
47 | yield (latextools.latex_to_png_dvipng, s, wrap) |
|
47 | yield (latextools.latex_to_png_dvipng, s, wrap) | |
48 |
|
48 | |||
49 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): |
|
49 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
50 | yield (latextools.latex_to_png_dvipng, s, wrap) |
|
50 | yield (latextools.latex_to_png_dvipng, s, wrap) | |
51 |
|
51 | |||
52 | @skipif_not_matplotlib |
|
52 | @skipif_not_matplotlib | |
53 | def test_latex_to_png_mpl_runs(): |
|
53 | def test_latex_to_png_mpl_runs(): | |
54 | """ |
|
54 | """ | |
55 | Test that latex_to_png_mpl just runs without error. |
|
55 | Test that latex_to_png_mpl just runs without error. | |
56 | """ |
|
56 | """ | |
57 | def mock_kpsewhich(filename): |
|
57 | def mock_kpsewhich(filename): | |
58 | nt.assert_equals(filename, "breqn.sty") |
|
58 | nt.assert_equals(filename, "breqn.sty") | |
59 | return None |
|
59 | return None | |
60 |
|
60 | |||
61 | for (s, wrap) in [("$x^2$", False), ("x^2", True)]: |
|
61 | for (s, wrap) in [("$x^2$", False), ("x^2", True)]: | |
62 | yield (latextools.latex_to_png_mpl, s, wrap) |
|
62 | yield (latextools.latex_to_png_mpl, s, wrap) | |
63 |
|
63 | |||
64 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): |
|
64 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
65 | yield (latextools.latex_to_png_mpl, s, wrap) |
|
65 | yield (latextools.latex_to_png_mpl, s, wrap) | |
66 |
|
66 | |||
67 | @skipif_not_matplotlib |
|
67 | @skipif_not_matplotlib | |
68 | def test_latex_to_html(): |
|
68 | def test_latex_to_html(): | |
69 | img = latextools.latex_to_html("$x^2$") |
|
69 | img = latextools.latex_to_html("$x^2$") | |
70 | nt.assert_in("data:image/png;base64,iVBOR", img) |
|
70 | nt.assert_in("data:image/png;base64,iVBOR", img) | |
71 |
|
71 | |||
72 |
|
72 | |||
73 | def test_genelatex_no_wrap(): |
|
73 | def test_genelatex_no_wrap(): | |
74 | """ |
|
74 | """ | |
75 | Test genelatex with wrap=False. |
|
75 | Test genelatex with wrap=False. | |
76 | """ |
|
76 | """ | |
77 | def mock_kpsewhich(filename): |
|
77 | def mock_kpsewhich(filename): | |
78 | assert False, ("kpsewhich should not be called " |
|
78 | assert False, ("kpsewhich should not be called " | |
79 | "(called with {0})".format(filename)) |
|
79 | "(called with {0})".format(filename)) | |
80 |
|
80 | |||
81 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): |
|
81 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
82 | nt.assert_equals( |
|
82 | nt.assert_equals( | |
83 | '\n'.join(latextools.genelatex("body text", False)), |
|
83 | '\n'.join(latextools.genelatex("body text", False)), | |
84 | r'''\documentclass{article} |
|
84 | r'''\documentclass{article} | |
85 | \usepackage{amsmath} |
|
85 | \usepackage{amsmath} | |
86 | \usepackage{amsthm} |
|
86 | \usepackage{amsthm} | |
87 | \usepackage{amssymb} |
|
87 | \usepackage{amssymb} | |
88 | \usepackage{bm} |
|
88 | \usepackage{bm} | |
89 | \pagestyle{empty} |
|
89 | \pagestyle{empty} | |
90 | \begin{document} |
|
90 | \begin{document} | |
91 | body text |
|
91 | body text | |
92 | \end{document}''') |
|
92 | \end{document}''') | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | def test_genelatex_wrap_with_breqn(): |
|
95 | def test_genelatex_wrap_with_breqn(): | |
96 | """ |
|
96 | """ | |
97 | Test genelatex with wrap=True for the case breqn.sty is installed. |
|
97 | Test genelatex with wrap=True for the case breqn.sty is installed. | |
98 | """ |
|
98 | """ | |
99 | def mock_kpsewhich(filename): |
|
99 | def mock_kpsewhich(filename): | |
100 | nt.assert_equals(filename, "breqn.sty") |
|
100 | nt.assert_equals(filename, "breqn.sty") | |
101 | return "path/to/breqn.sty" |
|
101 | return "path/to/breqn.sty" | |
102 |
|
102 | |||
103 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): |
|
103 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
104 | nt.assert_equals( |
|
104 | nt.assert_equals( | |
105 | '\n'.join(latextools.genelatex("x^2", True)), |
|
105 | '\n'.join(latextools.genelatex("x^2", True)), | |
106 | r'''\documentclass{article} |
|
106 | r'''\documentclass{article} | |
107 | \usepackage{amsmath} |
|
107 | \usepackage{amsmath} | |
108 | \usepackage{amsthm} |
|
108 | \usepackage{amsthm} | |
109 | \usepackage{amssymb} |
|
109 | \usepackage{amssymb} | |
110 | \usepackage{bm} |
|
110 | \usepackage{bm} | |
111 | \usepackage{breqn} |
|
111 | \usepackage{breqn} | |
112 | \pagestyle{empty} |
|
112 | \pagestyle{empty} | |
113 | \begin{document} |
|
113 | \begin{document} | |
114 | \begin{dmath*} |
|
114 | \begin{dmath*} | |
115 | x^2 |
|
115 | x^2 | |
116 | \end{dmath*} |
|
116 | \end{dmath*} | |
117 | \end{document}''') |
|
117 | \end{document}''') | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | def test_genelatex_wrap_without_breqn(): |
|
120 | def test_genelatex_wrap_without_breqn(): | |
121 | """ |
|
121 | """ | |
122 | Test genelatex with wrap=True for the case breqn.sty is not installed. |
|
122 | Test genelatex with wrap=True for the case breqn.sty is not installed. | |
123 | """ |
|
123 | """ | |
124 | def mock_kpsewhich(filename): |
|
124 | def mock_kpsewhich(filename): | |
125 | nt.assert_equals(filename, "breqn.sty") |
|
125 | nt.assert_equals(filename, "breqn.sty") | |
126 | return None |
|
126 | return None | |
127 |
|
127 | |||
128 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): |
|
128 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
129 | nt.assert_equals( |
|
129 | nt.assert_equals( | |
130 | '\n'.join(latextools.genelatex("x^2", True)), |
|
130 | '\n'.join(latextools.genelatex("x^2", True)), | |
131 | r'''\documentclass{article} |
|
131 | r'''\documentclass{article} | |
132 | \usepackage{amsmath} |
|
132 | \usepackage{amsmath} | |
133 | \usepackage{amsthm} |
|
133 | \usepackage{amsthm} | |
134 | \usepackage{amssymb} |
|
134 | \usepackage{amssymb} | |
135 | \usepackage{bm} |
|
135 | \usepackage{bm} | |
136 | \pagestyle{empty} |
|
136 | \pagestyle{empty} | |
137 | \begin{document} |
|
137 | \begin{document} | |
138 | $$x^2$$ |
|
138 | $$x^2$$ | |
139 | \end{document}''') |
|
139 | \end{document}''') |
General Comments 0
You need to be logged in to leave comments.
Login now