def test_tagClassMethods(self):
        '''
            test_tagClassMethods - test class methods like addClass, removeClass, hasClass
        '''

        tag = AdvancedTag('div')

        ret = tag.removeClass('blah')
        assert ret is None , "Expected to get None from tag.removeClass trying to remove non-existant class. Got: " + repr(ret)

        # Set initial classes
        tag.className = "hello world"

        # Add a class
        tag.addClass("welcome")

        assert 'class="hello world welcome"' in str(tag) , "Expected addClass to add class, but did not change HTML representation. Got: " + str(tag)

        assert tag.className == "hello world welcome" , "Expected addClass to add class, but did not change className property. Got: " + repr(tag.className)

        assert tag.classList == ['hello', 'world', 'welcome'] , "Expected addClass to add class, but did not return expected ordered classList. Got: " + repr(tag.classList)

        assert tag.hasClass("hello") , "Expected hasClass('hello') to return True."
        assert tag.hasClass("world") , "Expected hasClass('world') to return True."
        assert tag.hasClass("welcome") , "Expected hasClass('welcome') to return True."
        assert not tag.hasClass("blah") , "Expected hasClass('blah') to return False."


        # Remove middle class

        tag.removeClass("world")

        assert 'class="hello welcome"' in str(tag) , "Expected removeClass to remove class, but did not change HTML representation. Got: " + str(tag)

        assert tag.className == "hello welcome" , "Expected removeClass to remove class, but did not change className property. Got: " + repr(tag.className)

        assert tag.classList == ["hello", "welcome" ], "Expected removeClass to remove class, but did get expected classList. Got: " + repr(tag.classList)


        # Try to add a duplicate class

        tag.addClass("hello")
        assert 'class="hello welcome"' in str(tag) , "Expected addClass to not add duplicate class, but changed HTML representation. Got: " + str(tag)

        assert tag.className == "hello welcome" , "Expected addClass to not add duplicate class, but changed className property. Got: " + repr(tag.className)

        assert tag.classList == ['hello', 'welcome' ] , "Expected addClass to not add duplicate class, but did not return expected ordered classList. Got: " + repr(tag.classList)
