Skip to content

Commit 78c145e

Browse files
committed
Add unit tests
1 parent 3e22be6 commit 78c145e

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

src/embed_tests/ClassManagerTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,76 @@ def contains(dictionary, key):
11791179
Assert.IsFalse(result);
11801180
}
11811181

1182+
[Test]
1183+
public void SupportsLenOperatorForIEnumerableWithCountProperty()
1184+
{
1185+
using var _ = Py.GIL();
1186+
1187+
var module = PyModule.FromString("SupportsLenOperatorForIEnumerableWithCountProperty", $@"
1188+
from clr import AddReference
1189+
AddReference(""Python.EmbeddingTest"")
1190+
1191+
from Python.EmbeddingTest import *
1192+
1193+
def length(enumerable):
1194+
return len(enumerable)
1195+
");
1196+
1197+
using var length = module.GetAttr("length");
1198+
1199+
Assert.Multiple(() =>
1200+
{
1201+
var enumerableWithCount = new EnumerableWithCount();
1202+
using var pyEnumerableWithCount = enumerableWithCount.ToPython();
1203+
var count = length.Invoke(pyEnumerableWithCount).As<int>();
1204+
Assert.AreEqual(enumerableWithCount.Count, count);
1205+
1206+
var genericEnumerableWithCount = new GenericEnumerableWithCount();
1207+
using var pyGenericEnumerableWithCount = genericEnumerableWithCount.ToPython();
1208+
count = length.Invoke(pyGenericEnumerableWithCount).As<int>();
1209+
Assert.AreEqual(genericEnumerableWithCount.Count, count);
1210+
1211+
var derivedEnumerableWithCount = new DerivedEnumerableWithCount();
1212+
using var pyDerivedEnumerableWithCount = derivedEnumerableWithCount.ToPython();
1213+
count = length.Invoke(pyDerivedEnumerableWithCount).As<int>();
1214+
Assert.AreEqual(derivedEnumerableWithCount.Count, count);
1215+
});
1216+
}
1217+
1218+
private class EnumerableWithCount : IEnumerable
1219+
{
1220+
public int Count => 123;
1221+
public IEnumerator GetEnumerator()
1222+
{
1223+
for (int i = 0; i < Count; i++)
1224+
{
1225+
yield return i;
1226+
}
1227+
}
1228+
}
1229+
1230+
private class GenericEnumerableWithCount : IEnumerable<int>
1231+
{
1232+
public int Count => 123;
1233+
1234+
public IEnumerator<int> GetEnumerator()
1235+
{
1236+
for (int i = 0; i < Count; i++)
1237+
{
1238+
yield return i;
1239+
}
1240+
}
1241+
1242+
IEnumerator IEnumerable.GetEnumerator()
1243+
{
1244+
return GetEnumerator();
1245+
}
1246+
}
1247+
1248+
private class DerivedEnumerableWithCount : GenericEnumerableWithCount
1249+
{
1250+
}
1251+
11821252
public class TestDictionary<TKey, TValue> : IDictionary
11831253
{
11841254
private readonly Dictionary<TKey, TValue> _data = new();

0 commit comments

Comments
 (0)