93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
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
|