forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestModuleObject.cs
More file actions
88 lines (78 loc) · 2.67 KB
/
TestModuleObject.cs
File metadata and controls
88 lines (78 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using NUnit.Framework;
using Python.Runtime;
namespace Python.EmbeddingTest
{
internal class TestModuleObject
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}
[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}
[Test]
public void TestGetAttr()
{
PyDict locals = new PyDict();
PythonEngine.Exec(@"
import clr
clr.setPreload(True)
import Python.Runtime
dict = dir(Python.Runtime)
values = []
keys = []
for key in dict:
keys.append(key)
values.append(getattr(Python.Runtime, key))
clr.setPreload(False)
", null, locals);
// At this point dir/getattr consistency was tested
// Assert attributes of different types are included
using var keys = PyList.AsList(locals.GetItem("keys"));
using var values = PyList.AsList(locals.GetItem("values"));
var keyList = keys.ToList();
var valueList = values.ToList();
// class
CollectionAssert.Contains(keyList, "PyObject");
var index = keyList.IndexOf("PyObject");
Assert.AreEqual(typeof(PyObject), valueList[index]);
// nested namespace
CollectionAssert.Contains(keyList, "Codecs");
index = keyList.IndexOf("Codecs");
Assert.AreEqual(typeof(ModuleObject), valueList[index].GetType());
Assert.AreEqual("Python.Runtime.Codecs", ((ModuleObject)valueList[index]).moduleName);
// custom module attribute
CollectionAssert.Contains(keyList, "__class__");
index = keyList.IndexOf("__class__");
Assert.AreEqual(typeof(PyObject), valueList[index].GetType());
StringAssert.Contains("ModuleObject", valueList[index].ToString());
// inherited/native attribute
CollectionAssert.Contains(keyList, "__delattr__");
index = keyList.IndexOf("__delattr__");
Assert.AreEqual(typeof(PyObject), valueList[index].GetType());
StringAssert.Contains("delattr", valueList[index].ToString());
}
[Test]
public void TestNestedNamespacePreload()
{
PyDict locals = new PyDict();
PythonEngine.Exec(@"
import clr
clr.setPreload(True)
import Python.Runtime
dict = dir(getattr(Python.Runtime, 'Codecs'))
keys = []
for key in dict:
keys.append(key)
clr.setPreload(False)
", null, locals);
using var keys = PyList.AsList(locals.GetItem("keys"));
var keyList = keys.ToList();
CollectionAssert.Contains(keyList, "DecoderGroup");
}
}
}