diff options
-rw-r--r-- | coverage/templite.py | 2 | ||||
-rw-r--r-- | test/test_templite.py | 22 |
2 files changed, 23 insertions, 1 deletions
diff --git a/coverage/templite.py b/coverage/templite.py index ec845076..2eab864a 100644 --- a/coverage/templite.py +++ b/coverage/templite.py @@ -40,7 +40,7 @@ class Templite(object): """Convert Django-style data references into Python-native ones.""" # Pull out loops. text = re.sub( - r"{% for ([a-z0-9_]+) in ([a-z0-9_.|]+) %}(.*){% endfor %}", + r"(?s){% for ([a-z0-9_]+) in ([a-z0-9_.|]+) %}(.*?){% endfor %}", self._loop_repl, text ) # Protect actual percent signs in the text. diff --git a/test/test_templite.py b/test/test_templite.py index 962aebb1..20f5fe77 100644 --- a/test/test_templite.py +++ b/test/test_templite.py @@ -8,6 +8,9 @@ class AnyOldObject: class TemplateTest(unittest.TestCase): + def try_render(self, text, ctx, result): + self.assertEqual(Templite(text).render(ctx), result) + def test_passthrough(self): # Strings without variables are passed through unchanged. self.assertEqual(Templite("Hello").render(), "Hello") @@ -95,6 +98,25 @@ class TemplateTest(unittest.TestCase): render(locals()), "Look: 4, 3, 2, 1, done." ) + def test_empty_loops(self): + self.assertEqual( + Templite("Empty: {% for n in nums %}{{n}}, {% endfor %}done."). + render({'nums':[]}), "Empty: done." + ) + + def test_multiline_loops(self): + self.assertEqual( + Templite("Look: \n{% for n in nums %}\n{{n}}, \n{% endfor %}done."). + render({'nums':[1,2,3]}), "Look: \n\n1, \n\n2, \n\n3, \ndone." + ) + + def test_multiple_loops(self): + self.try_render( + "{% for n in nums %}{{n}}{% endfor %} and {% for n in nums %}{{n}}{% endfor %}", + {'nums': [1,2,3]}, + "123 and 123" + ) + if __name__ == '__main__': unittest.main() |