changeset 166:d43f50402cf2 trunk

Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch.
author cmlenz
date Thu, 17 Aug 2006 14:53:28 +0000
parents 54a4be707664
children 1999291f7a30
files markup/template.py markup/tests/template.py
diffstat 2 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/markup/template.py
+++ b/markup/template.py
@@ -152,6 +152,8 @@
         try:
             self.expr = value and Expression(value, filename, lineno) or None
         except SyntaxError, err:
+            err.msg += ' in expression "%s" of "%s" directive' % (value,
+                                                                  self.name)
             raise TemplateSyntaxError(err, filename, lineno,
                                       offset + (err.offset or 0))
 
@@ -164,6 +166,11 @@
             expr = ' "%s"' % self.expr.source
         return '<%s%s>' % (self.__class__.__name__, expr)
 
+    def name(self):
+        """Return the local name of the directive as it is used in templates."""
+        return self.__class__.__name__.lower().replace('directive', '')
+    name = property(name)
+
 
 def _apply_directives(stream, ctxt, directives):
     """Apply the given directives to the stream."""
@@ -593,6 +600,9 @@
 
     def __call__(self, stream, ctxt, directives):
         choose = ctxt['_choose']
+        if not choose:
+            raise TemplateSyntaxError('when directives can only be used inside '
+                                      'a choose directive', *stream.next()[2])
         if choose.matched:
             return []
         value = self.expr.evaluate(ctxt)
@@ -615,6 +625,10 @@
     """
     def __call__(self, stream, ctxt, directives):
         choose = ctxt['_choose']
+        if not choose:
+            raise TemplateSyntaxError('an otherwise directive can only be used '
+                                      'inside a choose directive',
+                                      *stream.next()[2])
         if choose.matched:
             return []
         choose.matched = True
--- a/markup/tests/template.py
+++ b/markup/tests/template.py
@@ -124,6 +124,26 @@
             <span>foo</span>
         </doc>""", str(tmpl.generate()))
 
+    def test_when_outside_choose(self):
+        """
+        Verify that a `when` directive outside of a `choose` directive is
+        reported as an error.
+        """
+        tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
+          <div py:when="xy" />
+        </doc>""")
+        self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
+
+    def test_when_outside_choose(self):
+        """
+        Verify that an `otherwise` directive outside of a `choose` directive is
+        reported as an error.
+        """
+        tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
+          <div py:otherwise="" />
+        </doc>""")
+        self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
+
     def test_as_element(self):
         """
         Verify that the directive can also be used as an element.
Copyright (C) 2012-2017 Edgewall Software