changeset 181:e103b75a96ce trunk

Some error message improvements for template directives. Thanks to Christian Boos for the patch!
author cmlenz
date Mon, 21 Aug 2006 19:51:07 +0000
parents 061491fb4ea8
children 2f30ce3fb85e
files ChangeLog markup/template.py markup/tests/template.py
diffstat 3 files changed, 41 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -29,6 +29,8 @@
  * Default values for arguments of template functions now also work with
    constants and complex expressions (they only worked for string or number
    literals before).
+ * XPath expressions in now support XPath variables ($var) in predicates
+   (ticket #31).
 
 
 Version 0.1
--- a/markup/template.py
+++ b/markup/template.py
@@ -61,8 +61,7 @@
     """
 
     def __init__(self, name, filename='<string>', lineno=-1):
-        msg = 'bad directive "%s" (%s, line %d)' % (name.localname, filename,
-                                                    lineno)
+        msg = 'bad directive "%s"' % name.localname
         TemplateSyntaxError.__init__(self, msg, filename, lineno)
 
 
@@ -374,6 +373,9 @@
     ATTRIBUTE = 'each'
 
     def __init__(self, value, filename=None, lineno=-1, offset=-1):
+        if ' in ' not in value:
+            raise TemplateSyntaxError('"in" keyword missing in "for" directive',
+                                      filename, lineno, offset)
         targets, value = value.split(' in ', 1)
         self.targets = [str(name.strip()) for name in targets.split(',')]
         Directive.__init__(self, value.strip(), filename, lineno, offset)
@@ -606,10 +608,14 @@
     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])
+            raise TemplateSyntaxError('"when" directives can only be used '
+                                      'inside a "choose" directive',
+                                      *stream.next()[2])
         if choose.matched:
             return []
+        if not self.expr:
+            raise TemplateSyntaxError('"when" directive has no test condition',
+                                      *stream.next()[2])
         value = self.expr.evaluate(ctxt)
         try:
             if value == choose.value:
@@ -631,8 +637,8 @@
     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',
+            raise TemplateSyntaxError('an "otherwise" directive can only be '
+                                      'used inside a "choose" directive',
                                       *stream.next()[2])
         if choose.matched:
             return []
--- a/markup/tests/template.py
+++ b/markup/tests/template.py
@@ -134,7 +134,7 @@
         </doc>""")
         self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
 
-    def test_when_outside_choose(self):
+    def test_otherwise_outside_choose(self):
         """
         Verify that an `otherwise` directive outside of a `choose` directive is
         reported as an error.
@@ -144,6 +144,32 @@
         </doc>""")
         self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
 
+    def test_when_without_test(self):
+        """
+        Verify that an `when` directive that doesn't have a `test` attribute
+        is reported as an error.
+        """
+        tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
+          <div py:choose="" py:strip="">
+            <py:when>foo</py:when>
+          </div>
+        </doc>""")
+        self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
+
+    def test_otherwise_without_test(self):
+        """
+        Verify that an `otherwise` directive can be used without a `test`
+        attribute.
+        """
+        tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
+          <div py:choose="" py:strip="">
+            <py:otherwise>foo</py:otherwise>
+          </div>
+        </doc>""")
+        self.assertEqual("""<doc>
+            foo
+        </doc>""", 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