All Downloads are FREE. Search and download functionalities are using the official Maven repository.

robotframework-2.7.7.utest.utils.test_htmlwriter.py Maven / Gradle / Ivy

The newest version!
import os
import unittest
from StringIO import StringIO

from robot.utils import HtmlWriter
from robot.utils.asserts import assert_equals


class TestHtmlWriter(unittest.TestCase):

    def setUp(self):
        self.output = StringIO()
        self.writer = HtmlWriter(self.output)

    def test_start(self):
        self.writer.start('r')
        self._verify('\n')

    def test_start_without_newline(self):
        self.writer.start('robot', newline=False)
        self._verify('')

    def test_start_with_attribute(self):
        self.writer.start('robot', {'name': 'Suite1'}, False)
        self._verify('')

    def test_start_with_attributes(self):
        self.writer.start('test', {'class': '123', 'x': 'y', 'a': 'z'})
        self._verify('\n')

    def test_start_with_non_ascii_attributes(self):
        self.writer.start('test', {'name': u'\xA7', u'\xE4': u'\xA7'})
        self._verify(u'\n')

    def test_start_with_quotes_in_attribute_value(self):
        self.writer.start('x', {'q':'"', 'qs': '""""', 'a': "'"}, False)
        self._verify('')

    def test_start_with_html_in_attribute_values(self):
        self.writer.start('x', {'1':'<', '2': '&', '3': ''}, False)
        self._verify('')

    def test_start_with_newlines_and_tabs_in_attribute_values(self):
        self.writer.start('x', {'1':'\n', '3': 'A\nB\tC', '2': '\t', '4': '\r\n'}, False)
        self._verify('')

    def test_end(self):
        self.writer.start('robot', newline=False)
        self.writer.end('robot')
        self._verify('\n')

    def test_end_without_newline(self):
        self.writer.start('robot', newline=False)
        self.writer.end('robot', newline=False)
        self._verify('')

    def test_end_alone(self):
        self.writer.end('suite', newline=False)
        self._verify('')

    def test_content(self):
        self.writer.start('robot')
        self.writer.content('Hello world!')
        self._verify('\nHello world!')

    def test_content_with_non_ascii_data(self):
        self.writer.start('robot', newline=False)
        self.writer.content(u'Circle is 360\xB0. ')
        self.writer.content(u'Hyv\xE4\xE4 \xFC\xF6t\xE4!')
        self.writer.end('robot', newline=False)
        expected = u'Circle is 360\xB0. Hyv\xE4\xE4 \xFC\xF6t\xE4!'
        self._verify('%s' % expected)

    def test_multiple_content(self):
        self.writer.start('robot')
        self.writer.content('Hello world!')
        self.writer.content('Hi again!')
        self._verify('\nHello world!Hi again!')

    def test_content_with_chars_needing_escaping(self):
        self.writer.content('Me, "Myself" & I > U')
        self._verify('Me, "Myself" & I > U')

    def test_content_alone(self):
        self.writer.content('hello')
        self._verify('hello')

    def test_none_content(self):
        self.writer.start('robot')
        self.writer.content(None)
        self.writer.content('')
        self._verify('\n')

    def test_element(self):
        self.writer.element('div', 'content', {'id': '1'})
        self.writer.element('i', newline=False)
        self._verify('
content
\n') def test_line_separator(self): self._test_line_separator('\n') self._test_line_separator('LINESEP') def _test_line_separator(self, linesep): output = StringIO() writer = HtmlWriter(output, line_separator=linesep) writer.start('b') writer.end('b') writer.element('i') expected = '%(LS)s%(LS)s%(LS)s' % {'LS': linesep} assert_equals(repr(output.getvalue()), repr(expected)) def test_encoding(self): self._test_encoding('UTF-8') self._test_encoding('ISO-8859-1') def _test_encoding(self, encoding): self.output = StringIO() writer = HtmlWriter(self.output, encoding=encoding) writer.start(u'p', attrs={'name': u'hyv\xe4\xe4'}, newline=False) writer.content(u'y\xf6') writer.element('i', u't\xe4', newline=False) writer.end('p', newline=False) self._verify(u'

y\xf6t\xe4

'.encode(encoding)) def _verify(self, expected): actual = self.output.getvalue() if '\n' in expected: expected = expected.replace('\n', os.linesep) assert_equals(actual, expected) if __name__ == '__main__': unittest.main()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy