Beispiel #1
0
 def test_class_in_init(self) -> None:
     tag = Tag(class_='a')
     self.assertIsTrue(tag.hasClass('a'))
     self.assertIsTrue(tag.hasClasses())
     self.assertRegex(tag.html, '<tag rimo_id="\d+" class="a"></tag>')
     tag.removeClass('a')
     self.assertIsFalse(tag.hasClass('a'))
     self.assertIsFalse(tag.hasClasses())
     self.assertRegex(tag.html, '<tag rimo_id="\d+"></tag>')
Beispiel #2
0
class TestTagBase(TestCase):
    def setUp(self):
        self.tag = Tag()
        self.c1 = Tag()
        self.c2 = Tag()

    def test_class_addremove(self):
        self.assertIsFalse(self.tag.hasClasses())
        self.assertIsFalse(self.tag.hasClass('a'))
        self.assertRegex(self.tag.html, '<tag rimo_id="\d+"></tag>')
        self.tag.addClass('a')
        self.assertIsTrue(self.tag.hasClasses())
        self.assertIsTrue(self.tag.hasClass('a'))
        self.assertIsFalse(self.tag.hasClass('b'))
        self.assertRegex(self.tag.html, '<tag rimo_id="\d+" class="a"></tag>')
        self.tag.removeClass('a')
        self.assertIsFalse(self.tag.hasClasses())
        self.assertIsFalse(self.tag.hasClass('a'))
        self.assertRegex(self.tag.html, '<tag rimo_id="\d+"></tag>')

    def test_class_in_init(self) -> None:
        tag = Tag(class_='a')
        self.assertIsTrue(tag.hasClass('a'))
        self.assertIsTrue(tag.hasClasses())
        self.assertRegex(tag.html, '<tag rimo_id="\d+" class="a"></tag>')
        tag.removeClass('a')
        self.assertIsFalse(tag.hasClass('a'))
        self.assertIsFalse(tag.hasClasses())
        self.assertRegex(tag.html, '<tag rimo_id="\d+"></tag>')

    def test_class_addremove_multi(self):
        self.tag.addClass('a', 'b', 'c')
        self.assertIsTrue(self.tag.hasClasses())
        self.assertIsTrue(self.tag.hasClass('a'))
        self.assertIsTrue(self.tag.hasClass('b'))
        self.assertIsTrue(self.tag.hasClass('c'))
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+" class="a b c"></tag>',
        )
        self.tag.removeClass('a', 'c')
        self.assertIsTrue(self.tag.hasClasses())
        self.assertIsFalse(self.tag.hasClass('a'))
        self.assertIsTrue(self.tag.hasClass('b'))
        self.assertIsFalse(self.tag.hasClass('c'))
        self.assertRegex(self.tag.html, '<tag rimo_id="\d+" class="b"></tag>')

    def test_class_addremove_multi_string(self):
        with self.assertRaises(ValueError):
            self.tag.addClass('a b')

    def test_class_getset(self) -> None:
        self.assertEqual(self.tag['class'], None)
        self.tag.addClass('a')
        self.assertEqual(self.tag['class'], 'a')
        self.tag['class'] = 'b'
        self.assertEqual(self.tag['class'], 'b')
        self.assertIsFalse(self.tag.hasClass('a'))
        self.assertIsTrue(self.tag.hasClass('b'))

    def test_class_remove_error(self) -> None:
        with self.assertLogs('wdom.tag', 'WARNING'):
            self.tag.removeClass('a')

    def test_type_class(self) -> None:
        class A(Tag):
            tag = 'input'
            type_ = 'button'
        a = A()
        self.assertRegex(a.html, '<input type="button" rimo_id="\d+">')

    def test_type_init(self) -> None:
        a = Tag(type='button')
        self.assertRegex(a.html, '<tag type="button" rimo_id="\d+"></tag>')

    def test_type_attr(self) -> None:
        a = Tag()
        a.setAttribute('type', 'checkbox')
        self.assertRegex(a.html, '<tag rimo_id="\d+" type="checkbox"></tag>')

    def test_type_setter(self) -> None:
        class Check(Tag):
            type_ = 'checkbox'
        a = Check()
        b = Check()
        c = Check()
        b['type'] = 'radio'
        c.setAttribute('type', 'text')
        d = Check()
        self.assertRegex(a.html, '<tag type="checkbox" rimo_id="\d+"></tag>')
        self.assertRegex(b.html, '<tag type="radio" rimo_id="\d+"></tag>')
        self.assertRegex(c.html, '<tag type="text" rimo_id="\d+"></tag>')
        self.assertRegex(d.html, '<tag type="checkbox" rimo_id="\d+"></tag>')

    def test_hidden(self):
        self.tag.show()
        self.assertRegex(self.tag.html, '<tag rimo_id="\d+"></tag>')
        self.tag.hide()
        self.assertRegex(self.tag.html, '<tag rimo_id="\d+" hidden></tag>')
        self.tag.show()
        self.assertRegex(self.tag.html, '<tag rimo_id="\d+"></tag>')

    def test_clone_node_sharrow_class(self):
        self.tag.appendChild(self.c1)
        self.tag.addClass('a')
        clone = self.tag.cloneNode()
        self.assertRegex(clone.html, '<tag rimo_id="\d+" class="a"></tag>')

        clone.removeClass('a')
        self.assertRegex(clone.html, '<tag rimo_id="\d+"></tag>')
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+"></tag></tag>',
        )

        clone.addClass('b')
        self.assertRegex(clone.html, '<tag rimo_id="\d+" class="b"></tag>')
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+"></tag></tag>',
        )

    def test_clone_node_sharrow_hidden(self):
        self.tag.hide()
        clone = self.tag.cloneNode()
        self.assertRegex(clone.html, '<tag rimo_id="\d+" hidden></tag>')
        clone.show()
        self.assertRegex(self.tag.html, '<tag rimo_id="\d+" hidden></tag>')
        self.assertRegex(clone.html, '<tag rimo_id="\d+"></tag>')

    def test_clone_node_deep_class(self):
        self.tag.appendChild(self.c1)
        self.tag.addClass('a')
        self.c1.addClass('b')
        clone = self.tag.cloneNode(deep=True)
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+" class="b">'
            '</tag></tag>',
        )
        self.assertRegex(
            clone.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+" class="b">'
            '</tag></tag>',
        )

        clone.childNodes[0].removeClass('b')
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+" class="b">'
            '</tag></tag>',
        )
        self.assertRegex(
            clone.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+"></tag></tag>',
        )

        self.c1.removeClass('b')
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+"></tag></tag>',
        )
        self.assertRegex(
            clone.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+"></tag></tag>',
        )

        clone.addClass('c')
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+"></tag></tag>',
        )
        self.assertRegex(
            clone.html,
            '<tag rimo_id="\d+" class="a c"><tag rimo_id="\d+"></tag></tag>',
        )

        clone.removeClass('a')
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+" class="a"><tag rimo_id="\d+"></tag></tag>',
        )
        self.assertRegex(
            clone.html,
            '<tag rimo_id="\d+" class="c"><tag rimo_id="\d+"></tag></tag>',
        )

    def test_clone_node_deep_hidden(self):
        self.tag.appendChild(self.c1)
        self.c1.hide()
        clone = self.tag.cloneNode(deep=True)
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+"><tag rimo_id="\d+" hidden></tag></tag>',
        )
        self.assertRegex(
            clone.html,
            '<tag rimo_id="\d+"><tag rimo_id="\d+" hidden></tag></tag>',
        )

        self.c1.show()
        self.assertRegex(
            self.tag.html,
            '<tag rimo_id="\d+"><tag rimo_id="\d+"></tag></tag>',
        )
        self.assertRegex(
            clone.html,
            '<tag rimo_id="\d+"><tag rimo_id="\d+" hidden></tag></tag>',
        )

    def test_class_of_class(self):
        class A(Tag):
            tag = 'a'
            class_ = 'a1'
        self.assertEqual(A.get_class_list().toString(), 'a1')
        a = A()
        self.assertRegex(a.html, '<a rimo_id="\d+" class="a1"></a>')
        a.addClass('a2')
        self.assertRegex(a.html, '<a rimo_id="\d+" class="a1 a2"></a>')
        with self.assertLogs('wdom.tag', 'WARNING'):
            a.removeClass('a1')
        self.assertRegex(a.html, '<a rimo_id="\d+" class="a1 a2"></a>')

    def test_classes_multiclass(self):
        class A(Tag):
            tag = 'a'
            class_ = 'a1 a2'
        self.assertEqual(A.get_class_list().toString(), 'a1 a2')
        a = A()
        a.addClass('a3', 'a4')
        self.assertRegex(a.html, '<a rimo_id="\d+" class="a1 a2 a3 a4"></a>')

    def test_classes_inherit_class(self):
        class A(Tag):
            tag = 'a'
            class_ = 'a1 a2'

        class B(A):
            tag = 'b'
            class_ = 'b1 b2'

        self.assertEqual(B.get_class_list().toString(), 'a1 a2 b1 b2')
        b = B()
        b.addClass('b3')
        self.assertRegex(
            b.html,
            '<b rimo_id="\d+" class="a1 a2 b1 b2 b3"></b>',
        )

    def test_classes_notinherit_class(self):
        class A(Tag):
            tag = 'a'
            class_ = 'a1 a2'

        class B(A):
            tag = 'b'
            class_ = 'b1 b2'
            inherit_class = False

        self.assertEqual(B.get_class_list().toString(), 'b1 b2')
        b = B()
        b.addClass('b3')
        self.assertRegex(b.html, '<b rimo_id="\d+" class="b1 b2 b3"></b>')

        class C(B):
            tag = 'c'
            class_ = 'c1 c2'
        self.assertEqual(C.get_class_list().toString(), 'b1 b2 c1 c2')