Пример #1
0
    def test_requires_iterable(self):
        from functional import filter

        try:
            filter(str, 5)
        except TypeError:
            pass
        else:
            raise AssertionError("Failed to raise TypeError")
Пример #2
0
 def test_requires_callable(self):
     from functional import filter
     
     try:
         filter(None, [4, 5])
     except TypeError:
         pass
     else:
         raise AssertionError("Failed to raise TypeError")
Пример #3
0
    def test_requires_callable(self):
        from functional import filter

        try:
            filter(None, [4, 5])
        except TypeError:
            pass
        else:
            raise AssertionError("Failed to raise TypeError")
Пример #4
0
    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")
Пример #5
0
 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")
Пример #6
0
 def test_requires_iterable(self):
     from functional import filter
     
     try:
         filter(str, 5)
     except TypeError:
         pass
     else:
         raise AssertionError("Failed to raise TypeError")
Пример #7
0
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 ] ]
Пример #8
0
 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")
Пример #9
0
    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))
Пример #10
0
    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))
Пример #11
0
    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")
Пример #12
0
    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
Пример #13
0
    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")
Пример #14
0
 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")
Пример #15
0
 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
Пример #16
0
    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
Пример #17
0
 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
Пример #18
0
 def test_filter(self):
     self.assertEqual(F.filter(_list, lambda x: x % 2 == 0), [2, 4])
Пример #19
0
 def as_listcomp(func, seq):
     return filter(func, seq), [x for x in seq if func(x)]
Пример #20
0
 def as_listcomp(func, seq):
     return filter(func, seq), [x for x in seq if func(x)]