示例#2
0
    def test_classNames(self):
        tag = AdvancedTag('div')
        tag.addClass('abc')

        assert tag.hasClass('abc'), 'Failed to add class'
        assert 'abc' in tag.outerHTML, 'Failed to add class in outerHTML'

        tag.addClass('def')

        assert tag.hasClass('abc'), 'Failed to retain class'
        assert 'abc' in tag.outerHTML, ' Failed to retain in outerHTML'

        assert tag.hasClass('def'), 'Failed to add second class'
        assert 'def' in tag.outerHTML, ' Failed to add to outerHTML'

        tag.removeClass('abc')
        assert not tag.hasClass('abc'), 'Failed to remove class'
        assert 'abc' not in tag.outerHTML, 'Failed to remove class from outerHTML'

        assert tag.hasClass('def'), 'Failed to retain class'
        assert 'def' in tag.outerHTML, ' Failed to retain in outerHTML'
    def test_classNames(self):
        tag = AdvancedTag('div')
        tag.addClass('abc')

        assert tag.hasClass('abc'), 'Failed to add class'
        assert 'abc' in tag.outerHTML , 'Failed to add class in outerHTML'

        tag.addClass('def')

        assert tag.hasClass('abc'), 'Failed to retain class'
        assert 'abc' in tag.outerHTML , ' Failed to retain in outerHTML'

        assert tag.hasClass('def'), 'Failed to add second class'
        assert 'def' in tag.outerHTML , ' Failed to add to outerHTML'

        tag.removeClass('abc')
        assert not tag.hasClass('abc'), 'Failed to remove class'
        assert 'abc' not in tag.outerHTML , 'Failed to remove class from outerHTML'

        assert tag.hasClass('def'), 'Failed to retain class'
        assert 'def' in tag.outerHTML , ' Failed to retain in outerHTML'
    def test_stripClassName(self):
        '''
            test_stripClassName - Ensure class names are properly stripped
        '''
        x = AdvancedTag('div')

        x.addClass("  hello")

        assert x.className == "hello" , "Expected className to be stripped after addClass. Got: " + repr(x.className)

        assert x.classList == ["hello"] , "Expected 'class' to be stripped in 'classList' after addClass. Got: " + repr(x.classList)


        x = AdvancedTag('div')

        x.className = "  hello   goodbye"

        assert x.className == "hello goodbye" , "Expected className to be stripped after setting .className. Got: " + repr(x.className)

        assert x.classList == ["hello", "goodbye"] , "Expected className to be stripped and empty strings removed on classList. Got: " + repr(x.classList)


        x.addClass("hello  ")

        assert x.className == "hello goodbye" , "Expected addClass to strip class name before trying to add so as to not duplicate. Got: " + repr(x.className)

        assert x.classList == ["hello", "goodbye"] , "Expected addClass to strip class name before trying to add so as to not duplicate. Got: " + repr(x.classList)


        x.addClass("")

        assert x.className == "hello goodbye" , "Expected addClass on empty string to not affect className. Got: " + repr(x.className)

        assert x.classList == ["hello", "goodbye"], "Expected addClass on empty string to not affect classList. Got: " + repr(x.classList)

        x.addClass("  ")

        assert x.className == "hello goodbye" , "Expected addClass on whitespace string to not affect className. Got: " + repr(x.className)

        assert x.classList == ["hello", "goodbye"], "Expected addClass on whitespace string to not affect classList. Got: " + repr(x.classList)

        x.removeClass("")

        assert x.className == "hello goodbye" , "Expected removeClass on empty string to not affect className. Got: " + repr(x.className)

        assert x.classList == ["hello", "goodbye"], "Expected removeClass on empty string to not affect classList. Got: " + repr(x.classList)

        x.removeClass(" ")

        assert x.className == "hello goodbye" , "Expected removeClass on whitespace string to not affect className. Got: " + repr(x.className)

        assert x.classList == ["hello", "goodbye"], "Expected removeClass on whitespace string to not affect classList. Got: " + repr(x.classList)

        x = AdvancedTag('div')

        x.className = "hello   goodbye  "

        assert x.className == "hello goodbye" , "Expected setting class with extra whitespace to have extra whitespace stripped. className attribute not as expected. Got: " + repr(x.className)

        assert x.classList == ["hello", "goodbye"], "Expected setting class with extra whitespace to have extra whitespace stripped. classList not as expected. Got: " + repr(x.className)

        x.addClass('cheddar butter')

        assert x.className == 'hello goodbye cheddar butter' , 'Expected tag.addClass("cheddar butter") to add both "cheddar" and "butter" classes, but .className attribute only shows: ' + repr(x.className)

        assert x.classList == ['hello', 'goodbye', 'cheddar', 'butter'] , 'Expected tag.addClass("cheddar butter") to add both "cheddar" and "butter" classes, but .classList attribute only shows: ' + repr(x.classList)


        x.removeClass("   hello    cheddar ")

        assert x.className == "goodbye butter" , "Expected tag.removeClass('   hello    cheddar ') to split and strip and remove both 'hello' and 'cheddar' classes. className only shows: " + repr(x.className)

        assert x.classList == ["goodbye", "butter"] , "Expected tag.removeClass('   hello    cheddar ') to split and strip and remove both 'hello' and 'cheddar' classes. classList only shows: " + repr(x.classList)
