import unittest
from xml.etree.ElementTree import Element, SubElement

import exactly_lib.util.textformat.rendering.html.utils
from exactly_lib.util.textformat.rendering.html import utils as sut
from exactly_lib_test.util.textformat.rendering.html.test_resources import as_unicode_str


def suite() -> unittest.TestSuite:
    return unittest.makeSuite(TestComplexElementPopulator)


class TestComplexElementPopulator(unittest.TestCase):
    def test_simple_document(self):
        # ARRANGE #
        root = Element('root')
        complex_populator = sut.ComplexElementPopulator([
            SingleParaPopulator('first'),
            SingleParaPopulator('second'),
        ])
        # ACT #
        complex_populator.apply(root)
        # ASSERT #
        xml_string = as_unicode_str(root)
        self.assertEqual('<root>'
                         '<p>first</p>'
                         '<p>second</p>'
                         '</root>',
                         xml_string)


class SingleParaPopulator(exactly_lib.util.textformat.rendering.html.utils.ElementPopulator):
    def __init__(self, para_text: str):
        self.para_text = para_text

    def apply(self, parent: Element):
        SubElement(parent, 'p').text = self.para_text


if __name__ == '__main__':
    unittest.TextTestRunner().run(suite())
