tests: add some tests for ezt templates

This commit is contained in:
Frédéric Péters 2014-12-27 12:07:22 +01:00
parent c6e89c6ee0
commit e7cdf7afba
1 changed files with 92 additions and 0 deletions

92
tests/test_ezt.py Normal file
View File

@ -0,0 +1,92 @@
import pytest
from StringIO import StringIO
from wcs.qommon.ezt import Template, UnclosedBlocksError, UnmatchedEndError
def test_simple_qualifier():
template = Template()
template.parse('<p>[foo]</p>')
output = StringIO()
template.generate(output, {'foo': 'bar'})
assert output.getvalue() == '<p>bar</p>'
def test_simple_qualifier_missing_variable():
template = Template()
template.parse('<p>[foo]</p>')
output = StringIO()
template.generate(output, {})
assert output.getvalue() == '<p>[foo]</p>'
def test_if_any():
template = Template()
template.parse('<p>[if-any foo]bar[end]</p>')
# boolean
output = StringIO()
template.generate(output, {'foo': True})
assert output.getvalue() == '<p>bar</p>'
# no value
output = StringIO()
template.generate(output, {})
assert output.getvalue() == '<p></p>'
# defined but evaluating to False
output = StringIO()
template.generate(output, {'foo': False})
assert output.getvalue() == '<p>bar</p>'
def test_if_any_else():
template = Template()
template.parse('<p>[if-any foo]bar[else]baz[end]</p>')
output = StringIO()
template.generate(output, {'foo': True})
assert output.getvalue() == '<p>bar</p>'
output = StringIO()
template.generate(output, {})
assert output.getvalue() == '<p>baz</p>'
def test_is():
template = Template()
template.parse('<p>[is foo "bar"]bar[end]</p>')
# no value
output = StringIO()
template.generate(output, {})
assert output.getvalue() == '<p></p>'
# defined but other value
output = StringIO()
template.generate(output, {'foo': 'baz'})
assert output.getvalue() == '<p></p>'
# defined with correct value
output = StringIO()
template.generate(output, {'foo': 'bar'})
assert output.getvalue() == '<p>bar</p>'
def test_callable_qualifier():
template = Template()
template.parse('<p>[foo]</p>')
output = StringIO()
template.generate(output, {'foo': lambda x: x.write('bar')})
assert output.getvalue() == '<p>bar</p>'
def test_unclosed_block():
template = Template()
with pytest.raises(UnclosedBlocksError):
template.parse('<p>[if-any]Test</p>')
try:
template.parse('<p>[if-any]Test</p>')
except UnclosedBlocksError as e:
assert e.column == 19 and e.line == 0
def test_unmatched_end():
template = Template()
with pytest.raises(UnmatchedEndError):
template.parse('<p>[if foo]Test[end]</p>')
try:
template.parse('<p>[if foo]Test[end]</p>')
except UnmatchedEndError as e:
assert e.column == 15 and e.line == 0