aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-04-13 17:33:33 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2012-04-13 17:33:33 -0400
commit9262cac6a62a96e6bc8235a1ff95d4dde5820722 (patch)
treed779cc4fc92995e7eaa0202f85562a3cfaab1628
parent8cce6101ac97aa59023f23185a5e1160330a93be (diff)
downloadexternal_python_mako-9262cac6a62a96e6bc8235a1ff95d4dde5820722.tar.gz
external_python_mako-9262cac6a62a96e6bc8235a1ff95d4dde5820722.tar.bz2
external_python_mako-9262cac6a62a96e6bc8235a1ff95d4dde5820722.zip
- remove context manager to allow 2.5 and 2.4 to work, [ticket:188]
- add conditionals to pygments-dependent tests that ensure pygments 1.4, separate check for no pygments.
-rw-r--r--mako/util.py1
-rw-r--r--test/__init__.py18
-rw-r--r--test/test_exceptions.py179
-rw-r--r--test/test_template.py9
4 files changed, 140 insertions, 67 deletions
diff --git a/mako/util.py b/mako/util.py
index 3151679..dc070a0 100644
--- a/mako/util.py
+++ b/mako/util.py
@@ -9,6 +9,7 @@ import sys
py3k = getattr(sys, 'py3kwarning', False) or sys.version_info >= (3, 0)
+py26 = sys.version_info >= (2, 6)
py24 = sys.version_info >= (2, 4) and sys.version_info < (2, 5)
jython = sys.platform.startswith('java')
win32 = sys.platform.startswith('win')
diff --git a/test/__init__.py b/test/__init__.py
index d2a1f25..7f258dd 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -1,6 +1,7 @@
from mako.template import Template
import unittest, os
-from mako.util import function_named, py3k
+from mako.util import py3k
+from mako.util import function_named
import re
from nose import SkipTest
@@ -91,3 +92,18 @@ def skip_if(predicate, reason=None):
return fn(*args, **kw)
return function_named(maybe, fn_name)
return decorate
+
+def requires_pygments_14(fn):
+ try:
+ import pygments
+ version = pygments.__version__
+ except:
+ version = "0"
+ return skip_if(lambda: version < "1.4")(fn)
+
+def requires_no_pygments(fn):
+ try:
+ import pygments
+ except:
+ pygments = None
+ return skip_if(lambda:pygments is not None)(fn) \ No newline at end of file
diff --git a/test/test_exceptions.py b/test/test_exceptions.py
index 8b7254b..9ddbc71 100644
--- a/test/test_exceptions.py
+++ b/test/test_exceptions.py
@@ -7,6 +7,7 @@ from mako.template import Template
from mako.lookup import TemplateLookup
from util import result_lines
from test import template_base, module_base, TemplateTest
+from test import requires_pygments_14, requires_no_pygments
class ExceptionsTest(TemplateTest):
def test_html_error_template(self):
@@ -53,8 +54,56 @@ class ExceptionsTest(TemplateTest):
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):
+ """test the html_error_template with a Template containing utf8
+ chars"""
- def test_utf8_html_error_template(self):
+ if util.py3k:
+ code = """# -*- coding: utf-8 -*-
+% if 2 == 2: /an error
+${'привет'}
+% endif
+"""
+ else:
+ code = """# -*- coding: utf-8 -*-
+% if 2 == 2: /an error
+${u'привет'}
+% endif
+"""
+ try:
+ template = Template(code)
+ template.render_unicode()
+ except exceptions.CompileException, ce:
+ html_error = exceptions.html_error_template().render()
+ if util.py3k:
+ assert ("CompileException: Fragment &#39;if 2 == 2: /an "
+ "error&#39; is not a partial control statement "
+ "at line: 2 char: 1").encode(sys.getdefaultencoding(), 'htmlentityreplace') in \
+ html_error
+ else:
+ assert ("CompileException: Fragment &#39;if 2 == 2: /an "
+ "error&#39; is not a partial control statement "
+ "at line: 2 char: 1") in \
+ html_error
+
+ if util.py3k:
+ assert u"".encode(sys.getdefaultencoding(),
+ 'htmlentityreplace') in html_error
+ else:
+ assert u'<pre>3</pre></div></td><td class="code">'\
+ '<div class="syntax-highlighted"><pre><span '\
+ 'class="cp">${</span><span class="s">u&#39;'\
+ '&#x43F;&#x440;&#x438;&#x432;&#x435;&#x442;'\
+ '&#39;</span><span class="cp">}</span>'.encode(
+ sys.getdefaultencoding(),
+ 'htmlentityreplace') in html_error
+ else:
+ assert False, ("This function should trigger a CompileException, "
+ "but didn't")
+
+ @requires_no_pygments
+ def test_utf8_html_error_template_no_pygments(self):
"""test the html_error_template with a Template containing utf8
chars"""
@@ -87,26 +136,11 @@ ${u'привет'}
html_error
if util.py3k:
- try:
- import pygments
- assert u"".encode(sys.getdefaultencoding(),
- 'htmlentityreplace') in html_error
- except ImportError:
- assert u"${&#39;привет&#39;}".encode(sys.getdefaultencoding(),
- 'htmlentityreplace') in html_error
+ assert u"${&#39;привет&#39;}".encode(sys.getdefaultencoding(),
+ 'htmlentityreplace') in html_error
else:
- try:
- import pygments
- assert u'<pre>3</pre></div></td><td class="code">'\
- '<div class="syntax-highlighted"><pre><span '\
- 'class="cp">${</span><span class="s">u&#39;'\
- '&#x43F;&#x440;&#x438;&#x432;&#x435;&#x442;'\
- '&#39;</span><span class="cp">}</span>'.encode(
- sys.getdefaultencoding(),
- 'htmlentityreplace') in html_error
- except ImportError:
- assert u"${u&#39;привет&#39;}".encode(sys.getdefaultencoding(),
- 'htmlentityreplace') in html_error
+ assert u"${u&#39;привет&#39;}".encode(sys.getdefaultencoding(),
+ 'htmlentityreplace') in html_error
else:
assert False, ("This function should trigger a CompileException, "
"but didn't")
@@ -141,7 +175,8 @@ ${u'привет'}
html_error = exceptions.html_error_template().render()
assert u"RuntimeError: 日本".encode('ascii', 'ignore') in html_error
- def test_format_exceptions(self):
+ @requires_pygments_14
+ def test_format_exceptions_pygments(self):
l = TemplateLookup(format_exceptions=True)
l.put_string("foo.html", """
@@ -153,19 +188,62 @@ ${foobar}
${self.body()}
""")
- try:
- import pygments
- assert '<div class="sourceline"><table class="syntax-highlightedtable">'\
- '<tr><td class="linenos"><div class="linenodiv"><pre>3</pre>'\
- '</div></td><td class="code"><div class="syntax-highlighted">'\
- '<pre><span class="err">$</span><span class="p">{</span>'\
- '<span class="n">foobar</span><span class="p">}</span>' in \
- result_lines(l.get_template("foo.html").render_unicode())
- except ImportError:
- assert '<div class="sourceline">${foobar}</div>' in \
- result_lines(l.get_template("foo.html").render_unicode())
+ assert '<div class="sourceline"><table class="syntax-highlightedtable">'\
+ '<tr><td class="linenos"><div class="linenodiv"><pre>3</pre>'\
+ '</div></td><td class="code"><div class="syntax-highlighted">'\
+ '<pre><span class="err">$</span><span class="p">{</span>'\
+ '<span class="n">foobar</span><span class="p">}</span>' in \
+ result_lines(l.get_template("foo.html").render_unicode())
+
+ @requires_no_pygments
+ def test_format_exceptions_no_pygments(self):
+ l = TemplateLookup(format_exceptions=True)
+
+ l.put_string("foo.html", """
+<%inherit file="base.html"/>
+${foobar}
+ """)
+
+ l.put_string("base.html", """
+ ${self.body()}
+ """)
+
+ 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):
+ """test that htmlentityreplace formatting is applied to
+ exceptions reported with format_exceptions=True"""
- def test_utf8_format_exceptions(self):
+ l = TemplateLookup(format_exceptions=True)
+ if util.py3k:
+ l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""")
+ else:
+ l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""")
+
+ if util.py3k:
+ assert '<table class="error syntax-highlightedtable"><tr><td '\
+ 'class="linenos"><div class="linenodiv"><pre>2</pre>'\
+ '</div></td><td class="code"><div class="error '\
+ 'syntax-highlighted"><pre><span class="cp">${</span>'\
+ '<span class="s">&#39;привет&#39;</span> <span class="o">+</span> '\
+ '<span class="n">foobar</span><span class="cp">}</span>'\
+ '<span class="x"></span>' in \
+ result_lines(l.get_template("foo.html").render().decode('utf-8'))
+ else:
+ assert '<table class="error syntax-highlightedtable"><tr><td '\
+ 'class="linenos"><div class="linenodiv"><pre>2</pre>'\
+ '</div></td><td class="code"><div class="error '\
+ 'syntax-highlighted"><pre><span class="cp">${</span>'\
+ '<span class="s">u&#39;&#x43F;&#x440;&#x438;&#x432;'\
+ '&#x435;&#x442;&#39;</span> <span class="o">+</span> '\
+ '<span class="n">foobar</span><span class="cp">}</span>'\
+ '<span class="x"></span>' in \
+ result_lines(l.get_template("foo.html").render().decode('utf-8'))
+
+ @requires_no_pygments
+ def test_utf8_format_exceptions_no_pygments(self):
"""test that htmlentityreplace formatting is applied to
exceptions reported with format_exceptions=True"""
@@ -176,37 +254,12 @@ ${foobar}
l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""")
if util.py3k:
- try:
- import pygments
- assert '<table class="error syntax-highlightedtable"><tr><td '\
- 'class="linenos"><div class="linenodiv"><pre>2</pre>'\
- '</div></td><td class="code"><div class="error '\
- 'syntax-highlighted"><pre><span class="cp">${</span>'\
- '<span class="s">&#39;привет&#39;</span> <span class="o">+</span> '\
- '<span class="n">foobar</span><span class="cp">}</span>'\
- '<span class="x"></span>' in \
- result_lines(l.get_template("foo.html").render().decode('utf-8'))
- except ImportError:
- assert u'<div class="sourceline">${&#39;привет&#39; + foobar}</div>'\
- in result_lines(l.get_template("foo.html").render().decode('utf-8'))
+ assert u'<div class="sourceline">${&#39;привет&#39; + foobar}</div>'\
+ in result_lines(l.get_template("foo.html").render().decode('utf-8'))
else:
- try:
- import pygments
-
- assert '<table class="error syntax-highlightedtable"><tr><td '\
- 'class="linenos"><div class="linenodiv"><pre>2</pre>'\
- '</div></td><td class="code"><div class="error '\
- 'syntax-highlighted"><pre><span class="cp">${</span>'\
- '<span class="s">u&#39;&#x43F;&#x440;&#x438;&#x432;'\
- '&#x435;&#x442;&#39;</span> <span class="o">+</span> '\
- '<span class="n">foobar</span><span class="cp">}</span>'\
- '<span class="x"></span>' in \
- result_lines(l.get_template("foo.html").render().decode('utf-8'))
-
- except ImportError:
- assert '${u&#39;&#x43F;&#x440;&#x438;&#x432;&#x435;'\
- '&#x442;&#39; + foobar}' in \
- result_lines(l.get_template("foo.html").render().decode('utf-8'))
+ assert '${u&#39;&#x43F;&#x440;&#x438;&#x432;&#x435;'\
+ '&#x442;&#39; + foobar}' in \
+ result_lines(l.get_template("foo.html").render().decode('utf-8'))
def test_custom_tback(self):
diff --git a/test/test_template.py b/test/test_template.py
index 6a617ef..a1ab11e 100644
--- a/test/test_template.py
+++ b/test/test_template.py
@@ -873,6 +873,7 @@ class ControlTest(TemplateTest):
filters=lambda s:s.strip()
)
+ @skip_if(lambda: not util.py26)
def test_blank_control_8(self):
self._do_memory_test(
"""
@@ -970,6 +971,7 @@ class ControlTest(TemplateTest):
filters=lambda s:s.strip()
)
+ @skip_if(lambda: not util.py26)
def test_commented_blank_control_8(self):
self._do_memory_test(
"""
@@ -1093,9 +1095,10 @@ class ModuleDirTest(TemplateTest):
def test_custom_writer(self):
canary = []
def write_module(source, outputpath):
- with open(outputpath, 'wb') as f:
- canary.append(outputpath)
- f.write(source)
+ f = open(outputpath, 'wb')
+ canary.append(outputpath)
+ f.write(source)
+ f.close()
lookup = TemplateLookup(template_base, module_writer=write_module,
module_directory=module_base)
t = lookup.get_template('/modtest.html')