forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc75e01
commit d91ceab
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |