Example #1
0
def test_delegate_equality():
    """Test delegate equality."""

    d = StringDelegate(hello_func)
    ob = DelegateTest()
    ob.stringDelegate = d
    assert ob.stringDelegate == d
Example #2
0
    def test_delegate_equality(self):
        """Test delegate equality."""

        d = StringDelegate(hello_func)
        ob = DelegateTest()
        ob.stringDelegate = d
        self.assertTrue(ob.stringDelegate == d)
Example #3
0
    def testDelegateEquality(self):
        """Test delegate equality."""

        def sayhello():
            return "hello"

        d = StringDelegate(sayhello)
        ob = DelegateTest()
        ob.stringDelegate = d
        self.failUnless(ob.stringDelegate == d)
Example #4
0
    def test_delegate_from_managed_instance_method(self):
        """Test delegate implemented with a managed instance method."""
        ob = DelegateTest()
        d = StringDelegate(ob.SayHello)

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
Example #5
0
def test_delegate_from_managed_static_method():
    """Test delegate implemented with a managed static method."""
    d = StringDelegate(DelegateTest.StaticSayHello)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #6
0
    def testDelegateFromManagedInstanceMethod(self):
        """Test delegate implemented with a managed instance method."""
        ob = DelegateTest()
        d = StringDelegate(ob.SayHello)

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")
Example #7
0
def test_delegate_from_managed_instance_method():
    """Test delegate implemented with a managed instance method."""
    ob = DelegateTest()
    d = StringDelegate(ob.SayHello)

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #8
0
    def testDelegateFromManagedStaticMethod(self):
        """Test delegate implemented with a managed static method."""
        d = StringDelegate(DelegateTest.StaticSayHello)
        ob = DelegateTest()

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")
Example #9
0
def test_delegate_from_function():
    """Test delegate implemented with a Python function."""

    d = StringDelegate(hello_func)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #10
0
    def test_delegate_from_function(self):
        """Test delegate implemented with a Python function."""

        d = StringDelegate(hello_func)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
Example #11
0
    def test_delegate_from_delegate(self):
        """Test delegate implemented with another delegate."""
        d1 = StringDelegate(hello_func)
        d2 = StringDelegate(d1)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d2) == "hello")
        self.assertTrue(d2() == "hello")

        ob.stringDelegate = d2
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
Example #12
0
def test_delegate_from_delegate():
    """Test delegate implemented with another delegate."""
    d1 = StringDelegate(hello_func)
    d2 = StringDelegate(d1)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d2) == "hello"
    assert d2() == "hello"

    ob.stringDelegate = d2
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #13
0
def test_delegate_from_method():
    """Test delegate implemented with a Python instance method."""

    inst = HelloClass()
    d = StringDelegate(inst.hello)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #14
0
def test_delegate_from_callable():
    """Test delegate implemented with a Python callable object."""

    inst = HelloClass()
    d = StringDelegate(inst)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #15
0
    def testDelegateFromFunction(self):
        """Test delegate implemented with a Python function."""

        def sayhello():
            return "hello"

        d = StringDelegate(sayhello)
        ob = DelegateTest()

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")
Example #16
0
    def testDelegateFromDelegate(self):
        """Test delegate implemented with another delegate."""

        def sayhello():
            return "hello"

        d1 = StringDelegate(sayhello)
        d2 = StringDelegate(d1)
        ob = DelegateTest()

        self.failUnless(ob.CallStringDelegate(d2) == "hello")
        self.failUnless(d2() == "hello")

        ob.stringDelegate = d2
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")
Example #17
0
    def testDelegateFromCallable(self):
        """Test delegate implemented with a Python callable object."""

        class Hello:
            def __call__(self):
                return "hello"

        inst = Hello()
        d = StringDelegate(inst)
        ob = DelegateTest()

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")
Example #18
0
    def testDelegateFromMethod(self):
        """Test delegate implemented with a Python instance method."""

        class Hello:
            def sayhello(self):
                return "hello"

        inst = Hello()
        d = StringDelegate(inst.sayhello)
        ob = DelegateTest()

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")
Example #19
0
    def testDelegateFromClassMethod(self):
        """Test delegate implemented with a Python class method."""

        class Hello:
            def sayhello(self):
                return "hello"

            sayhello = classmethod(sayhello)

        d = StringDelegate(Hello.sayhello)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")

        inst = Hello()
        d = StringDelegate(inst.sayhello)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
Example #20
0
    def testMulticastDelegate(self):
        """Test multicast delegates."""
        class Multi:
            def __init__(self):
                self.value = 0

            def count(self):
                self.value += 1
                return 'ok'

        inst = Multi()
        d1 = StringDelegate(inst.count)
        d2 = StringDelegate(inst.count)

        md = System.Delegate.Combine(d1, d2)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(md) == "ok")
        self.assertTrue(inst.value == 2)

        self.assertTrue(md() == "ok")
        self.assertTrue(inst.value == 4)
Example #21
0
def test_int_ref_int_ref_string_delegate():
    """Test delegate with a ref int and ref string parameter."""
    from Python.Test import IntRefIntRefStringDelegate
    intData = 7
    stringData = 'goodbye'

    def ref_hello_func(intValue, stringValue):
        assert intData == intValue
        assert stringData == stringValue
        return (intValue + len(stringValue), intValue + 1, stringValue + '!')

    d = IntRefIntRefStringDelegate(ref_hello_func)
    result = d(intData, stringData)
    assert result == (intData + len(stringData), intData + 1, stringData + '!')

    ob = DelegateTest()
    result = ob.CallIntRefIntRefStringDelegate(d, intData, stringData)
    assert result == (intData + len(stringData), intData + 1, stringData + '!')

    def not_a_tuple(intValue, stringValue):
        return 'a'

    d = IntRefIntRefStringDelegate(not_a_tuple)
    with pytest.raises(TypeError):
        result = d(intData, stringData)

    def short_tuple(intValue, stringValue):
        return (5,)

    d = IntRefIntRefStringDelegate(short_tuple)
    with pytest.raises(TypeError):
        result = d(intData, stringData)

    def wrong_return_type(intValue, stringValue):
        return ('a', 7, 'b')

    d = IntRefIntRefStringDelegate(wrong_return_type)
    with pytest.raises(TypeError):
        result = d(intData, stringData)
Example #22
0
def test_out_int_delegate():
    """Test delegate with an out int parameter."""
    from Python.Test import OutIntDelegate
    value = 7

    def out_hello_func(ignored):
        return 5

    d = OutIntDelegate(out_hello_func)
    result = d(value)
    assert result == 5

    ob = DelegateTest()
    result = ob.CallOutIntDelegate(d, value)
    assert result == 5

    def implicit_handler(ignored):
        return '5'

    d = OutIntDelegate(implicit_handler)
    result = d(value)
    assert result == 5
Example #23
0
def test_delegate_from_managed_static_method():
    """Test delegate implemented with a managed static method."""
    d = StringDelegate(DelegateTest.StaticSayHello)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #24
0
def test_delegate_from_managed_instance_method():
    """Test delegate implemented with a managed instance method."""
    ob = DelegateTest()
    d = StringDelegate(ob.SayHello)

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #25
0
    def testDelegateFromManagedInstanceMethod(self):
        """Test delegate implemented with a managed instance method."""
        ob = DelegateTest()
        d = StringDelegate(ob.SayHello)

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")
Example #26
0
    def testDelegateFromManagedStaticMethod(self):
        """Test delegate implemented with a managed static method."""
        d = StringDelegate(DelegateTest.StaticSayHello)
        ob = DelegateTest()

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")
Example #27
0
def test_delegate_from_delegate():
    """Test delegate implemented with another delegate."""
    d1 = StringDelegate(hello_func)
    d2 = StringDelegate(d1)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d2) == "hello"
    assert d2() == "hello"

    ob.stringDelegate = d2
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #28
0
def test_delegate_from_function():
    """Test delegate implemented with a Python function."""

    d = StringDelegate(hello_func)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #29
0
def test_delegate_from_callable():
    """Test delegate implemented with a Python callable object."""

    inst = HelloClass()
    d = StringDelegate(inst)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
Example #30
0
def test_delegate_from_method():
    """Test delegate implemented with a Python instance method."""

    inst = HelloClass()
    d = StringDelegate(inst.hello)
    ob = DelegateTest()

    assert ob.CallStringDelegate(d) == "hello"
    assert d() == "hello"

    ob.stringDelegate = d
    assert ob.CallStringDelegate(ob.stringDelegate) == "hello"
    assert ob.stringDelegate() == "hello"
    def testDelegateFromFunction(self):
        """Test delegate implemented with a Python function."""
        def sayhello():
            return "hello"

        d = StringDelegate(sayhello)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
    def testDelegateFromDelegate(self):
        """Test delegate implemented with another delegate."""
        def sayhello():
            return "hello"

        d1 = StringDelegate(sayhello)
        d2 = StringDelegate(d1)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d2) == "hello")
        self.assertTrue(d2() == "hello")

        ob.stringDelegate = d2
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
    def testDelegateFromCallable(self):
        """Test delegate implemented with a Python callable object."""
        class Hello:
            def __call__(self):
                return "hello"

        inst = Hello()
        d = StringDelegate(inst)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
    def testDelegateFromMethod(self):
        """Test delegate implemented with a Python instance method."""
        class Hello:
            def sayhello(self):
                return "hello"

        inst = Hello()
        d = StringDelegate(inst.sayhello)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
Example #35
0
    def test_delegate_from_static_method(self):
        """Test delegate implemented with a Python static method."""

        d = StringDelegate(HelloClass.s_hello)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")

        inst = HelloClass()
        d = StringDelegate(inst.s_hello)
        ob = DelegateTest()

        self.assertTrue(ob.CallStringDelegate(d) == "hello")
        self.assertTrue(d() == "hello")

        ob.stringDelegate = d
        self.assertTrue(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.assertTrue(ob.stringDelegate() == "hello")
Example #36
0
    def testDelegates(self):
        from Python.Test import DelegateTest, StringDelegate
        import System

        self.notify("Running delegate leak check...")

        for i in xrange(self.count):
            if i == 10:
                self.start_test()

            # Delegate from function
            testob = DelegateTest()
            d = StringDelegate(hello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Delegate from instance method
            inst = Hello()
            testob = DelegateTest()
            d = StringDelegate(inst.hello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del inst
            del d

            # Delegate from static method
            testob = DelegateTest()
            d = StringDelegate(Hello.s_hello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Delegate from class method
            testob = DelegateTest()
            d = StringDelegate(Hello.c_hello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Delegate from callable object
            inst = Hello()
            testob = DelegateTest()
            d = StringDelegate(inst)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del inst
            del d

            # Delegate from managed instance method
            testob = DelegateTest()
            d = StringDelegate(testob.SayHello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Delegate from managed static method
            testob = DelegateTest()
            d = StringDelegate(DelegateTest.StaticSayHello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Nested delegates
            testob = DelegateTest()
            d1 = StringDelegate(hello)
            d2 = StringDelegate(d1)
            testob.CallStringDelegate(d2)
            testob.stringDelegate = d2
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d1
            del d2

            # Multicast delegates
            testob = DelegateTest()
            d1 = StringDelegate(hello)
            d2 = StringDelegate(hello)
            md = System.Delegate.Combine(d1, d2)
            testob.CallStringDelegate(md)
            testob.stringDelegate = md
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d1
            del d2
            del md
        
        self.end_test()
Example #37
0
 def create_object():
     return DelegateTest()
Example #38
0
    def testDelegates(self):
        from Python.Test import DelegateTest, StringDelegate
        import System

        self.notify("Running delegate leak check...")

        for i in xrange(self.count):
            if i == 10:
                self.start_test()

            # Delegate from function
            testob = DelegateTest()
            d = StringDelegate(hello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Delegate from instance method
            inst = Hello()
            testob = DelegateTest()
            d = StringDelegate(inst.hello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del inst
            del d

            # Delegate from static method
            testob = DelegateTest()
            d = StringDelegate(Hello.s_hello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Delegate from class method
            testob = DelegateTest()
            d = StringDelegate(Hello.c_hello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Delegate from callable object
            inst = Hello()
            testob = DelegateTest()
            d = StringDelegate(inst)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del inst
            del d

            # Delegate from managed instance method
            testob = DelegateTest()
            d = StringDelegate(testob.SayHello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Delegate from managed static method
            testob = DelegateTest()
            d = StringDelegate(DelegateTest.StaticSayHello)
            testob.CallStringDelegate(d)
            testob.stringDelegate = d
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d

            # Nested delegates
            testob = DelegateTest()
            d1 = StringDelegate(hello)
            d2 = StringDelegate(d1)
            testob.CallStringDelegate(d2)
            testob.stringDelegate = d2
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d1
            del d2

            # Multicast delegates
            testob = DelegateTest()
            d1 = StringDelegate(hello)
            d2 = StringDelegate(hello)
            md = System.Delegate.Combine(d1, d2)
            testob.CallStringDelegate(md)
            testob.stringDelegate = md
            testob.stringDelegate()
            testob.stringDelegate = None
            del testob
            del d1
            del d2
            del md

        self.end_test()
Example #39
0
    def testDelegateFromClassMethod(self):
        """Test delegate implemented with a Python class method."""

        class Hello:
            def sayhello(self):
                return "hello"
            sayhello = classmethod(sayhello)

        d = StringDelegate(Hello.sayhello)
        ob = DelegateTest()

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")

        inst = Hello()
        d = StringDelegate(inst.sayhello)
        ob = DelegateTest()

        self.failUnless(ob.CallStringDelegate(d) == "hello")
        self.failUnless(d() == "hello")

        ob.stringDelegate = d
        self.failUnless(ob.CallStringDelegate(ob.stringDelegate) == "hello")
        self.failUnless(ob.stringDelegate() == "hello")