diff options
author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2022-12-19 20:42:55 +0000 |
---|---|---|
committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2022-12-19 20:42:55 +0000 |
commit | a1bbe365ba7515d7028938fdec5d41e3bc84120a (patch) | |
tree | 6a5c08a6206af6ab434690f560e196d45ff67bb1 | |
parent | 9384edebf2178318103f9cb1ce74959aa575bb56 (diff) | |
download | docutils-a1bbe365ba7515d7028938fdec5d41e3bc84120a.tar.gz |
Refactor latex writer tests.
Define default test settings as class attribute.
Use separate test functions for different settings instead of
running all sub-test from one function.
Simplify naming.
Add test for "use_bibtex" setting (will be fixed in next commit).
Add stub for testing `core.publish_parts()` with the "latex" writer.
(Currently publish_parts does not work with "latex".)
git-svn-id: https://svn.code.sf.net/p/docutils/code/trunk@9311 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
-rwxr-xr-x | docutils/test/test_writers/test_latex2e.py | 347 | ||||
-rw-r--r-- | docutils/test/test_writers/test_latex2e_misc.py | 66 |
2 files changed, 218 insertions, 195 deletions
diff --git a/docutils/test/test_writers/test_latex2e.py b/docutils/test/test_writers/test_latex2e.py index 7356eb143..436df6117 100755 --- a/docutils/test/test_writers/test_latex2e.py +++ b/docutils/test/test_writers/test_latex2e.py @@ -29,143 +29,129 @@ ham = os.path.relpath(DATA_ROOT/'ham.tex').replace('\\', '/') class WriterPublishTestCase(unittest.TestCase): maxDiff = None - - def test_publish(self): - writer_name = 'latex' - settings = { - '_disable_config': True, - 'strict_visitor': True, - 'use_latex_toc': False, - # avoid latex writer future warnings: - 'use_latex_citations': False, - 'legacy_column_widths': True, + writer_name = 'latex' + settings = { + '_disable_config': True, + 'strict_visitor': True, + # Explicit set current default to avoid latex writer future warnings: + 'use_latex_citations': False, + 'legacy_column_widths': True, } - for name, cases in totest.items(): - for casenum, (case_input, case_expected) in enumerate(cases): - with self.subTest(id=f'totest[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) - - settings['use_latex_toc'] = True - for name, cases in totest_latex_toc.items(): - for casenum, (case_input, case_expected) in enumerate(cases): - with self.subTest(id=f'totest_latex_toc[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) + def test_defaults(self): + for name, cases in samples_default.items(): + for casenum, (rst_input, expected) in enumerate(cases): + with self.subTest(id=f'samples_default[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=self.settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_docutils_toc(self): + settings = self.settings.copy() + settings['use_latex_toc'] = False + for name, cases in samples_docutils_toc.items(): + for casenum, (rst_input, expected) in enumerate(cases): + with self.subTest(id=f'samples_docutils_toc[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_book(self): + settings = self.settings.copy() settings['documentclass'] = 'book' - for name, cases in totest_latex_toc_book.items(): - for casenum, (case_input, case_expected) in enumerate(cases): - with self.subTest( - id=f'totest_latex_toc_book[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) - - del settings['documentclass'] + for name, cases in samples_book.items(): + for casenum, (rst_input, expected) in enumerate(cases): + with self.subTest(id=f'samples_book[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_latex_sectnum(self): + settings = self.settings.copy() settings['use_latex_toc'] = False settings['sectnum_xform'] = False - for name, cases in totest_latex_sectnum.items(): - for casenum, (case_input, case_expected) in enumerate(cases): + for name, cases in samples_latex_sectnum.items(): + for casenum, (rst_input, expected) in enumerate(cases): with self.subTest( - id=f'totest_latex_sectnum[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) - - settings['sectnum_xform'] = True + id=f'samples_latex_sectnum[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_latex_citations(self): + settings = self.settings.copy() settings['use_latex_citations'] = True - for name, cases in totest_latex_citations.items(): - for casenum, (case_input, case_expected) in enumerate(cases): + for name, cases in samples_latex_citations.items(): + for casenum, (rst_input, expected) in enumerate(cases): with self.subTest( - id=f'totest_latex_citations[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) - + id=f'samples_latex_citations[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_table_style_auto(self): + settings = self.settings.copy() settings['table_style'] = ['colwidths-auto'] - for name, cases in totest_table_style_auto.items(): - for casenum, (case_input, case_expected) in enumerate(cases): + for name, cases in samples_table_style_auto.items(): + for casenum, (rst_input, expected) in enumerate(cases): with self.subTest( - id=f'totest_table_style_auto[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) - + id=f'samples_table_style_auto[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_booktabs(self): + settings = self.settings.copy() settings['table_style'] = ['booktabs'] - for name, cases in totest_table_style_booktabs.items(): - for casenum, (case_input, case_expected) in enumerate(cases): - with self.subTest( - id=f'totest_table_style_booktabs[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) - + for name, cases in samples_table_style_booktabs.items(): + for casenum, (rst_input, expected) in enumerate(cases): + with self.subTest(id=f'samples_booktabs[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_link_stylesheet(self): + settings = self.settings.copy() + settings['stylesheet_path'] = f'{spam},{ham}' + for name, cases in samples_stylesheet.items(): + for casenum, (rst_input, expected) in enumerate(cases): + with self.subTest(id=f'samples_link_stylesheet[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_embed_embed_stylesheet(self): + settings = self.settings.copy() settings['stylesheet_path'] = f'{spam},{ham}' - for name, cases in totest_stylesheet.items(): - for casenum, (case_input, case_expected) in enumerate(cases): - with self.subTest( - id=f'totest_stylesheet[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) - settings['embed_stylesheet'] = True settings['warning_stream'] = '' - for name, cases in totest_stylesheet_embed.items(): - for casenum, (case_input, case_expected) in enumerate(cases): - with self.subTest( - id=f'totest_stylesheet_embed[{name!r}][{casenum}]'): - output = publish_string( - source=case_input, - writer_name=writer_name, - settings_overrides=settings.copy(), - ) - if isinstance(output, bytes): - output = output.decode('utf-8') - self.assertEqual(output, case_expected) + for name, cases in samples_stylesheet_embed.items(): + for casenum, (rst_input, expected) in enumerate(cases): + with self.subTest(id=f'samples_embed_stylesheet[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + self.assertEqual(output.decode('utf-8'), expected) + + def test_bibtex(self): + settings = self.settings.copy() + settings['use_bibtex'] = ['alpha', 'xampl'] + for name, cases in samples_bibtex.items(): + for casenum, (rst_input, expected) in enumerate(cases): + with self.subTest(id=f'samples_bibtex[{name!r}][{casenum}]'): + output = publish_string(source=rst_input, + writer_name=self.writer_name, + settings_overrides=settings) + # print(output.decode('utf-8')) + self.assertEqual(output.decode('utf-8'), expected) head_template = string.Template( @@ -243,17 +229,18 @@ head_alltt = head_template.substitute( + '\\usepackage{alltt}\n')) -totest = {} -totest_latex_toc = {} -totest_latex_toc_book = {} -totest_latex_sectnum = {} -totest_latex_citations = {} -totest_stylesheet = {} -totest_stylesheet_embed = {} -totest_table_style_auto = {} -totest_table_style_booktabs = {} +samples_default = {} +samples_docutils_toc = {} +samples_book = {} +samples_latex_sectnum = {} +samples_latex_citations = {} +samples_stylesheet = {} +samples_stylesheet_embed = {} +samples_table_style_auto = {} +samples_table_style_booktabs = {} +samples_bibtex = {} -totest['url_chars'] = [ +samples_default['url_chars'] = [ ["http://nowhere/url_with%28parens%29", head + r""" \url{http://nowhere/url_with\%28parens\%29} @@ -262,7 +249,7 @@ head + r""" """], ] -totest['textcomp'] = [ +samples_default['textcomp'] = [ ["2 µm is just 2/1000000 m", head_textcomp + r""" 2 µm is just 2/1000000 m @@ -271,7 +258,7 @@ head_textcomp + r""" """], ] -totest['spanish_quote'] = [ +samples_default['spanish_quote'] = [ [".. role:: language-es\n\nUnd damit :language-es:`basta`!", head_template.substitute(dict(parts, requirements=r"""\usepackage{ifthen} @@ -285,7 +272,7 @@ Und damit \foreignlanguage{spanish}{basta}! """], ] -totest['code_role'] = [ +samples_default['code_role'] = [ [':code:`x=1`', head_template.substitute(dict(parts, requirements=parts['requirements'] + '\\usepackage{color}\n', @@ -297,8 +284,7 @@ head_template.substitute(dict(parts, requirements=parts['requirements'] """], ] -totest['table_of_contents'] = [ -# input +samples_docutils_toc['table_of_contents'] = [ ["""\ .. contents:: Table of Contents @@ -371,8 +357,7 @@ Paragraph 2. """], ] -totest['footnote_text'] = [ -# input +samples_default['footnote_text'] = [ ["""\ .. [1] paragraph @@ -413,8 +398,7 @@ paragraph """], ] -totest_latex_toc['no_sectnum'] = [ -# input +samples_default['no_sectnum'] = [ ["""\ .. contents:: @@ -438,8 +422,7 @@ head_template.substitute(dict(parts, """], ] -totest_latex_toc['sectnum'] = [ -# input +samples_default['sectnum'] = [ ["""\ .. contents:: .. sectnum:: @@ -464,8 +447,7 @@ head_template.substitute(dict(parts, """], ] -totest_latex_toc['depth'] = [ -# input +samples_default['depth'] = [ ["""\ .. contents:: :depth: 1 @@ -491,8 +473,7 @@ head_template.substitute(dict(parts, """], ] -totest_latex_toc_book['depth'] = [ -# input +samples_book['depth'] = [ ["""\ .. contents:: :depth: 1 @@ -521,8 +502,7 @@ head_template.substitute(dict(parts, ] -totest_latex_sectnum['no_sectnum'] = [ -# input +samples_latex_sectnum['no_sectnum'] = [ ["""\ some text @@ -543,8 +523,7 @@ some text """], ] -totest_latex_sectnum['sectnum'] = [ -# input +samples_latex_sectnum['sectnum'] = [ ["""\ .. sectnum:: @@ -568,8 +547,7 @@ some text """], ] -totest_latex_citations['citations_with_underscore'] = [ -# input +samples_latex_citations['citations_with_underscore'] = [ ["""\ Just a test citation [my_cite2006]_. @@ -591,8 +569,7 @@ The underscore is mishandled. ] -totest_latex_citations['adjacent_citations'] = [ -# input +samples_latex_citations['adjacent_citations'] = [ ["""\ Two non-citations: [MeYou2007]_[YouMe2007]_. @@ -630,8 +607,7 @@ important. ] -totest['enumerated_lists'] = [ -# input +samples_default['enumerated_lists'] = [ ["""\ 1. Item 1. 2. Second to the previous item this one will explain @@ -683,8 +659,7 @@ head + r""" # as active character (e.g. de (ngerman)). -totest['table_caption'] = [ -# input +samples_default['table_caption'] = [ ["""\ .. table:: Foo @@ -709,7 +684,7 @@ head_table + r""" """], ] -totest['table_styles'] = [ +samples_default['table_styles'] = [ ["""\ .. table:: :class: borderless @@ -817,7 +792,7 @@ head_table + """ """], ] -totest_table_style_booktabs['table_styles'] = [ +samples_table_style_booktabs['table_styles'] = [ # borderless overrides "booktabs" table_style ["""\ .. table:: @@ -886,7 +861,7 @@ head_booktabs + """ \\end{document} """], ] -totest_table_style_auto['table_styles'] = [ +samples_table_style_auto['table_styles'] = [ ["""\ .. table:: :class: borderless @@ -947,8 +922,7 @@ head_table + """ """], ] -totest['table_align'] = [ -# input +samples_default['table_align'] = [ ["""\ .. table:: :align: right @@ -973,7 +947,7 @@ head_table + """ """], ] -totest['table_empty_cells'] = [ +samples_default['table_empty_cells'] = [ ["""\ ===== ====== Title @@ -1034,7 +1008,7 @@ c4 """], ] -totest['table_nonstandard_class'] = [ +samples_default['table_nonstandard_class'] = [ ["""\ .. table:: :class: my-class @@ -1089,8 +1063,7 @@ head_template.substitute( # The "[" needs to be protected (otherwise it will be seen as an # option to "\\", "\item", etc. ). -totest['bracket_protection'] = [ -# input +samples_default['bracket_protection'] = [ [""" * [no option] to this item """, @@ -1103,8 +1076,7 @@ head + r""" """], ] -totest['literal_block'] = [ -# input +samples_default['literal_block'] = [ ["""\ Test special characters { [ \\\\ ] } in literal block:: @@ -1127,7 +1099,7 @@ Test special characters \{ {[} \textbackslash{} {]} \} in literal block: """], ] -totest['raw'] = [ +samples_default['raw'] = [ [r""".. raw:: latex $E=mc^2$ @@ -1186,7 +1158,7 @@ compound paragraph continuation. """], ] -totest['title_with_inline_markup'] = [ +samples_default['title_with_inline_markup'] = [ ["""\ This is the *Title* =================== @@ -1228,8 +1200,7 @@ This is the \emph{document}. """], ] -totest_stylesheet['two-styles'] = [ -# input +samples_stylesheet['two-styles'] = [ ["""two stylesheet links in the header""", head_template.substitute(dict(parts, stylesheet=r"""\usepackage{%s} @@ -1241,8 +1212,7 @@ two stylesheet links in the header """], ] -totest_stylesheet_embed['two-styles'] = [ -# input +samples_stylesheet_embed['two-styles'] = [ ["""two stylesheets embedded in the header""", head_template.substitute(dict(parts, stylesheet=r"""% Cannot embed stylesheet: @@ -1257,5 +1227,28 @@ two stylesheets embedded in the header """], ] +samples_bibtex['alpha'] = [ +["""\ +Just a test citation [book-full]_. +""", +head + r""" +Just a test citation \cite{book-full}. + +\bibliographystyle{alpha} +\bibliography{xampl} + +\end{document} +"""], +["""\ +No bibliography if there is no citation. +""", +head + r""" +No bibliography if there is no citation. + +\end{document} +"""], +] + + if __name__ == '__main__': unittest.main() diff --git a/docutils/test/test_writers/test_latex2e_misc.py b/docutils/test/test_writers/test_latex2e_misc.py index 87f63fc43..a11f24c5e 100644 --- a/docutils/test/test_writers/test_latex2e_misc.py +++ b/docutils/test/test_writers/test_latex2e_misc.py @@ -32,45 +32,75 @@ bar """ -class TocTestCase(unittest.TestCase): +class PublishTestCase(unittest.TestCase): + + settings = {'_disable_config': True, + # avoid latex writer future warnings: + 'use_latex_citations': False, + 'legacy_column_widths': True, + } def test_publish_from_doctree(self): - """Ignore the Docutils-generated ToC, when ``use_latex_toc`` - is True. (This did happen when publishing from a doctree.) + """Ignore the Docutils-generated ToC when ``use_latex_toc`` is True. + + (This did happen when publishing from a doctree.) """ - mysettings = {'output_encoding': 'unicode', - '_disable_config': True, - # avoid latex writer future warnings: - 'use_latex_citations': False, - 'legacy_column_widths': True, - } + settings = self.settings.copy() + settings['output_encoding'] = 'unicode' doctree = core.publish_doctree(contents_test_input, - settings_overrides=mysettings) + settings_overrides=settings) result = core.publish_from_doctree(doctree, writer_name='latex', - settings_overrides=mysettings) + settings_overrides=settings) self.assertNotIn(r'\item \hyperref[foo]{foo}', result) # self.assertIn(r'\tableofcontents', result) + def test_publish_parts(self): + """Check for the presence of documented parts. + """ + # parts = core.publish_parts(contents_test_input, + # settings_overrides=self.settings) + # documented_parts = [ + # 'abstract', + # 'body', + # 'body_pre_docinfo', + # 'dedication', + # 'docinfo', + # 'encoding', + # 'fallbacks', + # 'head_prefix', + # 'latex_preamble', + # 'pdfsetup', + # 'requirements', + # 'stylesheet', + # 'subtitle', + # 'title', + # 'titledata', + # 'version', + # 'whole' + # ] + # TODO: activate and fix test. + # self.assertEqual(sorted(parts.keys()), documented_parts) + class WarningsTestCase(unittest.TestCase): def test_future_warnings(self): """Warn about changing defaults.""" # Warn only if not set (uncommenting should make test fail): - mysettings = {'_disable_config': True, - # 'use_latex_citations': False, - # 'legacy_column_widths': True, - 'output_encoding': 'unicode', - } + settings = {'_disable_config': True, + # 'use_latex_citations': False, + # 'legacy_column_widths': True, + 'output_encoding': 'unicode', + } with self.assertWarnsRegex(FutureWarning, '"legacy_column_widths" will change'): core.publish_string('warnings test', writer_name='latex', - settings_overrides=mysettings) + settings_overrides=settings) with self.assertWarnsRegex(FutureWarning, '"use_latex_citations" will change'): core.publish_string('warnings test', writer_name='latex', - settings_overrides=mysettings) + settings_overrides=settings) if __name__ == '__main__': |