summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-11-16 12:50:29 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2010-11-16 12:50:29 -0500
commit73a9d804e07e442f5b065a1a27cf5eb5319edf7b (patch)
tree97080514853f15843d9648779a9a4fb38b0691e8
parent7b988e4f7b289a534ac349f003d997087b8bf565 (diff)
downloadmako-73a9d804e07e442f5b065a1a27cf5eb5319edf7b.tar.gz
run the lexer test through combination pytidy/manual cleanup, to at
least remove huge lines
-rw-r--r--test/__init__.py9
-rw-r--r--test/test_lexer.py634
2 files changed, 427 insertions, 216 deletions
diff --git a/test/__init__.py b/test/__init__.py
index 3280216..f9e3118 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -1,6 +1,7 @@
from mako.template import Template
import unittest, os
from mako.util import function_named, py3k
+import re
from nose import SkipTest
@@ -67,6 +68,14 @@ def assert_raises(except_cls, callable_, *args, **kw):
# assert outside the block so it works for AssertionError too !
assert success, "Callable did not raise an exception"
+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:
+ assert re.search(msg, str(e)), "%r !~ %s" % (msg, e)
+ print str(e)
+
def skip_if(predicate, reason=None):
"""Skip a test if predicate is true."""
reason = reason or predicate.__name__
diff --git a/test/test_lexer.py b/test/test_lexer.py
index a49fc0c..f35b2ea 100644
--- a/test/test_lexer.py
+++ b/test/test_lexer.py
@@ -5,7 +5,7 @@ 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, eq_
+from test import TemplateTest, template_base, skip_if, eq_, assert_raises_message
# create fake parsetree classes which are constructed
# exactly as the repr() of a real parsetree object.
@@ -34,7 +34,9 @@ class %s(object):
)
""" % clsname) in locals()
-
+# NOTE: most assertion expressions were generated, then formatted
+# by PyTidy, hence the dense formatting.
+
class LexerTest(TemplateTest):
def _compare(self, node, expected):
@@ -50,10 +52,13 @@ class LexerTest(TemplateTest):
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 ', (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 = """
@@ -68,7 +73,8 @@ class LexerTest(TemplateTest):
assert str(e) == "Unclosed tag: <%def> at line: 5 char: 9"
def test_onlyclosed_tag(self):
- template = """
+ template = \
+ """
<%def name="foo()">
foo
</%def>
@@ -77,16 +83,20 @@ class LexerTest(TemplateTest):
hi.
"""
- self.assertRaises(exceptions.SyntaxException, Lexer(template).parse)
+ self.assertRaises(exceptions.SyntaxException,
+ Lexer(template).parse)
def test_noexpr_allowed(self):
- template = """
+ template = \
+ """
<%namespace name="${foo}"/>
"""
- self.assertRaises(exceptions.CompileException, Lexer(template).parse)
-
+ self.assertRaises(exceptions.CompileException,
+ Lexer(template).parse)
+
def test_unmatched_tag(self):
- template = """
+ template = \
+ """
<%namespace name="bar">
<%def name="foo()">
foo
@@ -96,25 +106,29 @@ class LexerTest(TemplateTest):
hi.
"""
- self.assertRaises(exceptions.SyntaxException, Lexer(template).parse)
+ self.assertRaises(exceptions.SyntaxException,
+ Lexer(template).parse)
def test_nonexistent_tag(self):
template = """
<%lala x="5"/>
"""
- self.assertRaises(exceptions.CompileException, Lexer(template).parse)
-
+ self.assertRaises(exceptions.CompileException,
+ Lexer(template).parse)
+
def test_wrongcase_tag(self):
- template = """
+ template = \
+ """
<%DEF name="foo()">
</%def>
"""
-
- self.assertRaises(exceptions.CompileException, Lexer(template).parse)
+ self.assertRaises(exceptions.CompileException,
+ Lexer(template).parse)
def test_percent_escape(self):
- template = """
+ template = \
+ """
%% some whatever.
@@ -123,13 +137,16 @@ 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(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))]))
def test_text_tag(self):
- template = """
+ template = \
+ """
## comment
% if foo:
hi
@@ -151,105 +168,149 @@ class LexerTest(TemplateTest):
% 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'\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'''
+
+ ''', (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 = """
+ template = \
+ """
<%def lala>
hi
</%def>
"""
- self.assertRaises(exceptions.CompileException, Lexer(template).parse)
+ self.assertRaises(exceptions.CompileException,
+ Lexer(template).parse)
def test_def_syntax_2(self):
- template = """
+ template = \
+ """
<%def name="lala">
hi
</%def>
"""
- self.assertRaises(exceptions.CompileException, Lexer(template).parse)
+ self.assertRaises(exceptions.CompileException,
+ Lexer(template).parse)
def test_whitespace_equals(self):
- template = """
+ template = \
+ """
<%def name = "adef()" >
adef
</%def>
- """
-
+ """
node = Lexer(template).parse()
- 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))])
- )
+ 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 = """
+ template = \
+ """
<%self:go x="1" y="2" z="${'hi' + ' ' + 'there'}"/>
"""
nodes = Lexer(template).parse()
- 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))])
- )
+ self._compare(nodes, TemplateNode({},
+ [Text(u'''
+
+ ''', (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 = """
+ template = \
+ """
<%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 ', (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 = """
+ template = \
+ """
<%self:go x="1" y="${process()}">
this is the body
</%self:go>
"""
nodes = Lexer(template).parse()
- 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))])
- )
+ self._compare(nodes, TemplateNode({},
+ [Text(u'''
+
+ ''', (1, 1)),
+ CallNamespaceTag(u'self:go', {u'x': u'1', u'y'
+ : u'${process()}'}, (3, 13),
+ [Text(u'''
+ this is the body
+ ''',
+ (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."""
-
- template = """
+ 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()
- 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(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):
- template = """
+ 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'\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'''
+
+ some template
+ ''',
+ (2, 48))]))
+
def test_nesting(self):
- template = """
+ template = \
+ """
<%namespace name="ns">
<%def name="lala(hi, there)">
@@ -259,60 +320,76 @@ class LexerTest(TemplateTest):
"""
nodes = Lexer(template).parse()
- 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))])
- )
-
+ self._compare(nodes, TemplateNode({},
+ [Text(u'''
+
+ ''', (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'''
+
+ ''', (7, 22))]))
+
if util.py3k:
def test_code(self):
template = \
- """
- some text
-
- <%
- print("hi")
- for x in range(1,5):
- print(x)
- %>
-
- more text
-
- <%!
- import foo
- %>
- """
+"""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))])
+ 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))])
)
+
+
else:
+
def test_code(self):
template = \
- """
- some text
-
- <%
- print "hi"
- for x in range(1,5):
- print x
- %>
-
- more text
-
- <%!
- import foo
- %>
- """
+"""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))])
+ 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))])
)
def test_code_and_tags(self):
- template = """
+ template = \
+ """
<%namespace name="foo">
<%def name="x()">
this is x
@@ -332,13 +409,24 @@ class LexerTest(TemplateTest):
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()\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()\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))]))
def test_expression(self):
- template = """
+ template = \
+ """
this is some ${text} and this is ${textwith | escapes, moreescapes}
<%def name="hi()">
give me ${foo()} and ${bar()}
@@ -346,10 +434,19 @@ class LexerTest(TemplateTest):
${hi()}
"""
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))])
- )
+ 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):
@@ -360,7 +457,11 @@ class LexerTest(TemplateTest):
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))])
+ TemplateNode({}, [
+ Text(u'\n \n ', (1, 1)),
+ Expression(u'x and "|" or "hi"', [], (3, 13)),
+ Text(u'\n ', (3, 33))
+ ])
)
template = """
@@ -370,39 +471,44 @@ class LexerTest(TemplateTest):
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))])
+ 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):
if util.py3k:
template = """<% print('hi %>') %>"""
nodes = Lexer(template).parse()
- self._compare(
- nodes,
- TemplateNode({}, [Code(u"print('hi %>') \n", False, (1, 1))])
- )
+ 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))])
- )
-
+ self._compare(nodes, TemplateNode({},
+ [Code(u"print 'hi %>' \n", False, (1, 1))]))
+
def test_tricky_code_2(self):
- template = """<%
+ template = \
+ """<%
# someone's comment
%>
"""
nodes = Lexer(template).parse()
- self._compare(
- nodes,
- TemplateNode({}, [Code(u" \n # someone's comment\n \n", False, (1, 1)), Text(u'\n ', (3, 11))])
- )
+ self._compare(nodes, TemplateNode({},
+ [Code(u"""
+ # someone's comment
+
+""",
+ False, (1, 1)), Text(u'\n ', (3, 11))]))
if util.py3k:
def test_tricky_code_3(self):
- template= """<%
+ template = \
+ """<%
print('hi')
# this is a comment
# another comment
@@ -413,13 +519,25 @@ class LexerTest(TemplateTest):
# 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))])
- )
+ self._compare(nodes, TemplateNode({},
+ [Code(u"""
+print('hi')
+# this is a comment
+# another comment
+x = 7 # someone's '''comment
+print('''
+ there
+ ''')
+# someone else's comment
+
+""",
+ False, (1, 1)),
+ Text(u" '''and now some text '''", (10,
+ 11))]))
else:
def test_tricky_code_3(self):
- template= """<%
+ template = \
+ """<%
print 'hi'
# this is a comment
# another comment
@@ -430,13 +548,18 @@ class LexerTest(TemplateTest):
# 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))])
- )
+ 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 = """
+ template = \
+ """
text text la la
% if foo():
mroe text la la blah blah
@@ -450,25 +573,30 @@ text text la la
"""
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))])
- )
+ 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 = \
-"""
-
-
-% for file in requestattr['toc'].filenames:
+"""% for file in requestattr['toc'].filenames:
x
% endfor
"""
nodes = Lexer(template).parse()
- 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))])
- )
+ 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))]))
def test_long_control_lines(self):
template = \
@@ -481,7 +609,15 @@ text text la la
nodes = Lexer(template).parse()
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))])
+ 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):
@@ -491,11 +627,11 @@ text text la la
% for x in range(1,5):
% endif
"""
- try:
- nodes = Lexer(template).parse()
- assert False
- except exceptions.SyntaxException, e:
- assert str(e) == "Keyword 'endif' doesn't match keyword 'for' at line: 5 char: 1"
+ assert_raises_message(
+ exceptions.SyntaxException,
+ "Keyword 'endif' doesn't match keyword 'for' at line: 5 char: 1",
+ Lexer(template).parse
+ )
def test_unmatched_control_2(self):
template = """
@@ -504,11 +640,12 @@ text text la la
% for x in range(1,5):
% endfor
"""
- try:
- nodes = Lexer(template).parse()
- assert False
- except exceptions.SyntaxException, e:
- assert str(e) == "Unterminated control keyword: 'if' at line: 3 char: 1"
+
+ assert_raises_message(
+ exceptions.SyntaxException,
+ "Unterminated control keyword: 'if' at line: 3 char: 1",
+ Lexer(template).parse
+ )
def test_unmatched_control_3(self):
template = """
@@ -518,14 +655,15 @@ text text la la
% endlala
% endif
"""
- try:
- nodes = Lexer(template).parse()
- assert False
- except exceptions.SyntaxException, e:
- assert str(e) == "Keyword 'endlala' doesn't match keyword 'for' at line: 5 char: 1"
+ assert_raises_message(
+ exceptions.SyntaxException,
+ "Keyword 'endlala' doesn't match keyword 'for' at line: 5 char: 1",
+ Lexer(template).parse
+ )
def test_ternary_control(self):
- template = """
+ template = \
+ """
% if x:
hi
% elif y+7==10:
@@ -535,15 +673,22 @@ text text la la
% else:
hi
% 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(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"/>
+ template = \
+ """<%namespace name="foo" file="somefile.html"/>
## inherit from foobar.html
<%inherit file="foobar.html"/>
@@ -565,36 +710,76 @@ 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(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 = """
+ template = \
+ """
% if x: #comment
hi
% else: #next
hi
% 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(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 = open(self._file_path("crlf.html"), 'rb').read()
nodes = Lexer(template).parse()
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))])
+ 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))
+ ])
)
- assert flatten_result(Template(template).render()) == """<html> like the name says. 1 2 3 Dizzy </html>"""
+ assert flatten_result(Template(template).render()) \
+ == """<html> like the name says. 1 2 3 Dizzy </html>"""
def test_comments(self):
- template = """
+ template = \
+ """
<style>
#someselector
# other non comment stuff
@@ -614,13 +799,22 @@ comment
hi
"""
nodes = Lexer(template).parse()
- 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))])
- )
+ 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'''
+
+hi
+''', (16, 8))]))
def test_docs(self):
- template = """
+ template = \
+ """
<%doc>
this is a comment
</%doc>
@@ -631,22 +825,30 @@ hi
</%def>
"""
nodes = Lexer(template).parse()
- 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))])
- )
+ 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):
- return re.sub(r'(?<=\n)\s*#[^#]', "##", text)
- template = """
+ return re.sub(r'(?<=\n)\s*#[^#]', '##', text)
+
+ template = \
+ """
hi
# old style comment
# 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(u'''\n hi\n''',
+ (1, 1)), Comment(u'old style comment', (3, 1)),
+ Comment(u'another comment', (4, 1))]))