diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-03-04 23:45:40 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-03-04 23:45:40 +0000 |
commit | a629df3f7ef4e36573671018a25e2e9aa0889dbf (patch) | |
tree | eaefb6faad4bbaaf66ddfa27346b9cf2b29a414d /test | |
parent | 4d91d760cd4ef62192c74ff0aa6c27c3d6dff844 (diff) | |
download | external_python_mako-a629df3f7ef4e36573671018a25e2e9aa0889dbf.tar.gz external_python_mako-a629df3f7ef4e36573671018a25e2e9aa0889dbf.tar.bz2 external_python_mako-a629df3f7ef4e36573671018a25e2e9aa0889dbf.zip |
- merged -r481:499 of py3k branch.
- Python 3 support is added ! See README.py3k
for installation and testing notes.
[ticket:119]
Diffstat (limited to 'test')
-rw-r--r-- | test/__init__.py | 13 | ||||
-rw-r--r-- | test/templates/chs_unicode_py3k.html | 11 | ||||
-rw-r--r-- | test/templates/read_unicode_py3k.html | 10 | ||||
-rw-r--r-- | test/templates/unicode_arguments_py3k.html | 10 | ||||
-rw-r--r-- | test/templates/unicode_code_py3k.html | 7 | ||||
-rw-r--r-- | test/templates/unicode_expr_py3k.html | 2 | ||||
-rw-r--r-- | test/test_ast.py | 79 | ||||
-rw-r--r-- | test/test_def.py | 33 | ||||
-rw-r--r-- | test/test_exceptions.py | 59 | ||||
-rw-r--r-- | test/test_inheritance.py | 27 | ||||
-rw-r--r-- | test/test_lexer.py | 259 | ||||
-rw-r--r-- | test/test_template.py | 223 |
12 files changed, 540 insertions, 193 deletions
diff --git a/test/__init__.py b/test/__init__.py index c8c2a4d..1bad221 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -15,17 +15,26 @@ class TemplateTest(unittest.TestCase): return Template(uri=filename, filename=filepath, module_directory=module_base, **kw) def _file_path(self, filename): + name, ext = os.path.splitext(filename) + + if py3k: + py3k_path = os.path.join(template_base, name + "_py3k" + ext) + if os.path.exists(py3k_path): + return py3k_path + return os.path.join(template_base, filename) def _do_file_test(self, filename, expected, filters=None, unicode_=True, template_args=None, **kw): t1 = self._file_template(filename, **kw) - self._do_test(t1, expected, filters=filters, unicode_=unicode_, template_args=template_args) + self._do_test(t1, expected, filters=filters, + unicode_=unicode_, template_args=template_args) def _do_memory_test(self, source, expected, filters=None, unicode_=True, template_args=None, **kw): t1 = Template(text=source, **kw) - self._do_test(t1, expected, filters=filters, unicode_=unicode_, template_args=template_args) + self._do_test(t1, expected, filters=filters, + unicode_=unicode_, template_args=template_args) def _do_test(self, template, expected, filters=None, template_args=None, unicode_=True): if template_args is None: diff --git a/test/templates/chs_unicode_py3k.html b/test/templates/chs_unicode_py3k.html new file mode 100644 index 0000000..35b888d --- /dev/null +++ b/test/templates/chs_unicode_py3k.html @@ -0,0 +1,11 @@ +## -*- encoding:utf8 -*- +<% + msg = '新中国的主席' +%> + +<%def name="welcome(who, place='北京')"> +Welcome ${who} to ${place}. +</%def> + +${name} 是 ${msg}<br/> +${welcome('你')} diff --git a/test/templates/read_unicode_py3k.html b/test/templates/read_unicode_py3k.html new file mode 100644 index 0000000..380d356 --- /dev/null +++ b/test/templates/read_unicode_py3k.html @@ -0,0 +1,10 @@ +<% +try: + file_content = open(path) +except: + raise "Should never execute here" +doc_content = ''.join(file_content.readlines()) +file_content.close() +%> + +${bytes(doc_content, encoding='utf-8')} diff --git a/test/templates/unicode_arguments_py3k.html b/test/templates/unicode_arguments_py3k.html new file mode 100644 index 0000000..97e6d3a --- /dev/null +++ b/test/templates/unicode_arguments_py3k.html @@ -0,0 +1,10 @@ +# coding: utf-8 + +<%def name="my_def(x)"> + x is: ${x} +</%def> + +${my_def('drôle de petit voix m’a réveillé')} +<%self:my_def x='drôle de petit voix m’a réveillé'/> +<%self:my_def x="${'drôle de petit voix m’a réveillé'}"/> +<%call expr="my_def('drôle de petit voix m’a réveillé')"/> diff --git a/test/templates/unicode_code_py3k.html b/test/templates/unicode_code_py3k.html new file mode 100644 index 0000000..76ed9cc --- /dev/null +++ b/test/templates/unicode_code_py3k.html @@ -0,0 +1,7 @@ +## -*- coding: utf-8 -*- +<% + x = "drôle de petit voix m’a réveillé." +%> +% if x=="drôle de petit voix m’a réveillé.": + hi, ${x} +% endif diff --git a/test/templates/unicode_expr_py3k.html b/test/templates/unicode_expr_py3k.html new file mode 100644 index 0000000..4898257 --- /dev/null +++ b/test/templates/unicode_expr_py3k.html @@ -0,0 +1,2 @@ +## -*- coding: utf-8 -*- +${"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"} diff --git a/test/test_ast.py b/test/test_ast.py index ed21456..bfdfd90 100644 --- a/test/test_ast.py +++ b/test/test_ast.py @@ -1,14 +1,12 @@ import unittest from mako import ast, exceptions, pyparser, util +from test import eq_ exception_kwargs = {'source':'', 'lineno':0, 'pos':0, 'filename':''} class AstParseTest(unittest.TestCase): - def setUp(self): - pass - def tearDown(self): - pass + def test_locate_identifiers(self): """test the location of identifiers in a python code string""" code = """ @@ -21,8 +19,8 @@ foo.hoho.lala.bar = 7 + gah.blah + u + blah for lar in (1,2,3): gh = 5 x = 12 -print "hello world, ", a, b -print "Another expr", c +("hello world, ", a, b) +("Another expr", c) """ parsed = ast.PythonCode(code, **exception_kwargs) assert parsed.declared_identifiers == set(['a','b','c', 'g', 'h', 'i', 'u', 'k', 'j', 'gh', 'lar', 'x']) @@ -51,7 +49,7 @@ for x in data: code = """ x = x + 5 for y in range(1, y): - print "hi" + ("hi",) [z for z in range(1, z)] (q for q in range (1, q)) """ @@ -59,9 +57,17 @@ for y in range(1, y): assert parsed.undeclared_identifiers == set(['x', 'y', 'z', 'q', 'range']) def test_locate_identifiers_4(self): - code = """ + if util.py3k: + code = """ +x = 5 +(y, ) +def mydef(mydefarg): + print("mda is", mydefarg) +""" + else: + code = """ x = 5 -print y +(y, ) def mydef(mydefarg): print "mda is", mydefarg """ @@ -70,7 +76,16 @@ def mydef(mydefarg): assert parsed.declared_identifiers == set(['mydef', 'x']) def test_locate_identifiers_5(self): - code = """ + if util.py3k: + code = """ +try: + print(x) +except: + print(y) +""" + else: + + code = """ try: print x except: @@ -86,8 +101,15 @@ def foo(): """ parsed = ast.PythonCode(code, **exception_kwargs) assert parsed.undeclared_identifiers == set(['bar']) - - code = """ + + if util.py3k: + code = """ +def lala(x, y): + return x, y, z +print(x) +""" + else: + code = """ def lala(x, y): return x, y, z print x @@ -95,8 +117,17 @@ print x parsed = ast.PythonCode(code, **exception_kwargs) assert parsed.undeclared_identifiers == set(['z', 'x']) assert parsed.declared_identifiers == set(['lala']) - - code = """ + + if util.py3k: + code = """ +def lala(x, y): + def hoho(): + def bar(): + z = 7 +print(z) +""" + else: + code = """ def lala(x, y): def hoho(): def bar(): @@ -131,11 +162,7 @@ class Hi(object): from foo import * import x as bar """ - try: - parsed = ast.PythonCode(code, **exception_kwargs) - assert False - except exceptions.CompileException, e: - assert str(e).startswith("'import *' is not supported") + self.assertRaises(exceptions.CompileException, ast.PythonCode, code, **exception_kwargs) def test_python_fragment(self): parsed = ast.PythonFragment("for x in foo:", **exception_kwargs) @@ -144,9 +171,12 @@ import x as bar parsed = ast.PythonFragment("try:", **exception_kwargs) - parsed = ast.PythonFragment("except MyException, e:", **exception_kwargs) - assert parsed.declared_identifiers == set(['e']) - assert parsed.undeclared_identifiers == set(['MyException']) + if util.py3k: + parsed = ast.PythonFragment("except MyException as e:", **exception_kwargs) + else: + parsed = ast.PythonFragment("except MyException, e:", **exception_kwargs) + eq_(parsed.declared_identifiers, set(['e'])) + eq_(parsed.undeclared_identifiers, set(['MyException'])) def test_argument_list(self): parsed = ast.ArgumentList("3, 5, 'hi', x+5, context.get('lala')", **exception_kwargs) @@ -184,8 +214,6 @@ import x as bar code = "str((x+7*y) / foo.bar(5,6)) + lala('ho')" astnode = pyparser.parse(code) newcode = pyparser.ExpressionGenerator(astnode).value() - #print "newcode:" + newcode - #print "result:" + eval(code, local_dict) assert (eval(code, local_dict) == eval(newcode, local_dict)) a = ["one", "two", "three"] @@ -195,8 +223,6 @@ import x as bar code = "a[2] + hoho['somevalue'] + repr(g[3:5]) + repr(g[3:]) + repr(g[:5])" astnode = pyparser.parse(code) newcode = pyparser.ExpressionGenerator(astnode).value() - #print newcode - #print "result:", eval(code, local_dict) assert(eval(code, local_dict) == eval(newcode, local_dict)) local_dict={'f':lambda :9, 'x':7} @@ -209,7 +235,6 @@ import x as bar local_dict={} astnode = pyparser.parse(code) newcode = pyparser.ExpressionGenerator(astnode).value() - #print code, newcode assert(eval(code, local_dict)) == eval(newcode, local_dict), "%s != %s" % (code, newcode) diff --git a/test/test_def.py b/test/test_def.py index 6cb1fdf..1f7a39a 100644 --- a/test/test_def.py +++ b/test/test_def.py @@ -1,9 +1,9 @@ from mako.template import Template from mako import lookup -import unittest +from test import TemplateTest from util import flatten_result, result_lines -class DefTest(unittest.TestCase): +class DefTest(TemplateTest): def test_def_noargs(self): template = Template(""" @@ -61,6 +61,7 @@ class DefTest(unittest.TestCase): def test_toplevel(self): """test calling a def from the top level""" + template = Template(""" this is the body @@ -73,15 +74,20 @@ class DefTest(unittest.TestCase): this is b, ${x} ${y} </%def> - """, output_encoding='utf-8') - assert flatten_result(template.get_def("a").render()) == "this is a" - assert flatten_result(template.get_def("b").render(x=10, y=15)) == "this is b, 10 15" - assert flatten_result(template.get_def("body").render()) == "this is the body" + """) + + self._do_test(template.get_def("a"), "this is a", filters=flatten_result) + self._do_test(template.get_def("b"), "this is b, 10 15", + template_args={'x':10, 'y':15}, + filters=flatten_result) + self._do_test(template.get_def("body"), "this is the body", filters=flatten_result) + -class ScopeTest(unittest.TestCase): +class ScopeTest(TemplateTest): """test scoping rules. The key is, enclosing scope always takes precedence over contextual scope.""" + def test_scope_one(self): - t = Template(""" + self._do_memory_test(""" <%def name="a()"> this is a, and y is ${y} </%def> @@ -94,8 +100,11 @@ class ScopeTest(unittest.TestCase): ${a()} -""") - assert flatten_result(t.render(y=None)) == "this is a, and y is None this is a, and y is 7" +""", + "this is a, and y is None this is a, and y is 7", + filters=flatten_result, + template_args={'y':None} + ) def test_scope_two(self): t = Template(""" @@ -372,7 +381,7 @@ class ScopeTest(unittest.TestCase): "this is a, x is 15" ] -class NestedDefTest(unittest.TestCase): +class NestedDefTest(TemplateTest): def test_nested_def(self): t = Template(""" @@ -512,7 +521,7 @@ class NestedDefTest(unittest.TestCase): """) assert flatten_result(t.render(x=5)) == "b. c. x is 10. a: x is 5 x is 5" -class ExceptionTest(unittest.TestCase): +class ExceptionTest(TemplateTest): def test_raise(self): template = Template(""" <% diff --git a/test/test_exceptions.py b/test/test_exceptions.py index 52c9544..57b3bac 100644 --- a/test/test_exceptions.py +++ b/test/test_exceptions.py @@ -2,7 +2,7 @@ import sys import unittest -from mako import exceptions +from mako import exceptions, util from mako.template import Template from mako.lookup import TemplateLookup from util import result_lines @@ -15,9 +15,9 @@ class ExceptionsTest(unittest.TestCase): """ try: template = Template(code) - template.render() + template.render_unicode() except exceptions.CompileException, ce: - html_error = exceptions.html_error_template().render() + html_error = exceptions.html_error_template().render_unicode() assert ("CompileException: Fragment 'i = 0' is not a partial " "control statement") in html_error assert '<style>' in html_error @@ -26,13 +26,13 @@ class ExceptionsTest(unittest.TestCase): assert html_error_stripped.startswith('<html>') assert html_error_stripped.endswith('</html>') - not_full = exceptions.html_error_template().render(full=False) + not_full = exceptions.html_error_template().render_unicode(full=False) assert '<html>' not in not_full assert '</html>' not in not_full assert '<style>' in not_full assert '</style>' in not_full - no_css = exceptions.html_error_template().render(css=False) + no_css = exceptions.html_error_template().render_unicode(css=False) assert '<style>' not in no_css assert '</style>' not in no_css else: @@ -41,20 +41,33 @@ class ExceptionsTest(unittest.TestCase): def test_utf8_html_error_template(self): """test the html_error_template with a Template containing utf8 chars""" - code = """# -*- coding: utf-8 -*- + + 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() + template.render_unicode() except exceptions.CompileException, ce: html_error = exceptions.html_error_template().render() assert ("CompileException: Fragment 'if 2 == 2: /an " "error' is not a partial control " - "statement at line: 2 char: 1") in html_error - assert u"3 ${u'привет'}".encode(sys.getdefaultencoding(), + "statement at line: 2 char: 1") in html_error.decode('utf-8') + + if util.py3k: + assert u"3 ${'привет'}".encode(sys.getdefaultencoding(), + 'htmlentityreplace') in html_error + else: + assert u"3 ${u'привет'}".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error else: assert False, ("This function should trigger a CompileException, " @@ -66,8 +79,12 @@ ${u'привет'} raise RuntimeError('test') except: html_error = exceptions.html_error_template().render() - assert 'RuntimeError: test' in html_error - assert "foo = u'日本'" in html_error + if util.py3k: + assert 'RuntimeError: test' in html_error.decode('utf-8') + assert u"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): @@ -75,8 +92,7 @@ ${u'привет'} raise RuntimeError(u'日本') except: html_error = exceptions.html_error_template().render() - assert 'RuntimeError: 日本' in html_error - assert "RuntimeError(u'日本')" in html_error + assert u"RuntimeError: 日本".encode('ascii', 'ignore') in html_error def test_format_exceptions(self): l = TemplateLookup(format_exceptions=True) @@ -90,16 +106,21 @@ ${foobar} ${self.body()} """) - assert '<div class="sourceline">${foobar}</div>' in result_lines(l.get_template("foo.html").render()) + assert '<div class="sourceline">${foobar}</div>' in result_lines(l.get_template("foo.html").render_unicode()) def test_utf8_format_exceptions(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}""") - l.put_string("foo.html", """# -*- coding: utf-8 -*- -${u'привет' + foobar} -""") - - assert '''<div class="highlight">2 ${u\'привет\' + foobar}</div>''' in result_lines(l.get_template("foo.html").render()) + if util.py3k: + assert u'<div class="sourceline">${\'привет\' + foobar}</div>'\ + in result_lines(l.get_template("foo.html").render().decode('utf-8')) + else: + assert '<div class="highlight">2 ${u\'привет\' + foobar}</div>' \ + in result_lines(l.get_template("foo.html").render().decode('utf-8')) diff --git a/test/test_inheritance.py b/test/test_inheritance.py index c9c6990..9f978d2 100644 --- a/test/test_inheritance.py +++ b/test/test_inheritance.py @@ -1,5 +1,5 @@ from mako.template import Template -from mako import lookup +from mako import lookup, util import unittest from util import flatten_result, result_lines @@ -187,10 +187,10 @@ ${next.body()} this is the base. <% - sorted = pageargs.items() - sorted.sort() + sorted_ = pageargs.items() + sorted_ = sorted(sorted_) %> - pageargs: (type: ${type(pageargs)}) ${sorted} + pageargs: (type: ${type(pageargs)}) ${sorted_} <%def name="foo()"> ${next.body(**context.kwargs)} </%def> @@ -202,11 +202,20 @@ ${next.body()} <%page args="x, y, z=7"/> print ${x}, ${y}, ${z} """) - assert result_lines(collection.get_template('index').render(x=5,y=10)) == [ - "this is the base.", - "pageargs: (type: <type 'dict'>) [('x', 5), ('y', 10)]", - "print 5, 10, 7" - ] + + if util.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)]", + "print 5, 10, 7" + ] + else: + assert result_lines(collection.get_template('index').render_unicode(x=5,y=10)) == [ + "this is the base.", + "pageargs: (type: <type 'dict'>) [('x', 5), ('y', 10)]", + "print 5, 10, 7" + ] + def test_pageargs_2(self): collection = lookup.TemplateLookup() collection.put_string("base", """ diff --git a/test/test_lexer.py b/test/test_lexer.py index d934860..00d16af 100644 --- a/test/test_lexer.py +++ b/test/test_lexer.py @@ -1,14 +1,45 @@ import unittest from mako.lexer import Lexer -from mako import exceptions +from mako import exceptions, util from util import flatten_result, result_lines from mako.template import Template import re -from test import TemplateTest, template_base, skip_if - - +from test import TemplateTest, template_base, skip_if, eq_ + +# create fake parsetree classes which are constructed +# exactly as the repr() of a real parsetree object. +# this allows us to use a Python construct as the source +# of a comparable repr(), which is also hit by the 2to3 tool. + +def repr_arg(x): + if isinstance(x, dict): + return util.sorted_dict_repr(x) + else: + return repr(x) + +from mako import parsetree +for cls in parsetree.__dict__.values(): + if isinstance(cls, type) and \ + issubclass(cls, parsetree.Node): + clsname = cls.__name__ + exec (""" +class %s(object): + def __init__(self, *args): + self.args = args + def __repr__(self): + return "%%s(%%s)" %% ( + self.__class__.__name__, + ", ".join(repr_arg(x) for x in self.args) + ) +""" % clsname) in locals() + + class LexerTest(TemplateTest): + + def _compare(self, node, expected): + eq_(repr(node), repr(expected)) + def test_text_and_tag(self): template = """ <b>Hello world</b> @@ -19,7 +50,10 @@ class LexerTest(TemplateTest): and some more text. """ node = Lexer(template).parse() - assert repr(node) == r"""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 ', (3, 28))"]), Text(u'\n \n and some more text.\n', (5, 16))])""" + 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 ', (3, 28))]), Text(u'\n \n and some more text.\n', (5, 16))]) + ) def test_unclosed_tag(self): template = """ @@ -96,7 +130,10 @@ class LexerTest(TemplateTest): % endif """ node = Lexer(template).parse() - assert repr(node) == r"""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'\n\n ', (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(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'\n\n ', (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))]) + ) def test_def_syntax(self): template = """ @@ -122,7 +159,10 @@ class LexerTest(TemplateTest): """ node = Lexer(template).parse() - assert repr(node) == r"""TemplateNode({}, [Text(u'\n ', (1, 1)), DefTag(u'def', {u'name': u'adef()'}, (2, 13), ["Text(u'\\n adef\\n ', (2, 36))"]), Text(u'\n ', (4, 20))])""" + self._compare( + node, + TemplateNode({}, [Text(u'\n ', (1, 1)), DefTag(u'def', {u'name': u'adef()'}, (2, 13), [Text(u'\n adef\n ', (2, 36))]), Text(u'\n ', (4, 20))]) + ) def test_ns_tag_closed(self): template = """ @@ -130,14 +170,20 @@ class LexerTest(TemplateTest): <%self:go x="1" y="2" z="${'hi' + ' ' + 'there'}"/> """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n \n ', (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))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\n \n ', (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))]) + ) def test_ns_tag_empty(self): template = """ <%form:option value=""></%form:option> """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n ', (1, 1)), CallNamespaceTag(u'form:option', {u'value': u''}, (2, 13), []), Text(u'\n ', (2, 51))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\n ', (1, 1)), CallNamespaceTag(u'form:option', {u'value': u''}, (2, 13), []), Text(u'\n ', (2, 51))]) + ) def test_ns_tag_open(self): template = """ @@ -147,19 +193,26 @@ class LexerTest(TemplateTest): </%self:go> """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n \n ', (1, 1)), CallNamespaceTag(u'self:go', {u'x': u'1', u'y': u'${process()}'}, (3, 13), ["Text(u'\\n this is the body\\n ', (3, 46))"]), Text(u'\n ', (5, 24))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\n \n ', (1, 1)), CallNamespaceTag(u'self:go', {u'x': u'1', u'y': u'${process()}'}, (3, 13), [Text(u'\n this is the body\n ', (3, 46))]), Text(u'\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.""" + you can still trip up the expression parsing, + though, unless we integrated really deeply somehow with AST.""" + template = """ <%call expr="foo>bar and 'lala' or 'hoho'"/> <%call expr='foo<bar and hoho>lala and "x" + "y"'/> """ nodes = Lexer(template).parse() - #print nodes - assert repr(nodes) == r"""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(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))]) + ) def test_pagetag(self): @@ -169,7 +222,10 @@ class LexerTest(TemplateTest): some template """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n ', (1, 1)), PageTag(u'page', {u'args': u'a, b', u'cached': u'True'}, (2, 13), []), Text(u'\n \n some template\n ', (2, 48))])""" + 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'\n \n some template\n ', (2, 48))]) + ) def test_nesting(self): template = """ @@ -182,10 +238,38 @@ class LexerTest(TemplateTest): """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n \n ', (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'\n \n ', (7, 22))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\n \n ', (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'\n \n ', (7, 22))]) + ) - def test_code(self): - template = """ + if util.py3k: + def test_code(self): + template = \ + """ + some text + + <% + print("hi") + for x in range(1,5): + print(x) + %> + + more text + + <%! + import foo + %> + """ + nodes = Lexer(template).parse() + self._compare( + nodes, + TemplateNode({}, [Text(u'\n some text\n \n ', (1, 1)), Code(u'\nprint("hi")\nfor x in range(1,5):\n print(x)\n \n', False, (4, 9)), Text(u'\n \n more text\n \n ', (8, 11)), Code(u'\nimport foo\n \n', True, (12, 9)), Text(u'\n ', (14, 11))]) + ) + else: + def test_code(self): + template = \ + """ some text <% @@ -200,9 +284,11 @@ class LexerTest(TemplateTest): import foo %> """ - nodes = Lexer(template).parse() - #print nodes - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n some text\n \n ', (1, 1)), Code(u'\nprint "hi"\nfor x in range(1,5):\n print x\n \n', False, (4, 9)), Text(u'\n \n more text\n \n ', (8, 11)), Code(u'\nimport foo\n \n', True, (12, 9)), Text(u'\n ', (14, 11))])""" + nodes = Lexer(template).parse() + self._compare( + nodes, + TemplateNode({}, [Text(u'\n some text\n \n ', (1, 1)), Code(u'\nprint "hi"\nfor x in range(1,5):\n print x\n \n', False, (4, 9)), Text(u'\n \n more text\n \n ', (8, 11)), Code(u'\nimport foo\n \n', True, (12, 9)), Text(u'\n ', (14, 11))]) + ) def test_code_and_tags(self): template = """ @@ -225,7 +311,10 @@ class LexerTest(TemplateTest): result: <%call expr="foo.x(result)"/> """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""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()\nfor 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))])""" + 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()\nfor 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))]) + ) def test_expression(self): template = """ @@ -236,7 +325,10 @@ class LexerTest(TemplateTest): ${hi()} """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""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))])""" + 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))]) + ) def test_tricky_expression(self): @@ -245,36 +337,68 @@ class LexerTest(TemplateTest): ${x and "|" or "hi"} """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n \n ', (1, 1)), Expression(u'x and "|" or "hi"', [], (3, 13)), Text(u'\n ', (3, 33))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\n \n ', (1, 1)), Expression(u'x and "|" or "hi"', [], (3, 13)), Text(u'\n ', (3, 33))]) + ) template = """ ${hello + '''heres '{|}' text | | }''' | escape1} """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n \n ', (1, 1)), Expression(u"hello + '''heres '{|}' text | | }''' ", ['escape1'], (3, 13)), Text(u'\n ', (3, 62))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\n \n ', (1, 1)), Expression(u"hello + '''heres '{|}' text | | }''' ", ['escape1'], (3, 13)), Text(u'\n ', (3, 62))]) + ) def test_tricky_code(self): - template = """<% print 'hi %>' %>""" - nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Code(u"print 'hi %>' \n", False, (1, 1))])""" + if util.py3k: + template = """<% print('hi %>') %>""" + nodes = Lexer(template).parse() + self._compare( + nodes, + TemplateNode({}, [Code(u"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))]) + ) - template = r""" - <% - lines = src.split('\n') - %> -""" - nodes = Lexer(template).parse() - def test_tricky_code_2(self): template = """<% # someone's comment %> """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Code(u" \n # someone's comment\n \n", False, (1, 1)), Text(u'\n ', (3, 11))])""" - - template= """<% + self._compare( + nodes, + TemplateNode({}, [Code(u" \n # someone's comment\n \n", False, (1, 1)), Text(u'\n ', (3, 11))]) + ) + + if util.py3k: + def test_tricky_code_3(self): + template= """<% + print('hi') + # this is a comment + # another comment + x = 7 # someone's '''comment + print(''' + there + ''') + # someone else's comment + %> '''and now some text '''""" + nodes = Lexer(template).parse() + self._compare( + nodes, + TemplateNode({}, [Code(u"\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", False, (1, 1)), Text(u" '''and now some text '''", (10, 11))]) + ) + else: + def test_tricky_code_3(self): + template= """<% print 'hi' # this is a comment # another comment @@ -284,8 +408,11 @@ class LexerTest(TemplateTest): ''' # someone else's comment %> '''and now some text '''""" - nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Code(u"\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", False, (1, 1)), Text(u" '''and now some text '''", (10, 11))])""" + nodes = Lexer(template).parse() + self._compare( + nodes, + TemplateNode({}, [Code(u"\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", False, (1, 1)), Text(u" '''and now some text '''", (10, 11))]) + ) def test_control_lines(self): template = """ @@ -302,8 +429,10 @@ text text la la """ nodes = Lexer(template).parse() - #print nodes - assert repr(nodes) == r"""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))])""" + 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))]) + ) def test_control_lines_2(self): template = \ @@ -315,7 +444,10 @@ text text la la % endfor """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n\n\n', (1, 1)), ControlLine(u'for', u"for file in requestattr['toc'].filenames:", False, (4, 1)), Text(u' x\n', (5, 1)), ControlLine(u'for', u'endfor', True, (6, 1))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\n\n\n', (1, 1)), ControlLine(u'for', u"for file in requestattr['toc'].filenames:", False, (4, 1)), Text(u' x\n', (5, 1)), ControlLine(u'for', u'endfor', True, (6, 1))]) + ) def test_long_control_lines(self): template = \ @@ -326,7 +458,10 @@ text text la la % endfor """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""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))])""" + 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))]) + ) def test_unmatched_control(self): template = """ @@ -381,7 +516,10 @@ text text la la % endif """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""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(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))]) + ) def test_integration(self): template = """<%namespace name="foo" file="somefile.html"/> @@ -406,8 +544,10 @@ text text la la </table> """ nodes = Lexer(template).parse() - expected = r"""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))])""" - assert repr(nodes) == expected + 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))]) + ) def test_comment_after_statement(self): template = """ @@ -418,12 +558,18 @@ text text la la % endif #end """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""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(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))]) + ) def test_crlf(self): - template = file(self._file_path("crlf.html")).read() + template = open(self._file_path("crlf.html"), 'rb').read() nodes = Lexer(template).parse() - assert repr(nodes) == r"""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)), Text(u'', (9, 13)), 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))])""" + 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)), Text(u'', (9, 13)), 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))]) + ) assert flatten_result(Template(template).render()) == """<html> like the name says. 1 2 3 Dizzy </html>""" def test_comments(self): @@ -447,7 +593,10 @@ comment hi """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\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'\n\nhi\n', (16, 8))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\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'\n\nhi\n', (16, 8))]) + ) def test_docs(self): template = """ @@ -461,7 +610,10 @@ hi </%def> """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [Text(u'\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 ', (6, 13))", "Text(u'\\n ', (8, 20))"]), Text(u'\n ', (9, 16))])""" + self._compare( + nodes, + TemplateNode({}, [Text(u'\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 ', (6, 13)), Text(u'\n ', (8, 20))]), Text(u'\n ', (9, 16))]) + ) def test_preprocess(self): def preproc(text): @@ -472,5 +624,8 @@ hi # another comment """ nodes = Lexer(template, preprocessor=preproc).parse() - assert repr(nodes) == r"""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(u'\n hi\n', (1, 1)), Comment(u'old style comment', (3, 1)), Comment(u'another comment', (4, 1))]) + ) diff --git a/test/test_template.py b/test/test_template.py index f06ab5b..970565a 100644 --- a/test/test_template.py +++ b/test/test_template.py @@ -3,7 +3,7 @@ from mako.template import Template, ModuleTemplate from mako.lookup import TemplateLookup from mako.ext.preprocessors import convert_comments -from mako import exceptions +from mako import exceptions, util import re, os from util import flatten_result, result_lines import codecs @@ -50,10 +50,13 @@ class EncodingTest(TemplateTest): directories=[template_base], output_encoding='utf-8', default_filters=['decode.utf8']) - template = lookup.get_template('/chs_unicode.html') + if util.py3k: + template = lookup.get_template('/chs_unicode_py3k.html') + else: + template = lookup.get_template('/chs_unicode.html') eq_( - flatten_result(template.render(name='毛泽东')), - '毛泽东 是 新中国的主席<br/> Welcome 你 to 北京.' + flatten_result(template.render_unicode(name='毛泽东')), + u'毛泽东 是 新中国的主席<br/> Welcome 你 to 北京.' ) def test_unicode_bom(self): @@ -76,14 +79,14 @@ class EncodingTest(TemplateTest): def test_unicode_memory(self): val = u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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'), + ("## -*- coding: utf-8 -*-\n" + val).encode('utf-8'), u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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 petit 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'), + ("## -*- coding: utf-8 -*-\n" + val).encode('utf-8'), u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" ) @@ -96,19 +99,28 @@ class EncodingTest(TemplateTest): <%text>Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »</%text> </%call>""" self._do_memory_test( - "## -*- coding: utf-8 -*-\n" + val.encode('utf-8'), + ("## -*- coding: utf-8 -*-\n" + val).encode('utf-8'), u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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): - self._do_memory_test( - u"""## -*- coding: utf-8 -*- - ${u"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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 petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", - filters = lambda s:s.strip() - ) + if util.py3k: + self._do_memory_test( + u"""## -*- coding: utf-8 -*- + ${"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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 petit 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"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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 petit 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( @@ -118,29 +130,54 @@ class EncodingTest(TemplateTest): ) def test_unicode_literal_in_code(self): - self._do_memory_test( - u"""## -*- coding: utf-8 -*- - <% - context.write(u"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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 petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""", - filters=lambda s:s.strip() - ) + if util.py3k: + self._do_memory_test( + u"""## -*- coding: utf-8 -*- + <% + context.write("Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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 petit 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 -*- + <% + context.write(u"Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit 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 petit 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): - self._do_memory_test( - u"""## -*- coding: utf-8 -*- - <% - x = u"drôle de petit voix m’a réveillé." - %> - % if x==u"drôle de petit voix m’a réveillé.": - hi, ${x} - % endif - """.encode('utf-8'), - u"""hi, drôle de petit voix m’a réveillé.""", - filters=lambda s:s.strip(), - ) + if util.py3k: + self._do_memory_test( + u"""## -*- coding: utf-8 -*- + <% + x = "drôle de petit voix m’a réveillé." + %> + % if x=="drôle de petit voix m’a réveillé.": + hi, ${x} + % endif + """.encode('utf-8'), + u"""hi, drôle de petit voix m’a réveillé.""", + filters=lambda s:s.strip(), + ) + else: + self._do_memory_test( + u"""## -*- coding: utf-8 -*- + <% + x = u"drôle de petit voix m’a réveillé." + %> + % if x==u"drôle de petit voix m’a réveillé.": + hi, ${x} + % endif + """.encode('utf-8'), + u"""hi, drôle de petit voix m’a réveillé.""", + filters=lambda s:s.strip(), + ) def test_unicode_literal_in_tag(self): self._do_file_test( @@ -155,7 +192,7 @@ class EncodingTest(TemplateTest): ) self._do_memory_test( - file(self._file_path("unicode_arguments.html")).read(), + open(self._file_path("unicode_arguments.html"), 'rb').read(), [ u'x is: drôle de petit voix m’a réveillé', u'x is: drôle de petit voix m’a réveillé', @@ -166,45 +203,83 @@ class EncodingTest(TemplateTest): ) def test_unicode_literal_in_def(self): - self._do_memory_test( - 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""", - filters=flatten_result - ) + if util.py3k: + self._do_memory_test( + 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""", + filters=flatten_result + ) + + self._do_memory_test( + 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""", + filters=flatten_result + ) + else: + self._do_memory_test( + 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""", + filters=flatten_result + ) - self._do_memory_test( - 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""", - filters=flatten_result - ) + self._do_memory_test( + 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""", + filters=flatten_result + ) def test_input_encoding(self): """test the 'input_encoding' flag on Template, and that unicode objects arent double-decoded""" - self._do_memory_test( - u"hello ${f(u'śląsk')}", - u"hello śląsk", - input_encoding='utf-8', - 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} - ) + if util.py3k: + self._do_memory_test( + u"hello ${f('śląsk')}", + u"hello śląsk", + input_encoding='utf-8', + 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} + ) + else: + self._do_memory_test( + u"hello ${f(u'śląsk')}", + u"hello śląsk", + input_encoding='utf-8', + 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} + ) def test_raw_strings(self): """test that raw strings go straight thru with default_filters turned off""" @@ -243,9 +318,13 @@ class EncodingTest(TemplateTest): def test_read_unicode(self): lookup = TemplateLookup(directories=[template_base], filesystem_checks=True, output_encoding='utf-8') - template = lookup.get_template('/read_unicode.html') + if util.py3k: + template = lookup.get_template('/read_unicode_py3k.html') + else: + template = lookup.get_template('/read_unicode.html') data = template.render(path=self._file_path('internationalization.html')) + @skip_if(lambda: util.py3k) def test_bytestring_passthru(self): self._do_file_test( 'chs_utf8.html', @@ -418,7 +497,7 @@ class ControlTest(TemplateTest): t = Template(""" ## this is a template. % for x in y: - % if x.has_key('test'): + % if 'test' in x: yes x has test % else: no x does not have test @@ -474,7 +553,7 @@ class RichTracebackTest(TemplateTest): filename = 'unicode_syntax_error.html' else: filename = 'unicode_runtime_error.html' - source = file(self._file_path(filename)).read() + source = open(self._file_path(filename), 'rb').read() if not utf8: source = source.decode('utf-8') templateargs = {'filename':self._file_path(filename)} |