Skip to content

Commit

Permalink
More error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 7, 2014
1 parent 3e0907e commit 1c42c78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 7 additions & 3 deletions template-engine/templite.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ def flush_output():
code.indent()
elif words[0].startswith('end'):
# Endsomething. Pop the ops stack.
if len(words) != 1:
self._syntax_error("Don't understand end", token)
end_what = words[0][3:]
if not ops_stack:
self._syntax_error("Too many ends", token)
start_what = ops_stack.pop()
if start_what != end_what:
self._syntax_error("Mismatched end tag", end_what)
Expand All @@ -177,14 +181,14 @@ def flush_output():
if token:
buffered.append(repr(token))

if ops_stack:
self._syntax_error("Unmatched action tag", ops_stack[-1])

flush_output()

for var_name in self.all_vars - self.loop_vars:
vars_code.add_line("c_%s = ctx[%r]" % (var_name, var_name))

if ops_stack:
self._syntax_error("Unmatched action tag", ops_stack[-1])

code.add_line("return ''.join(result)")
code.dedent()
self._render_function = code.get_globals()['render']
Expand Down
14 changes: 12 additions & 2 deletions template-engine/test_templite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for templite."""

import re
from templite import Templite, TempliteSyntaxError
from unittest import TestCase

Expand Down Expand Up @@ -32,7 +33,8 @@ def try_render(self, text, ctx=None, result=None):
self.assertEqual(actual, result)

def assertSynErr(self, msg):
return self.assertRaisesRegexp(TempliteSyntaxError, msg)
pat = "^" + re.escape(msg) + "$"
return self.assertRaisesRegexp(TempliteSyntaxError, pat)

def test_passthrough(self):
# Strings without variables are passed through unchanged.
Expand Down Expand Up @@ -250,7 +252,7 @@ def test_malformed_if(self):
with self.assertSynErr("Don't understand if: '{% if this or that %}'"):
self.try_render("Buh? {% if this or that %}hi!{% endif %}")

def test_malformed_for_(self):
def test_malformed_for(self):
with self.assertSynErr("Don't understand for: '{% for %}'"):
self.try_render("Weird: {% for %}loop{% endfor %}")
with self.assertSynErr("Don't understand for: '{% for x from y %}'"):
Expand All @@ -263,3 +265,11 @@ def test_bad_nesting(self):
self.try_render("{% if x %}X")
with self.assertSynErr("Mismatched end tag: 'for'"):
self.try_render("{% if x %}X{% endfor %}")
with self.assertSynErr("Too many ends: '{% endif %}'"):
self.try_render("{% if x %}{% endif %}{% endif %}")

def test_malformed_end(self):
with self.assertSynErr("Don't understand end: '{% end if %}'"):
self.try_render("{% if x %}X{% end if %}")
with self.assertSynErr("Don't understand end: '{% endif now %}'"):
self.try_render("{% if x %}X{% endif now %}")

0 comments on commit 1c42c78

Please sign in to comment.