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

Skip to content

Commit 5070db0

Browse files
committed
Add tests/refactor existing
1 parent cef6153 commit 5070db0

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

src/embed_tests/dynamic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Python.EmbeddingTest
77
{
8-
public class dynamicTest
8+
public class DynamicTest
99
{
1010
private Py.GILState gil;
1111

src/embed_tests/pythonexception.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ public void Dispose()
3333
public void TestMessage()
3434
{
3535
var list = new PyList();
36-
try
37-
{
38-
PyObject junk = list[0];
39-
}
40-
catch (PythonException e)
41-
{
42-
Assert.AreEqual("IndexError : list index out of range", e.Message);
43-
}
36+
PyObject foo = null;
37+
38+
var ex = Assert.Throws<PythonException>(() => foo = list[0]);
39+
40+
Assert.AreEqual("IndexError : list index out of range", ex.Message);
41+
Assert.IsNull(foo);
4442
}
4543

4644
[Test]

src/embed_tests/pytuple.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ public void TestPyTupleEmpty()
4343
}
4444
}
4545

46+
[Test]
47+
public void TestPyTupleBadCtor()
48+
{
49+
using (Py.GIL())
50+
{
51+
var i = new PyInt(5);
52+
PyTuple t = null;
53+
54+
var ex = Assert.Throws<ArgumentException>(() => t = new PyTuple(i));
55+
56+
Assert.AreEqual("object is not a tuple", ex.Message);
57+
Assert.IsNull(t);
58+
}
59+
}
60+
4661
/// <summary>
4762
/// Test PyTuple.Concat(...) doesn't let invalid appends happen
4863
/// and throws and exception.
@@ -51,6 +66,8 @@ public void TestPyTupleEmpty()
5166
/// Test has second purpose. Currently it generated an Exception
5267
/// that the GC failed to remove often and caused AppDomain unload
5368
/// errors at the end of tests. See GH#397 for more info.
69+
/// <para />
70+
/// Curious, on PY27 it gets a Unicode on the ex.Message. On PY3+ its string.
5471
/// </remarks>
5572
[Test]
5673
public void TestPyTupleInvalidAppend()
@@ -59,7 +76,12 @@ public void TestPyTupleInvalidAppend()
5976
{
6077
PyObject s = new PyString("foo");
6178
var t = new PyTuple();
62-
Assert.Throws<PythonException>(() => t.Concat(s));
79+
80+
var ex = Assert.Throws<PythonException>(() => t.Concat(s));
81+
82+
StringAssert.StartsWith("TypeError : can only concatenate tuple", ex.Message);
83+
Assert.AreEqual(0, t.Length());
84+
Assert.IsEmpty(t);
6385
}
6486
}
6587

@@ -114,5 +136,23 @@ public void TestNewPyTupleFromPyTuple()
114136
Assert.IsInstanceOf(typeof(PyTuple), t);
115137
}
116138
}
139+
140+
/// <remarks>
141+
/// TODO: Should this throw ArgumentError instead?
142+
/// </remarks>
143+
[Test]
144+
public void TestInvalidAsTuple()
145+
{
146+
using (Py.GIL())
147+
{
148+
var i = new PyInt(5);
149+
PyTuple t = null;
150+
151+
var ex = Assert.Throws<PythonException>(() => t = PyTuple.AsTuple(i));
152+
153+
Assert.AreEqual("TypeError : 'int' object is not iterable", ex.Message);
154+
Assert.IsNull(t);
155+
}
156+
}
117157
}
118158
}

0 commit comments

Comments
 (0)