diff options
Diffstat (limited to 'creole/tests/utils/base_unittest.py')
| -rw-r--r-- | creole/tests/utils/base_unittest.py | 235 |
1 files changed, 129 insertions, 106 deletions
diff --git a/creole/tests/utils/base_unittest.py b/creole/tests/utils/base_unittest.py index f9002fa..bffcf09 100644 --- a/creole/tests/utils/base_unittest.py +++ b/creole/tests/utils/base_unittest.py @@ -11,47 +11,44 @@ :license: GNU GPL v3 or above, see LICENSE for more details. """ -from __future__ import division, absolute_import, print_function, unicode_literals + import re import warnings -from creole.tests.utils.utils import MarkupTest -from creole.py3compat import TEXT_TYPE +from creole import creole2html, html2creole, html2rest, html2textile +from creole.exceptions import DocutilsImportError +from creole.tests.utils.utils import MarkupTest try: import textile except ImportError: test_textile = False warnings.warn( - "Markup error: The Python textile library isn't installed." - " Download: http://pypi.python.org/pypi/textile" + "Markup error: The Python textile library isn't installed." " Download: http://pypi.python.org/pypi/textile" ) else: test_textile = True -from creole.exceptions import DocutilsImportError -from creole import creole2html, html2creole, html2textile, html2rest - try: from creole.rest_tools.clean_writer import rest2html except DocutilsImportError as err: REST_INSTALLED = False - warnings.warn("Can't run all ReSt unittests: %s" % err) + warnings.warn(f"Can't run all ReSt unittests: {err}") else: REST_INSTALLED = True tabs2spaces_re = re.compile(r"^(\t*)(.*?)$", re.M) - def tabs2spaces(html): """ form reformating textile html code >>> tabs2spaces("\\t<p>one<br />\\n\\t\\ttwo<br />\\n\\t\\t\\ttree</p>") '<p>one<br />\\n two<br />\\n tree</p>' """ + def reformat_tabs(match): tabs = match.group(1) text = match.group(2) @@ -60,8 +57,9 @@ def tabs2spaces(html): if indent < 0: indent = 0 -# print(len(tabs), indent, repr(tabs), text) + # print(len(tabs), indent, repr(tabs), text) return " " * indent + text + return tabs2spaces_re.sub(reformat_tabs, html) @@ -70,17 +68,15 @@ def strip_html_lines(html, strip_lines=False): >>> strip_html_lines("\t<p>foo \\n\\n\t\t bar</p>", strip_lines=True) '<p>foo\\nbar</p>' """ - html = "\n".join( - [line.strip(" \t") for line in html.splitlines() if line] - ) + html = "\n".join([line.strip(" \t") for line in html.splitlines() if line]) return html - class BaseCreoleTest(MarkupTest): """ Basic unittest class for all python-creole unittest classes. """ + def _debug_text(self, msg, raw_text): text = raw_text.replace(" ", ".") text = text.replace("\n", "\\n\n") @@ -88,7 +84,7 @@ class BaseCreoleTest(MarkupTest): print print("_" * 79) - print(" Debug Text: %s" % msg) + print(f" Debug Text: {msg}") print(text) print("-" * 79) @@ -105,12 +101,20 @@ class BaseCreoleTest(MarkupTest): f(member, container, *args, **kwargs) def assert_creole2html( - self, raw_creole, raw_html, - strip_lines=False, debug=False, - parser_kwargs={}, emitter_kwargs={}, - block_rules=None, blog_line_breaks=True, macros=None, verbose=None, stderr=None, - strict=False, - ): + self, + raw_creole, + raw_html, + strip_lines=False, + debug=False, + parser_kwargs={}, + emitter_kwargs={}, + block_rules=None, + blog_line_breaks=True, + macros=None, + verbose=None, + stderr=None, + strict=False, + ): """ compare the generated html code from the markup string >creole_string< with the >html_string< reference. @@ -121,10 +125,10 @@ class BaseCreoleTest(MarkupTest): # prepare whitespace on test strings markup_string = self._prepare_text(raw_creole) - assert isinstance(markup_string, TEXT_TYPE) + assert isinstance(markup_string, str) html_string = self._prepare_text(raw_html) - assert isinstance(html_string, TEXT_TYPE) + assert isinstance(html_string, str) if strip_lines: html_string = strip_html_lines(html_string, strip_lines) if debug: @@ -132,9 +136,13 @@ class BaseCreoleTest(MarkupTest): # convert creole markup into html code out_string = creole2html( - markup_string, debug, - block_rules=block_rules, blog_line_breaks=blog_line_breaks, - macros=macros, verbose=verbose, stderr=stderr, + markup_string, + debug, + block_rules=block_rules, + blog_line_breaks=blog_line_breaks, + macros=macros, + verbose=verbose, + stderr=stderr, strict=strict, ) if debug: @@ -148,64 +156,73 @@ class BaseCreoleTest(MarkupTest): # compare self.assertEqual(out_string, html_string, msg="creole2html") - def assert_html2creole2(self, creole, html, - debug=False, - unknown_emit=None, - strict=False, - ): + def assert_html2creole2( + self, creole, html, debug=False, unknown_emit=None, strict=False, + ): # convert html code into creole markup - out_string = html2creole( - html, debug, unknown_emit=unknown_emit, strict=strict - ) + out_string = html2creole(html, debug, unknown_emit=unknown_emit, strict=strict) if debug: self._debug_text("assert_html2creole() html2creole", out_string) # compare self.assertEqual(out_string, creole, msg="html2creole") - def assert_html2creole(self, raw_creole, raw_html, \ - strip_lines=False, debug=False, - # OLD API: - parser_kwargs={}, emitter_kwargs={}, - # html2creole: - unknown_emit=None, - strict=False, - ): + def assert_html2creole( + self, + raw_creole, + raw_html, + strip_lines=False, + debug=False, + # OLD API: + parser_kwargs={}, + emitter_kwargs={}, + # html2creole: + unknown_emit=None, + strict=False, + ): """ Compare the genereted markup from the given >raw_html< html code, with the given >creole_string< reference string. """ self.assertEqual(parser_kwargs, {}, "parser_kwargs is deprecated!") self.assertEqual(emitter_kwargs, {}, "parser_kwargs is deprecated!") -# assert isinstance(raw_html, TEXT_TYPE) -# creole_string = unicode(creole_string, encoding="utf8") -# raw_html = unicode(raw_html, "utf8") + # assert isinstance(raw_html, str) + # creole_string = unicode(creole_string, encoding="utf8") + # raw_html = unicode(raw_html, "utf8") self.assertNotEqual(raw_creole, raw_html) # prepare whitespace on test strings creole = self._prepare_text(raw_creole) - assert isinstance(creole, TEXT_TYPE) + assert isinstance(creole, str) if debug: self._debug_text("assert_creole2html() markup", creole) html = self._prepare_text(raw_html) - assert isinstance(html, TEXT_TYPE) + assert isinstance(html, str) self.assert_html2creole2(creole, html, debug, unknown_emit, strict) - def cross_compare_creole(self, creole_string, html_string, - strip_lines=False, debug=False, - # creole2html old API: - creole_parser_kwargs={}, html_emitter_kwargs={}, - # html2creole old API: - html_parser_kwargs={}, creole_emitter_kwargs={}, - - # creole2html new API: - block_rules=None, blog_line_breaks=True, macros=None, stderr=None, - # html2creole: - unknown_emit=None - ): + def cross_compare_creole( + self, + creole_string, + html_string, + strip_lines=False, + debug=False, + # creole2html old API: + creole_parser_kwargs={}, + html_emitter_kwargs={}, + # html2creole old API: + html_parser_kwargs={}, + creole_emitter_kwargs={}, + # creole2html new API: + block_rules=None, + blog_line_breaks=True, + macros=None, + stderr=None, + # html2creole: + unknown_emit=None, + ): """ Cross compare with: * creole2html @@ -216,23 +233,28 @@ class BaseCreoleTest(MarkupTest): self.assertEqual(html_parser_kwargs, {}, "html_parser_kwargs is deprecated!") self.assertEqual(creole_emitter_kwargs, {}, "creole_emitter_kwargs is deprecated!") - assert isinstance(creole_string, TEXT_TYPE) - assert isinstance(html_string, TEXT_TYPE) + assert isinstance(creole_string, str) + assert isinstance(html_string, str) self.assertNotEqual(creole_string, html_string) self.assert_creole2html( - creole_string, html_string, strip_lines, debug, - block_rules=block_rules, blog_line_breaks=blog_line_breaks, - macros=macros, stderr=stderr, + creole_string, + html_string, + strip_lines, + debug, + block_rules=block_rules, + blog_line_breaks=blog_line_breaks, + macros=macros, + stderr=stderr, ) self.assert_html2creole( - creole_string, html_string, strip_lines, debug, - unknown_emit=unknown_emit, + creole_string, html_string, strip_lines, debug, unknown_emit=unknown_emit, ) - def assert_html2textile(self, textile_string, html_string, \ - strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={}): + def assert_html2textile( + self, textile_string, html_string, strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={} + ): """ Check html2textile """ @@ -255,21 +277,21 @@ class BaseCreoleTest(MarkupTest): return textile_string, html_string - def cross_compare_textile(self, textile_string, html_string, \ - strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={}): + def cross_compare_textile( + self, textile_string, html_string, strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={} + ): """ Checks: * html2textile * textile2html """ -# assert isinstance(textile_string, TEXT_TYPE) -# assert isinstance(html_string, TEXT_TYPE) + # assert isinstance(textile_string, str) + # assert isinstance(html_string, str) self.assertNotEqual(textile_string, html_string) # compare html -> textile textile_string, html_string = self.assert_html2textile( - textile_string, html_string, - strip_lines, debug, parser_kwargs, emitter_kwargs + textile_string, html_string, strip_lines, debug, parser_kwargs, emitter_kwargs ) # compare textile -> html @@ -286,8 +308,9 @@ class BaseCreoleTest(MarkupTest): self.assertEqual(html_string, html, msg="textile2html") - def assert_html2rest(self, rest_string, html_string, \ - strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={}): + def assert_html2rest( + self, rest_string, html_string, strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={} + ): """ Check html to reStructuredText converter """ @@ -310,8 +333,9 @@ class BaseCreoleTest(MarkupTest): return rest_string, html_string - def assert_rest2html(self, rest_string, html_string, \ - strip_lines=False, debug=False, prepare_strings=True, **kwargs): + def assert_rest2html( + self, rest_string, html_string, strip_lines=False, debug=False, prepare_strings=True, **kwargs + ): # compare rest -> html if not REST_INSTALLED: @@ -330,37 +354,40 @@ class BaseCreoleTest(MarkupTest): print(html) html = html.strip() -# html = html.replace("<br />", "<br />\n") -# html = tabs2spaces(html) + # html = html.replace("<br />", "<br />\n") + # html = tabs2spaces(html) if strip_lines: html = strip_html_lines(html, strip_lines) self.assertEqual(html, html_string, msg="rest2html") - def cross_compare_rest(self, rest_string, html_string, \ - strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={}): -# assert isinstance(textile_string, TEXT_TYPE) -# assert isinstance(html_string, TEXT_TYPE) + def cross_compare_rest( + self, rest_string, html_string, strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={} + ): + # assert isinstance(textile_string, str) + # assert isinstance(html_string, str) self.assertNotEqual(rest_string, html_string) rest_string, html_string = self.assert_html2rest( - rest_string, html_string, - strip_lines, debug, parser_kwargs, emitter_kwargs + rest_string, html_string, strip_lines, debug, parser_kwargs, emitter_kwargs ) # compare rest -> html self.assert_rest2html( - rest_string, html_string, - strip_lines=strip_lines, debug=debug, - prepare_strings=False, + rest_string, html_string, strip_lines=strip_lines, debug=debug, prepare_strings=False, ) - def cross_compare(self, - html_string, - creole_string=None, - textile_string=None, - rest_string=None, - strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={}): + def cross_compare( + self, + html_string, + creole_string=None, + textile_string=None, + rest_string=None, + strip_lines=False, + debug=False, + parser_kwargs={}, + emitter_kwargs={}, + ): """ Cross compare with: * creole2html @@ -369,20 +396,16 @@ class BaseCreoleTest(MarkupTest): * html2ReSt """ if creole_string: - self.cross_compare_creole( - creole_string, html_string, strip_lines, debug, parser_kwargs, emitter_kwargs - ) + self.cross_compare_creole(creole_string, html_string, strip_lines, debug, parser_kwargs, emitter_kwargs) if textile_string: - self.cross_compare_textile( - textile_string, html_string, strip_lines, debug, parser_kwargs, emitter_kwargs - ) + self.cross_compare_textile(textile_string, html_string, strip_lines, debug, parser_kwargs, emitter_kwargs) if rest_string: - self.cross_compare_rest( - rest_string, html_string, strip_lines, debug, parser_kwargs, emitter_kwargs - ) + self.cross_compare_rest(rest_string, html_string, strip_lines, debug, parser_kwargs, emitter_kwargs) -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + print(doctest.testmod()) |
