diff options
| author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2022-02-21 13:53:31 +0000 |
|---|---|---|
| committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2022-02-21 13:53:31 +0000 |
| commit | afff22f353d004e4e5baa879775a8e24cb5de5a2 (patch) | |
| tree | 44db7390a3a3fe059d971c7def9a6a69bfb3ba57 /docutils/test | |
| parent | 68f5aee63b74990f832e6cb89cbb7d0580860d23 (diff) | |
| download | docutils-afff22f353d004e4e5baa879775a8e24cb5de5a2.tar.gz | |
Simplify/modernise tests for exceptions and warnings.
Use unicode.assertWarnsRegex() instead of "hand written"
warnings catching.
Use unicode.assertRaises() as context manager
(unless this would mean an additional code line).
git-svn-id: https://svn.code.sf.net/p/docutils/code/trunk@9011 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils/test')
| -rw-r--r-- | docutils/test/local-writer.py | 4 | ||||
| -rwxr-xr-x | docutils/test/test_io.py | 7 | ||||
| -rwxr-xr-x | docutils/test/test_nodes.py | 11 | ||||
| -rw-r--r-- | docutils/test/test_parsers/test_get_parser_class.py | 4 | ||||
| -rw-r--r-- | docutils/test/test_parsers/test_recommonmark/test_misc.py | 11 | ||||
| -rwxr-xr-x | docutils/test/test_publisher.py | 8 | ||||
| -rw-r--r-- | docutils/test/test_readers/test_get_reader_class.py | 4 | ||||
| -rwxr-xr-x | docutils/test/test_statemachine.py | 37 | ||||
| -rwxr-xr-x | docutils/test/test_utils.py | 66 | ||||
| -rwxr-xr-x | docutils/test/test_writers/test_docutils_xml.py | 4 | ||||
| -rw-r--r-- | docutils/test/test_writers/test_get_writer_class.py | 4 | ||||
| -rw-r--r-- | docutils/test/test_writers/test_html5_polyglot_misc.py | 18 | ||||
| -rw-r--r-- | docutils/test/test_writers/test_latex2e_misc.py | 13 |
13 files changed, 83 insertions, 108 deletions
diff --git a/docutils/test/local-writer.py b/docutils/test/local-writer.py index 891bb9273..4e0c19604 100644 --- a/docutils/test/local-writer.py +++ b/docutils/test/local-writer.py @@ -8,10 +8,6 @@ mini-writer to test get_writer_class with local writer import docutils from docutils import nodes, writers, languages -try: - import roman -except ImportError: - import docutils.utils.roman as roman class Writer(writers.Writer): diff --git a/docutils/test/test_io.py b/docutils/test/test_io.py index 7c21b4b24..8d3cc3f32 100755 --- a/docutils/test/test_io.py +++ b/docutils/test/test_io.py @@ -11,7 +11,6 @@ Test module for io.py. from io import StringIO, BytesIO import sys import unittest -import warnings import DocutilsTestSupport # must be imported before docutils from docutils import io @@ -185,11 +184,9 @@ class OutputTests(unittest.TestCase): self.assertEqual(self.udrain.getvalue(), self.udata) def test_FileOutput_hande_io_errors_deprection_warning(self): - with warnings.catch_warnings(record=True) as wng: - warnings.simplefilter("always") + with self.assertWarnsRegex(DeprecationWarning, + '"handle_io_errors" is ignored'): fo = io.FileOutput(handle_io_errors=True) - self.assertEqual(len(wng), 1, "Expected a DeprecationWarning.") - assert issubclass(wng[0].category, DeprecationWarning) # With destination in binary mode, data must be binary string # and is written as-is: diff --git a/docutils/test/test_nodes.py b/docutils/test/test_nodes.py index 605ffa2d9..d1c9e998f 100755 --- a/docutils/test/test_nodes.py +++ b/docutils/test/test_nodes.py @@ -8,7 +8,6 @@ Test module for nodes.py. """ import unittest -import warnings import DocutilsTestSupport # must be imported before docutils from DocutilsTestSupport import nodes, utils @@ -67,11 +66,9 @@ class TextTests(unittest.TestCase): r"<#text: 'Mary had a lit ...'>") def test_Text_rawsource_deprection_warning(self): - with warnings.catch_warnings(record=True) as wng: - warnings.simplefilter("always") + with self.assertWarnsRegex(DeprecationWarning, + '"rawsource" is ignored'): nodes.Text('content', rawsource='content') - self.assertEqual(len(wng), 1, "Expected a DeprecationWarning.") - assert issubclass(wng[-1].category, DeprecationWarning) class ElementTests(unittest.TestCase): @@ -708,8 +705,8 @@ class NodeVisitorTests(unittest.TestCase): def test_dispatch_visit_unknown(self): # raise exception if no visit/depart methods are defined for node class - self.assertRaises(NotImplementedError, - self.visitor.dispatch_visit, self.element) + with self.assertRaises(NotImplementedError): + self.visitor.dispatch_visit(self.element) def test_dispatch_visit_optional(self): # silently skip nodes of a calss in tuple nodes.NodeVisitor.optional diff --git a/docutils/test/test_parsers/test_get_parser_class.py b/docutils/test/test_parsers/test_get_parser_class.py index fec6a2bab..b1262cff5 100644 --- a/docutils/test/test_parsers/test_get_parser_class.py +++ b/docutils/test/test_parsers/test_get_parser_class.py @@ -21,8 +21,8 @@ class GetParserClassTestCase(DocutilsTestSupport.StandardTestCase): # raises ImportError on failure def test_bogus_parser(self): - self.assertRaises(ImportError, - get_parser_class, 'nope') + with self.assertRaises(ImportError): + get_parser_class('nope') def test_local_parser(self): # requires local-parser.py in test directory (testroot) diff --git a/docutils/test/test_parsers/test_recommonmark/test_misc.py b/docutils/test/test_parsers/test_recommonmark/test_misc.py index 4c7052c7a..02e7ba490 100644 --- a/docutils/test/test_parsers/test_recommonmark/test_misc.py +++ b/docutils/test/test_parsers/test_recommonmark/test_misc.py @@ -72,13 +72,10 @@ class RecommonmarkParserTests(unittest.TestCase): class RecommonmarkMissingTests(unittest.TestCase): def test_missing_parser_message(self): - try: - output = publish_string(sample_with_html, - parser_name='recommonmark') - except ImportError as err: - self.assertIn( - 'requires the package https://pypi.org/project/recommonmark', - str(err)) + with self.assertRaisesRegex(ImportError, + 'requires the package .*recommonmark'): + publish_string(sample_with_html, parser_name='recommonmark') + if __name__ == '__main__': unittest.main() diff --git a/docutils/test/test_publisher.py b/docutils/test/test_publisher.py index f00fc7344..c3fa0cc43 100755 --- a/docutils/test/test_publisher.py +++ b/docutils/test/test_publisher.py @@ -65,20 +65,16 @@ class PublisherTests(DocutilsTestSupport.StandardTestCase): # exits with a short message, if `traceback` is False, # pass IOErrors to calling application if `traceback` is True - try: + with self.assertRaises(IOError): core.publish_cmdline(argv=['nonexisting/path'], settings_overrides={'traceback': True}) - except IOError as e: - self.assertTrue(isinstance(e, io.InputError)) def test_output_error_handling(self): # pass IOErrors to calling application if `traceback` is True - try: + with self.assertRaises(io.OutputError): core.publish_cmdline(argv=['data/include.txt', 'nonexisting/path'], settings_overrides={'traceback': True}) - except IOError as e: - self.assertTrue(isinstance(e, io.OutputError)) class PublishDoctreeTestCase(DocutilsTestSupport.StandardTestCase, docutils.SettingsSpec): diff --git a/docutils/test/test_readers/test_get_reader_class.py b/docutils/test/test_readers/test_get_reader_class.py index 72eb4a20e..8c9566047 100644 --- a/docutils/test/test_readers/test_get_reader_class.py +++ b/docutils/test/test_readers/test_get_reader_class.py @@ -22,8 +22,8 @@ class GetReaderClassTestCase(DocutilsTestSupport.StandardTestCase): # raises ImportError on failure def test_bogus_reader(self): - self.assertRaises(ImportError, - get_reader_class, 'nope') + with self.assertRaises(ImportError): + get_reader_class('nope') def test_local_reader(self): # requires local-reader.py in test directory (testroot) diff --git a/docutils/test/test_statemachine.py b/docutils/test/test_statemachine.py index 3532590a6..4e2a9b723 100755 --- a/docutils/test/test_statemachine.py +++ b/docutils/test/test_statemachine.py @@ -110,8 +110,8 @@ class EmptySMTests(unittest.TestCase): def test_add_state(self): self.sm.add_state(statemachine.State) self.assertTrue(len(self.sm.states) == 1) - self.assertRaises(statemachine.DuplicateStateError, self.sm.add_state, - statemachine.State) + with self.assertRaises(statemachine.DuplicateStateError): + self.sm.add_state(statemachine.State) self.sm.add_state(statemachine.StateWS) self.assertTrue(len(self.sm.states) == 2) @@ -122,8 +122,8 @@ class EmptySMTests(unittest.TestCase): def test_get_state(self): self.assertRaises(statemachine.UnknownStateError, self.sm.get_state) self.sm.add_states((statemachine.State, statemachine.StateWS)) - self.assertRaises(statemachine.UnknownStateError, self.sm.get_state, - 'unknownState') + with self.assertRaises(statemachine.UnknownStateError): + self.sm.get_state('unknownState') self.assertTrue(isinstance(self.sm.get_state('State'), statemachine.State)) self.assertTrue(isinstance(self.sm.get_state('StateWS'), @@ -196,8 +196,8 @@ class SMWSTests(unittest.TestCase): textblock = self.sm.get_text_block(flush_left=1) self.assertEqual(textblock, testtext[:1]) self.sm.next_line(2) - self.assertRaises(statemachine.UnexpectedIndentationError, - self.sm.get_text_block, flush_left=1) + with self.assertRaises(statemachine.UnexpectedIndentationError): + self.sm.get_text_block(flush_left=1) def test_run(self): self.assertEqual(self.sm.run(testtext), expected) @@ -221,18 +221,17 @@ class EmptyStateTests(unittest.TestCase): self.assertEqual(len(self.state.transitions), 0) self.state.add_transitions(['None'], {'None': None}) self.assertEqual(len(self.state.transitions), 1) - self.assertRaises(statemachine.UnknownTransitionError, - self.state.add_transitions, ['bogus'], {}) - self.assertRaises(statemachine.DuplicateTransitionError, - self.state.add_transitions, ['None'], - {'None': None}) + with self.assertRaises(statemachine.UnknownTransitionError): + self.state.add_transitions(['bogus'], {}) + with self.assertRaises(statemachine.DuplicateTransitionError): + self.state.add_transitions(['None'], {'None': None}) def test_add_transition(self): self.assertEqual(len(self.state.transitions), 0) self.state.add_transition('None', None) self.assertEqual(len(self.state.transitions), 1) - self.assertRaises(statemachine.DuplicateTransitionError, - self.state.add_transition, 'None', None) + with self.assertRaises(statemachine.DuplicateTransitionError): + self.state.add_transition('None', None) def test_remove_transition(self): self.assertEqual(len(self.state.transitions), 0) @@ -240,8 +239,8 @@ class EmptyStateTests(unittest.TestCase): self.assertEqual(len(self.state.transitions), 1) self.state.remove_transition('None') self.assertEqual(len(self.state.transitions), 0) - self.assertRaises(statemachine.UnknownTransitionError, - self.state.remove_transition, 'None') + with self.assertRaises(statemachine.UnknownTransitionError): + self.state.remove_transition('None') def test_make_transition(self): dummy = re.compile('dummy') @@ -250,10 +249,10 @@ class EmptyStateTests(unittest.TestCase): self.assertEqual(self.state.make_transition('nop'), (dummy, self.state.nop, self.state.__class__.__name__)) - self.assertRaises(statemachine.TransitionPatternNotFound, - self.state.make_transition, 'None') - self.assertRaises(statemachine.TransitionMethodNotFound, - self.state.make_transition, 'bogus') + with self.assertRaises(statemachine.TransitionPatternNotFound): + self.state.make_transition('None') + with self.assertRaises(statemachine.TransitionMethodNotFound): + self.state.make_transition('bogus') def test_make_transitions(self): dummy = re.compile('dummy') diff --git a/docutils/test/test_utils.py b/docutils/test/test_utils.py index 31c4ec7a7..5e95818eb 100755 --- a/docutils/test/test_utils.py +++ b/docutils/test/test_utils.py @@ -66,8 +66,9 @@ class ReporterTests(unittest.TestCase): 'test data:: (ERROR/3) an error\n') def test_level4(self): - self.assertRaises(utils.SystemMessage, self.reporter.system_message, 4, - 'a severe error, raises an exception') + with self.assertRaises(utils.SystemMessage): + self.reporter.system_message( + 4, 'a severe error, raises an exception') self.assertEqual(self.stream.getvalue(), 'test data:: (SEVERE/4) ' 'a severe error, raises an exception\n') @@ -149,20 +150,18 @@ class QuietReporterTests(unittest.TestCase): class NameValueTests(unittest.TestCase): def test_extract_name_value(self): - self.assertRaises(utils.NameValueError, utils.extract_name_value, - 'hello') - self.assertRaises(utils.NameValueError, utils.extract_name_value, - 'hello') - self.assertRaises(utils.NameValueError, utils.extract_name_value, - '=hello') - self.assertRaises(utils.NameValueError, utils.extract_name_value, - 'hello=') - self.assertRaises(utils.NameValueError, utils.extract_name_value, - 'hello="') - self.assertRaises(utils.NameValueError, utils.extract_name_value, - 'hello="something') - self.assertRaises(utils.NameValueError, utils.extract_name_value, - 'hello="something"else') + with self.assertRaises(utils.NameValueError): + utils.extract_name_value('hello') + with self.assertRaises(utils.NameValueError): + utils.extract_name_value('=hello') + with self.assertRaises(utils.NameValueError): + utils.extract_name_value('hello=') + with self.assertRaises(utils.NameValueError): + utils.extract_name_value('hello="') + with self.assertRaises(utils.NameValueError): + utils.extract_name_value('hello="something') + with self.assertRaises(utils.NameValueError): + utils.extract_name_value('hello="something"else') output = utils.extract_name_value( """att1=val1 att2=val2 att3="value number '3'" att4=val4""") self.assertEqual(output, [('att1', 'val1'), ('att2', 'val2'), @@ -181,11 +180,11 @@ class ExtensionOptionTests(unittest.TestCase): utils.assemble_option_dict(input, self.optionspec), {'a': 1, 'bbb': 2.0, 'cdef': ('hol%s' % chr(224))}) input = utils.extract_name_value('a=1 b=2.0 c=hol%s' % chr(224)) - self.assertRaises(KeyError, utils.assemble_option_dict, - input, self.optionspec) + with self.assertRaises(KeyError): + utils.assemble_option_dict(input, self.optionspec) input = utils.extract_name_value('a=1 bbb=two cdef=hol%s' % chr(224)) - self.assertRaises(ValueError, utils.assemble_option_dict, - input, self.optionspec) + with self.assertRaises(ValueError): + utils.assemble_option_dict(input, self.optionspec) def test_extract_extension_options(self): field_list = nodes.field_list() @@ -205,32 +204,29 @@ class ExtensionOptionTests(unittest.TestCase): {'a': 1, 'bbb': 2.0, 'cdef': 'hol\u00e0', 'empty': None}) - self.assertRaises(KeyError, utils.extract_extension_options, - field_list, {}) + with self.assertRaises(KeyError): + utils.extract_extension_options(field_list, {}) field_list += nodes.field( '', nodes.field_name('', 'cdef'), nodes.field_body('', nodes.paragraph('', 'one'), nodes.paragraph('', 'two'))) - self.assertRaises(utils.BadOptionDataError, - utils.extract_extension_options, - field_list, self.optionspec) + with self.assertRaises(utils.BadOptionDataError): + utils.extract_extension_options(field_list, self.optionspec) field_list[-1] = nodes.field( '', nodes.field_name('', 'cdef bad'), nodes.field_body('', nodes.paragraph('', 'no arguments'))) - self.assertRaises(utils.BadOptionError, - utils.extract_extension_options, - field_list, self.optionspec) + with self.assertRaises(utils.BadOptionError): + utils.extract_extension_options(field_list, self.optionspec) field_list[-1] = nodes.field( '', nodes.field_name('', 'cdef'), nodes.field_body('', nodes.paragraph('', 'duplicate'))) - self.assertRaises(utils.DuplicateOptionError, - utils.extract_extension_options, - field_list, self.optionspec) + with self.assertRaises(utils.DuplicateOptionError): + utils.extract_extension_options(field_list, self.optionspec) field_list[-2] = nodes.field( '', nodes.field_name('', 'unkown'), nodes.field_body('', nodes.paragraph('', 'unknown'))) - self.assertRaises(KeyError, utils.extract_extension_options, - field_list, self.optionspec) + with self.assertRaises(KeyError): + utils.extract_extension_options(field_list, self.optionspec) class HelperFunctionTests(unittest.TestCase): @@ -381,8 +377,8 @@ class StylesheetFunctionTests(unittest.TestCase): # must not be used together self.stylesheet = 'ham.css, missing.css' self.stylesheet_path = 'man.css, miss2.css' - self.assertRaises(AssertionError, - utils.get_stylesheet_list, self) + with self.assertRaises(AssertionError): + utils.get_stylesheet_list(self) diff --git a/docutils/test/test_writers/test_docutils_xml.py b/docutils/test/test_writers/test_docutils_xml.py index c6f8bb434..0ee39a020 100755 --- a/docutils/test/test_writers/test_docutils_xml.py +++ b/docutils/test/test_writers/test_docutils_xml.py @@ -196,8 +196,8 @@ class DocutilsXMLTestCase(DocutilsTestSupport.StandardTestCase): '<test>inline raw XML</test>\n']) settings['halt_level'] = 2 # convert info messages to exceptions settings['warning_stream'] = '' - self.assertRaises(docutils.utils.SystemMessage, - publish_xml, settings, invalid_raw_xml_source) + with self.assertRaises(docutils.utils.SystemMessage): + publish_xml(settings, invalid_raw_xml_source) if __name__ == '__main__': diff --git a/docutils/test/test_writers/test_get_writer_class.py b/docutils/test/test_writers/test_get_writer_class.py index f347db68d..06edbf931 100644 --- a/docutils/test/test_writers/test_get_writer_class.py +++ b/docutils/test/test_writers/test_get_writer_class.py @@ -23,8 +23,8 @@ class GetWriterClassTestCase(DocutilsTestSupport.StandardTestCase): # raises ImportError on failure def test_bogus_writer(self): - self.assertRaises(ImportError, - get_writer_class, 'nope') + with self.assertRaises(ImportError): + get_writer_class('nope') def test_local_writer(self): # requires local-writer.py in test directory (testroot) diff --git a/docutils/test/test_writers/test_html5_polyglot_misc.py b/docutils/test/test_writers/test_html5_polyglot_misc.py index e24f61080..3f87d10ec 100644 --- a/docutils/test/test_writers/test_html5_polyglot_misc.py +++ b/docutils/test/test_writers/test_html5_polyglot_misc.py @@ -9,7 +9,6 @@ Miscellaneous HTML writer tests. """ import os -import warnings if __name__ == '__main__': import __init__ @@ -138,17 +137,14 @@ class SettingsTestCase(DocutilsTestSupport.StandardTestCase): self.assertIn('dl.docutils dd {\n margin-bottom: 0.5em }', styles) def test_future_warnings(self): - """Warn about changing defaults.""" - mys={'_disable_config': True, - 'embed_images': False, - } - with warnings.catch_warnings(record=True) as wngs: - warnings.simplefilter("always") + """Warn about deprecated setting name.""" + my_settings={'_disable_config': True, + 'embed_images': False, + } + with self.assertWarnsRegex(FutureWarning, + '"embed_images" will be removed'): core.publish_string('warnings test', writer_name='html5', - settings_overrides=mys) - n_w = sum(issubclass(wng.category, FutureWarning) - for wng in wngs) - self.assertTrue(n_w > 0, "Expected a FutureWarning.") + settings_overrides=my_settings) class MathTestCase(DocutilsTestSupport.StandardTestCase): diff --git a/docutils/test/test_writers/test_latex2e_misc.py b/docutils/test/test_writers/test_latex2e_misc.py index 0e0a65454..eedec8ed8 100644 --- a/docutils/test/test_writers/test_latex2e_misc.py +++ b/docutils/test/test_writers/test_latex2e_misc.py @@ -15,7 +15,6 @@ """ Miscellaneous LaTeX writer tests. """ -import warnings if __name__ == '__main__': import __init__ @@ -58,17 +57,19 @@ class WarningsTestCase(DocutilsTestSupport.StandardTestCase): 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, } - with warnings.catch_warnings(record=True) as wngs: - warnings.simplefilter("always") + with self.assertWarnsRegex(FutureWarning, + '"legacy_column_widths" will change'): + core.publish_string('warnings test', writer_name='latex', + settings_overrides=mysettings) + with self.assertWarnsRegex(FutureWarning, + '"use_latex_citations" will change'): core.publish_string('warnings test', writer_name='latex', settings_overrides=mysettings) - n_w = sum(issubclass(wng.category, FutureWarning) - for wng in wngs) - self.assertTrue(n_w > 1, "Expected 2 FutureWarnings.") if __name__ == '__main__': |
