|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""Test clrmethod and clrproperty support for calling methods and getting/setting python properties from CLR.""" |
| 4 | + |
| 5 | +import Python.Test as Test |
| 6 | +import System |
| 7 | +import pytest |
| 8 | +import clr |
| 9 | + |
| 10 | +class ExampleClrClass(System.Object): |
| 11 | + __namespace__ = "PyTest" |
| 12 | + def __init__(self): |
| 13 | + self._x = 3 |
| 14 | + @clr.clrmethod(int, [int]) |
| 15 | + def test(self, x): |
| 16 | + return x*2 |
| 17 | + |
| 18 | + def get_X(self): |
| 19 | + return self._x |
| 20 | + def set_X(self, value): |
| 21 | + self._x = value |
| 22 | + X = clr.clrproperty(int, get_X, set_X) |
| 23 | + |
| 24 | + @clr.clrproperty(int) |
| 25 | + def Y(self): |
| 26 | + return self._x * 2 |
| 27 | + |
| 28 | +def test_set_and_get_property_from_py(): |
| 29 | + """Test usage of CLR defined reference types.""" |
| 30 | + t = ExampleClrClass() |
| 31 | + assert t.X == 3 |
| 32 | + assert t.Y == 3 * 2 |
| 33 | + t.X = 4 |
| 34 | + assert t.X == 4 |
| 35 | + assert t.Y == 4 * 2 |
| 36 | + |
| 37 | +def test_set_and_get_property_from_clr(): |
| 38 | + """Test usage of CLR defined reference types.""" |
| 39 | + t = ExampleClrClass() |
| 40 | + assert t.GetType().GetProperty("X").GetValue(t) == 3 |
| 41 | + assert t.GetType().GetProperty("Y").GetValue(t) == 3 * 2 |
| 42 | + t.GetType().GetProperty("X").SetValue(t, 4) |
| 43 | + assert t.GetType().GetProperty("X").GetValue(t) == 4 |
| 44 | + assert t.GetType().GetProperty("Y").GetValue(t) == 4 * 2 |
| 45 | + |
| 46 | + |
| 47 | +def test_set_and_get_property_from_clr_and_py(): |
| 48 | + """Test usage of CLR defined reference types.""" |
| 49 | + t = ExampleClrClass() |
| 50 | + assert t.GetType().GetProperty("X").GetValue(t) == 3 |
| 51 | + assert t.GetType().GetProperty("Y").GetValue(t) == 3 * 2 |
| 52 | + assert t.X == 3 |
| 53 | + assert t.Y == 3 * 2 |
| 54 | + t.GetType().GetProperty("X").SetValue(t, 4) |
| 55 | + assert t.GetType().GetProperty("X").GetValue(t) == 4 |
| 56 | + assert t.GetType().GetProperty("Y").GetValue(t) == 4 * 2 |
| 57 | + assert t.X == 4 |
| 58 | + assert t.Y == 4 * 2 |
| 59 | + t.X = 5 |
| 60 | + assert t.GetType().GetProperty("X").GetValue(t) == 5 |
| 61 | + assert t.GetType().GetProperty("Y").GetValue(t) == 5 * 2 |
| 62 | + assert t.X == 5 |
| 63 | + assert t.Y == 5 * 2 |
| 64 | + |
| 65 | +def test_method_invocation_from_py(): |
| 66 | + t = ExampleClrClass() |
| 67 | + assert t.test(41) == 41*2 |
| 68 | + |
| 69 | +def test_method_invocation_from_clr(): |
| 70 | + t = ExampleClrClass() |
| 71 | + assert t.GetType().GetMethod("test").Invoke(t, [37]) == 37*2 |
0 commit comments