diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-05-29 18:18:04 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-05-31 14:03:02 -0400 |
commit | fb7f0437323ba836c68947d38f3604c3336e3a9b (patch) | |
tree | 756512a2b68f1c7270f028b487228fd0e3f13778 /test/test_exceptions.py | |
parent | db498f217e03d772e0c0c37a526226d48ed790e2 (diff) | |
download | external_python_mako-fb7f0437323ba836c68947d38f3604c3336e3a9b.tar.gz external_python_mako-fb7f0437323ba836c68947d38f3604c3336e3a9b.tar.bz2 external_python_mako-fb7f0437323ba836c68947d38f3604c3336e3a9b.zip |
Use tox / zimports
Change-Id: Ia047c7052a6d242c2cf1c7a83981f1e38ea4d928
Diffstat (limited to 'test/test_exceptions.py')
-rw-r--r-- | test/test_exceptions.py | 287 |
1 files changed, 183 insertions, 104 deletions
diff --git a/test/test_exceptions.py b/test/test_exceptions.py index 242577f..c680591 100644 --- a/test/test_exceptions.py +++ b/test/test_exceptions.py @@ -1,13 +1,16 @@ # -*- coding: utf-8 -*- import sys -from mako import exceptions, compat -from mako.template import Template -from mako.lookup import TemplateLookup +from mako import compat +from mako import exceptions from mako.compat import u -from test.util import result_lines +from mako.lookup import TemplateLookup +from mako.template import Template +from test import requires_no_pygments_exceptions +from test import requires_pygments_14 from test import TemplateTest -from test import requires_pygments_14, requires_no_pygments_exceptions +from test.util import result_lines + class ExceptionsTest(TemplateTest): def test_html_error_template(self): @@ -21,24 +24,28 @@ class ExceptionsTest(TemplateTest): assert False except exceptions.CompileException: html_error = exceptions.html_error_template().render_unicode() - assert ("CompileException: Fragment 'i = 0' is not " - "a partial control statement at line: 2 char: 1") in html_error - assert '<style>' in html_error + assert ( + "CompileException: Fragment 'i = 0' is not " + "a partial control statement at line: 2 char: 1" + ) in html_error + assert "<style>" in html_error html_error_stripped = html_error.strip() - assert html_error_stripped.startswith('<html>') - assert html_error_stripped.endswith('</html>') + assert html_error_stripped.startswith("<html>") + assert html_error_stripped.endswith("</html>") - not_full = exceptions.html_error_template().\ - render_unicode(full=False) - assert '<html>' not in not_full - assert '<style>' in not_full + not_full = exceptions.html_error_template().render_unicode( + full=False + ) + assert "<html>" not in not_full + assert "<style>" in not_full - no_css = exceptions.html_error_template().\ - render_unicode(css=False) - assert '<style>' not in no_css + no_css = exceptions.html_error_template().render_unicode(css=False) + assert "<style>" not in no_css else: - assert False, ("This function should trigger a CompileException, " - "but didn't") + assert False, ( + "This function should trigger a CompileException, " + "but didn't" + ) def test_text_error_template(self): code = """ @@ -50,9 +57,11 @@ class ExceptionsTest(TemplateTest): assert False except exceptions.CompileException: text_error = exceptions.text_error_template().render_unicode() - assert 'Traceback (most recent call last):' in text_error - assert ("CompileException: Fragment 'i = 0' is not a partial " - "control statement") in text_error + assert "Traceback (most recent call last):" in text_error + assert ( + "CompileException: Fragment 'i = 0' is not a partial " + "control statement" + ) in text_error @requires_pygments_14 def test_utf8_html_error_template_pygments(self): @@ -77,28 +86,40 @@ ${u'привет'} except exceptions.CompileException: html_error = exceptions.html_error_template().render() if compat.py3k: - assert ("CompileException: Fragment 'if 2 == 2: /an " - "error' is not a partial control statement " - "at line: 2 char: 1").encode(sys.getdefaultencoding(), 'htmlentityreplace') in \ - html_error - else: - assert ("CompileException: Fragment 'if 2 == 2: /an " + assert ( + ( + "CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control statement " - "at line: 2 char: 1") in \ - html_error + "at line: 2 char: 1" + ).encode(sys.getdefaultencoding(), "htmlentityreplace") + in html_error + ) + else: + assert ( + "CompileException: Fragment 'if 2 == 2: /an " + "error' is not a partial control statement " + "at line: 2 char: 1" + ) in html_error if compat.py3k: - assert "".encode(sys.getdefaultencoding(), - 'htmlentityreplace') in html_error + assert ( + "".encode(sys.getdefaultencoding(), "htmlentityreplace") + in html_error + ) else: - assert '''\ - 'привет'\ - ''</span><span class="cp">}</span>'.encode( - sys.getdefaultencoding(), - 'htmlentityreplace') in html_error + assert ( + "'" + "привет" + ''</span><span class="cp">}</span>'.encode( + sys.getdefaultencoding(), "htmlentityreplace" + ) + in html_error + ) else: - assert False, ("This function should trigger a CompileException, " - "but didn't") + assert False, ( + "This function should trigger a CompileException, " + "but didn't" + ) @requires_no_pygments_exceptions def test_utf8_html_error_template_no_pygments(self): @@ -123,87 +144,117 @@ ${u'привет'} except exceptions.CompileException: html_error = exceptions.html_error_template().render() if compat.py3k: - assert ("CompileException: Fragment 'if 2 == 2: /an " - "error' is not a partial control statement " - "at line: 2 char: 1").encode(sys.getdefaultencoding(), - 'htmlentityreplace') in \ - html_error - else: - assert ("CompileException: Fragment 'if 2 == 2: /an " + assert ( + ( + "CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control statement " - "at line: 2 char: 1") in \ - html_error + "at line: 2 char: 1" + ).encode(sys.getdefaultencoding(), "htmlentityreplace") + in html_error + ) + else: + assert ( + "CompileException: Fragment 'if 2 == 2: /an " + "error' is not a partial control statement " + "at line: 2 char: 1" + ) in html_error if compat.py3k: - assert "${'привет'}".encode(sys.getdefaultencoding(), - 'htmlentityreplace') in html_error + assert ( + "${'привет'}".encode( + sys.getdefaultencoding(), "htmlentityreplace" + ) + in html_error + ) else: - assert u("${u'привет'}").encode(sys.getdefaultencoding(), - 'htmlentityreplace') in html_error + assert ( + u("${u'привет'}").encode( + sys.getdefaultencoding(), "htmlentityreplace" + ) + in html_error + ) else: - assert False, ("This function should trigger a CompileException, " - "but didn't") + assert False, ( + "This function should trigger a CompileException, " + "but didn't" + ) def test_format_closures(self): try: - exec("def foo():"\ - " raise RuntimeError('test')", locals()) - foo() + exec("def foo():" " raise RuntimeError('test')", locals()) + foo() # noqa except: html_error = exceptions.html_error_template().render() assert "RuntimeError: test" in str(html_error) def test_py_utf8_html_error_template(self): try: - foo = u('日本') - raise RuntimeError('test') + foo = u("日本") # noqa + raise RuntimeError("test") except: html_error = exceptions.html_error_template().render() if compat.py3k: - assert 'RuntimeError: test' in html_error.decode('utf-8') - assert "foo = u('日本')" in html_error.decode('utf-8') + assert "RuntimeError: test" in html_error.decode("utf-8") + assert "foo = u("日本")" in html_error.decode("utf-8") else: - assert 'RuntimeError: test' in html_error - assert "foo = u('日本')" in html_error + assert "RuntimeError: test" in html_error + assert "foo = u("日本")" in html_error def test_py_unicode_error_html_error_template(self): try: - raise RuntimeError(u('日本')) + raise RuntimeError(u("日本")) except: html_error = exceptions.html_error_template().render() - assert u("RuntimeError: 日本").encode('ascii', 'ignore') in html_error + assert ( + u("RuntimeError: 日本").encode("ascii", "ignore") in html_error + ) @requires_pygments_14 def test_format_exceptions_pygments(self): l = TemplateLookup(format_exceptions=True) - l.put_string("foo.html", """ + l.put_string( + "foo.html", + """ <%inherit file="base.html"/> ${foobar} - """) + """, + ) - l.put_string("base.html", """ + l.put_string( + "base.html", + """ ${self.body()} - """) + """, + ) - assert '<div class="sourceline"><table class="syntax-highlightedtable">' in \ - l.get_template("foo.html").render_unicode() + assert ( + '<div class="sourceline"><table class="syntax-highlightedtable">' + in l.get_template("foo.html").render_unicode() + ) @requires_no_pygments_exceptions def test_format_exceptions_no_pygments(self): l = TemplateLookup(format_exceptions=True) - l.put_string("foo.html", """ + l.put_string( + "foo.html", + """ <%inherit file="base.html"/> ${foobar} - """) + """, + ) - l.put_string("base.html", """ + l.put_string( + "base.html", + """ ${self.body()} - """) + """, + ) - assert '<div class="sourceline">${foobar}</div>' in \ - result_lines(l.get_template("foo.html").render_unicode()) + assert '<div class="sourceline">${foobar}</div>' in result_lines( + l.get_template("foo.html").render_unicode() + ) @requires_pygments_14 def test_utf8_format_exceptions_pygments(self): @@ -212,17 +263,24 @@ ${foobar} l = TemplateLookup(format_exceptions=True) if compat.py3k: - l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""") + l.put_string( + "foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""" + ) else: - l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""") + l.put_string( + "foo.html", + """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""", + ) if compat.py3k: - assert ''привет'</span>' in \ - l.get_template("foo.html").render().decode('utf-8') + assert "'привет'</span>" in l.get_template( + "foo.html" + ).render().decode("utf-8") else: - assert ''прив'\ - 'ет'</span>' in \ - l.get_template("foo.html").render().decode('utf-8') + assert ( + "'прив" + "ет'</span>" + ) in l.get_template("foo.html").render().decode("utf-8") @requires_no_pygments_exceptions def test_utf8_format_exceptions_no_pygments(self): @@ -231,44 +289,59 @@ ${foobar} l = TemplateLookup(format_exceptions=True) if compat.py3k: - l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""") + l.put_string( + "foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""" + ) else: - l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""") + l.put_string( + "foo.html", + """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""", + ) if compat.py3k: - assert '<div class="sourceline">${'привет' + foobar}</div>'\ - in result_lines(l.get_template("foo.html").render().decode('utf-8')) + assert ( + '<div class="sourceline">${'привет' + foobar}</div>' + in result_lines( + l.get_template("foo.html").render().decode("utf-8") + ) + ) else: - assert '${u'приве'\ - 'т' + foobar}' in \ - result_lines(l.get_template("foo.html").render().decode('utf-8')) + assert ( + "${u'приве" + "т' + foobar}" + in result_lines( + l.get_template("foo.html").render().decode("utf-8") + ) + ) def test_mod_no_encoding(self): mod = __import__("test.foo.mod_no_encoding").foo.mod_no_encoding try: - mod.run() + mod.run() except: t, v, tback = sys.exc_info() - html_error = exceptions.html_error_template().\ - render_unicode(error=v, traceback=tback) + exceptions.html_error_template().render_unicode( + error=v, traceback=tback + ) def test_custom_tback(self): try: raise RuntimeError("error 1") - foo('bar') + foo("bar") # noqa except: t, v, tback = sys.exc_info() try: raise RuntimeError("error 2") except: - html_error = exceptions.html_error_template().\ - render_unicode(error=v, traceback=tback) + html_error = exceptions.html_error_template().render_unicode( + error=v, traceback=tback + ) # obfuscate the text so that this text # isn't in the 'wrong' exception - assert "".join(reversed(");93#&rab;93#&(oof")) in html_error + assert "".join(reversed(");touq&rab;touq&(oof")) in html_error def test_tback_no_trace_from_py_file(self): try: @@ -282,9 +355,13 @@ ${foobar} sys.exc_clear() # and don't even send what we have. - html_error = exceptions.html_error_template().\ - render_unicode(error=v, traceback=None) - assert "local variable 'y' referenced before assignment" in html_error + html_error = exceptions.html_error_template().render_unicode( + error=v, traceback=None + ) + assert ( + "local variable 'y' referenced before assignment" + in html_error + ) def test_tback_trace_from_py_file(self): t = self._file_template("runtimeerr.html") @@ -292,7 +369,9 @@ ${foobar} t.render() assert False except: - html_error = exceptions.html_error_template().\ - render_unicode() + html_error = exceptions.html_error_template().render_unicode() - assert "local variable 'y' referenced before assignment" in html_error + assert ( + "local variable 'y' referenced before assignment" + in html_error + ) |