示例#5
0
    def test_delAttributes(self):
        '''
            test_delAttributes - Test deleting attributes
        '''
        tag = AdvancedTag('div')

        tag.setAttribute('id', 'abc')

        tag.style = 'display: block; float: left'

        tagHTML = tag.toHTML()

        assert 'id="abc"' in tagHTML, 'Expected id="abc" to be present in tag html. Got: ' + tagHTML
        assert 'style=' in tagHTML, 'Expected style attribute to be present in tag html. Got: ' + tagHTML

        assert tag.style.display == 'block', 'Expected style.display to be "block". Style is: ' + str(
            tag.style)
        assert tag.style.float == 'left', 'Expected style.float to be "left". Style is: ' + str(
            tag.style)

        del tag.attributes['id']

        tagHTML = tag.toHTML()

        assert not tag.id, "Expected id to be empty after deleting attribute. It is: " + repr(
            tag.id)
        assert 'id="abc"' not in tagHTML, 'Expected id attribute to NOT be present in tagHTML after delete. Got: ' + tagHTML

        del tag.attributes['style']

        tagHTML = tag.toHTML()

        assert 'style=' not in tagHTML, 'Expected style attribute to NOT be present in tagHTML after delete. Got: ' + tagHTML
        assert str(
            tag.style
        ) == '', 'Expected str(tag.style) to be empty string after delete of style attribute. It was: ' + repr(
            str(tag.style))
        assert tag.style.display == '', 'Expected display style property to be cleared after delete of style attribute. Style is: ' + str(
            tag.style)
        assert tag.style.float == '', 'Expected float style property to be cleared after delete of style attribute. Style is: ' + str(
            tag.style)

        tag.style = 'font-weight: bold; float: right'

        tagHTML = tag.toHTML()

        assert 'style=' in tagHTML, 'Expected after deleting style, then setting it again style shows up as HTML attr. Got: ' + tagHTML

        assert 'font-weight: bold' in tagHTML, 'Expected after deleting style, then setting it again the properties show up in the HTML "style" attribute. Got: ' + tagHTML

        assert id(tag.getAttribute('style', '')) == id(
            tag.style
        ), 'Expected after deleting style, then setting it again the attribute remains linked.'

        assert tag.style.fontWeight == 'bold', 'Expected after deleting style, then setting it again everything works as expected. Set style to "font-weight: bold; float: left" but on tag.style it is: ' + str(
            tag.style)

        tag.addClass('bold-item')
        tag.addClass('blue-font')

        assert 'bold-item' in tag.classList, 'Did addClass("bold-item") but did not find it in classList. classList is: ' + repr(
            tag.classList)
        assert 'blue-font' in tag.classList, 'Did addClass("blue-font") but did not find it in classList. classList is: ' + repr(
            tag.classList)

        classes = tag.getAttribute('class')

        assert 'bold-item' in classes and 'blue-font' in classes, 'Expected class attribute to contain both classes. Got: ' + str(
            classes)

        # This should call del tag._attributes['class']
        tag.removeAttribute('class')

        assert 'bold-item' not in tag.classList, 'After removing class, expected to not find classes in tag.classList. classList is: ' + repr(
            tag.classList)

        assert len(
            tag.classList
        ) == 0, 'Expected to have no classes in classList after delete. Got %d' % (
            len(tag.classList), )

        assert tag.getAttribute(
            'class'
        ) == '', 'Expected after removing class it would be an empty string'

        assert tag.getAttribute(
            'clazz'
        ) is None, 'Expected default getAttribute on non-set attribute to be None'
    def test_stripClassName(self):
        '''
            test_stripClassName - Ensure class names are properly stripped
        '''
        x = AdvancedTag('div')

        x.addClass("  hello")

        assert x.className == "hello", "Expected className to be stripped after addClass. Got: " + repr(
            x.className)

        assert x.classList == [
            "hello"
        ], "Expected 'class' to be stripped in 'classList' after addClass. Got: " + repr(
            x.classList)

        x = AdvancedTag('div')

        x.className = "  hello   goodbye"

        assert x.className == "hello goodbye", "Expected className to be stripped after setting .className. Got: " + repr(
            x.className)

        assert x.classList == [
            "hello", "goodbye"
        ], "Expected className to be stripped and empty strings removed on classList. Got: " + repr(
            x.classList)

        x.addClass("hello  ")

        assert x.className == "hello goodbye", "Expected addClass to strip class name before trying to add so as to not duplicate. Got: " + repr(
            x.className)

        assert x.classList == [
            "hello", "goodbye"
        ], "Expected addClass to strip class name before trying to add so as to not duplicate. Got: " + repr(
            x.classList)

        x.addClass("")

        assert x.className == "hello goodbye", "Expected addClass on empty string to not affect className. Got: " + repr(
            x.className)

        assert x.classList == [
            "hello", "goodbye"
        ], "Expected addClass on empty string to not affect classList. Got: " + repr(
            x.classList)

        x.addClass("  ")

        assert x.className == "hello goodbye", "Expected addClass on whitespace string to not affect className. Got: " + repr(
            x.className)

        assert x.classList == [
            "hello", "goodbye"
        ], "Expected addClass on whitespace string to not affect classList. Got: " + repr(
            x.classList)

        x.removeClass("")

        assert x.className == "hello goodbye", "Expected removeClass on empty string to not affect className. Got: " + repr(
            x.className)

        assert x.classList == [
            "hello", "goodbye"
        ], "Expected removeClass on empty string to not affect classList. Got: " + repr(
            x.classList)

        x.removeClass(" ")

        assert x.className == "hello goodbye", "Expected removeClass on whitespace string to not affect className. Got: " + repr(
            x.className)

        assert x.classList == [
            "hello", "goodbye"
        ], "Expected removeClass on whitespace string to not affect classList. Got: " + repr(
            x.classList)

        x = AdvancedTag('div')

        x.className = "hello   goodbye  "

        assert x.className == "hello goodbye", "Expected setting class with extra whitespace to have extra whitespace stripped. className attribute not as expected. Got: " + repr(
            x.className)

        assert x.classList == [
            "hello", "goodbye"
        ], "Expected setting class with extra whitespace to have extra whitespace stripped. classList not as expected. Got: " + repr(
            x.className)

        x.addClass('cheddar butter')

        assert x.className == 'hello goodbye cheddar butter', 'Expected tag.addClass("cheddar butter") to add both "cheddar" and "butter" classes, but .className attribute only shows: ' + repr(
            x.className)

        assert x.classList == [
            'hello', 'goodbye', 'cheddar', 'butter'
        ], 'Expected tag.addClass("cheddar butter") to add both "cheddar" and "butter" classes, but .classList attribute only shows: ' + repr(
            x.classList)

        x.removeClass("   hello    cheddar ")

        assert x.className == "goodbye butter", "Expected tag.removeClass('   hello    cheddar ') to split and strip and remove both 'hello' and 'cheddar' classes. className only shows: " + repr(
            x.className)

        assert x.classList == [
            "goodbye", "butter"
        ], "Expected tag.removeClass('   hello    cheddar ') to split and strip and remove both 'hello' and 'cheddar' classes. classList only shows: " + repr(
            x.classList)
    def test_tagClassMethods(self):
        '''
            test_tagClassMethods - test class methods like addClass, removeClass, hasClass
        '''

        tag = AdvancedTag('div')

        ret = tag.removeClass('blah')
        assert ret is None, "Expected to get None from tag.removeClass trying to remove non-existant class. Got: " + repr(
            ret)

        # Set initial classes
        tag.className = "hello world"

        # Add a class
        tag.addClass("welcome")

        assert 'class="hello world welcome"' in str(
            tag
        ), "Expected addClass to add class, but did not change HTML representation. Got: " + str(
            tag)

        assert tag.className == "hello world welcome", "Expected addClass to add class, but did not change className property. Got: " + repr(
            tag.className)

        assert tag.classList == [
            'hello', 'world', 'welcome'
        ], "Expected addClass to add class, but did not return expected ordered classList. Got: " + repr(
            tag.classList)

        assert tag.hasClass(
            "hello"), "Expected hasClass('hello') to return True."
        assert tag.hasClass(
            "world"), "Expected hasClass('world') to return True."
        assert tag.hasClass(
            "welcome"), "Expected hasClass('welcome') to return True."
        assert not tag.hasClass(
            "blah"), "Expected hasClass('blah') to return False."

        # Remove middle class

        tag.removeClass("world")

        assert 'class="hello welcome"' in str(
            tag
        ), "Expected removeClass to remove class, but did not change HTML representation. Got: " + str(
            tag)

        assert tag.className == "hello welcome", "Expected removeClass to remove class, but did not change className property. Got: " + repr(
            tag.className)

        assert tag.classList == [
            "hello", "welcome"
        ], "Expected removeClass to remove class, but did get expected classList. Got: " + repr(
            tag.classList)

        # Try to add a duplicate class

        tag.addClass("hello")
        assert 'class="hello welcome"' in str(
            tag
        ), "Expected addClass to not add duplicate class, but changed HTML representation. Got: " + str(
            tag)

        assert tag.className == "hello welcome", "Expected addClass to not add duplicate class, but changed className property. Got: " + repr(
            tag.className)

        assert tag.classList == [
            'hello', 'welcome'
        ], "Expected addClass to not add duplicate class, but did not return expected ordered classList. Got: " + repr(
            tag.classList)