popwhile tests

This commit is contained in:
enkore 2013-03-08 16:08:19 +01:00
parent 37b69fd158
commit ea4ef6d66d

View File

@ -10,15 +10,13 @@ from i3pystatus.core import util
def get_random_string(length=6, chars=string.printable): def get_random_string(length=6, chars=string.printable):
return ''.join(random.choice(chars) for x in range(length)) return ''.join(random.choice(chars) for x in range(length))
def lchop_factory(prefix, string): def lchop(prefix, string):
def test(): chopped = util.lchop(string, prefix)
chopped = util.lchop(string, prefix) if string.startswith(prefix):
if string.startswith(prefix): assert len(chopped) == len(string) - len(prefix)
assert len(chopped) == len(string) - len(prefix) if prefix:
if prefix: assert not chopped.startswith(prefix)
assert not chopped.startswith(prefix)
test.description = "lchop_test:prefix={}:string={}".format(prefix, string)
return test
def lchop_test_generator(): def lchop_test_generator():
cases = [ cases = [
@ -54,21 +52,19 @@ def lchop_test_generator():
('^|j7N!mV0o(?*1>p?dy', '\\ZdA&:\t\x0b:8\t|7.Kl,oHw-\x0cS\nwZlND~uC@le`Sm'), ('^|j7N!mV0o(?*1>p?dy', '\\ZdA&:\t\x0b:8\t|7.Kl,oHw-\x0cS\nwZlND~uC@le`Sm'),
] ]
for prefix, string in cases: for prefix, string in cases:
yield lchop_factory(prefix, prefix+string) yield lchop, prefix, prefix+string
yield lchop_factory(prefix, string) yield lchop, prefix, string
yield lchop_factory(string, string) yield lchop, string, string
yield lchop_factory(string, prefix) yield lchop, string, prefix
yield lchop_factory("", string) yield lchop, "", string
yield lchop_factory(prefix, "") yield lchop, prefix, ""
def partition(iterable, limit, assrt):
partitions = util.partition(iterable, limit)
partitions = [sorted(partition) for partition in partitions]
for item in assrt:
assert sorted(item) in partitions
def partition_factory(iterable, limit, assrt):
def test():
partitions = util.partition(iterable, limit)
partitions = [sorted(partition) for partition in partitions]
for item in assrt:
assert sorted(item) in partitions
test.description = "partition_test:iterable={}:limit={}:expected={}".format(iterable, limit, assrt)
return test
def partition_test_generator(): def partition_test_generator():
cases = [ cases = [
@ -79,4 +75,19 @@ def partition_test_generator():
] ]
for iterable, limit, assrt in cases: for iterable, limit, assrt in cases:
yield partition_factory(iterable, limit, assrt) yield partition, iterable, limit, assrt
def popwhile(iterable, predicate, assrt):
assert list(util.popwhile(predicate, iterable)) == assrt
def popwhile_test_generator():
cases = [
([1, 2, 3, 4], lambda x: x < 2, []),
([1, 2, 3, 4], lambda x: x < 5 and x > 2, [4, 3]),
([1, 2, 3, 4], lambda x: x == 4, [4]),
([1, 2, 3, 4], lambda x: True, [4, 3, 2, 1]),
([1, 2], lambda x: False, []),
]
for iterable, predicate, assrt in cases:
yield popwhile, iterable, predicate, assrt