コード例 #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
ファイル: test_core.py プロジェクト: babueter/pybootstrap
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))