secretary/test_secretary.py

65 lines
2.7 KiB
Python
Raw Normal View History

2013-08-17 10:54:53 -06:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
2013-08-17 10:54:53 -06:00
import os
from xml.dom.minidom import getDOMImplementation
from unittest import TestCase
2014-07-19 18:53:15 -06:00
from secretary import UndefinedSilently, pad_string, Renderer
2013-08-17 10:54:53 -06:00
def test_undefined_silently():
undefined = UndefinedSilently()
assert isinstance(undefined(), UndefinedSilently)
assert isinstance(undefined.attribute, UndefinedSilently)
assert str(undefined) == ''
def test_pad_string():
assert pad_string('TEST') == '0TEST'
assert pad_string('TEST', 4) == 'TEST'
assert pad_string(1) == '00001'
class RenderTestCase(TestCase):
def setUp(self):
root = os.path.dirname(__file__)
impl = getDOMImplementation()
template = os.path.join(root, 'simple_template.odt')
self.document = impl.createDocument(None, "some_tag", None)
2014-07-19 18:53:15 -06:00
self.engine = Renderer()
self.engine.render(template)
2013-08-17 10:54:53 -06:00
def test__unescape_entities(self):
test_samples = {
'{{ "greater_than_1" if 1>0 }}': '{{ "greater_than_1" if 1>0 }}',
'{{ "lower_than_1" if 1&lt;0 }}': '{{ "lower_than_1" if 1<0 }}',
'{{ if <text:s> multiple_spaces }}': '{{ if multiple_spaces }}',
'{{ if </text:s> multiple_spaces }}': '{{ if multiple_spaces }}',
'{{ if <text:s/> multiple_spaces }}': '{{ if multiple_spaces }}',
2015-04-29 16:31:15 -06:00
'{{ if <text:span/>[1,2,3]<text:span>&lt;</text:span>2 }}': '{{ if [1,2,3] < 2 }}',
}
for test, expect in test_samples.items():
assert self.engine._unescape_entities(test) == expect
def test__encode_escape_chars(self):
test_samples = {
'<text:a>\n</text:a>': '<text:a><text:line-break/></text:a>',
'<text:h>\n</text:h>': '<text:h><text:line-break/></text:h>',
'<text:p>\n</text:p>': '<text:p><text:line-break/></text:p>',
2014-08-17 19:33:14 -06:00
'<text:p>Hello\n</text:p>': '<text:p>Hello<text:line-break/></text:p>',
'<text:p>Hello\nWorld\n!</text:p>': '<text:p>Hello<text:line-break/>World<text:line-break/>!</text:p>',
'<text:ruby-base>\n</text:ruby-base>': '<text:ruby-base><text:line-break/></text:ruby-base>',
2014-08-17 19:33:14 -06:00
'<text:meta>\u0009</text:meta>': '<text:meta><text:tab/></text:meta>',
'<text:meta-field>\n</text:meta-field>': '<text:meta-field><text:line-break/></text:meta-field>',
}
for test, expect in test_samples.items():
assert self.engine._encode_escape_chars(test) == expect
2013-08-17 10:54:53 -06:00
def test_create_test_node(self):
2014-07-19 18:53:15 -06:00
assert self.engine.create_text_node(self.document, 'text').toxml() == 'text'
2013-08-17 10:54:53 -06:00
def test_create_text_span_node(self):
2014-07-19 18:53:15 -06:00
assert self.engine.create_text_span_node(self.document, 'text').toxml() == '<text:span>text</text:span>'
2013-08-17 10:54:53 -06:00