def test_multiple_dicts(self): a = {'a': 1, 'b': 2, 'c': 3} b = {'a': 4, 'b': 5, 'c': 6} c = {'a': 7, 'b': 8, 'c': 9} predicate = lambda *args: any(x % 4 == 0 for x in args) result = nested.filter(predicate, a, b, c) self.assertEqual({'a': (1, 4, 7), 'b': (2, 5, 8)}, result)
def test_multiple_lists(self): a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] predicate = lambda *args: any(x % 4 == 0 for x in args) result = nested.filter(predicate, a, b, c) self.assertEqual([(1, 4, 7), (2, 5, 8)], result)
def test_namedtuple_nested(self): Foo = collections.namedtuple('Foo', 'value1, value2') foo = Foo(1, [1, 2, 3]) self.assertEqual(Foo(None, [2, 3]), nested.filter(lambda x: x > 1, foo))
def test_namedtuple_multiple(self): Foo = collections.namedtuple('Foo', 'value1, value2') foo = Foo(1, 2) bar = Foo(2, 3) result = nested.filter(lambda x, y: x + y > 3, foo, bar) self.assertEqual(Foo(None, (2, 3)), result)
def test_namedtuple(self): Foo = collections.namedtuple('Foo', 'value1, value2') self.assertEqual(Foo(1, None), nested.filter(lambda x: x == 1, Foo(1, 2)))
def test_remove_empty_containers(self): data = [(1, 2, 3), 4, {'foo': [5, 6], 'bar': 7}] self.assertEqual([], nested.filter(lambda x: False, data))
def test_mixed_structure(self): predicate = lambda x: x % 2 == 0 data = [(1, 2), 3, {'foo': [4]}] self.assertEqual([(2, ), {'foo': [4]}], nested.filter(predicate, data))
def test_single_dict(self): predicate = lambda x: x % 2 == 0 data = {'a': 1, 'b': 2, 'c': 3, 'd': 4} self.assertEqual({'b': 2, 'd': 4}, nested.filter(predicate, data))
def test_base_case(self): self.assertEqual((), nested.filter(lambda x: False, 1))
def test_empty(self): self.assertEqual({}, nested.filter(lambda x: True, {})) self.assertEqual({}, nested.filter(lambda x: False, {}))