Example #1
0
        signal.disconnect_all (test.simple_handler)
        signal.emit (3)

        signal.connect (test.simple_handler)
        signal.emit (4)

        signal.unblock (test.simple_handler)
        signal.emit (5)

        test.assert_results (1, 1, 4, 5)



import __future__

if NotifyTestCase.note_skipped_tests ('with_statement' in __future__.all_feature_names):
    from test._2_5.signal import SignalContextManagerTestCase



if __name__ == '__main__':
    unittest.main ()



# Local variables:
# mode: python
# python-indent: 4
# indent-tabs-mode: nil
# fill-column: 90
# End:
Example #2
0
        self.collect_garbage ()
        self.assertEqual (reference (), None)



class SlowGCProtectorTestCase (_GCProtectorTestCase):

    def test_protection_1 (self):
        self._do_test_protection (SlowGCProtector ())

    def test_protection_2 (self):
        self._do_test_double_protection (SlowGCProtector ())



if NotifyTestCase.note_skipped_tests (HAVE_FAST_IMPLEMENTATIONS,
                                      NotifyTestCase.REASON_INVALID_FOR_IMPLEMENTATION):

    class FastGCProtectorTestCase (_GCProtectorTestCase):

        def test_protection_1 (self):
            self._do_test_protection (FastGCProtector ())

        def test_protection_2 (self):
            self._do_test_double_protection (FastGCProtector ())


    class RaisingGCProtectorTestCase (_GCProtectorTestCase):

        def test_protection_1 (self):
            self._do_test_protection (RaisingGCProtector ())
Example #3
0
            self.assertEqual(protector.num_protected_objects, 0)

        self.collect_garbage()
        self.assertEqual(reference(), None)


class SlowGCProtectorTestCase(_GCProtectorTestCase):
    def test_protection_1(self):
        self._do_test_protection(SlowGCProtector())

    def test_protection_2(self):
        self._do_test_double_protection(SlowGCProtector())


if NotifyTestCase.note_skipped_tests(
        HAVE_FAST_IMPLEMENTATIONS,
        NotifyTestCase.REASON_INVALID_FOR_IMPLEMENTATION):

    class FastGCProtectorTestCase(_GCProtectorTestCase):
        def test_protection_1(self):
            self._do_test_protection(FastGCProtector())

        def test_protection_2(self):
            self._do_test_double_protection(FastGCProtector())

    class RaisingGCProtectorTestCase(_GCProtectorTestCase):
        def test_protection_1(self):
            self._do_test_protection(RaisingGCProtector())

        def test_protection_2(self):
            self._do_test_double_protection(RaisingGCProtector())
Example #4
0
        signal.block(test.simple_handler)
        signal.emit(2)

        signal.disconnect_all(test.simple_handler)
        signal.emit(3)

        signal.connect(test.simple_handler)
        signal.emit(4)

        signal.unblock(test.simple_handler)
        signal.emit(5)

        test.assert_results(1, 1, 4, 5)


import __future__

if NotifyTestCase.note_skipped_tests(
        'with_statement' in __future__.all_feature_names):
    from test._2_5.signal import SignalContextManagerTestCase

if __name__ == '__main__':
    unittest.main()

