Mike Bayer avatar Mike Bayer committed 1bce86a

- 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.

Comments (0)

Files changed (4)

 
 
 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')
 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
                 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)

test/test_exceptions.py

 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):
             assert ("CompileException: Fragment 'i = 0' is not a partial "
                     "control statement") in text_error
 
- 
-    def test_utf8_html_error_template(self):
+    @requires_pygments_14
+    def test_utf8_html_error_template_pygments(self):
         """test the html_error_template with a Template containing utf8
         chars"""
  
                         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"".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'<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"""
+ 
+        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"${&#39;привет&#39;}".encode(sys.getdefaultencoding(),
+                                        'htmlentityreplace') in html_error
+            else:
+                assert u"${u&#39;привет&#39;}".encode(sys.getdefaultencoding(),
+                                        'htmlentityreplace') in html_error
         else:
             assert False, ("This function should trigger a CompileException, "
                            "but didn't")
             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", """
         ${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())
  
-    def test_utf8_format_exceptions(self):
+    @requires_pygments_14
+    def test_utf8_format_exceptions_pygments(self):
         """test that htmlentityreplace formatting is applied to 
            exceptions reported with format_exceptions=True"""
  
             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 '<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:
-            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'))
 
-                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"""
+ 
+        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}""")
 
-            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'))
+        if util.py3k:
+            assert u'<div class="sourceline">${&#39;привет&#39; + foobar}</div>'\
+                in result_lines(l.get_template("foo.html").render().decode('utf-8'))
+        else:
+            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):

test/test_template.py

             filters=lambda s:s.strip()
         )
  
+    @skip_if(lambda: not util.py26)
     def test_blank_control_8(self):
         self._do_memory_test(
             """
             filters=lambda s:s.strip()
         )
  
+    @skip_if(lambda: not util.py26)
     def test_commented_blank_control_8(self):
         self._do_memory_test(
             """
     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')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.