ezt: detect unmatched else tags (#11581)

This commit is contained in:
Frédéric Péters 2016-06-24 18:43:40 +02:00
parent 8187dba752
commit c4385ae8c6
3 changed files with 19 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import pytest
from StringIO import StringIO
from wcs.qommon.ezt import Template, UnclosedBlocksError, UnmatchedEndError
from wcs.qommon.ezt import Template, UnclosedBlocksError, UnmatchedEndError, UnmatchedElseError
def test_simple_qualifier():
template = Template()
@ -91,6 +91,15 @@ def test_unmatched_end():
except UnmatchedEndError as e:
assert e.column == 15 and e.line == 0
def test_unmatched_else():
template = Template()
with pytest.raises(UnmatchedElseError):
template.parse('<p>[else]</p>')
try:
template.parse('<p>[else]</p>')
except UnmatchedElseError as e:
assert e.column == 3 and e.line == 0
def test_array_index():
template = Template()
template.parse('<p>[foo.0]</p>')

View File

@ -377,7 +377,10 @@ class Template:
if len(args) > 1:
raise ArgCountSyntaxError(str(args[1:]), line, column)
### check: don't allow for 'for' cmd
idx = stack[-1][1]
try:
idx = stack[-1][1]
except IndexError:
raise UnmatchedElseError('', line, column)
true_section = program[idx:]
del program[idx:]
stack[-1][3] = true_section
@ -751,6 +754,9 @@ class UnclosedBlocksError(EZTException):
class UnmatchedEndError(EZTException):
"""This error may be caused by a misspelled if directive."""
class UnmatchedElseError(EZTException):
"""This error may be caused by a misspelled if directive."""
class BaseUnavailableError(EZTException):
"""Base location is unavailable, which disables includes."""

View File

@ -2263,11 +2263,12 @@ class ComputedExpressionWidget(StringWidget):
ezt.NeedSequenceError: _('sequence required'),
ezt.UnclosedBlocksError: _('unclosed block'),
ezt.UnmatchedEndError: _('unmatched [end]'),
ezt.UnmatchedElseError: _('unmatched [else]'),
ezt.BaseUnavailableError: _('unavailable base location'),
ezt.BadFormatConstantError: _('bad format constant'),
ezt.UnknownFormatConstantError: _('unknown format constant'),
}.get(e.__class__))
if e.line:
if e.line is not None:
parts.append(_('at line %(line)d and column %(column)d') % {
'line': e.line+1,
'column': e.column+1})