Show More
@@ -1,101 +1,117 b'' | |||||
1 | """Tests for Latex exporter""" |
|
1 | """Tests for Latex exporter""" | |
2 |
|
2 | |||
3 | # Copyright (c) IPython Development Team. |
|
3 | # Copyright (c) IPython Development Team. | |
4 | # Distributed under the terms of the Modified BSD License. |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 |
|
5 | |||
6 | import os.path |
|
6 | import os.path | |
7 | import textwrap |
|
7 | import textwrap | |
|
8 | import re | |||
8 |
|
9 | |||
9 | from .base import ExportersTestsBase |
|
10 | from .base import ExportersTestsBase | |
10 | from ..latex import LatexExporter |
|
11 | from ..latex import LatexExporter | |
11 | from IPython.nbformat import current |
|
12 | from IPython.nbformat import current | |
12 | from IPython.testing.decorators import onlyif_cmds_exist |
|
13 | from IPython.testing.decorators import onlyif_cmds_exist | |
13 | from IPython.utils.tempdir import TemporaryDirectory |
|
14 | from IPython.utils.tempdir import TemporaryDirectory | |
14 |
|
15 | |||
15 |
|
16 | |||
16 | class TestLatexExporter(ExportersTestsBase): |
|
17 | class TestLatexExporter(ExportersTestsBase): | |
17 | """Contains test functions for latex.py""" |
|
18 | """Contains test functions for latex.py""" | |
18 |
|
19 | |||
19 | exporter_class = LatexExporter |
|
20 | exporter_class = LatexExporter | |
20 | should_include_raw = ['latex'] |
|
21 | should_include_raw = ['latex'] | |
21 |
|
22 | |||
22 | def test_constructor(self): |
|
23 | def test_constructor(self): | |
23 | """ |
|
24 | """ | |
24 | Can a LatexExporter be constructed? |
|
25 | Can a LatexExporter be constructed? | |
25 | """ |
|
26 | """ | |
26 | LatexExporter() |
|
27 | LatexExporter() | |
27 |
|
28 | |||
28 |
|
29 | |||
29 | @onlyif_cmds_exist('pandoc') |
|
30 | @onlyif_cmds_exist('pandoc') | |
30 | def test_export(self): |
|
31 | def test_export(self): | |
31 | """ |
|
32 | """ | |
32 | Can a LatexExporter export something? |
|
33 | Can a LatexExporter export something? | |
33 | """ |
|
34 | """ | |
34 | (output, resources) = LatexExporter().from_filename(self._get_notebook()) |
|
35 | (output, resources) = LatexExporter().from_filename(self._get_notebook()) | |
35 | assert len(output) > 0 |
|
36 | assert len(output) > 0 | |
36 |
|
37 | |||
37 |
|
38 | |||
38 | @onlyif_cmds_exist('pandoc') |
|
39 | @onlyif_cmds_exist('pandoc') | |
39 | def test_export_book(self): |
|
40 | def test_export_book(self): | |
40 | """ |
|
41 | """ | |
41 | Can a LatexExporter export using 'report' template? |
|
42 | Can a LatexExporter export using 'report' template? | |
42 | """ |
|
43 | """ | |
43 | (output, resources) = LatexExporter(template_file='report').from_filename(self._get_notebook()) |
|
44 | (output, resources) = LatexExporter(template_file='report').from_filename(self._get_notebook()) | |
44 | assert len(output) > 0 |
|
45 | assert len(output) > 0 | |
45 |
|
46 | |||
46 |
|
47 | |||
47 | @onlyif_cmds_exist('pandoc') |
|
48 | @onlyif_cmds_exist('pandoc') | |
48 | def test_export_basic(self): |
|
49 | def test_export_basic(self): | |
49 | """ |
|
50 | """ | |
50 | Can a LatexExporter export using 'article' template? |
|
51 | Can a LatexExporter export using 'article' template? | |
51 | """ |
|
52 | """ | |
52 | (output, resources) = LatexExporter(template_file='article').from_filename(self._get_notebook()) |
|
53 | (output, resources) = LatexExporter(template_file='article').from_filename(self._get_notebook()) | |
53 | assert len(output) > 0 |
|
54 | assert len(output) > 0 | |
54 |
|
55 | |||
55 |
|
56 | |||
56 | @onlyif_cmds_exist('pandoc') |
|
57 | @onlyif_cmds_exist('pandoc') | |
57 | def test_export_article(self): |
|
58 | def test_export_article(self): | |
58 | """ |
|
59 | """ | |
59 | Can a LatexExporter export using 'article' template? |
|
60 | Can a LatexExporter export using 'article' template? | |
60 | """ |
|
61 | """ | |
61 | (output, resources) = LatexExporter(template_file='article').from_filename(self._get_notebook()) |
|
62 | (output, resources) = LatexExporter(template_file='article').from_filename(self._get_notebook()) | |
62 | assert len(output) > 0 |
|
63 | assert len(output) > 0 | |
63 |
|
64 | |||
64 | @onlyif_cmds_exist('pandoc') |
|
65 | @onlyif_cmds_exist('pandoc') | |
65 | def test_very_long_cells(self): |
|
66 | def test_very_long_cells(self): | |
66 | """ |
|
67 | """ | |
67 | Torture test that long cells do not cause issues |
|
68 | Torture test that long cells do not cause issues | |
68 | """ |
|
69 | """ | |
69 | lorem_ipsum_text = textwrap.dedent("""\ |
|
70 | lorem_ipsum_text = textwrap.dedent("""\ | |
70 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec |
|
71 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec | |
71 | dignissim, ipsum non facilisis tempus, dui felis tincidunt metus, |
|
72 | dignissim, ipsum non facilisis tempus, dui felis tincidunt metus, | |
72 | nec pulvinar neque odio eget risus. Nulla nisi lectus, cursus |
|
73 | nec pulvinar neque odio eget risus. Nulla nisi lectus, cursus | |
73 | suscipit interdum at, ultrices sit amet orci. Mauris facilisis |
|
74 | suscipit interdum at, ultrices sit amet orci. Mauris facilisis | |
74 | imperdiet elit, vitae scelerisque ipsum dignissim non. Integer |
|
75 | imperdiet elit, vitae scelerisque ipsum dignissim non. Integer | |
75 | consequat malesuada neque sit amet pulvinar. Curabitur pretium |
|
76 | consequat malesuada neque sit amet pulvinar. Curabitur pretium | |
76 | ut turpis eget aliquet. Maecenas sagittis lacus sed lectus |
|
77 | ut turpis eget aliquet. Maecenas sagittis lacus sed lectus | |
77 | volutpat, eu adipiscing purus pulvinar. Maecenas consequat |
|
78 | volutpat, eu adipiscing purus pulvinar. Maecenas consequat | |
78 | luctus urna, eget cursus quam mollis a. Aliquam vitae ornare |
|
79 | luctus urna, eget cursus quam mollis a. Aliquam vitae ornare | |
79 | erat, non hendrerit urna. Sed eu diam nec massa egestas pharetra |
|
80 | erat, non hendrerit urna. Sed eu diam nec massa egestas pharetra | |
80 | at nec tellus. Fusce feugiat lacus quis urna sollicitudin volutpat. |
|
81 | at nec tellus. Fusce feugiat lacus quis urna sollicitudin volutpat. | |
81 | Quisque at sapien non nibh feugiat tempus ac ultricies purus. |
|
82 | Quisque at sapien non nibh feugiat tempus ac ultricies purus. | |
82 | """) |
|
83 | """) | |
83 | lorem_ipsum_text = lorem_ipsum_text.replace("\n"," ") + "\n\n" |
|
84 | lorem_ipsum_text = lorem_ipsum_text.replace("\n"," ") + "\n\n" | |
84 | large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000) |
|
85 | large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000) | |
85 |
|
86 | |||
86 | notebook_name = "lorem_ipsum_long.ipynb" |
|
87 | notebook_name = "lorem_ipsum_long.ipynb" | |
87 | nb = current.new_notebook( |
|
88 | nb = current.new_notebook( | |
88 | worksheets=[ |
|
89 | worksheets=[ | |
89 | current.new_worksheet(cells=[ |
|
90 | current.new_worksheet(cells=[ | |
90 | current.new_text_cell('markdown',source=large_lorem_ipsum_text) |
|
91 | current.new_text_cell('markdown',source=large_lorem_ipsum_text) | |
91 | ]) |
|
92 | ]) | |
92 | ] |
|
93 | ] | |
93 | ) |
|
94 | ) | |
94 |
|
95 | |||
95 | with TemporaryDirectory() as td: |
|
96 | with TemporaryDirectory() as td: | |
96 | nbfile = os.path.join(td, notebook_name) |
|
97 | nbfile = os.path.join(td, notebook_name) | |
97 | with open(nbfile, 'w') as f: |
|
98 | with open(nbfile, 'w') as f: | |
98 | current.write(nb, f, 'ipynb') |
|
99 | current.write(nb, f, 'ipynb') | |
99 |
|
100 | |||
100 | (output, resources) = LatexExporter(template_file='article').from_filename(nbfile) |
|
101 | (output, resources) = LatexExporter(template_file='article').from_filename(nbfile) | |
101 | assert len(output) > 0 |
|
102 | assert len(output) > 0 | |
|
103 | ||||
|
104 | @onlyif_cmds_exist('pandoc') | |||
|
105 | def test_prompt_number_color(self): | |||
|
106 | """ | |||
|
107 | Does LatexExporter properly format input and output prompts in color? | |||
|
108 | """ | |||
|
109 | (output, resources) = LatexExporter().from_filename(self._get_notebook()) | |||
|
110 | in_regex = r"In \[\{\\color\{incolor\}(.*)\}\]:" | |||
|
111 | out_regex = r"Out\[\{\\color\{outcolor\}(.*)\}\]:" | |||
|
112 | ||||
|
113 | ins = ["1", "2", "6", "7", "8", "10", "14", " ", " "] | |||
|
114 | outs = ["7", "10", "14"] | |||
|
115 | ||||
|
116 | assert re.findall(in_regex, output) == ins | |||
|
117 | assert re.findall(out_regex, output) == outs |
General Comments 0
You need to be logged in to leave comments.
Login now