Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 657d674

Browse files
committed
Clean-up Embedded tests
1 parent 6e4cf9d commit 657d674

11 files changed

+140
-149
lines changed

src/embed_tests/TestCustomMarshal.cs

+19-9
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,30 @@ namespace Python.EmbeddingTest
66
{
77
public class TestCustomMarshal
88
{
9+
[OneTimeSetUp]
10+
public void SetUp()
11+
{
12+
PythonEngine.Initialize();
13+
}
14+
15+
[OneTimeTearDown]
16+
public void Dispose()
17+
{
18+
PythonEngine.Shutdown();
19+
}
20+
921
[Test]
1022
public static void GetManagedStringTwice()
1123
{
1224
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);
1825

19-
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
20-
Assert.AreEqual(expected, s1);
21-
Assert.AreEqual(expected, s2);
22-
}
26+
IntPtr op = Runtime.Runtime.PyUnicode_FromString(expected);
27+
string s1 = Runtime.Runtime.GetManagedString(op);
28+
string s2 = Runtime.Runtime.GetManagedString(op);
29+
30+
Assert.AreEqual(1, Runtime.Runtime.Refcount(op));
31+
Assert.AreEqual(expected, s1);
32+
Assert.AreEqual(expected, s2);
2333
}
2434
}
2535
}

src/embed_tests/TestPyInt.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ public void TestCtorBadString()
134134
public void TestIsIntTypeTrue()
135135
{
136136
var i = new PyInt(5);
137-
Assert.IsTrue(PyInt.IsIntType(i));
137+
Assert.True(PyInt.IsIntType(i));
138138
}
139139

140140
[Test]
141141
public void TestIsIntTypeFalse()
142142
{
143143
var s = new PyString("Foo");
144-
Assert.IsFalse(PyInt.IsIntType(s));
144+
Assert.False(PyInt.IsIntType(s));
145145
}
146146

147147
[Test]

src/embed_tests/TestPyList.cs

+25-14
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,36 @@ namespace Python.EmbeddingTest
77
{
88
public class TestPyList
99
{
10+
[OneTimeSetUp]
11+
public void SetUp()
12+
{
13+
PythonEngine.Initialize();
14+
}
15+
16+
[OneTimeTearDown]
17+
public void Dispose()
18+
{
19+
PythonEngine.Shutdown();
20+
}
21+
1022
[Test]
1123
public void TestOnPyList()
1224
{
13-
using (Py.GIL())
25+
var list = new PyList();
26+
27+
list.Append(new PyString("foo"));
28+
list.Append(new PyString("bar"));
29+
list.Append(new PyString("baz"));
30+
var result = new List<string>();
31+
foreach (PyObject item in list)
1432
{
15-
var list = new PyList();
16-
list.Append(new PyString("foo"));
17-
list.Append(new PyString("bar"));
18-
list.Append(new PyString("baz"));
19-
var result = new List<string>();
20-
foreach (PyObject item in list)
21-
{
22-
result.Add(item.ToString());
23-
}
24-
Assert.AreEqual(3, result.Count);
25-
Assert.AreEqual("foo", result[0]);
26-
Assert.AreEqual("bar", result[1]);
27-
Assert.AreEqual("baz", result[2]);
33+
result.Add(item.ToString());
2834
}
35+
36+
Assert.AreEqual(3, result.Count);
37+
Assert.AreEqual("foo", result[0]);
38+
Assert.AreEqual("bar", result[1]);
39+
Assert.AreEqual("baz", result[2]);
2940
}
3041
}
3142
}

src/embed_tests/TestPyLong.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ public void TestCtorBadString()
150150
public void TestIsIntTypeTrue()
151151
{
152152
var i = new PyLong(5);
153-
Assert.IsTrue(PyLong.IsLongType(i));
153+
Assert.True(PyLong.IsLongType(i));
154154
}
155155

156156
[Test]
157157
public void TestIsLongTypeFalse()
158158
{
159159
var s = new PyString("Foo");
160-
Assert.IsFalse(PyLong.IsLongType(s));
160+
Assert.False(PyLong.IsLongType(s));
161161
}
162162

163163
[Test]

src/embed_tests/TestPyNumber.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public void Dispose()
2222
public void IsNumberTypeTrue()
2323
{
2424
var i = new PyInt(1);
25-
Assert.IsTrue(PyNumber.IsNumberType(i));
25+
Assert.True(PyNumber.IsNumberType(i));
2626
}
2727

