diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-12-13 23:15:02 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-12-13 23:15:02 +0000 |
commit | c1269a52f9df2272740939b115c73620498eef03 (patch) | |
tree | c446837b1097f51ed17e0d88ad1e0d3c04cf1afa /test/lexer.py | |
parent | 9f8da9afda795bfcc8a2dee457ed479f6e5358df (diff) | |
download | external_python_mako-c1269a52f9df2272740939b115c73620498eef03.tar.gz external_python_mako-c1269a52f9df2272740939b115c73620498eef03.tar.bz2 external_python_mako-c1269a52f9df2272740939b115c73620498eef03.zip |
<%def> tag now requires () in the name, i.e. <%def name="foo()">
Diffstat (limited to 'test/lexer.py')
-rw-r--r-- | test/lexer.py | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/test/lexer.py b/test/lexer.py index 8b7fe67..e51947b 100644 --- a/test/lexer.py +++ b/test/lexer.py @@ -7,19 +7,19 @@ class LexerTest(unittest.TestCase): def test_text_and_tag(self): template = """ <b>Hello world</b> - <%def name="foo"> + <%def name="foo()"> this is a def. </%def> and some more text. """ node = Lexer(template).parse() - assert repr(node) == r"""TemplateNode({}, [Text('\n<b>Hello world</b>\n ', (1, 1)), DefTag('def', {'name': 'foo'}, (3, 9), ["Text('\\n this is a def.\\n ', (3, 26))"]), Text('\n \n and some more text.\n', (5, 16))])""" + assert repr(node) == r"""TemplateNode({}, [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('\n \n and some more text.\n', (5, 16))])""" def test_unclosed_tag(self): template = """ - <%def name="foo"> + <%def name="foo()"> other text """ try: @@ -30,7 +30,7 @@ class LexerTest(unittest.TestCase): def test_onlyclosed_tag(self): template = """ - <%def name="foo"> + <%def name="foo()"> foo </%def> @@ -47,7 +47,7 @@ class LexerTest(unittest.TestCase): def test_unmatched_tag(self): template = """ <%namespace name="bar"> - <%def name="foo"> + <%def name="foo()"> foo </%namespace> </%def> @@ -82,19 +82,19 @@ class LexerTest(unittest.TestCase): % more code <%illegal compionent>/></> - <%def name="laal">def</%def> + <%def name="laal()">def</%def> </%text> - <%def name="foo">this is foo</%def> + <%def name="foo()">this is foo</%def> % if bar: code % endif """ node = Lexer(template).parse() - assert repr(node) == r"""TemplateNode({}, [Comment('comment', (1, 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, 26))"]), Text('\n', (16, 44)), ControlLine('if', 'if bar:', False, (17, 1)), Text(' code\n', (19, 1)), ControlLine('if', 'endif', True, (20, 1)), Text(' ', (21, 1))])""" + assert repr(node) == r"""TemplateNode({}, [Comment('comment', (1, 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', (16, 46)), ControlLine('if', 'if bar:', False, (17, 1)), Text(' code\n', (19, 1)), ControlLine('if', 'endif', True, (20, 1)), Text(' ', (21, 1))])""" def test_def_syntax(self): template = """ @@ -107,7 +107,19 @@ class LexerTest(unittest.TestCase): assert False except exceptions.CompileException, e: assert str(e) == "Missing attribute(s): 'name' at line: 2 char: 9" - + + def test_def_syntax_2(self): + template = """ + <%def name="lala"> + hi + </%def> + """ + try: + node = Lexer(template).parse() + assert False + except exceptions.CompileException, e: + assert str(e) == "Missing parenthesis in %def at line: 2 char: 9" + def test_expr_in_attribute(self): """test some slightly trickier expressions. @@ -157,10 +169,10 @@ class LexerTest(unittest.TestCase): def test_code_and_tags(self): template = """ <%namespace name="foo"> - <%def name="x"> + <%def name="x()"> this is x </%def> - <%def name="y"> + <%def name="y()"> this is y </%def> </%namespace> @@ -175,18 +187,18 @@ class LexerTest(unittest.TestCase): result: <%call expr="foo.x(result)"/> """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""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, 20))"])', "Text('\\n ', (5, 12))", 'DefTag(\'def\', {\'name\': \'y\'}, (6, 5), ["Text(\'\\\\n this is y\\\\n \', (6, 20))"])', "Text('\\n', (8, 12))"]), Text('\n\n', (9, 14)), Code('\nresult = []\ndata = get_data()\nfor x in data:\n result.append(x+7)\n\n', False, (11, 1)), Text('\n\n result: ', (16, 3)), CallTag('call', {'expr': 'foo.x(result)'}, (18, 13), []), Text('\n', (18, 42))])""" + assert repr(nodes) == r"""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()\nfor x in data:\n result.append(x+7)\n\n', 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 = """ this is some ${text} and this is ${textwith | escapes, moreescapes} - <%def name="hi"> + <%def name="hi()"> give me ${foo()} and ${bar()} </%def> ${hi()} """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [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, 25))", "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))])""" + assert repr(nodes) == r"""TemplateNode({}, [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 = """ @@ -281,10 +293,10 @@ text text la la # inherit from foobar.html <%inherit file="foobar.html"/> -<%def name="header"> +<%def name="header()"> <div>header</div> </%def> -<%def name="footer"> +<%def name="footer()"> <div> footer</div> </%def> @@ -299,7 +311,8 @@ text text la la </table> """ nodes = Lexer(template).parse() - assert repr(nodes) == r"""TemplateNode({}, [NamespaceTag('namespace', {'name': 'foo', 'file': 'somefile.html'}, (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, 21))"]), Text('\n', (7, 8)), DefTag('def', {'name': 'footer'}, (8, 1), ["Text('\\n <div> footer</div>\\n', (8, 21))"]), 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))])""" + assert repr(nodes) == r"""TemplateNode({}, [NamespaceTag('namespace', {'name': 'foo', 'file': 'somefile.html'}, (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))])""" + if __name__ == '__main__': unittest.main() |