示例#1
0
def test_common():
    """Test common string methods."""
    value = Color('this is a test.')

    assert '' == Color()
    assert 15 == len(value)
    assert 'this is a test.' == '{0}'.format(value)

    assert 'This is a test.' == value.capitalize()
    assert '  this is a test.   ' == value.center(20)
    assert '@@this is a test.@@@' == value.center(20, '@')
    assert 2 == value.count('is')
    assert value.endswith('test.')
    assert '    class' == Color('\tclass').expandtabs(4)
    assert 8 == value.find('a')
    assert 'test 123' == Color('test {0}').format('123')
    assert 8 == value.index('a')

    assert Color('a1').isalnum()
    assert not Color('a1.').isalnum()
    assert Color('a').isalpha()
    assert not Color('a1').isalpha()
    assert Color('1').isdecimal()
    assert not Color(u'⅕').isdecimal()
    assert Color(u'²').isdigit()
    assert not Color(u'⅕').isdigit()
    assert Color('a').islower()
    assert not Color('A').islower()
    assert Color(u'⅕').isnumeric()
    assert not Color('A').isnumeric()
    assert Color('    ').isspace()
    assert not Color('    x').isspace()
    assert Color('I Love To Test').istitle()
    assert not Color('I Love to Test').istitle()
    assert Color('A').isupper()
    assert not Color('a').isupper()

    assert 'test test' == Color(' ').join(('test', 'test'))
    assert 'this is a test.     ' == value.ljust(20)
    assert 'a' == Color('A').lower()
    assert 'a ' == Color(' a ').lstrip()
    assert ('this', ' ', 'is a test.') == value.partition(' ')
    assert 'this was a test.' == value.replace(' is ', ' was ')
    assert 13 == value.rfind('t')
    assert 13 == value.rindex('t')
    assert '     this is a test.' == value.rjust(20)
    assert ('this is a', ' ', 'test.') == value.rpartition(' ')
    assert ['this is a', 'test.'] == value.rsplit(' ', 1)
    assert ' a' == Color(' a ').rstrip()
    assert ['this', 'is', 'a', 'test.'] == value.split(' ')
    assert ['a', 'a'] == Color('a\na').splitlines()
    assert [1, 1] == [len(i) for i in Color('a\na').splitlines()]
    assert value.startswith('this')
    assert 'a' == Color(' a ').strip()
    assert 'Aa' == Color('aA').swapcase()
    assert 'This Is A Test.' == value.title()
    assert 'THIS IS A TEST.' == value.upper()
    assert '000001' == Color('1').zfill(6)
    assert '000000' == Color().zfill(6)
示例#2
0
def test_common():
    """Test common string methods."""
    value = Color("{red}this is a test.{/red}")

    assert Color("{red}this is a test.{/red}") == value
    assert Color("\033[31mthis is a test.\033[39m") == value
    assert 15 == len(value)
    assert "\033[31mthis is a test.\033[39m" == "{0}".format(value)

    assert "\033[31mThis is a test.\033[39m" == value.capitalize()
    assert "  \033[31mthis is a test.\033[39m   " == value.center(20)
    assert "@@\033[31mthis is a test.\033[39m@@@" == value.center(20, "@")
    assert 2 == value.count("is")
    assert 2 == Color("{red}I love m&ms{/red}").count("m")
    assert value.endswith("test.")
    assert "    \033[31mclass\033[39m" == Color("\t{red}class{/red}").expandtabs(4)
    assert 8 == value.find("a")
    assert 7 == Color("{red}I love m&ms{/red}").find("m")
    assert "\033[31mtest 123\033[39m" == Color("{red}test {0}{/red}").format("123")
    assert 8 == value.index("a")
    assert 7 == Color("{red}I love m&ms{/red}").index("m")

    assert Color("{red}a1{/red}").isalnum()
    assert not Color("{red}a1.{/red}").isalnum()
    assert Color("{red}a{/red}").isalpha()
    assert not Color("{red}a1{/red}").isalpha()
    assert Color("{red}1").isdecimal()
    assert not Color(u"{red}⅕{/red}").isdecimal()
    assert Color(u"{red}²{/red}").isdigit()
    assert not Color(u"{red}⅕{/red}").isdigit()
    assert Color("{red}a{/red}").islower()
    assert not Color("{red}A{/red}").islower()
    assert Color(u"{red}⅕{/red}").isnumeric()
    assert not Color("{red}A{/red}").isnumeric()
    assert Color("{red}    {/red}").isspace()
    assert not Color("{red}    x{/red}").isspace()
    assert Color("{red}I Love To Test{/red}").istitle()
    assert not Color("{red}I Love to Test{/red}").istitle()
    assert Color("{red}A{/red}").isupper()
    assert not Color("{red}a{/red}").isupper()

    assert "test\033[0mtest" == Color("{/all}").join(("test", "test"))
    assert "\033[31mthis is a test.\033[39m     " == value.ljust(20)
    assert "\033[31ma\033[39m" == Color("{red}A{/red}").lower()
    assert "\033[31ma\033[39m " == Color(" {red}a{/red} ").lstrip()
    assert "\033[31m a \033[39m" == Color("{red} a {/red}").lstrip()
    assert ("\033[31mthis", " ", "is a test.\033[39m") == value.partition(" ")
    assert "\033[31mthis was a test.\033[39m" == value.replace(" is ", " was ")
    assert 13 == value.rfind("t")
    assert 13 == value.rindex("t")
    assert "     \033[31mthis is a test.\033[39m" == value.rjust(20)
    assert ("\033[31mthis is a", " ", "test.\033[39m") == value.rpartition(" ")
    assert ["\033[31mthis is a", "test.\033[39m"] == value.rsplit(" ", 1)
    assert " \033[31ma\033[39m" == Color(" {red}a{/red} ").rstrip()
    assert "\033[31m a \033[39m" == Color("{red} a {/red}").rstrip()
    assert ["\033[31mthis", "is", "a", "test.\033[39m"] == value.split(" ")

    values = Color("{red}a{/red}\n{green}a{/green}").splitlines()
    assert ["\033[31ma\033[39m", "\033[32ma\033[39m"] == values
    assert [1, 1] == [len(i) for i in values]

    assert value.startswith("this")
    assert "\033[31ma\033[39m" == Color(" {red}a{/red} ").strip()
    assert "\033[31m a \033[39m" == Color("{red} a {/red}").strip()
    assert "\033[31mAa\033[39m" == Color("{red}aA{/red}").swapcase()
    assert "\033[31mThis Is A Test.\033[39m" == value.title()
    assert "\033[31mTHIS IS A TEST.\033[39m" == value.upper()
    assert "\033[31m000001\033[39m" == Color("{red}1{/red}").zfill(6)
    assert "00001\033[31m1\033[39m" == Color("1{red}1{/red}").zfill(6)