]> Devi Nivas Git - chevron.git/commitdiff
Handling missing opening tag
authorOjisama <ojisama.san@gmail.com>
Mon, 7 Aug 2017 16:45:25 +0000 (17:45 +0100)
committerOjisama <ojisama.san@gmail.com>
Mon, 7 Aug 2017 16:54:42 +0000 (17:54 +0100)
chevron/tokenizer.py
test_spec.py

index 4362eabda6a513c53371ebb72cc52f8cb154f0c2..50867356d921d082a23a872279610ac2f94a5e40 100644 (file)
@@ -208,7 +208,13 @@ def tokenize(template, def_ldel='{{', def_rdel='}}'):
         elif tag_type == 'end':
             # Then check to see if the last opened section
             # is the same as us
-            last_section = open_sections.pop()
+            try:
+                last_section = open_sections.pop()
+            except IndexError:
+                raise ChevronError('Trying to close tag "{0}"\n'
+                                   'Looks like it was not opened.\n'
+                                   'line {1}'
+                                   .format(tag_key, _CURRENT_LINE + 1))
             if tag_key != last_section:
                 # Otherwise we need to complain
                 raise ChevronError('Trying to close tag "{0}"\n'
index 4e043f3c38420fe48bdcbef7241b5b5fa86d6132..250f5478df2faacf72a4e0fc7d93c046ee6df2d9 100755 (executable)
@@ -215,6 +215,19 @@ class ExpandedCoverage(unittest.TestCase):
             except chevron.ChevronError as error:
                 self.assertEqual(error.msg, 'unclosed tag at line 3')
 
+    def test_no_opening_tag(self):
+        args = {
+            'template': 'oops, no opening tag {{/ closing_tag }}',
+            'data': {'foo': 'xx'}
+        }
+
+        try:
+            chevron.render(**args)
+        except chevron.ChevronError as error:
+            self.assertEqual(error.msg, 'Trying to close tag "closing_tag"\n'
+                                        'Looks like it was not opened.\n'
+                                        'line 2')
+
 
 # Run unit tests from command line
 if __name__ == "__main__":