Esempio n. 1
0
def test_delegate_from_class_method():
    """Test delegate implemented with a Python class method."""

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

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

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

    inst = HelloClass()
    d = StringDelegate(inst.c_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 testBoolDelegate(self):
        """Test boolean delegate."""

        def always_so_negative():
            return 0

        d = BoolDelegate(always_so_negative)
        ob = DelegateTest()
        ob.CallBoolDelegate(d)
        

        self.failUnless(not d())

        self.failUnless(not ob.CallBoolDelegate(d))
Esempio n. 3
0
    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")
Esempio n. 4
0
def test_out_string_delegate():
    """Test delegate with an out string parameter."""
    from Python.Test import OutStringDelegate
    value = 'goodbye'

    def out_hello_func(ignored):
        return 'hello'

    d = OutStringDelegate(out_hello_func)
    result = d(value)
    assert result == 'hello'

    ob = DelegateTest()
    result = ob.CallOutStringDelegate(d, value)
    assert result == 'hello'
Esempio n. 5
0
def test_multicast_delegate():
    """Test multicast delegates."""

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

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

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

    assert md() == "ok"
    assert inst.value == 4
    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")
Esempio n. 7
0
def test_ref_int_ref_string_delegate():
    """Test delegate with a ref int and ref string parameter."""
    from Python.Test import RefIntRefStringDelegate
    intData = 7
    stringData = 'goodbye'

    # Returns tuple (8, goodbye!)
    def ref_hello_func(intValue, stringValue):
        assert intData == intValue
        assert stringData == stringValue
        return (intValue + 1, stringValue + '!')

    d = RefIntRefStringDelegate(ref_hello_func)
    #Recieves tuple (none, 8, goodbye!)
    result = d(intData, stringData)
    assert result == (intData + 1, stringData + '!')

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

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

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

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

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

    def long_tuple(intValue, stringValue):
        return (5, 'a', 'b')

    d = RefIntRefStringDelegate(long_tuple)
    with pytest.raises(TypeError):
        result = d(intData, stringData)

    def wrong_tuple_item(intValue, stringValue):
        return ('a', 'b')

    d = RefIntRefStringDelegate(wrong_tuple_item)
    with pytest.raises(TypeError):
        result = d(intData, stringData)
Esempio n. 8
0
def test_ref_int_delegate():
    """Test delegate with a ref string parameter."""
    from Python.Test import RefIntDelegate
    value = 7

    def ref_hello_func(data):
        assert data == value
        return data + 1

    d = RefIntDelegate(ref_hello_func)
    result = d(value)
    assert result == value + 1

    ob = DelegateTest()
    result = ob.CallRefIntDelegate(d, value)
    assert result == value + 1
Esempio n. 9
0
def test_ref_string_delegate():
    """Test delegate with a ref string parameter."""
    from Python.Test import RefStringDelegate
    value = 'goodbye'

    def ref_hello_func(data):
        assert data == value
        return 'hello'

    d = RefStringDelegate(ref_hello_func)
    result = d(value)
    assert result == 'hello'

    ob = DelegateTest()
    result = ob.CallRefStringDelegate(d, value)
    assert result == '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")
    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")
Esempio n. 12
0
def test_bool_delegate():
    """Test boolean delegate."""
    from Python.Test import BoolDelegate

    def always_so_negative():
        return False

    d = BoolDelegate(always_so_negative)
    ob = DelegateTest()
    ob.CallBoolDelegate(d)

    assert not d()
    assert not ob.CallBoolDelegate(d)

    def always_so_positive():
        return 1
    bad = BoolDelegate(always_so_positive)
    with pytest.raises(TypeError):
        ob.CallBoolDelegate(bad)
Esempio n. 13
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 invalid_handler(ignored):
        return '5'

    d = OutIntDelegate(invalid_handler)
    with pytest.raises(TypeError):
        result = d(value)
    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)
Esempio n. 15
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)
Esempio n. 16
0
 def create_object():
     return DelegateTest()
Esempio n. 17
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()