# HG changeset patch # User cmlenz # Date 1156189867 0 # Node ID e103b75a96cecb231b20c9845663caf6eb0ec628 # Parent 061491fb4ea852dcc3e0f454741be8ed5bbf71a4 Some error message improvements for template directives. Thanks to Christian Boos for the patch! diff --git a/ChangeLog b/ChangeLog --- 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 diff --git a/markup/template.py b/markup/template.py --- a/markup/template.py +++ b/markup/template.py @@ -61,8 +61,7 @@ """ def __init__(self, name, filename='', 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 [] diff --git a/markup/tests/template.py b/markup/tests/template.py --- a/markup/tests/template.py +++ b/markup/tests/template.py @@ -134,7 +134,7 @@ """) 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 @@ """) 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(""" +
+ foo +
+
""") + 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(""" +
+ foo +
+
""") + self.assertEqual(""" + foo + """, str(tmpl.generate())) + def test_as_element(self): """ Verify that the directive can also be used as an element.