def test_count(self): assert self.obj.count(Str.green('o'), 2, 4) == self.raw.count('o', 2, 4) assert self.obj.count('x') == self.raw.count('x') assert self.obj.count(Str.red('x')) == 3 assert self.obj.count(Str.red('x')) < self.raw.count('x') assert self.obj.count(Str.green('oo')) == 1 assert self.obj.count(Str.red('o') + Str.green('o')) == 0 assert self.obj.count(Str.green('o') + Str.red('o')) == 1
def test_partition(self): assert self.obj.partition('x') == self.raw.partition('x') assert self.obj.partition(Str.red('x')) == ('', self.obj[:1], self.obj[1:]) assert self.obj.partition(Str.red('x')) != self.obj.partition( Str.green('x')) assert self.obj.partition(Str.green('o')) == (self.obj[:2], self.obj[2:3], self.obj[3:]) assert self.obj.partition(Str.blue('x')) == (self.obj[:], '', '')
def test_rpartition(self): assert self.obj.rpartition('x') == self.raw.rpartition('x') assert self.obj.rpartition(Str.red('x')) == (self.obj[:-1], self.obj[-1], '') assert self.obj.rpartition(Str.red('x')) != self.obj.rpartition( Str.green('x')) assert self.obj.rpartition(Str.green('o')) == (self.obj[:8], self.obj[8:-2], self.obj[-2:]) assert self.obj.rpartition(Str.blue('x')) == ('', '', self.obj[:])
class TestReplace(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_replace(self): assert self.obj.replace('o', 'XX') == self.raw.replace('o', 'XX') assert self.obj.replace(Str.blue('A'), 'XX') == self.raw.replace('A', 'XX') assert self.obj.replace(Str.red('o'), 'XX') != self.raw.replace( 'o', 'XX') assert self.obj.replace('o', Str.red('x')) == self.obj.replace( 'o', Str.red('x')) assert self.obj.replace('o', Str.red('x')) != self.obj.replace( 'o', Str.blue('x')) assert self.obj.replace(Str.red('x'), 'o') != self.obj.replace( Str.red('x'), Str.blue('o')) assert self.obj.replace(Str.red('x'), 'o') == self.obj.replace( Str.red('x'), Str('o')) assert self.obj.replace(Str.green('o'), 'x') != self.obj.replace( 'o', 'x', 2) assert self.obj.replace(Str.green('o'), 'x') == self.obj.replace('o', 'x', 3)
class TestOthers(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_upper(self): assert self.obj.upper() == self.raw.upper() assert self.obj.upper() != Str(self.raw).upper() def test_lower(self): assert self.obj.lower() == self.raw.lower() assert self.obj.lower() != Str(self.raw).lower() def test_swapcase(self): assert self.obj.swapcase() == self.raw.swapcase() assert self.obj.swapcase() != Str(self.raw).swapcase() def test_title(self): assert self.obj.title() == self.raw.title() assert self.obj.title() != Str(self.raw).title() def test_capitalize(self): assert self.obj.capitalize() == self.raw.capitalize() assert self.obj.capitalize() != Str(self.raw).capitalize() def test_casefold(self): assert self.obj.casefold() == self.raw.casefold() assert self.obj.casefold() != Str(self.raw).casefold() def test_startswith(self): assert self.obj.startswith('xxoo') is True assert self.obj.startswith('xoox') is False assert self.obj.startswith(Str.green('xx') + Str.red('oo')) is False assert self.obj.startswith(Str.red('xx') + Str.green('oo')) is True def test_endswith(self): assert self.obj.endswith('xxoo') is False assert self.obj.endswith('xoox') is True assert self.obj.endswith(Str.green('xo') + Str.red('ox')) is True assert self.obj.endswith(Str.red('xo') + Str.green('ox')) is False def test_zfill(self): assert self.obj.zfill(-1) == self.raw.zfill(-1) assert self.obj.zfill(0) == self.raw.zfill(0) assert self.obj.zfill(30) == self.raw.zfill(30) def test_encode(self): assert self.obj.encode() == self.raw.encode() assert self.obj.encode(encoding='gbk', errors='ignore') == self.raw.encode( encoding='gbk', errors='ignore') assert self.obj.encode(encoding='gbk', errors='replace') == self.raw.encode( encoding='gbk', errors='replace') with pytest.raises(UnicodeEncodeError): self.obj.encode(encoding='gbk', errors='strict') with pytest.raises(UnicodeEncodeError): self.raw.encode(encoding='gbk', errors='strict')
class TestPartition(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_partition(self): assert self.obj.partition('x') == self.raw.partition('x') assert self.obj.partition(Str.red('x')) == ('', self.obj[:1], self.obj[1:]) assert self.obj.partition(Str.red('x')) != self.obj.partition( Str.green('x')) assert self.obj.partition(Str.green('o')) == (self.obj[:2], self.obj[2:3], self.obj[3:]) assert self.obj.partition(Str.blue('x')) == (self.obj[:], '', '') def test_rpartition(self): assert self.obj.rpartition('x') == self.raw.rpartition('x') assert self.obj.rpartition(Str.red('x')) == (self.obj[:-1], self.obj[-1], '') assert self.obj.rpartition(Str.red('x')) != self.obj.rpartition( Str.green('x')) assert self.obj.rpartition(Str.green('o')) == (self.obj[:8], self.obj[8:-2], self.obj[-2:]) assert self.obj.rpartition(Str.blue('x')) == ('', '', self.obj[:])
class TestSplit(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_split(self): assert self.obj.split('x') == self.raw.split('x') assert self.obj.split(Str.magenta('ß')) == self.raw.split('ß') assert self.obj.split(Str.red('o')) == [self.obj[:-2], self.obj[-1]] assert self.obj.split( Str.green('o')) == [self.obj[:2], '', self.obj[4:8], self.obj[9:]] assert self.obj.split(Str.green('o'), 1) == self.raw.split('o', 1) assert self.obj.split(Str.green('o'), 2) == self.raw.split('o', 2) assert self.obj.split(Str.green('o'), 3) == self.raw.split('o', 3) assert self.obj.split(Str.green('o'), 4) != self.raw.split('o', 4) def test_rsplit(self): assert self.obj.rsplit('x') == self.raw.rsplit('x') assert self.obj.rsplit(Str.magenta('ß')) == self.raw.rsplit('ß') assert self.obj.rsplit(Str.red('o')) == [self.obj[:-2], self.obj[-1]] assert self.obj.rsplit( Str.green('o')) == [self.obj[:2], '', self.obj[4:8], self.obj[9:]] assert self.obj.rsplit(Str.green('o'), 1) != self.raw.rsplit('o', 1) assert self.obj.rsplit(Str.green('o'), 2) != self.raw.rsplit('o', 2) assert self.obj.rsplit(Str.green('o'), 3) != self.raw.rsplit('o', 3) assert self.obj.rsplit(Str.green('o'), 4) != self.raw.rsplit('o', 4)
def test_join(self): assert Str.red(',').join(['1', '2', '3', '4']) == ','.join(['1', '2', '3', '4']) assert Str.red('!!!').join(['1', '2', '3', '4']) == '!!!'.join(['1', '2', '3', '4']) assert Str.red(',').join(['1', Str.blue('2'), '3', '4']) == ','.join(['1', '2', '3', '4']) assert Str.red(',').join([ '1', Str.blue('2'), '3', '4' ]) != Str.red(',').join(['1', Str.red('2'), '3', '4']) assert Str.red(',').join([ '1', Str.blue('2'), '3', '4' ]) != Str.red(',').join(['1', '2', Str.red('3'), '4'])
class TestOthers(object): table = str.maketrans('xA', 'oB') raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_translate(self): assert self.obj.translate(self.table) == self.raw.translate(self.table)
def test_rsplit(self): assert self.obj.rsplit('x') == self.raw.rsplit('x') assert self.obj.rsplit(Str.magenta('ß')) == self.raw.rsplit('ß') assert self.obj.rsplit(Str.red('o')) == [self.obj[:-2], self.obj[-1]] assert self.obj.rsplit( Str.green('o')) == [self.obj[:2], '', self.obj[4:8], self.obj[9:]] assert self.obj.rsplit(Str.green('o'), 1) != self.raw.rsplit('o', 1) assert self.obj.rsplit(Str.green('o'), 2) != self.raw.rsplit('o', 2) assert self.obj.rsplit(Str.green('o'), 3) != self.raw.rsplit('o', 3) assert self.obj.rsplit(Str.green('o'), 4) != self.raw.rsplit('o', 4)
class TestOthers(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_isalpha(self): assert self.obj.isalpha() == self.raw.isalpha() assert self.obj.isalpha() == Str(self.raw).isalpha() def test_isalnum(self): assert self.obj.isalnum() == self.raw.isalnum() assert self.obj.isalnum() == Str(self.raw).isalnum() def test_isascii(self): if platform.python_version() >= '3.7': assert self.obj.isascii() == self.raw.isascii() assert self.obj.isascii() == Str(self.raw).isascii() def test_isdecimal(self): assert self.obj.isdecimal() == self.raw.isdecimal() assert self.obj.isdecimal() == Str(self.raw).isdecimal() def test_isdigit(self): assert self.obj.isdigit() == self.raw.isdigit() assert self.obj.isdigit() == Str(self.raw).isdigit() def test_isidentifier(self): assert self.obj.isidentifier() == self.raw.isidentifier() assert self.obj.isidentifier() == Str(self.raw).isidentifier() def test_islower(self): assert self.obj.islower() == self.raw.islower() assert self.obj.islower() == Str(self.raw).islower() def test_isnumeric(self): assert self.obj.isnumeric() == self.raw.isnumeric() assert self.obj.isnumeric() == Str(self.raw).isnumeric() def test_isprintable(self): assert self.obj.isprintable() == Str(self.raw).isprintable() assert self.obj.isprintable() == Str(self.raw).isprintable() def test_isspace(self): assert self.obj.isspace() == Str(self.raw).isspace() assert self.obj.isspace() == Str(self.raw).isspace() def test_istitle(self): assert self.obj.istitle() == Str(self.raw).istitle() assert self.obj.istitle() == Str(self.raw).istitle() def test_isupper(self): assert self.obj.isupper() == Str(self.raw).isupper() assert self.obj.isupper() == Str(self.raw).isupper()
class TestSplitlines(object): raw = 'xx\noo\tAß西\rxo\r\nox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_splitlines(self): assert self.obj.splitlines() == self.raw.splitlines() assert self.obj.splitlines(True) == self.raw.splitlines(True) assert self.obj.splitlines(False) == self.raw.splitlines(False)
class TestExpandtabs(object): raw = 'xx\too\nAß西\txo\tox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_expandtabs(self): assert self.obj.expandtabs() == self.raw.expandtabs() assert self.obj.expandtabs() == self.raw.expandtabs() assert self.obj.expandtabs(1) == self.raw.expandtabs(1) assert self.obj.expandtabs(16) == self.raw.expandtabs(16)
class TestCount(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_count(self): assert self.obj.count(Str.green('o'), 2, 4) == self.raw.count('o', 2, 4) assert self.obj.count('x') == self.raw.count('x') assert self.obj.count(Str.red('x')) == 3 assert self.obj.count(Str.red('x')) < self.raw.count('x') assert self.obj.count(Str.green('oo')) == 1 assert self.obj.count(Str.red('o') + Str.green('o')) == 0 assert self.obj.count(Str.green('o') + Str.red('o')) == 1
def test_replace(self): assert self.obj.replace('o', 'XX') == self.raw.replace('o', 'XX') assert self.obj.replace(Str.blue('A'), 'XX') == self.raw.replace('A', 'XX') assert self.obj.replace(Str.red('o'), 'XX') != self.raw.replace( 'o', 'XX') assert self.obj.replace('o', Str.red('x')) == self.obj.replace( 'o', Str.red('x')) assert self.obj.replace('o', Str.red('x')) != self.obj.replace( 'o', Str.blue('x')) assert self.obj.replace(Str.red('x'), 'o') != self.obj.replace( Str.red('x'), Str.blue('o')) assert self.obj.replace(Str.red('x'), 'o') == self.obj.replace( Str.red('x'), Str('o')) assert self.obj.replace(Str.green('o'), 'x') != self.obj.replace( 'o', 'x', 2) assert self.obj.replace(Str.green('o'), 'x') == self.obj.replace('o', 'x', 3)
class TestFind(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_find(self): assert self.obj.find('echoshoot') == -1 assert self.obj.find(Str.green('xo')) == 7 assert self.obj.find(Str.red('x') + Str.green('o')) == 1 assert self.obj.find('xo') == 1 def test_rfind(self): assert self.obj.rfind('echoshoot') == -1 assert self.obj.rfind(Str.green('xo')) == 7 assert self.obj.rfind(Str.red('x') + Str.green('o')) == 1 assert self.obj.rfind('xo') == 7
class TestIndex(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_index(self): with pytest.raises(ValueError): self.obj.index('echoshoot') assert self.obj.index(Str.green('xo')) == 7 assert self.obj.index(Str.red('x') + Str.green('o')) == 1 assert self.obj.index('xo') == 1 def test_rindex(self): with pytest.raises(ValueError): self.obj.rindex('echoshoot') assert self.obj.rindex(Str.green('xo')) == 7 assert self.obj.rindex(Str.red('x') + Str.green('o')) == 1 assert self.obj.rindex('xo') == 7
class TestStrip(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_lstrip(self): assert self.obj.lstrip(Str.green('o') + Str.red('x')) == self.raw.lstrip('o' + 'x') assert self.obj.lstrip(Str.green('o') + Str.red('x')) == self.obj[4:] def test_rstrip(self): assert self.obj.rstrip(Str.green('o') + Str.red('x')) == self.raw.rstrip('x') assert self.obj.rstrip(Str.green('o') + Str.red('x')) == self.obj[:-1] def test_strip(self): assert self.obj.strip(Str.green('o') + Str.red('x')) != self.raw.strip('o' + 'x') assert self.obj.strip(Str.green('o') + Str.red('x')) == self.obj[4:-1]
class TestJustCenter(object): raw = 'xxooAß西xoox' obj = Str.red(raw) obj[2:-2] = Str.green obj[4:-4] = Str.blue obj[5:-5] = Str.magenta def test_ljust(self): assert self.obj.ljust(10) == self.obj assert self.obj.ljust(30, '*') == self.raw.ljust(30, '*') assert self.obj.ljust(30, Str.blue('*')) == self.raw.ljust(30, '*') assert self.obj.ljust(30, Str.blue('*')) != self.obj.ljust(30, Str.red('*')) def test_rjust(self): assert self.obj.rjust(10) == self.obj assert self.obj.rjust(30, '*') == self.raw.rjust(30, '*') assert self.obj.rjust(30, Str.blue('*')) == self.raw.rjust(30, '*') assert self.obj.rjust(30, Str.blue('*')) != self.obj.rjust(30, Str.red('*')) def test_center(self): assert self.obj.center(10) == self.obj assert self.obj.center(30, '*') == self.raw.center(30, "*") assert self.obj.center(30, Str.blue('*')) == self.raw.center(30, "*") assert self.obj.center(30, Str.blue('*')) != self.obj.center(30, Str.red('*'))
def test_center(self): assert self.obj.center(10) == self.obj assert self.obj.center(30, '*') == self.raw.center(30, "*") assert self.obj.center(30, Str.blue('*')) == self.raw.center(30, "*") assert self.obj.center(30, Str.blue('*')) != self.obj.center(30, Str.red('*'))
def test_endswith(self): assert self.obj.endswith('xxoo') is False assert self.obj.endswith('xoox') is True assert self.obj.endswith(Str.green('xo') + Str.red('ox')) is True assert self.obj.endswith(Str.red('xo') + Str.green('ox')) is False
def test_strip(self): assert self.obj.strip(Str.green('o') + Str.red('x')) != self.raw.strip('o' + 'x') assert self.obj.strip(Str.green('o') + Str.red('x')) == self.obj[4:-1]
def test_rstrip(self): assert self.obj.rstrip(Str.green('o') + Str.red('x')) == self.raw.rstrip('x') assert self.obj.rstrip(Str.green('o') + Str.red('x')) == self.obj[:-1]
def test_rfind(self): assert self.obj.rfind('echoshoot') == -1 assert self.obj.rfind(Str.green('xo')) == 7 assert self.obj.rfind(Str.red('x') + Str.green('o')) == 1 assert self.obj.rfind('xo') == 7
def test_rjust(self): assert self.obj.rjust(10) == self.obj assert self.obj.rjust(30, '*') == self.raw.rjust(30, '*') assert self.obj.rjust(30, Str.blue('*')) == self.raw.rjust(30, '*') assert self.obj.rjust(30, Str.blue('*')) != self.obj.rjust(30, Str.red('*'))
def test_rindex(self): with pytest.raises(ValueError): self.obj.rindex('echoshoot') assert self.obj.rindex(Str.green('xo')) == 7 assert self.obj.rindex(Str.red('x') + Str.green('o')) == 1 assert self.obj.rindex('xo') == 7