def test_requires_iterable(self): from functional import filter try: filter(str, 5) except TypeError: pass else: raise AssertionError("Failed to raise TypeError")
def test_requires_callable(self): from functional import filter try: filter(None, [4, 5]) except TypeError: pass else: raise AssertionError("Failed to raise TypeError")
def test_single_iterable(self): from functional import filter try: filter(bool, [4, 5, 6], [6, 7, 8]) except TypeError: pass else: raise AssertionError("Failed to raise TypeError")
def test_filter(): assert functional.filter(lambda e: e % 2 == 0, functional.range(1, 11)) == \ [ 2, 4, 6, 8, 10 ] assert functional.filter(lambda e: e % 2 == 1, functional.range(1, 11)) == \ [ 1, 3, 5, 7, 9 ] assert functional.filter(lambda e: len(e) != 0, [ [ 1 ], [], [ 2 ], [], [ 3 ] ]) == \ [ [ 1 ], [ 2 ], [ 3 ] ]
def test_with_explosive_iter(self): from functional import filter def blowup(): yield 5 yield 6 yield False raise AssertionError() yield True try: filter(str, blowup()) except AssertionError: pass else: raise AssertionError("Failed to raise AssertionError")
def test_with_string_tuple_unicode_subclasses(self): from functional import filter def broken_child(parent, modifier): class child(parent): def __len__(self): return 8 def __getitem__(self, index): return modifier("b") def __iter__(self): for i in range(6): yield modifier("c") return child, child().__iter__ parents = [(str, str), (tuple, str)] try: unicode parents.append((unicode, unicode)) except NameError: pass for p, m in parents: child, answer = broken_child(p, m) try: self.assertEqual(filter(bool, child("aaaa")), list(answer())) except Exception, e: raise AssertionError("%s -- failed on %s, %s" % (e, p, m))
def test_filter_other_func(self): from functional import filter def as_listcomp(func, seq): return filter(func, seq), [x for x in seq if func(x)] def my_func(obj): return isinstance(obj, int) self.assertEqual(*as_listcomp(my_func, [4, False, 6])) self.assertEqual(*as_listcomp(my_func, "abc")) self.assertEqual(*as_listcomp(my_func, (5, None, 7))) def gen(): yield True yield False yield 6 self.assertEqual(filter(my_func, gen()), [x for x in gen() if my_func(x)]) try: unicode self.assertEqual(*as_listcomp(my_func, unicode("abc"))) except NameError: pass
def test_with_explosive_func(self): from functional import filter class blowup(object): def __init__(self): self.iter = iter(self) def __call__(self, obj): return self.iter.next() def __iter__(self): yield True yield True raise RuntimeError() try: filter(blowup(), [5, 6, 7, 8]) except RuntimeError: pass else: raise AssertionError("Failed to raise RuntimeError")
def test_filter_bool(self): from functional import filter def as_listcomp(func, seq): return filter(func, seq), [x for x in seq if func(x)] self.assertEqual(*as_listcomp(bool, [4, False, 6])) self.assertEqual(*as_listcomp(bool, "abc")) self.assertEqual(*as_listcomp(bool, (5, None, 7))) def gen(): yield True yield False yield 6 self.assertEqual(filter(bool, gen()), [x for x in gen() if x]) try: unicode self.assertEqual(*as_listcomp(bool, unicode("abc"))) except NameError: pass
def test_filter(self): self.assertEqual(F.filter(_list, lambda x: x % 2 == 0), [2, 4])
def as_listcomp(func, seq): return filter(func, seq), [x for x in seq if func(x)]