2828
[Test]
2929
public void IsNumberTypeFalse()
3030
{
3131
var s = new PyString("Foo");
32-
Assert.IsFalse(PyNumber.IsNumberType(s));
32+
Assert.False(PyNumber.IsNumberType(s));
3333
}
3434
}
3535
}

src/embed_tests/TestPyTuple.cs

+67-90
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@ namespace Python.EmbeddingTest
66
{
77
public class TestPyTuple
88
{
9+
[OneTimeSetUp]
10+
public void SetUp()
11+
{
12+
PythonEngine.Initialize();
13+
}
14+
15+
[OneTimeTearDown]
16+
public void Dispose()
17+
{
18+
PythonEngine.Shutdown();
19+
}
20+
921
/// <summary>
1022
/// Test IsTupleType without having to Initialize a tuple.
1123
/// PyTuple constructor use IsTupleType. This decouples the tests.
1224
/// </summary>
1325
[Test]
1426
public void TestStringIsTupleType()
1527
{
16-
using (Py.GIL())
17-
{
18-
var s = new PyString("foo");
19-
Assert.IsFalse(PyTuple.IsTupleType(s));
20-
}
28+
var s = new PyString("foo");
29+
Assert.False(PyTuple.IsTupleType(s));
2130
}
2231

2332
/// <summary>
@@ -26,72 +35,54 @@ public void TestStringIsTupleType()
2635
[Test]
2736
public void TestPyTupleIsTupleType()
2837
{
29-
using (Py.GIL())
30-
{
31-
var t = new PyTuple();
32-
Assert.IsTrue(PyTuple.IsTupleType(t));
33-
}
38+
var t = new PyTuple();
39+
Assert.True(PyTuple.IsTupleType(t));
3440
}
3541

3642
[Test]
3743
public void TestPyTupleEmpty()
3844
{
39-
using (Py.GIL())
40-
{
41-
var t = new PyTuple();
42-
Assert.AreEqual(0, t.Length());
43-
}
45+
var t = new PyTuple();
46+
Assert.AreEqual(0, t.Length());
4447
}
4548

4649
[Test]
4750
public void TestPyTupleBadCtor()
4851
{
49-
using (Py.GIL())
50-
{
51-
var i = new PyInt(5);
52-
PyTuple t = null;
52+
var i = new PyInt(5);
53+
PyTuple t = null;
5354

54-
var ex = Assert.Throws<ArgumentException>(() => t = new PyTuple(i));
55+
var ex = Assert.Throws<ArgumentException>(() => t = new PyTuple(i));
5556

56-
Assert.AreEqual("object is not a tuple", ex.Message);
57-
Assert.IsNull(t);
58-
}
57+
Assert.AreEqual("object is not a tuple", ex.Message);
58+
Assert.IsNull(t);
5959
}
6060

6161
[Test]
6262
public void TestPyTupleCtorEmptyArray()
6363
{
64-
using (Py.GIL())
65-
{
66-
var a = new PyObject[] { };
67-
var t = new PyTuple(a);
64+
var a = new PyObject[] { };
65+
var t = new PyTuple(a);
6866

69-
Assert.AreEqual(0, t.Length());
70-
}
67+
Assert.AreEqual(0, t.Length());
7168
}
7269

7370
[Test]
7471
public void TestPyTupleCtorArrayPyIntEmpty()
7572
{
76-
using (Py.GIL())
77-
{
78-
var a = new PyInt[] { };
79-
var t = new PyTuple(a);
73+
var a = new PyInt[] { };
74+
var t = new PyTuple(a);
8075

81-
Assert.AreEqual(0, t.Length());
82-
}
76+
Assert.AreEqual(0, t.Length());
8377
}
8478

8579
[Test]
8680
public void TestPyTupleCtorArray()
8781
{
88-
using (Py.GIL())
89-
{
90-
var a = new PyObject[] {new PyInt(1), new PyString("Foo") };
91-
var t = new PyTuple(a);
82+
var a = new PyObject[] { new PyInt(1), new PyString("Foo") };
83+
var t = new PyTuple(a);
9284

93-
Assert.AreEqual(2, t.Length());
94-
}
85+
Assert.AreEqual(2, t.Length());
9586
}
9687

9788
/// <summary>
@@ -108,69 +99,58 @@ public void TestPyTupleCtorArray()
10899
[Test]
109100
public void TestPyTupleInvalidAppend()
110101
{
111-
using (Py.GIL())
112-
{
113-
PyObject s = new PyString("foo");
114-
var t = new PyTuple();
102+
PyObject s = new PyString("foo");
103+
var t = new PyTuple();
115104

116-
var ex = Assert.Throws<PythonException>(() => t.Concat(s));
105+
var ex = Assert.Throws<PythonException>(() => t.Concat(s));
117106

118-
StringAssert.StartsWith("TypeError : can only concatenate tuple", ex.Message);
119-
Assert.AreEqual(0, t.Length());
120-
Assert.IsEmpty(t);
121-
}
107+
StringAssert.StartsWith("TypeError : can only concatenate tuple", ex.Message);
108+
Assert.AreEqual(0, t.Length());
109+
Assert.IsEmpty(t);
122110
}
123111

124112
[Test]
125113
public void TestPyTupleValidAppend()
126114
{
127-
using (Py.GIL())
128-
{
129-
var t0 = new PyTuple();
130-
var t = new PyTuple();
131-
t.Concat(t0);
132-
Assert.IsNotNull(t);
133-
Assert.IsInstanceOf(typeof(PyTuple), t);
134-
}
115+
var t0 = new PyTuple();
116+
var t = new PyTuple();
117+
t.Concat(t0);
118+
119+
Assert.IsNotNull(t);
120+
Assert.IsInstanceOf(typeof(PyTuple), t);
135121
}
136122

137123
[Test]
138124
public void TestPyTupleStringConvert()
139125
{
140-
using (Py.GIL())
141-
{
142-
PyObject s = new PyString("foo");
143-
PyTuple t = PyTuple.AsTuple(s);
144-
Assert.IsNotNull(t);
145-
Assert.IsInstanceOf(typeof(PyTuple), t);
146-
Assert.AreEqual("f", t[0].ToString());
147-
Assert.AreEqual("o", t[1].ToString());
148-
Assert.AreEqual("o", t[2].ToString());
149-
}
126+
PyObject s = new PyString("foo");
127+
PyTuple t = PyTuple.AsTuple(s);
128+
129+
Assert.IsNotNull(t);
130+
Assert.IsInstanceOf(typeof(PyTuple), t);
131+
Assert.AreEqual("f", t[0].ToString());
132+
Assert.AreEqual("o", t[1].ToString());
133+
Assert.AreEqual("o", t[2].ToString());
150134
}
151135

152136
[Test]
153137
public void TestPyTupleValidConvert()
154138
{
155-
using (Py.GIL())
156-
{
157-
var l = new PyList();
158-
PyTuple t = PyTuple.AsTuple(l);
159-
Assert.IsNotNull(t);
160-
Assert.IsInstanceOf(typeof(PyTuple), t);
161-
}
139+
var l = new PyList();
140+
PyTuple t = PyTuple.AsTuple(l);
141+
142+
Assert.IsNotNull(t);
143+
Assert.IsInstanceOf(typeof(PyTuple), t);
162144
}
163145

164146
[Test]
165147
public void TestNewPyTupleFromPyTuple()
166148
{
167-
using (Py.GIL())
168-
{
169-
var t0 = new PyTuple();
170-
var t = new PyTuple(t0);
171-
Assert.IsNotNull(t);
172-
Assert.IsInstanceOf(typeof(PyTuple), t);
173-
}
149+
var t0 = new PyTuple();
150+
var t = new PyTuple(t0);
151+
152+
Assert.IsNotNull(t);
153+
Assert.IsInstanceOf(typeof(PyTuple), t);
174154
}
175155

176156
/// <remarks>
@@ -179,16 +159,13 @@ public void TestNewPyTupleFromPyTuple()
179159
[Test]
180160
public void TestInvalidAsTuple()
181161
{
182-
using (Py.GIL())
183-
{
184-
var i = new PyInt(5);
185-
PyTuple t = null;
162+
var i = new PyInt(5);
163+
PyTuple t = null;
186164

187-
var ex = Assert.Throws<PythonException>(() => t = PyTuple.AsTuple(i));
165+
var ex = Assert.Throws<PythonException>(() => t = PyTuple.AsTuple(i));
188166

189-
Assert.AreEqual("TypeError : 'int' object is not iterable", ex.Message);
190-
Assert.IsNull(t);
191-
}
167+
Assert.AreEqual("TypeError : 'int' object is not iterable", ex.Message);
168+
Assert.IsNull(t);
192169
}
193170
}
194171
}

0 commit comments

Comments
 (0)