コード例 #1
0
    def __str__(self):
        bootstrap = Component(
            "link",
            rel="stylesheet",
            href=self.BOOTSTRAP_URL,
            integrety=self.BOOTSTRAP_INTEGRITY,
            crossorigin="anonymous",
        )
        meta_charset = Component("meta", charset=self.META_CHARSET)
        meta_viewport = Component(
            "meta",
            name="viewport",
            content=self.META_VIEWPORT_CONTENT,
        )
        text = '<!doctype html>\n'
        text += '<html lang={}>\n'.format(self.LANG)
        text += '    <head>\n'
        text += '        <!-- Required met tags -->\n'
        text += '        {}\n'.format(meta_charset)
        text += '        {}\n'.format(meta_viewport)
        text += '\n'
        text += '        <!-- Bootstrap css -->\n'
        text += '        {}\n'.format(bootstrap.html_open())

        for script in self.scripts:
            text += '        ' + str(script)

        text += '\n'
        text += '        <title>{}</title>\n'.format(self.title)
        text += '    </head>\n'
        text += '    <body>\n'

        return text
コード例 #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))