Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eirannejad committed Mar 15, 2024
1 parent dc75e01 commit d91ceab
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/test_method_dunder.py
Original file line number Diff line number Diff line change
@@ -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
63 changes: 63 additions & 0 deletions tests/test_method_getattr_self.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d91ceab

Please sign in to comment.