Пример #1
0
def add_class_attributes(component: Component, *values):
    if not component.get_attribute("class"):
        component.add_attribute(Attribute("class"))

    class_attribute = component.get_attribute("class")
    for value in values:
        class_attribute.add_value(value)
Пример #2
0
    def test_add_class_attributes(self):
        c = Component("div")
        self.assertTrue(c.get_attribute("class") is None)

        utils.add_class_attributes(c, CLASS1, CLASS2)
        self.assertTrue(c.get_attribute("class") is not None)
        self.assertTrue(CLASS1 in c.get_attribute("class").values)
        self.assertTrue(CLASS2 in c.get_attribute("class").values)
Пример #3
0
    def test_html_body(self):
        c = Component(COMPONENT_TAG)
        self.c.add_component(c)
        self.assertTrue(c in self.c.components)

        id = c.get_attribute("id").get_values()
        self.assertTrue(id in self.c.html_body())
Пример #4
0
    def test_del_component(self):
        c = Component(COMPONENT_TAG)
        self.c.add_component(c)
        self.assertTrue(c in self.c.components)

        id = c.get_attribute("id").get_values()
        self.c.del_component(id)
        self.assertFalse(c in self.c.components)
Пример #5
0
    def test_add_component(self):
        c = Component(COMPONENT_TAG)
        self.assertFalse(c.get_attribute("id"))
        self.assertFalse(c in self.c.components)

        # Test that ID is added to components
        self.c.add_component(c)
        self.assertTrue(c.get_attribute("id"))
        self.assertTrue(c in self.c.components)

        # Test that indent is updated on nested objects
        c1 = Container()
        c2 = Container()
        c1.add_component(c2)
        self.assertTrue(c1.indent == c2.indent-1)
        self.c.add_component(c1)
        self.assertTrue(self.c.indent == c1.indent-1)
        self.assertTrue(c1.indent == c2.indent-1)
Пример #6
0
    def test_get_component(self):
        component = Component(COMPONENT_TAG)
        container = Container()
        container.add_component(component)

        self.c.add_component(container)
        self.assertTrue(component in container.components)
        self.assertTrue(container in self.c.components)

        id = component.get_attribute("id").get_values()
        get_c = self.c.get_component(id)
        self.assertTrue(component is get_c)
        self.assertTrue(self.c.get_component('OBJECT_NOT_FOUND') is None)
Пример #7
0
class TestComponent(unittest.TestCase):
    def setUp(self):
        self.c = Component(
            COMPONENT_TAG,
            text=COMPONENT_TEXT,
            inline=COMPONENT_INLINE,
            indent=COMPONENT_INDENT,
            **{COMPONENT_ATTRIBUTE: COMPONENT_ATTRIBUTE_VALUE1}
        )

    def test_init(self):
        self.assertTrue(hasattr(self.c, "tag"))
        self.assertTrue(self.c.tag == COMPONENT_TAG)

        self.assertTrue(hasattr(self.c, "text"))
        self.assertTrue(self.c.text == COMPONENT_TEXT)

        self.assertTrue(hasattr(self.c, "inline"))
        self.assertTrue(self.c.inline == COMPONENT_INLINE)

        self.assertTrue(hasattr(self.c, "indent"))
        self.assertTrue(self.c.indent == COMPONENT_INDENT)

        self.assertTrue(hasattr(self.c, "attributes"))
        self.assertTrue(COMPONENT_ATTRIBUTE in self.c.attributes)

    def test_add_attribute(self):
        a = Attribute(ATTRIBUTE_NAME, ATTRIBUTE_VALUE1)

        self.assertFalse(a in self.c.attributes)
        self.c.add_attribute(a)
        self.assertTrue(a in self.c.attributes)

        # Test adding existing attribute appends values
        a = Attribute(ATTRIBUTE_NAME, ATTRIBUTE_VALUE2)
        self.c.add_attribute(a)
        self.assertTrue(ATTRIBUTE_VALUE1 in self.c.get_attribute(a).values)
        self.assertTrue(ATTRIBUTE_VALUE2 in self.c.get_attribute(a).values)

    def test_del_attribute(self):
        a = Attribute(ATTRIBUTE_NAME, ATTRIBUTE_VALUE1)
        self.c.add_attribute(a)

        self.assertTrue(ATTRIBUTE_NAME in self.c.attributes)
        self.c.del_attribute(ATTRIBUTE_NAME)
        self.assertFalse(ATTRIBUTE_NAME in self.c.attributes)

    def test_add_attributes(self):
        self.c.del_attribute(ATTRIBUTE_NAME)

        self.assertFalse(ATTRIBUTE_NAME in self.c.attributes)
        self.c.add_attributes(**{ATTRIBUTE_NAME: ATTRIBUTE_VALUE1})
        self.assertTrue(ATTRIBUTE_NAME in self.c.attributes)

    def test_get_attribute(self):
        a = self.c.get_attribute(COMPONENT_ATTRIBUTE)
        self.assertTrue(a is not None)
        self.assertTrue(isinstance(a, Attribute))
        self.assertTrue(a.name == COMPONENT_ATTRIBUTE)

    def test_clear_attributes(self):
        self.assertTrue(self.c.attributes)
        self.c.clear_attributes()
        self.assertFalse(self.c.attributes)

    def test_str(self):
        html_open = self.c.html_open()
        html_body = self.c.html_body()
        html_close = self.c.html_close()

        self.assertTrue(html_open in str(self.c))
        self.assertTrue(html_body in str(self.c))
        self.assertTrue(html_close in str(self.c))