diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-11-11 14:38:56 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-11-11 14:38:56 -0500 |
commit | 1eb56ef02a7fa825be99ddfb95f217a07dab1cdf (patch) | |
tree | bf0857b7f42dec7db03910799eed7e5dda5e7c2b /test | |
parent | 4ab9664a515c265238d420b0373f7225fe2e53c7 (diff) | |
download | mako-1eb56ef02a7fa825be99ddfb95f217a07dab1cdf.tar.gz |
- first pass at running a py3k compatible base in py2k as well.
having some weird unicode issues I can't debug; the meaning of
str.encode() seems to be changing globally somehow
Diffstat (limited to 'test')
-rw-r--r-- | test/__init__.py | 11 | ||||
-rw-r--r-- | test/templates/foo/modtest.html.py | 2 | ||||
-rw-r--r-- | test/templates/subdir/foo/modtest.html.py | 2 | ||||
-rw-r--r-- | test/test_ast.py | 4 | ||||
-rw-r--r-- | test/test_babelplugin.py | 40 | ||||
-rw-r--r-- | test/test_block.py | 14 | ||||
-rw-r--r-- | test/test_cache.py | 2 | ||||
-rw-r--r-- | test/test_call.py | 63 | ||||
-rw-r--r-- | test/test_decorators.py | 2 | ||||
-rw-r--r-- | test/test_def.py | 8 | ||||
-rw-r--r-- | test/test_exceptions.py | 63 | ||||
-rw-r--r-- | test/test_filters.py | 6 | ||||
-rw-r--r-- | test/test_inheritance.py | 37 | ||||
-rw-r--r-- | test/test_lexer.py | 542 | ||||
-rw-r--r-- | test/test_lookup.py | 4 | ||||
-rw-r--r-- | test/test_loop.py | 10 | ||||
-rw-r--r-- | test/test_lru.py | 61 | ||||
-rw-r--r-- | test/test_namespace.py | 2 | ||||
-rw-r--r-- | test/test_pygen.py | 24 | ||||
-rw-r--r-- | test/test_template.py | 188 | ||||
-rw-r--r-- | test/test_tgplugin.py | 2 | ||||
-rw-r--r-- | test/test_util.py | 6 |
22 files changed, 549 insertions, 544 deletions
diff --git a/test/__init__.py b/test/__init__.py index ded1a5d..c983c5d 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 py3k, py26, py25 +import unittest +import os +from mako.compat import py3k, py26, py25 from mako.util import function_named import re from mako.cache import CacheImpl, register_plugin @@ -64,7 +65,7 @@ def assert_raises(except_cls, callable_, *args, **kw): try: callable_(*args, **kw) success = False - except except_cls, e: + except except_cls as e: success = True # assert outside the block so it works for AssertionError too ! @@ -74,9 +75,9 @@ def assert_raises_message(except_cls, msg, callable_, *args, **kwargs): try: callable_(*args, **kwargs) assert False, "Callable did not raise an exception" - except except_cls, e: + except except_cls as e: assert re.search(msg, str(e)), "%r !~ %s" % (msg, e) - print str(e) + print(str(e)) def skip_if(predicate, reason=None): """Skip a test if predicate is true.""" diff --git a/test/templates/foo/modtest.html.py b/test/templates/foo/modtest.html.py index c5fb76a..e6fc8d8 100644 --- a/test/templates/foo/modtest.html.py +++ b/test/templates/foo/modtest.html.py @@ -17,7 +17,7 @@ def render_body(context,**pageargs): __M_locals = __M_dict_builtin(pageargs=pageargs) __M_writer = context.writer() # SOURCE LINE 1 - __M_writer(u'this is a test') + __M_writer('this is a test') return '' finally: context.caller_stack._pop_frame() diff --git a/test/templates/subdir/foo/modtest.html.py b/test/templates/subdir/foo/modtest.html.py index ca6f37b..b0f50c7 100644 --- a/test/templates/subdir/foo/modtest.html.py +++ b/test/templates/subdir/foo/modtest.html.py @@ -17,7 +17,7 @@ def render_body(context,**pageargs): __M_locals = __M_dict_builtin(pageargs=pageargs) __M_writer = context.writer() # SOURCE LINE 1 - __M_writer(u'this is a test') + __M_writer('this is a test') return '' finally: context.caller_stack._pop_frame() diff --git a/test/test_ast.py b/test/test_ast.py index 8e02811..84508c7 100644 --- a/test/test_ast.py +++ b/test/test_ast.py @@ -1,6 +1,6 @@ import unittest -from mako import ast, exceptions, pyparser, util +from mako import ast, exceptions, pyparser, util, compat from test import eq_, requires_python_2 exception_kwargs = { @@ -237,7 +237,7 @@ import x as bar parsed = ast.PythonFragment("try:", **exception_kwargs) - if util.py3k: + if compat.py3k: parsed = ast.PythonFragment( "except MyException as e:", **exception_kwargs) else: diff --git a/test/test_babelplugin.py b/test/test_babelplugin.py index 8769da0..55be33f 100644 --- a/test/test_babelplugin.py +++ b/test/test_babelplugin.py @@ -19,26 +19,26 @@ class ExtractMakoTestCase(TemplateTest): 'ungettext': (1, 2)}, ['TRANSLATOR:'], {})) expected = \ - [(1, '_', u'Page arg 1', []), - (1, '_', u'Page arg 2', []), - (10, 'gettext', u'Begin', []), - (14, '_', u'Hi there!', [u'TRANSLATOR: Hi there!']), - (19, '_', u'Hello', []), - (22, '_', u'Welcome', []), - (25, '_', u'Yo', []), - (36, '_', u'The', [u'TRANSLATOR: Ensure so and', u'so, thanks']), - (36, 'ungettext', (u'bunny', u'bunnies', None), []), - (41, '_', u'Goodbye', [u'TRANSLATOR: Good bye']), - (44, '_', u'Babel', []), - (45, 'ungettext', (u'hella', u'hellas', None), []), - (62, '_', u'The', [u'TRANSLATOR: Ensure so and', u'so, thanks']), - (62, 'ungettext', (u'bunny', u'bunnies', None), []), - (68, '_', u'Goodbye, really!', [u'TRANSLATOR: HTML comment']), - (71, '_', u'P.S. byebye', []), - (77, '_', u'Top', []), - (83, '_', u'foo', []), - (83, '_', u'baz', []), - (85, '_', u'bar', []) + [(1, '_', 'Page arg 1', []), + (1, '_', 'Page arg 2', []), + (10, 'gettext', 'Begin', []), + (14, '_', 'Hi there!', ['TRANSLATOR: Hi there!']), + (19, '_', 'Hello', []), + (22, '_', 'Welcome', []), + (25, '_', 'Yo', []), + (36, '_', 'The', ['TRANSLATOR: Ensure so and', 'so, thanks']), + (36, 'ungettext', ('bunny', 'bunnies', None), []), + (41, '_', 'Goodbye', ['TRANSLATOR: Good bye']), + (44, '_', 'Babel', []), + (45, 'ungettext', ('hella', 'hellas', None), []), + (62, '_', 'The', ['TRANSLATOR: Ensure so and', 'so, thanks']), + (62, 'ungettext', ('bunny', 'bunnies', None), []), + (68, '_', 'Goodbye, really!', ['TRANSLATOR: HTML comment']), + (71, '_', 'P.S. byebye', []), + (77, '_', 'Top', []), + (83, '_', 'foo', []), + (83, '_', 'baz', []), + (85, '_', 'bar', []) ] self.assertEqual(expected, messages) diff --git a/test/test_block.py b/test/test_block.py index e70b79c..0f5cf17 100644 --- a/test/test_block.py +++ b/test/test_block.py @@ -2,7 +2,7 @@ from mako.template import Template from mako.lookup import TemplateLookup from mako import exceptions from test import TemplateTest, assert_raises, assert_raises_message -from util import flatten_result, result_lines +from .util import flatten_result, result_lines @@ -361,7 +361,7 @@ class BlockTest(TemplateTest): <html> </%block> """) - self._do_test(template, [u'<html>'], + self._do_test(template, ['<html>'], filters=result_lines) def test_anon_in_named(self): @@ -506,7 +506,7 @@ class BlockTest(TemplateTest): """) self._do_test( l.get_template("caller"), - [u'foob, 3, 4'], + ['foob, 3, 4'], filters=result_lines ) @@ -518,7 +518,7 @@ class BlockTest(TemplateTest): """) self._do_test( t, - [u'foob, 3, 4'], + ['foob, 3, 4'], template_args={'val1':3, 'val2':4}, filters=result_lines ) @@ -532,7 +532,7 @@ class BlockTest(TemplateTest): """) self._do_test( t, - [u'foob, 3, 4'], + ['foob, 3, 4'], template_args={'val1':3, 'val2':4}, filters=result_lines ) @@ -545,7 +545,7 @@ class BlockTest(TemplateTest): """) self._do_test( t, - [u'foob, 3, 4'], + ['foob, 3, 4'], template_args={'val1':3, 'val2':4}, filters=result_lines ) @@ -564,6 +564,6 @@ class BlockTest(TemplateTest): """) self._do_test( l.get_template("caller"), - [u'foob, 3, 4'], + ['foob, 3, 4'], filters=result_lines )
\ No newline at end of file diff --git a/test/test_cache.py b/test/test_cache.py index bd8cf47..40539fb 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -2,7 +2,7 @@ from mako.template import Template from mako.lookup import TemplateLookup from mako import lookup import shutil, unittest, os, time -from util import result_lines +from .util import result_lines from test import TemplateTest, template_base, module_base from test import eq_ diff --git a/test/test_call.py b/test/test_call.py index 0bb6079..e735a72 100644 --- a/test/test_call.py +++ b/test/test_call.py @@ -1,6 +1,6 @@ from mako.template import Template from mako import util -from util import result_lines, flatten_result +from .util import result_lines, flatten_result from test import TemplateTest, eq_ class CallTest(TemplateTest): @@ -9,7 +9,7 @@ class CallTest(TemplateTest): <%def name="foo()"> hi im foo ${caller.body(y=5)} </%def> - + <%call expr="foo()" args="y, **kwargs"> this is the body, y is ${y} </%call> @@ -23,16 +23,16 @@ class CallTest(TemplateTest): <%def name="bar()"> this is bar </%def> - + <%def name="comp1()"> this comp1 should not be called </%def> - + <%def name="foo()"> foo calling comp1: ${caller.comp1(x=5)} foo calling body: ${caller.body()} </%def> - + <%call expr="foo()"> <%def name="comp1(x)"> this is comp1, ${x} @@ -46,10 +46,10 @@ class CallTest(TemplateTest): def test_new_syntax(self): """test foo:bar syntax, including multiline args and expression eval.""" - + # note the trailing whitespace in the bottom ${} expr, need to strip # that off < python 2.7 - + t = Template(""" <%def name="foo(x, y, q, z)"> ${x} @@ -57,26 +57,26 @@ class CallTest(TemplateTest): ${q} ${",".join("%s->%s" % (a, b) for a, b in z)} </%def> - + <%self:foo x="this is x" y="${'some ' + 'y'}" q=" this is q" - + z="${[ (1, 2), (3, 4), (5, 6) ] - + }"/> """) - + eq_( result_lines(t.render()), ['this is x', 'some y', 'this', 'is', 'q', '1->2,3->4,5->6'] ) - + def test_ccall_caller(self): t = Template(""" <%def name="outer_func()"> @@ -104,7 +104,7 @@ class CallTest(TemplateTest): "INNER END", "OUTER END", ] - + def test_stack_pop(self): t = Template(""" <%def name="links()" buffered="True"> @@ -130,7 +130,7 @@ class CallTest(TemplateTest): "</h1>", "Some links" ] - + def test_conditional_call(self): """test that 'caller' is non-None only if the immediate <%def> was called via <%call>""" @@ -169,12 +169,12 @@ class CallTest(TemplateTest): "BBB", "CCC" ] - + def test_chained_call(self): """test %calls that are chained through their targets""" t = Template(""" <%def name="a()"> - this is a. + this is a. <%call expr="b()"> this is a's ccall. heres my body: ${caller.body()} </%call> @@ -184,11 +184,11 @@ class CallTest(TemplateTest): whats in the body's caller's body ? ${context.caller_stack[-2].body()} </%def> - + <%call expr="a()"> heres the main templ call </%call> - + """) assert result_lines(t.render()) == [ 'this is a.', @@ -225,7 +225,7 @@ class CallTest(TemplateTest): "bar:", "this is bar body: 10" ] - + def test_nested_call_2(self): t = Template(""" x is ${x} @@ -240,13 +240,13 @@ class CallTest(TemplateTest): <%call expr="foo()"> <%def name="foosub(x)"> this is foo body: ${x} - + <%call expr="bar()"> <%def name="barsub()"> this is bar body: ${x} </%def> </%call> - + </%def> </%call> @@ -278,7 +278,7 @@ class CallTest(TemplateTest): ''') assert flatten_result(template.render()) == "foo" - + def test_nested_call_4(self): base = """ <%def name="A()"> @@ -340,7 +340,7 @@ class CallTest(TemplateTest): t = Template(""" <%def name="embedded()"> <%def name="a()"> - this is a. + this is a. <%call expr="b()"> this is a's ccall. heres my body: ${caller.body()} </%call> @@ -366,7 +366,7 @@ class CallTest(TemplateTest): "whats in the body's caller's body ?", 'heres the main templ call' ] - + def test_call_in_nested(self): t = Template(""" <%def name="a()"> @@ -407,7 +407,7 @@ class CallTest(TemplateTest): context.write("a is done") return '' %> - + <%def name="b()"> this is b our body: ${caller.body()} @@ -430,8 +430,7 @@ class CallTest(TemplateTest): </%call> - """) - #print t.code + """) assert result_lines(t.render()) == [ "test 1", "this is a", @@ -452,7 +451,7 @@ class CallTest(TemplateTest): "this is aa is done", "this is aa is done" ] - + def test_call_in_nested_2(self): t = Template(""" <%def name="a()"> @@ -483,14 +482,14 @@ class CallTest(TemplateTest): class SelfCacheTest(TemplateTest): """this test uses a now non-public API.""" - + def test_basic(self): t = Template(""" <%! cached = None %> <%def name="foo()"> - <% + <% global cached if cached: return "cached: " + cached @@ -503,7 +502,7 @@ class SelfCacheTest(TemplateTest): return cached %> </%def> - + ${foo()} ${foo()} """) @@ -512,4 +511,4 @@ class SelfCacheTest(TemplateTest): "cached:", "this is foo" ] - + diff --git a/test/test_decorators.py b/test/test_decorators.py index fc8768b..74e674c 100644 --- a/test/test_decorators.py +++ b/test/test_decorators.py @@ -1,7 +1,7 @@ from mako.template import Template from mako import lookup import unittest -from util import flatten_result, result_lines +from .util import flatten_result, result_lines class DecoratorTest(unittest.TestCase): def test_toplevel(self): diff --git a/test/test_def.py b/test/test_def.py index 5c7608f..73c1091 100644 --- a/test/test_def.py +++ b/test/test_def.py @@ -1,7 +1,7 @@ from mako.template import Template from mako import lookup from test import TemplateTest -from util import flatten_result, result_lines +from .util import flatten_result, result_lines from test import eq_, assert_raises class DefTest(TemplateTest): @@ -63,8 +63,8 @@ class DefTest(TemplateTest): </%def> """) # check that "a" is declared in "b", but not in "c" - assert "a" not in template.module.render_c.func_code.co_varnames - assert "a" in template.module.render_b.func_code.co_varnames + assert "a" not in template.module.render_c.__code__.co_varnames + assert "a" in template.module.render_b.__code__.co_varnames # then test output eq_( @@ -390,7 +390,7 @@ class ScopeTest(TemplateTest): ${enclosing()} """) try: - print t.render() + print(t.render()) assert False except UnboundLocalError: assert True diff --git a/test/test_exceptions.py b/test/test_exceptions.py index 5503a1f..d7feb9a 100644 --- a/test/test_exceptions.py +++ b/test/test_exceptions.py @@ -1,12 +1,12 @@ # -*- coding: utf-8 -*- import sys -import unittest -from mako import exceptions, util +from mako import exceptions, compat from mako.template import Template from mako.lookup import TemplateLookup -from util import result_lines -from test import template_base, module_base, TemplateTest +from mako.compat import u +from test.util import result_lines +from test import TemplateTest from test import requires_pygments_14, requires_no_pygments, \ requires_python_25_or_greater @@ -21,7 +21,7 @@ class ExceptionsTest(TemplateTest): template = Template(code) template.render_unicode() assert False - except exceptions.CompileException, ce: + except exceptions.CompileException as ce: 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 @@ -50,7 +50,7 @@ class ExceptionsTest(TemplateTest): template = Template(code) template.render_unicode() assert False - except exceptions.CompileException, ce: + except exceptions.CompileException as ce: 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 " @@ -61,7 +61,7 @@ class ExceptionsTest(TemplateTest): """test the html_error_template with a Template containing utf8 chars""" - if util.py3k: + if compat.py3k: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${'привет'} @@ -76,9 +76,9 @@ ${u'привет'} try: template = Template(code) template.render_unicode() - except exceptions.CompileException, ce: + except exceptions.CompileException as ce: html_error = exceptions.html_error_template().render() - if util.py3k: + 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 \ @@ -89,11 +89,11 @@ ${u'привет'} "at line: 2 char: 1") in \ html_error - if util.py3k: - assert u"".encode(sys.getdefaultencoding(), + if compat.py3k: + assert "".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error else: - assert u'<pre>3</pre></div></td><td class="code">'\ + assert '<pre>3</pre></div></td><td class="code">'\ '<div class="syntax-highlighted"><pre><span '\ 'class="cp">${</span><span class="s">u''\ 'привет'\ @@ -109,7 +109,7 @@ ${u'привет'} """test the html_error_template with a Template containing utf8 chars""" - if util.py3k: + if compat.py3k: code = """# -*- coding: utf-8 -*- % if 2 == 2: /an error ${'привет'} @@ -124,9 +124,9 @@ ${u'привет'} try: template = Template(code) template.render_unicode() - except exceptions.CompileException, ce: + except exceptions.CompileException as ce: html_error = exceptions.html_error_template().render() - if util.py3k: + 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 \ @@ -137,11 +137,11 @@ ${u'привет'} "at line: 2 char: 1") in \ html_error - if util.py3k: - assert u"${'привет'}".encode(sys.getdefaultencoding(), + if compat.py3k: + assert "${'привет'}".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error else: - assert u"${u'привет'}".encode(sys.getdefaultencoding(), + assert "${u'привет'}".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error else: assert False, ("This function should trigger a CompileException, " @@ -149,9 +149,8 @@ ${u'привет'} def test_format_closures(self): try: - exec "def foo():"\ - " raise RuntimeError('test')"\ - in locals() + exec("def foo():"\ + " raise RuntimeError('test')", locals()) foo() except: html_error = exceptions.html_error_template().render() @@ -160,23 +159,23 @@ ${u'привет'} @requires_python_25_or_greater def test_py_utf8_html_error_template(self): try: - foo = u'日本' + foo = '日本' raise RuntimeError('test') except: html_error = exceptions.html_error_template().render() - if util.py3k: + if compat.py3k: assert 'RuntimeError: test' in html_error.decode('utf-8') - assert u"foo = '日本'" in html_error.decode('utf-8') + assert "foo = '日本'" in html_error.decode('utf-8') else: 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): @@ -220,12 +219,12 @@ ${foobar} exceptions reported with format_exceptions=True""" l = TemplateLookup(format_exceptions=True) - if util.py3k: + if compat.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: + if compat.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 '\ @@ -251,13 +250,13 @@ ${foobar} exceptions reported with format_exceptions=True""" l = TemplateLookup(format_exceptions=True) - if util.py3k: + if compat.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 u'<div class="sourceline">${'привет' + foobar}</div>'\ + if compat.py3k: + assert '<div class="sourceline">${'привет' + foobar}</div>'\ in result_lines(l.get_template("foo.html").render().decode('utf-8')) else: assert '${u'приве'\ @@ -290,7 +289,7 @@ ${foobar} except: t, v, tback = sys.exc_info() - if not util.py3k: + if not compat.py3k: # blow away tracebaack info sys.exc_clear() diff --git a/test/test_filters.py b/test/test_filters.py index 658715f..d2a6d6f 100644 --- a/test/test_filters.py +++ b/test/test_filters.py @@ -3,7 +3,7 @@ from mako.template import Template import unittest from test import TemplateTest, eq_, requires_python_2 -from util import result_lines, flatten_result +from .util import result_lines, flatten_result class FilterTest(TemplateTest): def test_basic(self): @@ -87,7 +87,7 @@ class FilterTest(TemplateTest): some stuff.... ${x} """, default_filters=['decode.utf8']) #print t.code - assert t.render_unicode(x="voix m’a réveillé").strip() == u"some stuff.... voix m’a réveillé" + assert t.render_unicode(x="voix m’a réveillé").strip() == "some stuff.... voix m’a réveillé" def test_custom_default(self): t = Template(""" @@ -311,7 +311,7 @@ class BufferTest(unittest.TestCase): """) try: - print template.render() + print(template.render()) assert False except TypeError: assert True diff --git a/test/test_inheritance.py b/test/test_inheritance.py index a953847..08a46b3 100644 --- a/test/test_inheritance.py +++ b/test/test_inheritance.py @@ -1,7 +1,6 @@ -from mako.template import Template -from mako import lookup, util +from mako import lookup, compat import unittest -from util import flatten_result, result_lines +from test.util import result_lines class InheritanceTest(unittest.TestCase): def test_basic(self): @@ -52,7 +51,7 @@ main_body ${parent.d()} full stack from the top: ${self.name} ${parent.name} ${parent.context['parent'].name} ${parent.context['parent'].context['parent'].name} """) - + collection.put_string('layout', """ <%inherit file="general"/> <%def name="d()">layout_d</%def> @@ -94,11 +93,11 @@ ${next.body()} 'full stack from the top:', 'self:main self:layout self:general self:base' ] - + def test_includes(self): """test that an included template also has its full hierarchy invoked.""" collection = lookup.TemplateLookup() - + collection.put_string("base", """ <%def name="a()">base_a</%def> This is the base. @@ -120,7 +119,7 @@ ${next.body()} """) assert result_lines(collection.get_template("index").render()) == [ - 'This is the base.', + 'This is the base.', 'this is index.', 'a is: base_a', 'This is the base.', @@ -134,7 +133,7 @@ ${next.body()} """test that templates used via <%namespace> have access to an inheriting 'self', and that the full 'self' is also exported.""" collection = lookup.TemplateLookup() - + collection.put_string("base", """ <%def name="a()">base_a</%def> <%def name="b()">base_b</%def> @@ -194,7 +193,7 @@ ${next.body()} <%def name="foo()"> ${next.body(**context.kwargs)} </%def> - + ${foo()} """) collection.put_string("index", """ @@ -202,8 +201,8 @@ ${next.body()} <%page args="x, y, z=7"/> print ${x}, ${y}, ${z} """) - - if util.py3k: + + if compat.py3k: assert result_lines(collection.get_template('index').render_unicode(x=5,y=10)) == [ "this is the base.", "pageargs: (type: <class 'dict'>) [('x', 5), ('y', 10)]", @@ -215,14 +214,14 @@ ${next.body()} "pageargs: (type: <type 'dict'>) [('x', 5), ('y', 10)]", "print 5, 10, 7" ] - + def test_pageargs_2(self): collection = lookup.TemplateLookup() collection.put_string("base", """ this is the base. - + ${next.body(**context.kwargs)} - + <%def name="foo(**kwargs)"> ${next.body(**kwargs)} </%def> @@ -245,7 +244,7 @@ ${next.body()} "pageargs: 12, 15, 8", "pageargs: 5, 10, 16" ] - + def test_pageargs_err(self): collection = lookup.TemplateLookup() collection.put_string("base", """ @@ -258,11 +257,11 @@ ${next.body()} print ${x}, ${y}, ${z} """) try: - print collection.get_template('index').render(x=5,y=10) + print(collection.get_template('index').render(x=5,y=10)) assert False except TypeError: assert True - + def test_toplevel(self): collection = lookup.TemplateLookup() collection.put_string("base", """ @@ -305,7 +304,7 @@ ${next.body()} 'this is the base.', 'this is index.' ] - + def test_in_call(self): collection = lookup.TemplateLookup() collection.put_string("/layout.html",""" @@ -332,7 +331,7 @@ ${next.body()} </%def> <%inherit file="/layout.html"/> """) - + collection.put_string("/subdir/renderedtemplate.html",""" Holy smokes! <%inherit file="/subdir/layout.html"/> diff --git a/test/test_lexer.py b/test/test_lexer.py index 61204ff..f6be383 100644 --- a/test/test_lexer.py +++ b/test/test_lexer.py @@ -1,11 +1,9 @@ -import unittest - from mako.lexer import Lexer -from mako import exceptions, util -from util import flatten_result, result_lines +from mako import exceptions, util, compat +from test.util import flatten_result from mako.template import Template import re -from test import TemplateTest, template_base, skip_if, eq_, assert_raises_message +from test import TemplateTest, eq_, assert_raises_message # create fake parsetree classes which are constructed # exactly as the repr() of a real parsetree object. @@ -18,21 +16,31 @@ def repr_arg(x): else: return repr(x) +def _as_unicode(arg): + if isinstance(arg, compat.string_types): + return compat.text_type(arg) + elif isinstance(arg, dict): + return dict( + (_as_unicode(k), _as_unicode(v)) + for k, v in arg.items() + ) + else: + return arg from mako import parsetree -for cls in parsetree.__dict__.values(): +for cls in list(parsetree.__dict__.values()): if isinstance(cls, type) and \ issubclass(cls, parsetree.Node): clsname = cls.__name__ - exec (""" + exec((""" class %s(object): def __init__(self, *args): - self.args = args + self.args = [_as_unicode(arg) for arg in args] def __repr__(self): return "%%s(%%s)" %% ( self.__class__.__name__, ", ".join(repr_arg(x) for x in self.args) ) -""" % clsname) in locals() +""" % clsname), locals()) # NOTE: most assertion expressions were generated, then formatted # by PyTidy, hence the dense formatting. @@ -48,28 +56,28 @@ class LexerTest(TemplateTest): <%def name="foo()"> this is a def. </%def> - + and some more text. """ node = Lexer(template).parse() self._compare(node, TemplateNode({}, - [Text(u'''\n<b>Hello world</b>\n ''', (1, - 1)), DefTag(u'def', {u'name': u'foo()'}, (3, 9), - [Text(u'''\n this is a def.\n ''', + [Text('''\n<b>Hello world</b>\n ''', (1, + 1)), DefTag('def', {'name': 'foo()'}, (3, 9), + [Text('''\n this is a def.\n ''', (3, 28))]), - Text(u'''\n \n and some more text.\n''', + Text('''\n\n and some more text.\n''', (5, 16))])) def test_unclosed_tag(self): template = """ - + <%def name="foo()"> other text """ try: nodes = Lexer(template).parse() assert False - except exceptions.SyntaxException, e: + except exceptions.SyntaxException as e: assert str(e) == "Unclosed tag: <%def> at line: 5 char: 9" def test_onlyclosed_tag(self): @@ -78,9 +86,9 @@ class LexerTest(TemplateTest): <%def name="foo()"> foo </%def> - + </%namespace> - + hi. """ self.assertRaises(exceptions.SyntaxException, @@ -102,8 +110,8 @@ class LexerTest(TemplateTest): foo </%namespace> </%def> - - + + hi. """ self.assertRaises(exceptions.SyntaxException, @@ -121,7 +129,7 @@ class LexerTest(TemplateTest): """ <%DEF name="foo()"> </%def> - + """ self.assertRaises(exceptions.CompileException, Lexer(template).parse) @@ -129,7 +137,7 @@ class LexerTest(TemplateTest): def test_percent_escape(self): template = \ """ - + %% some whatever. %% more some whatever @@ -137,12 +145,12 @@ class LexerTest(TemplateTest): % endif """ node = Lexer(template).parse() - self._compare(node, TemplateNode({}, [Text(u'''\n \n''', - (1, 1)), Text(u'''% some whatever.\n\n''', (3, 2)), - Text(u' %% more some whatever\n', (5, 2)), - ControlLine(u'if', u'if foo:', False, (6, 1)), - ControlLine(u'if', u'endif', True, (7, 1)), - Text(u' ', (8, 1))])) + self._compare(node, TemplateNode({}, [Text('''\n\n''', + (1, 1)), Text('''% some whatever.\n\n''', (3, 2)), + Text(' %% more some whatever\n', (5, 2)), + ControlLine('if', 'if foo:', False, (6, 1)), + ControlLine('if', 'endif', True, (7, 1)), + Text(' ', (8, 1))])) def test_text_tag(self): template = \ @@ -153,44 +161,40 @@ class LexerTest(TemplateTest): % endif <%text> # more code - + % more code <%illegal compionent>/></> <%def name="laal()">def</%def> - - + + </%text> <%def name="foo()">this is foo</%def> - + % if bar: code % endif """ node = Lexer(template).parse() - self._compare(node, - TemplateNode({}, [Text(u'\n', (1, 1)), - Comment(u'comment', (2, 1)), - ControlLine(u'if', u'if foo:', False, (3, 1)), - Text(u' hi\n', (4, 1)), - ControlLine(u'if', u'endif', True, (5, 1)), - Text(u' ', (6, 1)), TextTag(u'text', {}, - (6, 9), - [Text(u'''\n # more code\n ''' - '''\n % more code\n ''' - '''<%illegal compionent>/></>\n ''' - '''<%def name="laal()">def</%def>\n ''' - ''' \n \n ''', - (6, 16))]), Text(u''' - - ''', (14, 17)), - DefTag(u'def', {u'name': u'foo()'}, (16, 9), - [Text(u'this is foo', (16, 28))]), - Text(u'''\n \n''', (16, 46)), - ControlLine(u'if', u'if bar:', False, (18, 1)), - Text(u' code\n', (19, 1)), - ControlLine(u'if', u'endif', True, (20, 1)), - Text(u' ', (21, 1))])) + self._compare(node, + TemplateNode({}, [Text('\n', (1, 1)), + Comment('comment', (2, 1)), + ControlLine('if', 'if foo:', False, (3, 1)), + Text(' hi\n', (4, 1)), + ControlLine('if', 'endif', True, (5, 1)), + Text(' ', (6, 1)), + TextTag('text', {}, (6, 9), + [Text('\n # more code\n\n ' + ' % more code\n <%illegal compionent>/></>\n' + ' <%def name="laal()">def</%def>\n\n\n ', + (6, 16))]), Text('\n\n ', (14, 17)), + DefTag('def', {'name': 'foo()'}, (16, 9), + [Text('this is foo', (16, 28))]), Text('\n\n', (16, 46)), + ControlLine('if', 'if bar:', False, (18, 1)), + Text(' code\n', (19, 1)), + ControlLine('if', 'endif', True, (20, 1)), + Text(' ', (21, 1))]) + ) def test_def_syntax(self): template = \ @@ -220,26 +224,26 @@ class LexerTest(TemplateTest): </%def> """ node = Lexer(template).parse() - self._compare(node, TemplateNode({}, [Text(u'\n ', - (1, 1)), DefTag(u'def', {u'name': u'adef()'}, (2, + self._compare(node, TemplateNode({}, [Text('\n ', + (1, 1)), DefTag('def', {'name': 'adef()'}, (2, 13), - [Text(u'''\n adef\n ''', - (2, 36))]), Text(u'\n ', (4, 20))])) + [Text('''\n adef\n ''', + (2, 36))]), Text('\n ', (4, 20))])) def test_ns_tag_closed(self): template = \ """ - + <%self:go x="1" y="2" z="${'hi' + ' ' + 'there'}"/> """ nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Text(u''' - + [Text(''' + ''', (1, 1)), - CallNamespaceTag(u'self:go', {u'x': u'1', u'y' - : u'2', u'z': u"${'hi' + ' ' + 'there'}"}, (3, - 13), []), Text(u'\n ', (3, 64))])) + CallNamespaceTag('self:go', {'x': '1', 'y' + : '2', 'z': "${'hi' + ' ' + 'there'}"}, (3, + 13), []), Text('\n ', (3, 64))])) def test_ns_tag_empty(self): template = \ @@ -247,34 +251,34 @@ class LexerTest(TemplateTest): <%form:option value=""></%form:option> """ nodes = Lexer(template).parse() - self._compare(nodes, TemplateNode({}, [Text(u'\n ', - (1, 1)), CallNamespaceTag(u'form:option', - {u'value': u''}, (2, 13), []), Text(u'\n ' + self._compare(nodes, TemplateNode({}, [Text('\n ', + (1, 1)), CallNamespaceTag('form:option', + {'value': ''}, (2, 13), []), Text('\n ' , (2, 51))])) def test_ns_tag_open(self): template = \ """ - + <%self:go x="1" y="${process()}"> this is the body </%self:go> """ nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Text(u''' - + [Text(''' + ''', (1, 1)), - CallNamespaceTag(u'self:go', {u'x': u'1', u'y' - : u'${process()}'}, (3, 13), - [Text(u''' + CallNamespaceTag('self:go', {'x': '1', 'y' + : '${process()}'}, (3, 13), + [Text(''' this is the body ''', - (3, 46))]), Text(u'\n ', (5, 24))])) + (3, 46))]), Text('\n ', (5, 24))])) def test_expr_in_attribute(self): """test some slightly trickier expressions. - + you can still trip up the expression parsing, though, unless we integrated really deeply somehow with AST.""" @@ -284,26 +288,26 @@ class LexerTest(TemplateTest): <%call expr='foo<bar and hoho>lala and "x" + "y"'/> """ nodes = Lexer(template).parse() - self._compare(nodes, TemplateNode({}, [Text(u'\n ', - (1, 1)), CallTag(u'call', {u'expr' - : u"foo>bar and 'lala' or 'hoho'"}, (2, 13), []), - Text(u'\n ', (2, 57)), CallTag(u'call' - , {u'expr': u'foo<bar and hoho>lala and "x" + "y"' - }, (3, 13), []), Text(u'\n ', (3, 64))])) + self._compare(nodes, TemplateNode({}, [Text('\n ', + (1, 1)), CallTag('call', {'expr' + : "foo>bar and 'lala' or 'hoho'"}, (2, 13), []), + Text('\n ', (2, 57)), CallTag('call' + , {'expr': 'foo<bar and hoho>lala and "x" + "y"' + }, (3, 13), []), Text('\n ', (3, 64))])) def test_pagetag(self): template = \ """ <%page cached="True", args="a, b"/> - + some template """ nodes = Lexer(template).parse() - self._compare(nodes, TemplateNode({}, [Text(u'\n ', - (1, 1)), PageTag(u'page', {u'args': u'a, b', - u'cached': u'True'}, (2, 13), []), - Text(u''' - + self._compare(nodes, TemplateNode({}, [Text('\n ', + (1, 1)), PageTag('page', {'args': 'a, b', + 'cached': 'True'}, (2, 13), []), + Text(''' + some template ''', (2, 48))])) @@ -311,31 +315,31 @@ class LexerTest(TemplateTest): def test_nesting(self): template = \ """ - + <%namespace name="ns"> <%def name="lala(hi, there)"> <%call expr="something()"/> </%def> </%namespace> - + """ nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Text(u''' - + [Text(''' + ''', (1, 1)), - NamespaceTag(u'namespace', {u'name': u'ns'}, (3, - 9), [Text(u'\n ', (3, 31)), - DefTag(u'def', {u'name': u'lala(hi, there)'}, (4, - 13), [Text(u'\n ', (4, 42)), - CallTag(u'call', {u'expr': u'something()'}, (5, - 17), []), Text(u'\n ', (5, 44))]), - Text(u'\n ', (6, 20))]), - Text(u''' - + NamespaceTag('namespace', {'name': 'ns'}, (3, + 9), [Text('\n ', (3, 31)), + DefTag('def', {'name': 'lala(hi, there)'}, (4, + 13), [Text('\n ', (4, 42)), + CallTag('call', {'expr': 'something()'}, (5, + 17), []), Text('\n ', (5, 44))]), + Text('\n ', (6, 20))]), + Text(''' + ''', (7, 22))])) - if util.py3k: + if compat.py3k: def test_code(self): template = \ """text @@ -350,14 +354,14 @@ more text %> """ nodes = Lexer(template).parse() - self._compare(nodes, + self._compare(nodes, TemplateNode({}, [ - Text(u'text\n ', (1, 1)), - Code(u'\nprint("hi")\nfor x in range(1,5):\n ' - 'print(x)\n \n', False, (2, 5)), - Text(u'\nmore text\n ', (6, 7)), - Code(u'\nimport foo\n \n', True, (8, 5)), - Text(u'\n', (10, 7))]) + Text('text\n ', (1, 1)), + Code('\nprint("hi")\nfor x in range(1,5):\n ' + 'print(x)\n \n', False, (2, 5)), + Text('\nmore text\n ', (6, 7)), + Code('\nimport foo\n \n', True, (8, 5)), + Text('\n', (10, 7))]) ) @@ -377,14 +381,14 @@ more text %> """ nodes = Lexer(template).parse() - self._compare(nodes, + self._compare(nodes, TemplateNode({}, [ - Text(u'text\n ', (1, 1)), - Code(u'\nprint "hi"\nfor x in range(1,5):\n ' - 'print x\n \n', False, (2, 5)), - Text(u'\nmore text\n ', (6, 7)), - Code(u'\nimport foo\n \n', True, (8, 5)), - Text(u'\n', (10, 7))]) + Text('text\n ', (1, 1)), + Code('\nprint "hi"\nfor x in range(1,5):\n ' + 'print x\n \n', False, (2, 5)), + Text('\nmore text\n ', (6, 7)), + Code('\nimport foo\n \n', True, (8, 5)), + Text('\n', (10, 7))]) ) def test_code_and_tags(self): @@ -409,20 +413,20 @@ more text result: <%call expr="foo.x(result)"/> """ nodes = Lexer(template).parse() - self._compare(nodes, TemplateNode({}, [Text(u'\n', (1, 1)), - NamespaceTag(u'namespace', {u'name': u'foo'}, (2, - 1), [Text(u'\n ', (2, 24)), DefTag(u'def', - {u'name': u'x()'}, (3, 5), - [Text(u'''\n this is x\n ''', (3, 22))]), - Text(u'\n ', (5, 12)), DefTag(u'def', {u'name' - : u'y()'}, (6, 5), - [Text(u'''\n this is y\n ''', (6, 22))]), - Text(u'\n', (8, 12))]), Text(u'''\n\n''', (9, 14)), - Code(u'''\nresult = []\ndata = get_data()\n''' + self._compare(nodes, TemplateNode({}, [Text('\n', (1, 1)), + NamespaceTag('namespace', {'name': 'foo'}, (2, + 1), [Text('\n ', (2, 24)), DefTag('def', + {'name': 'x()'}, (3, 5), + [Text('''\n this is x\n ''', (3, 22))]), + Text('\n ', (5, 12)), DefTag('def', {'name' + : 'y()'}, (6, 5), + [Text('''\n this is y\n ''', (6, 22))]), + Text('\n', (8, 12))]), Text('''\n\n''', (9, 14)), + Code('''\nresult = []\ndata = get_data()\n''' '''for x in data:\n result.append(x+7)\n\n''', - False, (11, 1)), Text(u'''\n\n result: ''', (16, - 3)), CallTag(u'call', {u'expr': u'foo.x(result)' - }, (18, 13), []), Text(u'\n', (18, 42))])) + False, (11, 1)), Text('''\n\n result: ''', (16, + 3)), CallTag('call', {'expr': 'foo.x(result)' + }, (18, 13), []), Text('\n', (18, 42))])) def test_expression(self): template = \ @@ -435,77 +439,77 @@ more text """ nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Text(u'\n this is some ', (1, 1)), - Expression(u'text', [], (2, 22)), - Text(u' and this is ', (2, 29)), - Expression(u'textwith ', ['escapes', 'moreescapes' - ], (2, 42)), Text(u'\n ', (2, 76)), - DefTag(u'def', {u'name': u'hi()'}, (3, 9), - [Text(u'\n give me ', (3, 27)), - Expression(u'foo()', [], (4, 21)), Text(u' and ', - (4, 29)), Expression(u'bar()', [], (4, 34)), - Text(u'\n ', (4, 42))]), Text(u'\n ' - , (5, 16)), Expression(u'hi()', [], (6, 9)), - Text(u'\n', (6, 16))])) + [Text('\n this is some ', (1, 1)), + Expression('text', [], (2, 22)), + Text(' and this is ', (2, 29)), + Expression('textwith ', ['escapes', 'moreescapes' + ], (2, 42)), Text('\n ', (2, 76)), + DefTag('def', {'name': 'hi()'}, (3, 9), + [Text('\n give me ', (3, 27)), + Expression('foo()', [], (4, 21)), Text(' and ', + (4, 29)), Expression('bar()', [], (4, 34)), + Text('\n ', (4, 42))]), Text('\n ' + , (5, 16)), Expression('hi()', [], (6, 9)), + Text('\n', (6, 16))])) def test_tricky_expression(self): template = """ - + ${x and "|" or "hi"} """ nodes = Lexer(template).parse() self._compare( nodes, TemplateNode({}, [ - Text(u'\n \n ', (1, 1)), - Expression(u'x and "|" or "hi"', [], (3, 13)), - Text(u'\n ', (3, 33)) + Text('\n\n ', (1, 1)), + Expression('x and "|" or "hi"', [], (3, 13)), + Text('\n ', (3, 33)) ]) ) template = """ - + ${hello + '''heres '{|}' text | | }''' | escape1} """ nodes = Lexer(template).parse() self._compare( nodes, TemplateNode({}, [ - Text(u'\n \n ', (1, 1)), - Expression(u"hello + '''heres '{|}' text | | }''' ", - ['escape1'], (3, 13)), - Text(u'\n ', (3, 62)) + Text('\n\n ', (1, 1)), + Expression("hello + '''heres '{|}' text | | }''' ", + ['escape1'], (3, 13)), + Text('\n ', (3, 62)) ]) ) def test_tricky_code(self): - if util.py3k: + if compat.py3k: template = """<% print('hi %>') %>""" nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Code(u"print('hi %>') \n", False, (1, 1))])) + [Code("print('hi %>') \n", False, (1, 1))])) else: template = """<% print 'hi %>' %>""" nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Code(u"print 'hi %>' \n", False, (1, 1))])) + [Code("print 'hi %>' \n", False, (1, 1))])) def test_tricky_code_2(self): template = \ - """<% + """<% # someone's comment - %> +%> """ nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Code(u""" + [Code(""" # someone's comment - + """, - False, (1, 1)), Text(u'\n ', (3, 11))])) + False, (1, 1)), Text('\n ', (3, 3))])) - if util.py3k: + if compat.py3k: def test_tricky_code_3(self): template = \ """<% @@ -517,10 +521,10 @@ more text there ''') # someone else's comment - %> '''and now some text '''""" +%> '''and now some text '''""" nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Code(u""" + [Code(""" print('hi') # this is a comment # another comment @@ -529,11 +533,11 @@ print(''' there ''') # someone else's comment - + """, False, (1, 1)), - Text(u" '''and now some text '''", (10, - 11))])) + Text(" '''and now some text '''", (10, + 3))])) else: def test_tricky_code_3(self): template = \ @@ -546,23 +550,23 @@ print(''' there ''' # someone else's comment - %> '''and now some text '''""" +%> '''and now some text '''""" nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Code(u"""\nprint 'hi'\n# this is a comment\n""" + [Code("""\nprint 'hi'\n# this is a comment\n""" """# another comment\nx = 7 """ """# someone's '''comment\nprint '''\n """ """there\n '''\n# someone else's """ - """comment\n \n""", + """comment\n\n""", False, (1, 1)), - Text(u" '''and now some text '''", (10,11))])) + Text(" '''and now some text '''", (10, 3))])) def test_tricky_code_4(self): template = \ """<% foo = "\\"\\\\" %>""" nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Code(u"""foo = "\\"\\\\" \n""", + [Code("""foo = "\\"\\\\" \n""", False, (1, 1))])) def test_tricky_code_5(self): @@ -570,9 +574,9 @@ print(''' """before ${ {'key': 'value'} } after""" nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Text(u'before ', (1, 1)), - Expression(u" {'key': 'value'} ", [], (1, 8)), - Text(u' after', (1, 29))])) + [Text('before ', (1, 1)), + Expression(" {'key': 'value'} ", [], (1, 8)), + Text(' after', (1, 29))])) def test_control_lines(self): template = \ @@ -587,21 +591,21 @@ text text la la tex tesl asdl l is ${l} kfmas d % endfor tetx text - + """ nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Text(u'''\ntext text la la\n''', (1, 1)), - ControlLine(u'if', u'if foo():', False, (3, 1)), - Text(u' mroe text la la blah blah\n', (4, 1)), - ControlLine(u'if', u'endif', True, (5, 1)), - Text(u'''\n and osme more stuff\n''', (6, - 1)), ControlLine(u'for', u'for l in range(1,5):', - False, (8, 1)), Text(u' tex tesl asdl l is ', - (9, 1)), Expression(u'l', [], (9, 24)), - Text(u' kfmas d\n', (9, 28)), ControlLine(u'for', - u'endfor', True, (10, 1)), - Text(u''' tetx text\n \n''', (11, 1))])) + [Text('''\ntext text la la\n''', (1, 1)), + ControlLine('if', 'if foo():', False, (3, 1)), + Text(' mroe text la la blah blah\n', (4, 1)), + ControlLine('if', 'endif', True, (5, 1)), + Text('''\n and osme more stuff\n''', (6, + 1)), ControlLine('for', 'for l in range(1,5):', + False, (8, 1)), Text(' tex tesl asdl l is ', + (9, 1)), Expression('l', [], (9, 24)), + Text(' kfmas d\n', (9, 28)), ControlLine('for', + 'endfor', True, (10, 1)), + Text(''' tetx text\n\n''', (11, 1))])) def test_control_lines_2(self): template = \ @@ -610,10 +614,10 @@ text text la la % endfor """ nodes = Lexer(template).parse() - self._compare(nodes, TemplateNode({}, [ControlLine(u'for', - u"for file in requestattr['toc'].filenames:", - False, (1, 1)), Text(u' x\n', (2, 1)), - ControlLine(u'for', u'endfor', True, (3, 1))])) + self._compare(nodes, TemplateNode({}, [ControlLine('for', + "for file in requestattr['toc'].filenames:", + False, (1, 1)), Text(' x\n', (2, 1)), + ControlLine('for', 'endfor', True, (3, 1))])) def test_long_control_lines(self): template = \ @@ -627,13 +631,13 @@ text text la la self._compare( nodes, TemplateNode({}, [ - Text(u'\n', (1, 1)), - ControlLine(u'for', u"for file in \\\n " - "requestattr['toc'].filenames:", - False, (2, 1)), - Text(u' x\n', (4, 1)), - ControlLine(u'for', u'endfor', True, (5, 1)), - Text(u' ', (6, 1)) + Text('\n', (1, 1)), + ControlLine('for', "for file in \\\n " + "requestattr['toc'].filenames:", + False, (2, 1)), + Text(' x\n', (4, 1)), + ControlLine('for', 'endfor', True, (5, 1)), + Text(' ', (6, 1)) ]) ) @@ -692,16 +696,16 @@ text text la la % endif """ nodes = Lexer(template).parse() - self._compare(nodes, TemplateNode({}, [Text(u'\n', (1, 1)), - ControlLine(u'if', u'if x:', False, (2, 1)), - Text(u' hi\n', (3, 1)), - ControlLine(u'elif', u'elif y+7==10:', False, (4, - 1)), Text(u' there\n', (5, 1)), - ControlLine(u'elif', u'elif lala:', False, (6, - 1)), Text(u' lala\n', (7, 1)), - ControlLine(u'else', u'else:', False, (8, 1)), - Text(u' hi\n', (9, 1)), - ControlLine(u'if', u'endif', True, (10, 1))])) + self._compare(nodes, TemplateNode({}, [Text('\n', (1, 1)), + ControlLine('if', 'if x:', False, (2, 1)), + Text(' hi\n', (3, 1)), + ControlLine('elif', 'elif y+7==10:', False, (4, + 1)), Text(' there\n', (5, 1)), + ControlLine('elif', 'elif lala:', False, (6, + 1)), Text(' lala\n', (7, 1)), + ControlLine('else', 'else:', False, (8, 1)), + Text(' hi\n', (9, 1)), + ControlLine('if', 'endif', True, (10, 1))])) def test_integration(self): template = \ @@ -727,27 +731,27 @@ text text la la </table> """ nodes = Lexer(template).parse() - self._compare(nodes, TemplateNode({}, [NamespaceTag(u'namespace' - , {u'file': u'somefile.html', u'name': u'foo'}, - (1, 1), []), Text(u'\n', (1, 46)), - Comment(u'inherit from foobar.html', (2, 1)), - InheritTag(u'inherit', {u'file': u'foobar.html'}, - (3, 1), []), Text(u'''\n\n''', (3, 31)), - DefTag(u'def', {u'name': u'header()'}, (5, 1), - [Text(u'''\n <div>header</div>\n''', (5, - 23))]), Text(u'\n', (7, 8)), DefTag(u'def', - {u'name': u'footer()'}, (8, 1), - [Text(u'''\n <div> footer</div>\n''', (8, - 23))]), Text(u'''\n\n<table>\n''', (10, 8)), - ControlLine(u'for', u'for j in data():', False, - (13, 1)), Text(u' <tr>\n', (14, 1)), - ControlLine(u'for', u'for x in j:', False, (15, - 1)), Text(u' <td>Hello ', (16, 1)), - Expression(u'x', ['h'], (16, 23)), Text(u'</td>\n' - , (16, 30)), ControlLine(u'for', u'endfor', True, - (17, 1)), Text(u' </tr>\n', (18, 1)), - ControlLine(u'for', u'endfor', True, (19, 1)), - Text(u'</table>\n', (20, 1))])) + self._compare(nodes, TemplateNode({}, [NamespaceTag('namespace' + , {'file': 'somefile.html', 'name': 'foo'}, + (1, 1), []), Text('\n', (1, 46)), + Comment('inherit from foobar.html', (2, 1)), + InheritTag('inherit', {'file': 'foobar.html'}, + (3, 1), []), Text('''\n\n''', (3, 31)), + DefTag('def', {'name': 'header()'}, (5, 1), + [Text('''\n <div>header</div>\n''', (5, + 23))]), Text('\n', (7, 8)), DefTag('def', + {'name': 'footer()'}, (8, 1), + [Text('''\n <div> footer</div>\n''', (8, + 23))]), Text('''\n\n<table>\n''', (10, 8)), + ControlLine('for', 'for j in data():', False, + (13, 1)), Text(' <tr>\n', (14, 1)), + ControlLine('for', 'for x in j:', False, (15, + 1)), Text(' <td>Hello ', (16, 1)), + Expression('x', ['h'], (16, 23)), Text('</td>\n' + , (16, 30)), ControlLine('for', 'endfor', True, + (17, 1)), Text(' </tr>\n', (18, 1)), + ControlLine('for', 'endfor', True, (19, 1)), + Text('</table>\n', (20, 1))])) def test_comment_after_statement(self): template = \ @@ -759,12 +763,12 @@ text text la la % endif #end """ nodes = Lexer(template).parse() - self._compare(nodes, TemplateNode({}, [Text(u'\n', (1, 1)), - ControlLine(u'if', u'if x: #comment', False, (2, - 1)), Text(u' hi\n', (3, 1)), - ControlLine(u'else', u'else: #next', False, (4, - 1)), Text(u' hi\n', (5, 1)), - ControlLine(u'if', u'endif #end', True, (6, 1))])) + self._compare(nodes, TemplateNode({}, [Text('\n', (1, 1)), + ControlLine('if', 'if x: #comment', False, (2, + 1)), Text(' hi\n', (3, 1)), + ControlLine('else', 'else: #next', False, (4, + 1)), Text(' hi\n', (5, 1)), + ControlLine('if', 'endif #end', True, (6, 1))])) def test_crlf(self): template = open(self._file_path("crlf.html"), 'rb').read() @@ -772,23 +776,23 @@ text text la la self._compare( nodes, TemplateNode({}, [ - Text(u'<html>\r\n\r\n', (1, 1)), - PageTag(u'page', { - u'args': u"a=['foo',\n 'bar']" - }, (3, 1), []), - Text(u'\r\n\r\nlike the name says.\r\n\r\n', (4, 26)), - ControlLine(u'for', u'for x in [1,2,3]:', False, (8, 1)), - Text(u' ', (9, 1)), - Expression(u'x', [], (9, 9)), - ControlLine(u'for', u'endfor', True, (10, 1)), - Text(u'\r\n', (11, 1)), - Expression(u"trumpeter == 'Miles' and " - "trumpeter or \\\n 'Dizzy'", - [], (12, 1)), - Text(u'\r\n\r\n', (13, 15)), - DefTag(u'def', {u'name': u'hi()'}, (15, 1), [ - Text(u'\r\n hi!\r\n', (15, 19))]), - Text(u'\r\n\r\n</html>\r\n', (17, 8)) + Text('<html>\r\n\r\n', (1, 1)), + PageTag('page', { + 'args': "a=['foo',\n 'bar']" + }, (3, 1), []), + Text('\r\n\r\nlike the name says.\r\n\r\n', (4, 26)), + ControlLine('for', 'for x in [1,2,3]:', False, (8, 1)), + Text(' ', (9, 1)), + Expression('x', [], (9, 9)), + ControlLine('for', 'endfor', True, (10, 1)), + Text('\r\n', (11, 1)), + Expression("trumpeter == 'Miles' and " + "trumpeter or \\\n 'Dizzy'", + [], (12, 1)), + Text('\r\n\r\n', (13, 15)), + DefTag('def', {'name': 'hi()'}, (15, 1), [ + Text('\r\n hi!\r\n', (15, 19))]), + Text('\r\n\r\n</html>\r\n', (17, 8)) ]) ) assert flatten_result(Template(template).render()) \ @@ -806,7 +810,7 @@ text text la la # also not a comment ## this is a comment - + this is ## not a comment <%doc> multiline @@ -817,14 +821,14 @@ hi """ nodes = Lexer(template).parse() self._compare(nodes, TemplateNode({}, - [Text(u'''\n<style>\n #someselector\n # ''' + [Text('''\n<style>\n #someselector\n # ''' '''other non comment stuff\n</style>\n''', - (1, 1)), Comment(u'a comment', (6, 1)), - Text(u'''\n# also not a comment\n\n''', (7, 1)), - Comment(u'this is a comment', (10, 1)), - Text(u''' \nthis is ## not a comment\n\n''', (11, - 1)), Comment(u''' multiline\ncomment\n''', (14, - 1)), Text(u''' + (1, 1)), Comment('a comment', (6, 1)), + Text('''\n# also not a comment\n\n''', (7, 1)), + Comment('this is a comment', (10, 1)), + Text('''\nthis is ## not a comment\n\n''', (11, + 1)), Comment(''' multiline\ncomment\n''', (14, + 1)), Text(''' hi ''', (16, 8))])) @@ -842,17 +846,17 @@ hi </%def> """ nodes = Lexer(template).parse() - self._compare(nodes, - TemplateNode({}, [Text(u'\n ', (1, + self._compare(nodes, + TemplateNode({}, [Text('\n ', (1, 1)), - Comment(u'''\n this is a comment\n ''', - (2, 9)), Text(u'\n ', (4, 16)), - DefTag(u'def', {u'name': u'foo()'}, (5, 9), - [Text(u'\n ', (5, 28)), - Comment(u'''\n this is the foo func\n''' + Comment('''\n this is a comment\n ''', + (2, 9)), Text('\n ', (4, 16)), + DefTag('def', {'name': 'foo()'}, (5, 9), + [Text('\n ', (5, 28)), + Comment('''\n this is the foo func\n''' ''' ''', - (6, 13)), Text(u'\n ', (8, 20))]), - Text(u'\n ', (9, 16))])) + (6, 13)), Text('\n ', (8, 20))]), + Text('\n ', (9, 16))])) def test_preprocess(self): @@ -866,6 +870,6 @@ hi # another comment """ nodes = Lexer(template, preprocessor=preproc).parse() - self._compare(nodes, TemplateNode({}, [Text(u'''\n hi\n''', - (1, 1)), Comment(u'old style comment', (3, 1)), - Comment(u'another comment', (4, 1))])) + self._compare(nodes, TemplateNode({}, [Text('''\n hi\n''', + (1, 1)), Comment('old style comment', (3, 1)), + Comment('another comment', (4, 1))])) diff --git a/test/test_lookup.py b/test/test_lookup.py index 40b9009..4cf82fe 100644 --- a/test/test_lookup.py +++ b/test/test_lookup.py @@ -1,7 +1,7 @@ from mako.template import Template from mako import lookup, exceptions, runtime from mako.util import FastEncodingBuffer -from util import flatten_result, result_lines +from .util import flatten_result, result_lines import unittest import os @@ -46,7 +46,7 @@ class LookupTest(unittest.TestCase): try: t.render() assert False - except exceptions.TemplateLookupException, e: + except exceptions.TemplateLookupException as e: assert str(e) == \ "Template 'memory:%s' has no TemplateLookup associated" % \ hex(id(t)) diff --git a/test/test_loop.py b/test/test_loop.py index 14912ee..acff53f 100644 --- a/test/test_loop.py +++ b/test/test_loop.py @@ -10,7 +10,7 @@ from mako.runtime import LoopStack, LoopContext from mako import exceptions from test import assert_raises_message from test import TemplateTest, eq_ -from util import flatten_result, result_lines +from .util import flatten_result, result_lines class TestLoop(unittest.TestCase): @@ -37,7 +37,7 @@ ${x} code = template.code assert not re.match(r"loop = __M_loop._enter\(:", code), "No need to "\ "generate a loop context if the loop variable wasn't accessed" - print template.render() + print(template.render()) def test_loop_demo(self): template = Template("""x|index|reverse_index|first|last|cycle|even|odd @@ -54,7 +54,7 @@ ${x}|${loop.index}|${loop.reverse_index}|${loop.first}|${loop.last}|${loop.cycle assert "loop = __M_loop._enter(" in code, "Generated a loop context since "\ "the loop variable was accessed" rendered = template.render() - print rendered + print(rendered) for line in expected: assert line in rendered, "Loop variables give information about "\ "the progress of the loop" @@ -101,7 +101,7 @@ ${x} ${loop.index} <- outer loop "b 1 <- parent loop" ] for line in expected: - print code + print(code) assert line in rendered, "The parent attribute of a loop gives "\ "you the previous loop context in the stack" @@ -174,7 +174,7 @@ class TestLoopContext(unittest.TestCase): length = len(self.iterable) expected = tuple([length-i-1 for i in range(length)]) actual = tuple(self.ctx.reverse_index for i in self.ctx) - print expected, actual + print(expected, actual) assert expected == actual, "The reverse_index is the number of "\ "iterations until the end" diff --git a/test/test_lru.py b/test/test_lru.py index ade48a3..cde5601 100644 --- a/test/test_lru.py +++ b/test/test_lru.py @@ -1,7 +1,10 @@ from mako.util import LRUCache -import string, unittest, time, random +import string +import unittest +import time +import random -import thread +from mako.compat import thread class item: def __init__(self, id): @@ -13,19 +16,19 @@ class item: class LRUTest(unittest.TestCase): - def testlru(self): + def testlru(self): l = LRUCache(10, threshold=.2) - + for id in range(1,20): l[id] = item(id) - + # first couple of items should be gone - self.assert_(not l.has_key(1)) - self.assert_(not l.has_key(2)) - + self.assert_(1 not in l) + self.assert_(2 not in l) + # next batch over the threshold of 10 should be present for id in range(11,20): - self.assert_(l.has_key(id)) + self.assert_(id in l) l[12] l[15] @@ -35,25 +38,25 @@ class LRUTest(unittest.TestCase): l[26] = item(26) l[27] = item(27) - self.assert_(not l.has_key(11)) - self.assert_(not l.has_key(13)) - + self.assert_(11 not in l) + self.assert_(13 not in l) + for id in (25, 24, 23, 14, 12, 19, 18, 17, 16, 15): - self.assert_(l.has_key(id)) + self.assert_(id in l) def _disabled_test_threaded(self): size = 100 threshold = .5 all_elems = 2000 - hot_zone = range(30,40) + hot_zone = list(range(30,40)) cache = LRUCache(size, threshold) - + # element to store class Element(object): def __init__(self, id): self.id = id self.regets = 0 - + # return an element. we will favor ids in the relatively small # "hot zone" 25% of the time. def get_elem(): @@ -61,7 +64,7 @@ class LRUTest(unittest.TestCase): return hot_zone[random.randint(0, len(hot_zone) - 1)] else: return random.randint(1, all_elems) - + total = [0] # request thread. def request_elem(): @@ -74,23 +77,23 @@ class LRUTest(unittest.TestCase): except KeyError: e = Element(id) cache[id] = e - + time.sleep(random.random() / 1000) for x in range(0,20): - thread.start_new_thread(request_elem, ()) - + _thread.start_new_thread(request_elem, ()) + # assert size doesn't grow unbounded, doesnt shrink well below size for x in range(0,5): time.sleep(1) - print "size:", len(cache) + print("size:", len(cache)) assert len(cache) < size + size * threshold * 2 assert len(cache) > size - (size * .1) - + # computs the average number of times a range of elements were "reused", # i.e. without being removed from the cache. def average_regets_in_range(start, end): - elem = [e for e in cache.values() if e.id >= start and e.id <= end] + elem = [e for e in list(cache.values()) if e.id >= start and e.id <= end] if len(elem) == 0: return 0 avg = sum([e.regets for e in elem]) / len(elem) @@ -99,13 +102,13 @@ class LRUTest(unittest.TestCase): hotzone_avg = average_regets_in_range(30, 40) control_avg = average_regets_in_range(450,760) total_avg = average_regets_in_range(0, 2000) - + # hotzone should be way above the others - print "total fetches", total[0], "hotzone", \ + print("total fetches", total[0], "hotzone", \ hotzone_avg, "control", \ - control_avg, "total", total_avg - + control_avg, "total", total_avg) + assert hotzone_avg > total_avg * 5 > control_avg * 5 - - + + diff --git a/test/test_namespace.py b/test/test_namespace.py index 3c4c689..5f6d253 100644 --- a/test/test_namespace.py +++ b/test/test_namespace.py @@ -1,6 +1,6 @@ from mako.template import Template from mako import lookup -from util import flatten_result, result_lines +from .util import flatten_result, result_lines from test import TemplateTest, eq_ class NamespaceTest(TemplateTest): diff --git a/test/test_pygen.py b/test/test_pygen.py index 58265ff..7671bd9 100644 --- a/test/test_pygen.py +++ b/test/test_pygen.py @@ -1,7 +1,9 @@ import unittest from mako.pygen import PythonPrinter, adjust_whitespace -from StringIO import StringIO +from mako.compat import StringIO +from test import eq_ + class GeneratePythonTest(unittest.TestCase): def test_generate_normal(self): @@ -48,7 +50,7 @@ if x > 7: print "hi" print "there" foo(lala) - """ +""" stream = StringIO() printer = PythonPrinter(stream) printer.writeline("import lala") @@ -59,7 +61,7 @@ if x > 7: printer.writeline("print y") printer.close() #print "->" + stream.getvalue().replace(' ', '#') + "<-" - assert stream.getvalue() == \ + eq_(stream.getvalue(), """import lala for x in foo: print x @@ -71,9 +73,9 @@ for x in foo: print "hi" print "there" foo(lala) - + print y -""" +""") def test_multi_line(self): block = \ """ @@ -99,7 +101,7 @@ and more block. do_more_stuff(g) """ - + def test_false_unindentor(self): stream = StringIO() printer = PythonPrinter(stream) @@ -114,7 +116,7 @@ and more block. None ]: printer.writeline(line) - + assert stream.getvalue() == \ """try: elsemyvar = 12 @@ -123,8 +125,8 @@ and more block. finally: dosomething """ , stream.getvalue() - - + + def test_backslash_line(self): block = \ """ @@ -167,9 +169,9 @@ print "hi" def test_blank_lines(self): text = """ print "hi" # a comment - + # more comments - + print g """ assert adjust_whitespace(text) == \ diff --git a/test/test_template.py b/test/test_template.py index 2821910..ff82581 100644 --- a/test/test_template.py +++ b/test/test_template.py @@ -4,10 +4,12 @@ from mako.template import Template, ModuleTemplate from mako.lookup import TemplateLookup from mako.ext.preprocessors import convert_comments from mako import exceptions, util, runtime +from mako import compat import re import os from util import flatten_result, result_lines import codecs +from mako.compat import u from test import TemplateTest, eq_, template_base, module_base, \ requires_python_26_or_greater, assert_raises, assert_raises_message, \ requires_python_2 @@ -26,7 +28,7 @@ class EncodingTest(TemplateTest): except: # <h3>Exception: <span style="color:red">Foobar</span></h3> markup = html_error_template().render(full=False, css=False) - if util.py3k: + if compat.py3k: assert '<span style="color:red">Foobar</span></h3>'\ .encode('ascii') not in markup assert '<span style="color:red"'\ @@ -40,35 +42,35 @@ class EncodingTest(TemplateTest): def test_unicode(self): self._do_memory_test( - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""") ) def test_encoding_doesnt_conflict(self): self._do_memory_test( - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), output_encoding='utf-8' ) def test_unicode_arg(self): - val = u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" + val = u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""") self._do_memory_test( "${val}", - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), template_args={'val':val} ) def test_unicode_file(self): self._do_file_test( "unicode.html", - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""") ) def test_unicode_file_code(self): self._do_file_test( 'unicode_code.html', - u"""hi, drôle de petite voix m’a réveillé.""", + u("""hi, drôle de petite voix m’a réveillé."""), filters=flatten_result ) @@ -77,24 +79,24 @@ class EncodingTest(TemplateTest): directories=[template_base], output_encoding='utf-8', default_filters=['decode.utf8']) - if util.py3k: + if compat.py3k: template = lookup.get_template('/chs_unicode_py3k.html') else: template = lookup.get_template('/chs_unicode.html') eq_( flatten_result(template.render_unicode(name='毛泽东')), - u'毛泽东 是 新中国的主席<br/> Welcome 你 to 北京.' + u('毛泽东 是 新中国的主席<br/> Welcome 你 to 北京.') ) def test_unicode_bom(self): self._do_file_test( 'bom.html', - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""") ) self._do_file_test( 'bommagic.html', - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""") ) self.assertRaises( @@ -104,105 +106,105 @@ class EncodingTest(TemplateTest): ) def test_unicode_memory(self): - val = u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" + val = u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""") self._do_memory_test( ("## -*- coding: utf-8 -*-\n" + val).encode('utf-8'), - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""") ) def test_unicode_text(self): - val = u"""<%text>Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »</%text>""" + val = u("""<%text>Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »</%text>""") self._do_memory_test( ("## -*- coding: utf-8 -*-\n" + val).encode('utf-8'), - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""") ) def test_unicode_text_ccall(self): - val = u""" + val = u(""" <%def name="foo()"> ${capture(caller.body)} </%def> <%call expr="foo()"> <%text>Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »</%text> - </%call>""" + </%call>""") self._do_memory_test( ("## -*- coding: utf-8 -*-\n" + val).encode('utf-8'), - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), filters=flatten_result ) def test_unicode_literal_in_expr(self): - if util.py3k: + if compat.py3k: self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- ${"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"} - """.encode('utf-8'), - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", + """).encode('utf-8'), + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), filters = lambda s:s.strip() ) else: self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- ${u"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"} - """.encode('utf-8'), - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", + """).encode('utf-8'), + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), filters = lambda s:s.strip() ) def test_unicode_literal_in_expr_file(self): self._do_file_test( 'unicode_expr.html', - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), lambda t:t.strip() ) def test_unicode_literal_in_code(self): - if util.py3k: + if compat.py3k: self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- <% context.write("Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »") %> - """.encode('utf-8'), - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", + """).encode('utf-8'), + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), filters=lambda s:s.strip() ) else: self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- <% context.write(u"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »") %> - """.encode('utf-8'), - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", + """).encode('utf-8'), + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), filters=lambda s:s.strip() ) def test_unicode_literal_in_controlline(self): - if util.py3k: + if compat.py3k: self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- <% x = "drôle de petite voix m’a réveillé." %> % if x=="drôle de petite voix m’a réveillé.": hi, ${x} % endif - """.encode('utf-8'), - u"""hi, drôle de petite voix m’a réveillé.""", + """).encode('utf-8'), + u("""hi, drôle de petite voix m’a réveillé."""), filters=lambda s:s.strip(), ) else: self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- <% x = u"drôle de petite voix m’a réveillé." %> % if x==u"drôle de petite voix m’a réveillé.": hi, ${x} % endif - """.encode('utf-8'), - u"""hi, drôle de petite voix m’a réveillé.""", + """).encode('utf-8'), + u("""hi, drôle de petite voix m’a réveillé."""), filters=lambda s:s.strip(), ) @@ -210,10 +212,10 @@ class EncodingTest(TemplateTest): self._do_file_test( "unicode_arguments.html", [ - u'x is: drôle de petite voix m’a réveillé', - u'x is: drôle de petite voix m’a réveillé', - u'x is: drôle de petite voix m’a réveillé', - u'x is: drôle de petite voix m’a réveillé', + u('x is: drôle de petite voix m’a réveillé'), + u('x is: drôle de petite voix m’a réveillé'), + u('x is: drôle de petite voix m’a réveillé'), + u('x is: drôle de petite voix m’a réveillé'), ], filters=result_lines ) @@ -221,59 +223,59 @@ class EncodingTest(TemplateTest): self._do_memory_test( open(self._file_path("unicode_arguments.html"), 'rb').read(), [ - u'x is: drôle de petite voix m’a réveillé', - u'x is: drôle de petite voix m’a réveillé', - u'x is: drôle de petite voix m’a réveillé', - u'x is: drôle de petite voix m’a réveillé', + u('x is: drôle de petite voix m’a réveillé'), + u('x is: drôle de petite voix m’a réveillé'), + u('x is: drôle de petite voix m’a réveillé'), + u('x is: drôle de petite voix m’a réveillé'), ], filters=result_lines ) def test_unicode_literal_in_def(self): - if util.py3k: + if compat.py3k: self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- <%def name="bello(foo, bar)"> Foo: ${ foo } Bar: ${ bar } </%def> <%call expr="bello(foo='árvíztűrő tükörfúrógép', bar='ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')"> - </%call>""".encode('utf-8'), - u"""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP""", + </%call>""").encode('utf-8'), + u("""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP"""), filters=flatten_result ) self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- <%def name="hello(foo='árvíztűrő tükörfúrógép', bar='ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')"> Foo: ${ foo } Bar: ${ bar } </%def> - ${ hello() }""".encode('utf-8'), - u"""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP""", + ${ hello() }""").encode('utf-8'), + u("""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP"""), filters=flatten_result ) else: self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- <%def name="bello(foo, bar)"> Foo: ${ foo } Bar: ${ bar } </%def> <%call expr="bello(foo=u'árvíztűrő tükörfúrógép', bar=u'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')"> - </%call>""".encode('utf-8'), - u"""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP""", + </%call>""").encode('utf-8'), + u("""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP"""), filters=flatten_result ) self._do_memory_test( - u"""## -*- coding: utf-8 -*- + u("""## -*- coding: utf-8 -*- <%def name="hello(foo=u'árvíztűrő tükörfúrógép', bar=u'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')"> Foo: ${ foo } Bar: ${ bar } </%def> - ${ hello() }""".encode('utf-8'), - u"""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP""", + ${ hello() }""").encode('utf-8'), + u("""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP"""), filters=flatten_result ) @@ -281,31 +283,31 @@ class EncodingTest(TemplateTest): """test the 'input_encoding' flag on Template, and that unicode objects arent double-decoded""" - if util.py3k: + if compat.py3k: self._do_memory_test( - u"hello ${f('śląsk')}", - u"hello śląsk", + u("hello ${f('śląsk')}"), + u("hello śląsk"), input_encoding='utf-8', - template_args={'f':lambda x:x} + template_args={'f': lambda x:x} ) self._do_memory_test( - u"## -*- coding: utf-8 -*-\nhello ${f('śląsk')}", - u"hello śląsk", - template_args={'f':lambda x:x} + u("## -*- coding: utf-8 -*-\nhello ${f('śląsk')}"), + u("hello śląsk"), + template_args={'f': lambda x:x} ) else: self._do_memory_test( - u"hello ${f(u'śląsk')}", - u"hello śląsk", + u("hello ${f(u'śląsk')}"), + u("hello śląsk"), input_encoding='utf-8', - template_args={'f':lambda x:x} + template_args={'f': lambda x:x} ) self._do_memory_test( - u"## -*- coding: utf-8 -*-\nhello ${f(u'śląsk')}", - u"hello śląsk", - template_args={'f':lambda x:x} + u("## -*- coding: utf-8 -*-\nhello ${f(u'śląsk')}"), + u("hello śląsk"), + template_args={'f': lambda x:x} ) def test_raw_strings(self): @@ -315,7 +317,7 @@ class EncodingTest(TemplateTest): """ self._do_memory_test( - u"## -*- coding: utf-8 -*-\nhello ${x}", + u("## -*- coding: utf-8 -*-\nhello ${x}"), "hello śląsk", default_filters=[], template_args={'x':'śląsk'}, @@ -326,23 +328,23 @@ class EncodingTest(TemplateTest): # now, the way you *should* be doing it.... self._do_memory_test( - u"## -*- coding: utf-8 -*-\nhello ${x}", - u"hello śląsk", - template_args={'x':u'śląsk'} + u("## -*- coding: utf-8 -*-\nhello ${x}"), + u("hello śląsk"), + template_args={'x':u('śląsk')} ) def test_encoding(self): self._do_memory_test( - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", - u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""".encode('utf-8'), + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""), + u("""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petite voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""").encode('utf-8'), output_encoding='utf-8', unicode_=False ) def test_encoding_errors(self): self._do_memory_test( - u"""KGB (transliteration of "КГБ") is the Russian-language abbreviation for Committee for State Security, (Russian: Комит́ет Госуд́арственной Безоп́асности (help·info); Komitet Gosudarstvennoy Bezopasnosti)""", - u"""KGB (transliteration of "КГБ") is the Russian-language abbreviation for Committee for State Security, (Russian: Комит́ет Госуд́арственной Безоп́асности (help·info); Komitet Gosudarstvennoy Bezopasnosti)""".encode('iso-8859-1', 'replace'), + u("""KGB (transliteration of "КГБ") is the Russian-language abbreviation for Committee for State Security, (Russian: Комит́ет Госуд́арственной Безоп́асности (help·info); Komitet Gosudarstvennoy Bezopasnosti)"""), + u("""KGB (transliteration of "КГБ") is the Russian-language abbreviation for Committee for State Security, (Russian: Комит́ет Госуд́арственной Безоп́асности (help·info); Komitet Gosudarstvennoy Bezopasnosti)""").encode('iso-8859-1', 'replace'), output_encoding='iso-8859-1', encoding_errors='replace', unicode_=False ) @@ -350,7 +352,7 @@ class EncodingTest(TemplateTest): def test_read_unicode(self): lookup = TemplateLookup(directories=[template_base], filesystem_checks=True, output_encoding='utf-8') - if util.py3k: + if compat.py3k: template = lookup.get_template('/read_unicode_py3k.html') else: template = lookup.get_template('/read_unicode.html') @@ -420,11 +422,7 @@ class PageArgsTest(TemplateTest): assert flatten_result(template.render(x=5, y=10)) == "this is page, 5, 10, 7" assert flatten_result(template.render(x=5, y=10, z=32)) == "this is page, 5, 10, 32" - try: - template.render(y=10) - assert False - except TypeError, e: - assert True + assert_raises(TypeError, template.render, y=10) def test_inherits(self): lookup = TemplateLookup() @@ -1012,11 +1010,11 @@ class RichTracebackTest(TemplateTest): def _do_test_traceback(self, utf8, memory, syntax): if memory: if syntax: - source = u'## coding: utf-8\n<% print "m’a réveillé. '\ - u'Elle disait: « S’il vous plaît… dessine-moi un mouton! » %>' + source = u('## coding: utf-8\n<% print "m’a réveillé. '\ + 'Elle disait: « S’il vous plaît… dessine-moi un mouton! » %>') else: - source = u'## coding: utf-8\n<% print u"m’a réveillé. '\ - u'Elle disait: « S’il vous plaît… dessine-moi un mouton! »" + str(5/0) %>' + source = u('## coding: utf-8\n<% print u"m’a réveillé. '\ + 'Elle disait: « S’il vous plaît… dessine-moi un mouton! »" + str(5/0) %>') if utf8: source = source.encode('utf-8') else: @@ -1036,7 +1034,7 @@ class RichTracebackTest(TemplateTest): if not syntax: template.render_unicode() assert False - except Exception, e: + except Exception: tback = exceptions.RichTraceback() if utf8: assert tback.source == source.decode('utf-8') diff --git a/test/test_tgplugin.py b/test/test_tgplugin.py index 3aa6122..560c2fe 100644 --- a/test/test_tgplugin.py +++ b/test/test_tgplugin.py @@ -1,7 +1,7 @@ import unittest from mako.ext.turbogears import TGPlugin -from util import flatten_result, result_lines +from .util import flatten_result, result_lines from test import TemplateTest, template_base tl = TGPlugin(options=dict(directories=[template_base]), extension='html') diff --git a/test/test_util.py b/test/test_util.py index b875f53..a7da274 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -2,7 +2,7 @@ import os import unittest -from mako import util, exceptions +from mako import util, exceptions, compat from test import eq_, skip_if, assert_raises_message class UtilTest(unittest.TestCase): @@ -22,7 +22,7 @@ class UtilTest(unittest.TestCase): eq_(buf.getvalue(), "string c string d") def test_fast_buffer_encoded(self): - s = u"drôl m’a rée « S’il" + s = "drôl m’a rée « S’il" buf = util.FastEncodingBuffer(encoding='utf-8') buf.write(s[0:10]) buf.write(s[10:]) @@ -34,7 +34,7 @@ class UtilTest(unittest.TestCase): data = util.read_file(fn, 'rb') self.failUnless('test_util' in str(data)) # str() for py3k - @skip_if(lambda: util.pypy, "Pypy does this differently") + @skip_if(lambda: compat.pypy, "Pypy does this differently") def test_load_module(self): fn = os.path.join(os.path.dirname(__file__), 'test_util.py') module = util.load_module('mako.template', fn) |