# Local variables:
# mode: python
# python-indent: 4
# indent-tabs-mode: nil
# fill-column: 90
# End:
Example #5
0
class BindingTestCase(NotifyTestCase):
    def test_creation(self):
        Binding(DUMMY.identity_function)
        WeakBinding(DUMMY.identity_function)
        RaisingWeakBinding(DUMMY.identity_function)

    def test_invocation(self):
        self.assertEqual(
            Binding(DUMMY.identity_function)(33, 'test'), (33, 'test'))
        self.assertEqual(
            WeakBinding(DUMMY.identity_function)(33, 'test'), (33, 'test'))
        self.assertEqual(
            RaisingWeakBinding(DUMMY.identity_function)(33, 'test'),
            (33, 'test'))

    def test_invocation_keywords(self):
        keywords = {'a': 1, 'b': 2}
        self.assertEqual(
            Binding(DUMMY.keyword_dict_function)(**keywords), keywords)
        self.assertEqual(
            WeakBinding(DUMMY.keyword_dict_function)(**keywords), keywords)
        self.assertEqual(
            RaisingWeakBinding(DUMMY.keyword_dict_function)(**keywords),
            keywords)

    def test_creation_with_arguments(self):
        self.assertEqual(
            Binding(DUMMY.identity_function, (33, ))('test'), (33, 'test'))
        self.assertEqual(
            WeakBinding(DUMMY.identity_function, (33, ))('test'), (33, 'test'))
        self.assertEqual(
            RaisingWeakBinding(DUMMY.identity_function, (33, ))('test'),
            (33, 'test'))

    def test_creation_with_keywords(self):
        keywords = {'a': 1, 'b': 2}
        self.assertEqual(
            Binding(DUMMY.keyword_dict_function, (), keywords)(), keywords)
        self.assertEqual(
            WeakBinding(DUMMY.keyword_dict_function, (), None, keywords)(),
            keywords)
        self.assertEqual(
            RaisingWeakBinding(DUMMY.keyword_dict_function, (), None,
                               keywords)(), keywords)

    if NotifyTestCase.note_skipped_tests(
            not NotifyTestCase.ALL_OBJECTS_ARE_WEAKLY_REFERABLE,
            NotifyTestCase.REASON_INVALID_FOR_IMPLEMENTATION):

        def test_unreferable_object_method_failure(self):
            class Test(object):
                __slots__ = ()

                def test(self):
                    pass

            self.assertRaises(CannotWeakReferenceError,
                              lambda: WeakBinding(Test().test))
            self.assertRaises(CannotWeakReferenceError,
                              lambda: RaisingWeakBinding(Test().test))

    def test_equality_1(self):
        for binding_type in (Binding, WeakBinding, RaisingWeakBinding):
            self.assert_equal_thoroughly(binding_type(DUMMY.identity_function),
                                         binding_type(DUMMY.identity_function))
            self.assert_equal_thoroughly(binding_type(Dummy.static_identity),
                                         binding_type(Dummy.static_identity))

            # We need to make sure objects don't get garbage collected before comparison.
            dummy1 = Dummy()
            dummy2 = Dummy()
            self.assert_not_equal_thoroughly(
                binding_type(dummy1.identity_function),
                binding_type(dummy2.identity_function))

            self.assert_not_equal_thoroughly(
                binding_type(DUMMY.identity_function),
                binding_type(DUMMY.static_identity))

    def test_equality_2(self):
        # This test won't work on Python 3000, since unbound methods are gone.
        if sys.version_info[0] < 3:

            def f(x):
                pass

            class A(object):
                test = f

            class B(object):
                test = f

            for binding_type in (Binding, WeakBinding, RaisingWeakBinding):
                self.assert_not_equal_thoroughly(binding_type(A.test),
                                                 binding_type(B.test))

    def test_equality_3(self):
        for binding_type in (Binding, WeakBinding, RaisingWeakBinding):
            self.assert_equal_thoroughly(
                binding_type(DUMMY.identity_function, ('a', 'b', 'c')),
                binding_type(DUMMY.identity_function, ('a', 'b', 'c')))
            self.assert_equal_thoroughly(
                binding_type(DUMMY.static_identity, ('a', 'b', 'c')),
                binding_type(DUMMY.static_identity, ('a', 'b', 'c')))

            self.assert_not_equal_thoroughly(
                binding_type(DUMMY.identity_function, ('a', 'b', 'c')),
                binding_type(DUMMY.identity_function, ('a', 'b', 'd')))
            self.assert_not_equal_thoroughly(
                binding_type(Dummy.static_identity, ('a', 'b', 'c')),
                binding_type(Dummy.static_identity, ('a', 'b', 'd')))

    def test_equality_4(self):
        def plain_function():
            pass

        a_lambda = lambda: None

        for binding_type in (Binding, WeakBinding, RaisingWeakBinding):
            self.assert_equal_thoroughly(DUMMY.identity_function,
                                         binding_type(DUMMY.identity_function))
            self.assert_equal_thoroughly(DUMMY.static_identity,
                                         binding_type(DUMMY.static_identity))
            self.assert_equal_thoroughly(Dummy.static_identity,
                                         binding_type(Dummy.static_identity))
            self.assert_equal_thoroughly(Dummy.static_identity,
                                         binding_type(Dummy.static_identity))
            self.assert_equal_thoroughly(plain_function,
                                         binding_type(plain_function))
            self.assert_equal_thoroughly(a_lambda, binding_type(a_lambda))

            # Same as above, but with non-empty argument list.
            self.assert_not_equal_thoroughly(
                DUMMY.identity_function,
                binding_type(DUMMY.identity_function, (0, )))
            self.assert_not_equal_thoroughly(
                DUMMY.static_identity,
                binding_type(DUMMY.static_identity, (0, )))
            self.assert_not_equal_thoroughly(
                Dummy.static_identity,
                binding_type(Dummy.static_identity, (0, )))
            self.assert_not_equal_thoroughly(
                Dummy.static_identity,
                binding_type(Dummy.static_identity, (0, )))
            self.assert_not_equal_thoroughly(
                plain_function, binding_type(plain_function, (0, )))
            self.assert_not_equal_thoroughly(a_lambda,
                                             binding_type(a_lambda, (0, )))

    def test_equality_5(self):
        keywords1 = {'a': 1, 'b': 2}
        keywords2 = {'a': 1, 'b': 3}

        for binding_type in (Binding, WeakBinding, RaisingWeakBinding):
            self.assert_equal_thoroughly(
                binding_type(DUMMY.identity_function, keywords=keywords1),
                binding_type(DUMMY.identity_function, keywords=keywords1))
            self.assert_equal_thoroughly(
                binding_type(DUMMY.static_identity, keywords=keywords1),
                binding_type(DUMMY.static_identity, keywords=keywords1))

            self.assert_not_equal_thoroughly(
                binding_type(DUMMY.identity_function, keywords=keywords1),
                binding_type(DUMMY.identity_function, keywords=keywords2))
            self.assert_not_equal_thoroughly(
                binding_type(Dummy.static_identity, keywords=keywords1),
                binding_type(Dummy.static_identity, keywords=keywords2))

    def test_garbage_collection_1(self):
        object = Dummy()
        method = WeakBinding(object.identity_function)

        self.assertEqual(method(15), 15)

        del object
        self.collect_garbage()

        self.assertEqual(method(15), None)

    def test_garbage_collection_2(self):
        object = Dummy()
        method = RaisingWeakBinding(object.identity_function)

        self.assertEqual(method(15), 15)

        del object
        self.collect_garbage()

        self.assertRaises(GarbageCollectedError, method)