diff --git a/tests/test_method_dunder.py b/tests/test_method_dunder.py new file mode 100644 index 000000000..e591f6316 --- /dev/null +++ b/tests/test_method_dunder.py @@ -0,0 +1,73 @@ +import System +from System.Collections import IEnumerable, IEnumerator + + +class CustomIterator(IEnumerator): + def __init__(self, enumerable): + self.enumerable = enumerable + self.position = -1 + + def get_Current(self): + if self.position == -1: return None + if self.position >= len(self.enumerable): return None + return self.enumerable[self.position] + + def MoveNext(self): + self.position += 1 + return self.position < len(self.enumerable) + + def Reset(self): + self.position = -1 + + +class CustomEnumerable(IEnumerable): + def __init__(self, values, attributes): + self.values = values + self.myattributes = attributes + + def __getitem__(self, key): + return self.values[key] + + def __len__(self): + return len(self.values) + + def __iter__(self): + for a in self.myattributes: + yield a + + def GetEnumerator(self): + return CustomIterator(self) + + +def test_custom_enumerable(): + e = CustomEnumerable([1, 2, 3], ['a', 'b']) + + assert len(e) == 3 + + assert e[0] == 1 + assert e[1] == 2 + assert e[2] == 3 + + l = list([x for x in e]) + assert len(l) == 2 + assert l[0] == 'a' + assert l[1] == 'b' + + +def test_custom_enumerator(): + e = CustomIterator([1, 2, 3]) + l = list([x for x in e]) + assert len(l) == 3 + assert l[0] == 1 + assert l[1] == 2 + assert l[2] == 3 + + +def test_custom_enumerable_getEnumerator(): + e = CustomEnumerable([1, 2, 3], ['a', 'b']) + + l = list(e.GetEnumerator()) + assert len(l) == 3 + assert l[0] == 1 + assert l[1] == 2 + assert l[2] == 3 diff --git a/tests/test_method_getattr_self.py b/tests/test_method_getattr_self.py new file mode 100644 index 000000000..6aa07cfd9 --- /dev/null +++ b/tests/test_method_getattr_self.py @@ -0,0 +1,63 @@ +from typing import Any + +import System + + +class T1(System.Object): + def __init__(self, value) -> None: + self.answer = value + + def __getattr__(self, key): + if key == 'answer': + return self.answer + return super().__getattr__() + + +class T2(System.Object): + def __init__(self, value) -> None: + self.answer = value + + def __getattribute__(self, __name: str) -> Any: + if __name == 'answer': + return 43 + return super().__getattribute__(__name) + + def __getattr__(self, key): + if key == 'answer': + return self.answer + return super().__getattr__() + + +class T3(System.Object): + answer = 44 + + def __getattr__(self, key): + if key == 'answer': + return self.answer + return super().__getattr__() + + +class T4(System.Object): + def __getattribute__(self, __name: str) -> Any: + if __name == 'answer': + return 45 + return super().__getattribute__(__name) + + def __getattr__(self, key): + if key == 'answer': + return self.answer + return super().__getattr__() + + +def test_method_getattr_self(): + t1 = T1(42) + assert t1.answer == 42 + + t2 = T2(42) + assert t2.answer == 43 + + t3 = T3() + assert t3.answer == 44 + + t4 = T4() + assert t4.answer == 45