Skip to content

Commit c9d7b71

Browse files
author
Rickard Holmberg
committed
Move the tests from test_runtime.py to TestRuntime.cs
1 parent 1a85eef commit c9d7b71

File tree

4 files changed

+123
-51
lines changed

4 files changed

+123
-51
lines changed

src/embed_tests/Python.EmbeddingTest.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="pyimport.cs" />
8585
<Compile Include="pyinitialize.cs" />
8686
<Compile Include="pyrunstring.cs" />
87+
<Compile Include="TestClrMethod.cs" />
8788
<Compile Include="TestConverter.cs" />
8889
<Compile Include="TestCustomMarshal.cs" />
8990
<Compile Include="TestExample.cs" />

src/embed_tests/TestClrMethod.cs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Python.Runtime;
4+
5+
namespace Python.EmbeddingTest
6+
{
7+
public class TestClrMethod
8+
{
9+
private IntPtr globals;
10+
11+
[OneTimeSetUp]
12+
public void SetUp()
13+
{
14+
PythonEngine.Initialize();
15+
globals = Runtime.Runtime.PyDict_New();
16+
Runtime.Runtime.PyDict_SetItemString(globals, "__builtins__", Runtime.Runtime.PyEval_GetBuiltins());
17+
Runtime.Runtime.PyDict_SetItemString(globals, "clr", ImportHook.GetCLRModule());
18+
Runtime.Runtime.PyDict_SetItemString(globals, "ExampleClrClass", CreateTestClass().Handle);
19+
}
20+
21+
[OneTimeTearDown]
22+
public void Dispose()
23+
{
24+
PythonEngine.Shutdown();
25+
}
26+
27+
[Test]
28+
public void SetAndGetPropertyTest()
29+
{
30+
dynamic a = InstantiateTestClass();
31+
Assert.AreEqual(3, (int) a.X);
32+
Assert.AreEqual(3 * 2, (int) a.Y);
33+
a.X = 4;
34+
Assert.AreEqual(4, (int) a.X);
35+
Assert.AreEqual(4 * 2, (int) a.Y);
36+
}
37+
38+
[Test]
39+
public void CallMethodTest()
40+
{
41+
dynamic a = InstantiateTestClass();
42+
Assert.AreEqual(4 * 2, (int) a.test(4));
43+
Assert.AreEqual(5 * 2, (int) a.test(5));
44+
}
45+
46+
private PyObject CreateTestClass()
47+
{
48+
var locals = new PyDict();
49+
PythonEngine.Exec(@"
50+
import System
51+
52+
class ExampleClrClass(System.Object):
53+
__namespace__ = ""PyTest""
54+
def __init__(self):
55+
self._x = 3
56+
@clr.clrmethod(int, [int])
57+
def test(self, x):
58+
return x*2
59+
60+
def get_X(self):
61+
return self._x
62+
def set_X(self, value):
63+
self._x = value
64+
X = clr.clrproperty(int, get_X, set_X)
65+
66+
@clr.clrproperty(int)
67+
def Y(self):
68+
return self._x * 2
69+
",
70+
globals, locals.Handle);
71+
72+
return locals.GetItem("ExampleClrClass");
73+
}
74+
75+
private PyObject InstantiateTestClass()
76+
{
77+
return PythonEngine.Eval(@"ExampleClrClass()", globals);
78+
}
79+
}
80+
}

src/embed_tests/TestRuntime.cs

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using NUnit.Framework;
33
using Python.Runtime;
44

@@ -47,5 +47,46 @@ public static void RefCountTest()
4747

4848
Runtime.Runtime.Py_Finalize();
4949
}
50+
51+
[Test]
52+
public static void PyCheck_Iter_PyObject_IsIterable_Test()
53+
{
54+
Runtime.Runtime.Py_Initialize();
55+
56+
// Tests that a python list is an iterable, but not an iterator
57+
var pyList = Runtime.Runtime.PyList_New(0);
58+
Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyList));
59+
Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyList));
60+
61+
// Tests that a python list iterator is both an iterable and an iterator
62+
var pyListIter = Runtime.Runtime.PyObject_GetIter(pyList);
63+
Assert.IsTrue(Runtime.Runtime.PyObject_IsIterable(pyListIter));
64+
Assert.IsTrue(Runtime.Runtime.PyIter_Check(pyListIter));
65+
66+
// Tests that a python float is neither an iterable nor an iterator
67+
var pyFloat = Runtime.Runtime.PyFloat_FromDouble(2.73);
68+
Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(pyFloat));
69+
Assert.IsFalse(Runtime.Runtime.PyIter_Check(pyFloat));
70+
71+
Runtime.Runtime.Py_Finalize();
72+
}
73+
74+
[Test]
75+
public static void PyCheck_Iter_PyObject_IsIterable_ThreadingLock_Test()
76+
{
77+
Runtime.Runtime.Py_Initialize();
78+
79+
// Create an instance of threading.Lock, which is one of the very few types that does not have the
80+
// TypeFlags.HaveIter set in Python 2. This tests a different code path in PyObject_IsIterable and PyIter_Check.
81+
var threading = Runtime.Runtime.PyImport_ImportModule("threading");
82+
var threadingDict = Runtime.Runtime.PyModule_GetDict(threading);
83+
var lockType = Runtime.Runtime.PyDict_GetItemString(threadingDict, "Lock");
84+
var lockInstance = Runtime.Runtime.PyObject_CallObject(lockType, Runtime.Runtime.PyTuple_New(0));
85+
86+
Assert.IsFalse(Runtime.Runtime.PyObject_IsIterable(lockInstance));
87+
Assert.IsFalse(Runtime.Runtime.PyIter_Check(lockInstance));
88+
89+
Runtime.Runtime.Py_Finalize();
90+
}
5091
}
5192
}

src/tests/test_runtime.py

-50
This file was deleted.

0 commit comments

Comments
 (0)