8000 add a test · pythonnet/pythonnet@f80ae87 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f80ae87

Browse files
committed
add a test
1 parent b500aa1 commit f80ae87

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/embed_tests/Codecs.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,42 @@ public void ListCodecTest()
9797
Assert.IsTrue(codec.CanDecode(x, typeof(ICollection<float>)));
9898
Assert.IsFalse(codec.CanDecode(x, typeof(bool)));
9999

100-
System.Collections.IEnumerable plainEnumerable = null;
101-
Assert.DoesNotThrow(() => { codec.TryDecode<System.Collections.IEnumerable>(x, out plainEnumerable); });
102-
Assert.IsNotNull(plainEnumerable);
103-
IList<object> list = null;
104-
list = plainEnumerable.Cast<object>().ToList();
105-
Assert.AreEqual(list.Count, 3);
106-
Assert.AreEqual(list[0], 1);
107-
Assert.AreEqual(list[1], 2);
108-
Assert.AreEqual(list[2], 3);
100+
Action<System.Collections.IEnumerable> checkPlainEnumerable = (System.Collections.IEnumerable enumerable) =>
101+
{
102+
Assert.IsNotNull(enumerable);
103+
IList<object> list = null;
104+
list = enumerable.Cast<object>().ToList();
105+
Assert.AreEqual(list.Count, 3);
106+
Assert.AreEqual(list[0], 1);
107+
Assert.AreEqual(list[1], 2);
108+
Assert.AreEqual(list[2], 3);
109+
};
110+
111+
//ensure a PyList can be converted to a plain IEnumerable
112+
System.Collections.IEnumerable plainEnumerable1 = null;
113+
Assert.DoesNotThrow(() => { codec.TryDecode<System.Collections.IEnumerable>(x, 8000 out plainEnumerable1); });
114+
checkPlainEnumerable(plainEnumerable1);
115+
116+
//ensure a python class which implements the iterator protocol can be converter to a plain IEnumerable
117+
var locals = new PyDict();
118+
PythonEngine.Exec(@"
119+
class foo():
120+
def __init__(self):
121+
self.counter = 0
122+
def __iter__(self):
123+
return self
124+
def __next__(self):
125+
if self.counter == 3:
126+
raise StopIteration
127+
self.counter = self.counter + 1
128+
return self.counter
129+
foo_instance = foo()
130+
", null, locals.Handle);
131+
132+
var foo = locals.GetItem("foo_instance");
133+
System.Collections.IEnumerable plainEnumerable2 = null;
134+
Assert.DoesNotThrow(() => { codec.TryDecode<System.Collections.IEnumerable>(x, out plainEnumerable2); });
135+
checkPlainEnumerable(plainEnumerable2);
109136
}
110137
}
111138

0 commit comments

Comments
 (0)
0