Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
/pythonnetPublic archive
forked frompythonnet/pythonnet

Commit977ee96

Browse files
committed
Add Tests
1 parentd7777b9 commit977ee96

10 files changed

+912
-11
lines changed

‎src/embed_tests/Python.EmbeddingTest.csproj

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

‎src/embed_tests/TestCustomMarshal.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
usingSystem;
2+
usingNUnit.Framework;
3+
usingPython.Runtime;
4+
5+
namespacePython.EmbeddingTest
6+
{
7+
publicclassTestCustomMarshal
8+
{
9+
[Test]
10+
publicstaticvoidGetManagedStringTwice()
11+
{
12+
conststringexpected="FooBar";
13+
using(Py.GIL())
14+
{
15+
IntPtrop=Runtime.Runtime.PyUnicode_FromString(expected);
16+
strings1=Runtime.Runtime.GetManagedString(op);
17+
strings2=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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
usingSystem;
2+
usingNUnit.Framework;
3+
usingPython.Runtime;
4+
5+
namespacePython.EmbeddingTest
6+
{
7+
publicclassTestPyAnsiString
8+
{
9+
[OneTimeSetUp]
10+
publicvoidSetUp()
11+
{
12+
PythonEngine.Initialize();
13+
}
14+
15+
[OneTimeTearDown]
16+
publicvoidDispose()
17+
{
18+
PythonEngine.Shutdown();
19+
}
20+
21+
[Test]
22+
publicvoidTestStringCtor()
23+
{
24+
conststringexpected="foo";
25+
varactual=newPyAnsiString(expected);
26+
Assert.AreEqual(expected,actual.ToString());
27+
}
28+
29+
[Test]
30+
publicvoidTestEmptyStringCtor()
31+
{
32+
conststringexpected="";
33+
varactual=newPyAnsiString(expected);
34+
Assert.AreEqual(expected,actual.ToString());
35+
}
36+
37+
[Test]
38+
publicvoidTestPyObjectCtor()
39+
{
40+
conststringexpected="Foo";
41+
42+
vart=newPyAnsiString(expected);
43+
varactual=newPyAnsiString(t);
44+
45+
Assert.AreEqual(expected,actual.ToString());
46+
}
47+
48+
[Test]
49+
publicvoidTestBadPyObjectCtor()
50+
{
51+
vart=newPyInt(5);
52+
PyAnsiStringactual=null;
53+
54+
varex=Assert.Throws<ArgumentException>(()=>actual=newPyAnsiString(t));
55+
56+
StringAssert.StartsWith("object is not a string",ex.Message);
57+
Assert.IsNull(actual);
58+
}
59+
60+
[Test]
61+
publicvoidTestCtorPtr()
62+
{
63+
conststringexpected="foo";
64+
65+
vart=newPyAnsiString(expected);
66+
varactual=newPyAnsiString(t.Handle);
67+
68+
Assert.AreEqual(expected,actual.ToString());
69+
}
70+
71+
[Test]
72+
publicvoidIsStringTrue()
73+
{
74+
vart=newPyAnsiString("foo");
75+
76+
Assert.True(PyAnsiString.IsStringType(t));
77+
}
78+
79+
[Test]
80+
publicvoidIsStringFalse()
81+
{
82+
vart=newPyInt(5);
83+
84+
Assert.False(PyAnsiString.IsStringType(t));
85+
}
86+
87+
[Test]
88+
[Ignore("Ambiguous behavior between PY2/PY3")]
89+
publicvoidTestUnicode()
90+
{
91+
conststringexpected="foo\u00e9";
92+
PyObjectactual=newPyAnsiString(expected);
93+
Assert.AreEqual(expected,actual.ToString());
94+
}
95+
}
96+
}

‎src/embed_tests/TestPyFloat.cs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
usingSystem;
2+
usingNUnit.Framework;
3+
usingPython.Runtime;
4+
5+
namespacePython.EmbeddingTest
6+
{
7+
/// <remarks>
8+
/// PyFloat implementation isn't complete, thus tests aren't complete.
9+
/// </remarks>
10+
publicclassTestPyFloat
11+
{
12+
[OneTimeSetUp]
13+
publicvoidSetUp()
14+
{
15+
PythonEngine.Initialize();
16+
}
17+
18+
[OneTimeTearDown]
19+
publicvoidDispose()
20+
{
21+
PythonEngine.Shutdown();
22+
}
23+
24+
[Test]
25+
publicvoidIntPtrCtor()
26+
{
27+
vari=newPyFloat(1);
28+
varii=newPyFloat(i.Handle);
29+
Assert.AreEqual(i.Handle,ii.Handle);
30+
}
31+
32+
[Test]
33+
publicvoidFloatCtor()
34+
{
35+
constfloata=4.5F;
36+
vari=newPyFloat(a);
37+
Assert.True(PyFloat.IsFloatType(i));
38+
// Assert.Assert.AreEqual(i, a.ToInt32());
39+
}
40+
41+
[Test]
42+
publicvoidPyObjectCtorGood()
43+
{
44+
vari=newPyFloat(5);
45+
vara=newPyFloat(i);
46+
Assert.True(PyFloat.IsFloatType(a));
47+
// Assert.Assert.AreEqual(i, a.ToInt32());
48+
}
49+
50+
[Test]
51+
publicvoidPyObjectCtorBad()
52+
{
53+
vari=newPyString("Foo");
54+
PyFloata=null;
55+
56+
varex=Assert.Throws<ArgumentException>(()=>a=newPyFloat(i));
57+
58+
StringAssert.StartsWith("object is not a float",ex.Message);
59+
Assert.IsNull(a);
60+
}
61+
62+
[Test]
63+
publicvoidDoubleCtor()
64+
{
65+
constdoublea=4.5;
66+
vari=newPyFloat(a);
67+
Assert.True(PyFloat.IsFloatType(i));
68+
// Assert.Assert.AreEqual(i, a.ToInt32());
69+
}
70+
71+
[Test]
72+
publicvoidStringIntCtor()
73+
{
74+
conststringa="5";
75+
vari=newPyFloat(a);
76+
Assert.True(PyFloat.IsFloatType(i));
77+
// Assert.Assert.AreEqual(i, a.ToInt32());
78+
}
79+
80+
[Test]
81+
publicvoidStringDoubleCtor()
82+
{
83+
conststringa="4.5";
84+
vari=newPyFloat(a);
85+
Assert.True(PyFloat.IsFloatType(i));
86+
// Assert.Assert.AreEqual(i, a.ToInt32());
87+
}
88+
89+
[Test]
90+
publicvoidStringBadCtor()
91+
{
92+
conststringi="Foo";
93+
PyFloata=null;
94+
95+
varex=Assert.Throws<PythonException>(()=>a=newPyFloat(i));
96+
97+
StringAssert.StartsWith("ValueError : could not convert string to float",ex.Message);
98+
Assert.IsNull(a);
99+
}
100+
101+
[Test]
102+
publicvoidIsFloatTrue()
103+
{
104+
constdoublea=4.5;
105+
vari=newPyFloat(a);
106+
Assert.True(PyFloat.IsFloatType(i));
107+
}
108+
109+
[Test]
110+
publicvoidIsFloatFalse()
111+
{
112+
vari=newPyString("Foo");
113+
Assert.False(PyFloat.IsFloatType(i));
114+
}
115+
116+
[Test]
117+
publicvoidAsFloatGood()
118+
{
119+
constdoublea=4.5;
120+
vari=newPyFloat(a);
121+
PyFloats=PyFloat.AsFloat(i);
122+
123+
Assert.True(PyFloat.IsFloatType(s));
124+
// Assert.Assert.AreEqual(i, a.ToInt32());
125+
}
126+
127+
[Test]
128+
publicvoidAsFloatBad()
129+
{
130+
vars=newPyString("Foo");
131+
PyFloata=null;
132+
133+
varex=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)

[8]ページ先頭

©2009-2025 Movatter.jp