tests: add some tests for ezt templates
This commit is contained in:
parent
c6e89c6ee0
commit
e7cdf7afba
|
@ -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
|
Loading…
Reference in New Issue