Skip to content

Commit 977ee96

Browse files
committed
Add Tests
1 parent d7777b9 commit 977ee96

10 files changed

+912
-11
lines changed

src/embed_tests/Python.EmbeddingTest.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,19 @@
8484
<Compile Include="pyimport.cs" />
8585
<Compile Include="pyinitialize.cs" />
8686
<Compile Include="pyrunstring.cs" />
87+
<Compile Include="TestCustomMarshal.cs" />
88+
<Compile Include="TestPyAnsiString.cs" />
89+
<Compile Include="TestPyFloat.cs" />
90+
<Compile Include="TestPyInt.cs" />
8791
<Compile Include="TestPyList.cs" />
8892
<Compile Include="TestPyLong.cs" />
93+
<Compile Include="TestPyNumber.cs" />
94+
<Compile Include="TestPySequence.cs" />
8995
<Compile Include="TestPyString.cs" />
9096
<Compile Include="TestPythonException.cs" />
9197
<Compile Include="TestPythonEngineProperties.cs" />
9298
<Compile Include="TestPyTuple.cs" />
99+
<Compile Include="TestRuntime.cs" />
93100
</ItemGroup>
94101
<ItemGroup>
95102
<ProjectReference Include="..\runtime\Python.Runtime.csproj">

src/embed_tests/TestCustomMarshal.cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Python.Runtime;
4+
5+
namespace Python.EmbeddingTest
6+
{
7+
public class TestCustomMarshal
8+
{
9+
[Test]
10+
public static void GetManagedStringTwice()
11+
{
12+
const string expected = "FooBar";
13+
using (Py.GIL())
14+
{
15+
IntPtr op = Runtime.Runtime.PyUnicode_FromString(expected);
16+
string s1 = Runtime.Runtime.GetManagedString(op);
17+
string s2 = Runtime.Runtime.GetManagedString(op);
18+
19+
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
20+
Assert.AreEqual(expected, s1);
21+
Assert.AreEqual(expected, s2);
22+
}
23+
}
24+
}
25+
}

src/embed_tests/TestPyAnsiString.cs

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Python.Runtime;
4+
5+
namespace Python.EmbeddingTest
6+
{
7+
public class TestPyAnsiString
8+
{
9+
[OneTimeSetUp]
10+
public void SetUp()
11+
{
12+
PythonEngine.Initialize();
13+
}
14+
15+
[OneTimeTearDown]
16+
public void Dispose()
17+
{
18+
PythonEngine.Shutdown();
19+
}
20+
21+
[Test]
22+
public void TestStringCtor()
23+
{
24+
const string expected = "foo";
25+
var actual = new PyAnsiString(expected);
26+
Assert.AreEqual(expected, actual.ToString());
27+
}
28+
29+
[Test]
30+
public void TestEmptyStringCtor()
31+
{
32+
const string expected = "";
33+
var actual = new PyAnsiString(expected);
34+
Assert.AreEqual(expected, actual.ToString());
35+
}
36+
37+
[Test]
38+
public void TestPyObjectCtor()
39+
{
40+
const string expected = "Foo";
41+
42+
var t = new PyAnsiString(expected);
43+
var actual = new PyAnsiString(t);
44+
45+
Assert.AreEqual(expected, actual.ToString());
46+
}
47+
48+
[Test]
49+
public void TestBadPyObjectCtor()
50+
{
51+
var t = new PyInt(5);
52+
PyAnsiString actual = null;
53+
54+
var ex = Assert.Throws<ArgumentException>(() => actual = new PyAnsiString(t));
55+
56+
StringAssert.StartsWith("object is not a string", ex.Message);
57+
Assert.IsNull(actual);
58+
}
59+
60+
[Test]
61+
public void TestCtorPtr()
62+
{
63+
const string expected = "foo";
64+
65+
var t = new PyAnsiString(expected);
66+
var actual = new PyAnsiString(t.Handle);
67+
68+
Assert.AreEqual(expected, actual.ToString());
69+
}
70+
71+
[Test]
72+
public void IsStringTrue()
73+
{
74+
var t = new PyAnsiString("foo");
75+
76+
Assert.True(PyAnsiString.IsStringType(t));
77+
}
78+
79+
[Test]
80+
public void IsStringFalse()
81+
{
82+
var t = new PyInt(5);
83+
84+
Assert.False(PyAnsiString.IsStringType(t));
85+
}
86+
87+
[Test]
88+
[Ignore("Ambiguous behavior between PY2/PY3")]
89+
public void TestUnicode()
90+
{
91+
const string expected = "foo\u00e9";
92+
PyObject actual = new PyAnsiString(expected);
93+
Assert.AreEqual(expected, actual.ToString());
94+
}
95+
}
96+
}

