def test_setClassNameString(self):
        '''
            test_setClassNameString - Test setting the "className" attribute on an AdvancedTag and it being reflected.
        '''

        tag = AdvancedTag('div')

        assert "class=" not in tag.getHTML() , "Expected to not find 'class=' when none is set. Got: " + tag.getHTML()

        assert 'class' not in tag.attributes , 'Expected "class" to not be "in" attributes'

        # Try initial set

        tag.className = "cheese is good"

        assert 'class' in tag.attributes , 'Expected "class" to be "in" attributes'


        assert tag.className == "cheese is good" , "Expected className to equal 'cheese is good' after assign on className attribute. Got: " + repr(tag.className)

        assert 'class="cheese is good"' in str(tag) , "Expected to find class=\"cheese is good\" after set on className attribute. Got: " + str(tag)

        assert tag.classList == ['cheese', 'is', 'good'] , "Expected classList to be set after setting on className attribute. Got: " + repr(tag.classList)

        assert 'class' in tag.attributes.keys() ,  'Expected "class" to be in .keys()'

        # Try changing

        tag.className = "hello world"

        assert tag.className == "hello world" , "Expected to be able to change className attribute. Got: " + repr(tag.className)

        assert 'class="hello world"' in str(tag) , "Expected to be able to change className and have it show up in html attribute. Got: " + str(tag)

        assert tag.classList == ['hello', 'world'] , "Expected to be able to change className attribute, but update not reflected in classList. Got: " + repr(tag.classList)

        # Try removing

        tag.className = ''

        assert tag.className == '' , "Expected to be able to clear className attribute. Got: " + repr(tag.className)

        assert "class=" not in str(tag) , "Expected class attribute to not be on HTML representation after clearing className. Got: " + str(tag)

        assert tag.classList == [] , "Expected to be able to clear className attribute, but did not update classList to empty list. Got: " + repr(tag.classList)
示例#2
0
    def test_specialAttributes(self):
        tag = AdvancedTag('div')
        tag.setAttribute('style', 'position: absolute')
        styleValue = str(tag.getAttribute('style'))
        styleValue = styleValue.strip()
        assert styleValue == 'position: absolute', 'Expected position: absolute, got %s' % (
            str(tag.getAttribute('style')), )

        tag.className = 'one two'
        assert str(tag.className).strip(
        ) == 'one two', 'Expected classname to be "one two", got %s' % (repr(
            str(tag.className).strip()), )
    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)
    def test_strClassList(self):
        '''
            test_strClassList - Assert that a classList can be str'd (equiv to tag.classList.toString()) to get a " " join
        '''

        x = AdvancedTag('div')

        x.className = 'hello world welcome to the pizza haven'

        classList = x.classList

        assert len(classList) == 7 , 'Expected classList to contain 7 elements'

        strClassName = str(classList)

        assert strClassName == 'hello world welcome to the pizza haven' , 'Expected to be able to str() the .classList to get className'

        assert strClassName == x.className , 'Expected str of classList to be the same as .className'
    def test_strClassList(self):
        '''
            test_strClassList - Assert that a classList can be str'd (equiv to tag.classList.toString()) to get a " " join
        '''

        x = AdvancedTag('div')

        x.className = 'hello world welcome to the pizza haven'

        classList = x.classList

        assert len(classList) == 7, 'Expected classList to contain 7 elements'

        strClassName = str(classList)

        assert strClassName == 'hello world welcome to the pizza haven', 'Expected to be able to str() the .classList to get className'

        assert strClassName == x.className, 'Expected str of classList to be the same as .className'
    def test_classListCopy(self):
        '''
            test_classListCopy - Test that changing the list returned from .classList
                    does NOT affect the class attributes on a node (same as JS).
        '''

        tag = AdvancedTag('div')

        tag.className = "hello world"

        classList = tag.classList

        classList.append('xxx')

        assert 'xxx' not in tag.className , 'Expected .classList return to be independent of source tag, but changing affected className attribute'

        assert 'class="hello world"' in str(tag) , 'Expected .classList return to be independent of source tag, but changing affected class in HTML representation'

        assert tag.classList == ['hello', 'world'] , 'Expected .classList return to be independent of source tag, but changing affected return of .classList'
    def test_classListCopy(self):
        '''
            test_classListCopy - Test that changing the list returned from .classList
                    does NOT affect the class attributes on a node (same as JS).
        '''

        tag = AdvancedTag('div')

        tag.className = "hello world"

        classList = tag.classList

        classList.append('xxx')

        assert 'xxx' not in tag.className, 'Expected .classList return to be independent of source tag, but changing affected className attribute'

        assert 'class="hello world"' in str(
            tag
        ), 'Expected .classList return to be independent of source tag, but changing affected class in HTML representation'

        assert tag.classList == [
            'hello', 'world'
        ], 'Expected .classList return to be independent of source tag, but changing affected return of .classList'
    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_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)
    def test_setClassNameString(self):
        '''
            test_setClassNameString - Test setting the "className" attribute on an AdvancedTag and it being reflected.
        '''

        tag = AdvancedTag('div')

        assert "class=" not in tag.getHTML(
        ), "Expected to not find 'class=' when none is set. Got: " + tag.getHTML(
        )

        assert 'class' not in tag.attributes, 'Expected "class" to not be "in" attributes'

        # Try initial set

        tag.className = "cheese is good"

        assert 'class' in tag.attributes, 'Expected "class" to be "in" attributes'

        assert tag.className == "cheese is good", "Expected className to equal 'cheese is good' after assign on className attribute. Got: " + repr(
            tag.className)

        assert 'class="cheese is good"' in str(
            tag
        ), "Expected to find class=\"cheese is good\" after set on className attribute. Got: " + str(
            tag)

        assert tag.classList == [
            'cheese', 'is', 'good'
        ], "Expected classList to be set after setting on className attribute. Got: " + repr(
            tag.classList)

        assert 'class' in tag.attributes.keys(
        ), 'Expected "class" to be in .keys()'

        # Try changing

        tag.className = "hello world"

        assert tag.className == "hello world", "Expected to be able to change className attribute. Got: " + repr(
            tag.className)

        assert 'class="hello world"' in str(
            tag
        ), "Expected to be able to change className and have it show up in html attribute. Got: " + str(
            tag)

        assert tag.classList == [
            'hello', 'world'
        ], "Expected to be able to change className attribute, but update not reflected in classList. Got: " + repr(
            tag.classList)

        # Try removing

        tag.className = ''

        assert tag.className == '', "Expected to be able to clear className attribute. Got: " + repr(
            tag.className)

        assert "class=" not in str(
            tag
        ), "Expected class attribute to not be on HTML representation after clearing className. Got: " + str(
            tag)

        assert tag.classList == [], "Expected to be able to clear className attribute, but did not update classList to empty list. Got: " + repr(
            tag.classList)