src/embed_tests/TestPyFloat.cs

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Python.Runtime;
4+
5+
namespace Python.EmbeddingTest
6+
{
7+
/// <remarks>
8+
/// PyFloat implementation isn't complete, thus tests aren't complete.
9+
/// </remarks>
10+
public class TestPyFloat
11+
{
12+
[OneTimeSetUp]
13+
public void SetUp()
14+
{
15+
PythonEngine.Initialize();
16+
}
17+
18+
[OneTimeTearDown]
19+
public void Dispose()
20+
{
21+
PythonEngine.Shutdown();
22+
}
23+
24+
[Test]
25+
public void IntPtrCtor()
26+
{
27+
var i = new PyFloat(1);
28+
var ii = new PyFloat(i.Handle);
29+
Assert.AreEqual(i.Handle, ii.Handle);
30+
}
31+
32+
[Test]
33+
public void FloatCtor()
34+
{
35+
const float a = 4.5F;
36+
var i = new PyFloat(a);
37+
Assert.True(PyFloat.IsFloatType(i));
38+
// Assert.Assert.AreEqual(i, a.ToInt32());
39+
}
40+
41+
[Test]
42+
public void PyObjectCtorGood()
43+
{
44+
var i = new PyFloat(5);
45+
var a = new PyFloat(i);
46+
Assert.True(PyFloat.IsFloatType(a));
47+
// Assert.Assert.AreEqual(i, a.ToInt32());
48+
}
49+
50+
[Test]
51+
public void PyObjectCtorBad()
52+
{
53+
var i = new PyString("Foo");
54+
PyFloat a = null;
55+
56+
var ex = Assert.Throws<ArgumentException>(() => a = new PyFloat(i));
57+
58+
StringAssert.StartsWith("object is not a float", ex.Message);
59+
Assert.IsNull(a);
60+
}
61+
62+
[Test]
63+
public void DoubleCtor()
64+
{
65+
const double a = 4.5;
66+
var i = new PyFloat(a);
67+
Assert.True(PyFloat.IsFloatType(i));
68+
// Assert.Assert.AreEqual(i, a.ToInt32());
69+
}
70+
71+
[Test]
72+
public void StringIntCtor()
73+
{
74+
const string a = "5";
75+
var i = new PyFloat(a);
76+
Assert.True(PyFloat.IsFloatType(i));
77+
// Assert.Assert.AreEqual(i, a.ToInt32());
78+
}
79+
80+
[Test]
81+
public void StringDoubleCtor()
82+
{
83+
const string a = "4.5";
84+
var i = new PyFloat(a);
85+
Assert.True(PyFloat.IsFloatType(i));
86+
// Assert.Assert.AreEqual(i, a.ToInt32());
87+
}
88+
89+
[Test]
90+
public void StringBadCtor()
91+
{
92+
const string i = "Foo";
93+
PyFloat a = null;
94+
95+
var ex = Assert.Throws<PythonException>(() => a = new PyFloat(i));
96+
97+
StringAssert.StartsWith("ValueError : could not convert string to float", ex.Message);
98+
Assert.IsNull(a);
99+
}
100+
101+
[Test]
102+
public void IsFloatTrue()
103+
{
104+
const double a = 4.5;
105+
var i = new PyFloat(a);
106+
Assert.True(PyFloat.IsFloatType(i));
107+
}
108+
109+
[Test]
110+
public void IsFloatFalse()
111+
{
112+
var i = new PyString("Foo");
113+
Assert.False(PyFloat.IsFloatType(i));
114+
}
115+
116+
[Test]
117+
public void AsFloatGood()
118+
{
119+
const double a = 4.5;
120+
var i = new PyFloat(a);
121+
PyFloat s = PyFloat.AsFloat(i);
122+
123+
Assert.True(PyFloat.IsFloatType(s));
124+
// Assert.Assert.AreEqual(i, a.ToInt32());
125+
}
126+
127+
[Test]
128+
public void AsFloatBad()
129+
{
130+
var s = new PyString("Foo");
131+
PyFloat a = null;
132+
133+
var ex = Assert.Throws<PythonException>(() => a = PyFloat.AsFloat(s));
134+
StringAssert.StartsWith("ValueError : could not convert string to float", ex.Message);
135+
Assert.IsNull(a);
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)