Skip to content

Commit bc8ea9b

Browse files
committed
Adjust asserts to >3 syntax
1 parent c7bc227 commit bc8ea9b

4 files changed

Lines changed: 44 additions & 52 deletions

File tree

‎src/embed_tests/Codecs.cs‎

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ static void TupleConversionsGeneric<T, TTuple>()
2525
TupleCodec<TTuple>.Register();
2626
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
2727
T restored = default;
28-
using (var scope = Py.CreateScope())
29-
{
30-
void Accept(T value) => restored = value;
31-
using var accept = new Action<T>(Accept).ToPython();
32-
scope.Set(nameof(tuple), tuple);
33-
scope.Set(nameof(accept), accept);
34-
scope.Exec($"{nameof(accept)}({nameof(tuple)})");
35-
Assert.That(actual: restored, Is.EqualTo(expected: tuple));
36-
}
28+
using var scope = Py.CreateScope();
29+
void Accept(T value) => restored = value;
30+
using var accept = new Action<T>(Accept).ToPython();
31+
scope.Set(nameof(tuple), tuple);
32+
scope.Set(nameof(accept), accept);
33+
scope.Exec($"{nameof(accept)}({nameof(tuple)})");
34+
Assert.That(actual: restored, Is.EqualTo(expected: tuple));
3735
}
3836

3937
[Test]
@@ -46,15 +44,13 @@ static void TupleConversionsObject<T, TTuple>()
4644
TupleCodec<TTuple>.Register();
4745
var tuple = Activator.CreateInstance(typeof(T), 42.0, "42", new object());
4846
T restored = default;
49-
using (var scope = Py.CreateScope())
50-
{
51-
void Accept(object value) => restored = (T)value;
52-
using var accept = new Action<object>(Accept).ToPython();
53-
scope.Set(nameof(tuple), tuple);
54-
scope.Set(nameof(accept), accept);
55-
scope.Exec($"{nameof(accept)}({nameof(tuple)})");
56-
Assert.That(actual: restored, Is.EqualTo(expected: tuple));
57-
}
47+
using var scope = Py.CreateScope();
48+
void Accept(object value) => restored = (T)value;
49+
using var accept = new Action<object>(Accept).ToPython();
50+
scope.Set(nameof(tuple), tuple);
51+
scope.Set(nameof(accept), accept);
52+
scope.Exec($"{nameof(accept)}({nameof(tuple)})");
53+
Assert.That(actual: restored, Is.EqualTo(expected: tuple));
5854
}
5955

6056
[Test]
@@ -92,7 +88,7 @@ public void ListDecoderTest()
9288
var codec = ListDecoder.Instance;
9389
var items = new List<PyObject>() { new PyInt(1), new PyInt(2), new PyInt(3) };
9490

95-
using var pyList = new PyList(items.ToArray());
91+
using var pyList = new PyList([.. items]);
9692

9793
using var pyListType = pyList.GetPythonType();
9894
Assert.IsTrue(codec.CanDecode(pyListType, typeof(IList<bool>)));
@@ -119,7 +115,7 @@ public void ListDecoderTest()
119115
IList<string> stringList = null;
120116
Assert.DoesNotThrow(() => { codec.TryDecode(pyList, out stringList); });
121117
Assert.That(3, Is.EqualTo(stringList.Count));
122-
Assert.Throws(typeof(InvalidCastException), () => { var x = stringList[0]; });
118+
Assert.Throws<InvalidCastException>(() => { var x = stringList[0]; });
123119

124120
//can't convert python iterable to list (this will require a copy which isn't lossless)
125121
using var foo = GetPythonIterable();
@@ -134,7 +130,7 @@ public void SequenceDecoderTest()
134130
var items = new List<PyObject>() { new PyInt(1), new PyInt(2), new PyInt(3) };
135131

136132
//SequenceConverter can only convert to any ICollection
137-
using var pyList = new PyList(items.ToArray());
133+
using var pyList = new PyList([.. items]);
138134
using var listType = pyList.GetPythonType();
139135
//it can convert a PyList, since PyList satisfies the python sequence protocol
140136

@@ -162,7 +158,7 @@ public void SequenceDecoderTest()
162158
//the IList will report a Count of 3.
163159
ICollection<string> stringCollection = null;
164160
Assert.DoesNotThrow(() => { codec.TryDecode(pyList, out stringCollection); });
165-
Assert.That(stringCollection.Count(), Is.EqualTo(3));
161+
Assert.That(stringCollection.Count, Is.EqualTo(3));
166162
Assert.Throws<InvalidCastException>(() => {
167163
string[] array = new string[3];
168164
stringCollection.CopyTo(array, 0);
@@ -177,7 +173,7 @@ public void SequenceDecoderTest()
177173
Assert.IsFalse(codec.CanDecode(fooType, typeof(ICollection<int>)));
178174

179175
//python tuples do satisfy the python sequence protocol
180-
var pyTuple = new PyTuple(items.ToArray());
176+
var pyTuple = new PyTuple([.. items]);
181177
var pyTupleType = pyTuple.GetPythonType();
182178

183179
Assert.IsTrue(codec.CanDecode(pyTupleType, typeof(ICollection<float>)));
@@ -215,7 +211,7 @@ public void IterableDecoderTest()
215211
var codec = IterableDecoder.Instance;
216212
var items = new List<PyObject>() { new PyInt(1), new PyInt(2), new PyInt(3) };
217213

218-
var pyList = new PyList(items.ToArray());
214+
var pyList = new PyList([.. items]);
219215
var pyListType = pyList.GetPythonType();
220216
Assert.IsFalse(codec.CanDecode(pyListType, typeof(IList<bool>)));
221217
Assert.IsTrue(codec.CanDecode(pyListType, typeof(System.Collections.IEnumerable)));
@@ -249,13 +245,13 @@ public void IterableDecoderTest()
249245
IEnumerable<string> stringEnumerable = null;
250246
Assert.DoesNotThrow(() => { codec.TryDecode(pyList, out stringEnumerable); });
251247

252-
Assert.Throws(typeof(InvalidCastException), () => {
248+
Assert.Throws<InvalidCastException>(() => {
253249
foreach (string item in stringEnumerable)
254250
{
255251
var x = item;
256252
}
257253
});
258-
Assert.Throws(typeof(InvalidCastException), () => {
254+
Assert.Throws<InvalidCastException>(() => {
259255
stringEnumerable.Count();
260256
});
261257

@@ -309,7 +305,7 @@ def CanEncode(self, clr_type):
309305
public void ExceptionEncoded()
310306
{
311307
PyObjectConversions.RegisterEncoder(new ValueErrorCodec());
312-
void CallMe() => throw new ValueErrorWrapper(TestExceptionMessage);
308+
static void CallMe() => throw new ValueErrorWrapper(TestExceptionMessage);
313309
var callMeAction = new Action(CallMe);
314310
using var scope = Py.CreateScope();
315311
scope.Exec(@"
@@ -385,7 +381,9 @@ public void ExceptionDecodedNoInstance()
385381
}
386382
}
387383

384+
#pragma warning disable IDE0060 // Remove unused parameter
388385
public static void AcceptsDateTime(DateTime v) {}
386+
#pragma warning restore IDE0060 // Remove unused parameter
389387

390388
[Test]
391389
public void As_Object_AffectedByDecoders()
@@ -412,15 +410,14 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
412410
}
413411
}
414412

415-
class ValueErrorWrapper : Exception
413+
class ValueErrorWrapper(string message) : Exception(message)
416414
{
417-
public ValueErrorWrapper(string message) : base(message) { }
418415
}
419416

420417
class ValueErrorCodec : IPyObjectEncoder, IPyObjectDecoder
421418
{
422419
public bool CanDecode(PyType objectType, Type targetType)
423-
=> this.CanEncode(targetType)
420+
=> CanEncode(targetType)
424421
&& PythonReferenceComparer.Instance.Equals(objectType, PythonEngine.Eval("ValueError"));
425422

426423
public bool CanEncode(Type type) => type == typeof(ValueErrorWrapper)
@@ -480,24 +477,18 @@ class ObjectToEncoderInstanceEncoder<T> : IPyObjectEncoder
480477
/// Decodes object of specified Python type to the predefined value <see cref="DecodeResult"/>
481478
/// </summary>
482479
/// <typeparam name="TTarget">Type of the <see cref="DecodeResult"/></typeparam>
483-
class DecoderReturningPredefinedValue<TTarget> : IPyObjectDecoder
480+
class DecoderReturningPredefinedValue<TTarget>(PyObject objectType, TTarget decodeResult) : IPyObjectDecoder
484481
{
485-
public PyObject TheOnlySupportedSourceType { get; }
486-
public TTarget DecodeResult { get; }
487-
488-
public DecoderReturningPredefinedValue(PyObject objectType, TTarget decodeResult)
489-
{
490-
this.TheOnlySupportedSourceType = objectType;
491-
this.DecodeResult = decodeResult;
492-
}
482+
public PyObject TheOnlySupportedSourceType { get; } = objectType;
483+
public TTarget DecodeResult { get; } = decodeResult;
493484

494485
public bool CanDecode(PyType objectType, Type targetType)
495486
=> PythonReferenceComparer.Instance.Equals(objectType, TheOnlySupportedSourceType)
496487
&& targetType == typeof(TTarget);
497488
public bool TryDecode<T>(PyObject pyObj, out T value)
498489
{
499490
if (typeof(T) != typeof(TTarget))
500-
throw new ArgumentException(nameof(T));
491+
throw new ArgumentException(null, nameof(T));
501492
value = (T)(object)DecodeResult;
502493
return true;
503494
}

‎src/embed_tests/Dynamic.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public void AssignNone()
3535
{
3636
dynamic sys = Py.Import("sys");
3737
sys.testattr = new StringBuilder();
38-
Assert.IsNotNull(sys.testattr);
38+
Assert.That(sys.testattr, Is.Not.Null);
3939

4040
sys.testattr = null;
41-
Assert.IsNull(sys.testattr);
41+
Assert.That(sys.testattr, Is.Null);
4242
}
4343

4444
/// <summary>
@@ -80,7 +80,7 @@ public void PassObjectInPython()
8080
Assert.That(sys.testattr3.ToString(), Is.EqualTo("True"));
8181

8282
// Compare in .NET
83-
Assert.IsTrue(sys.testattr1.Equals(sys.testattr2));
83+
Assert.That(sys.testattr1, Is.EqualTo(sys.testattr2));
8484
}
8585

8686
/// <summary>
@@ -103,7 +103,7 @@ public void PassPyObjectInNet()
103103
Assert.That(sys.testattr3.ToString(), Is.EqualTo("True"));
104104

105105
// Compare in .NET
106-
Assert.IsTrue(sys.testattr1.Equals(sys.testattr2));
106+
Assert.That(sys.testattr1, Is.EqualTo(sys.testattr2));
107107
}
108108

109109
// regression test for https://github.com/pythonnet/pythonnet/issues/1848
@@ -119,13 +119,13 @@ class MyEnum(enum.IntEnum):
119119
ERROR = 2
120120
121121
def get_status():
122-
return MyEnum.OK
122+
return MyEnum.OK
123123
"
124124
);
125125

126126
dynamic MyEnum = scope.Get("MyEnum");
127127
dynamic status = scope.Get("get_status").Invoke();
128-
Assert.IsTrue(status == MyEnum.OK);
128+
Assert.That(status, Is.EqualTo(MyEnum.OK));
129129
}
130130

131131
// regression test for https://github.com/pythonnet/pythonnet/issues/1680

‎src/embed_tests/NumPyTests.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ public void VarArg()
6767
{
6868
dynamic zX = np.array(new[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 8, 9, 0 } });
6969
dynamic grad = np.gradient(zX, 4.0, 5.0);
70-
dynamic grad2 = np.InvokeMethod("gradient", new PyObject[] {zX, new PyFloat(4.0), new PyFloat(5.0)});
70+
dynamic grad2 = np.InvokeMethod("gradient", new PyObject[] { zX, new PyFloat(4.0), new PyFloat(5.0) });
7171

72-
Assert.AreEqual(4.125, grad[0].sum().__float__().As<double>(), 0.001);
73-
Assert.AreEqual(-1.2, grad[1].sum().__float__().As<double>(), 0.001);
74-
Assert.AreEqual(4.125, grad2[0].sum().__float__().As<double>(), 0.001);
75-
Assert.AreEqual(-1.2, grad2[1].sum().__float__().As<double>(), 0.001);
72+
Assert.That(grad[0].sum().__float__().As<double>(), Is.EqualTo(4.125).Within(0.001));
73+
Assert.That(grad[1].sum().__float__().As<double>(), Is.EqualTo(-1.2).Within(0.001));
74+
Assert.That(grad2[0].sum().__float__().As<double>(), Is.EqualTo(4.125).Within(0.001));
75+
Assert.That(grad2[1].sum().__float__().As<double>(), Is.EqualTo(-1.2).Within(0.001));
7676
}
7777

7878
#pragma warning disable IDE1006

‎src/embed_tests/TestFinalizer.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public void CollectBasicObject()
6767

6868
Warn.If(shortWeak.IsAlive,
6969
"shortWeak is alive after FullGCCollect; runtime hasn't reclaimed the wrapper yet",
70-
shortWeak);
70+
shortWeak.ToString()
71+
);
7172
// longWeak.IsAlive at this point is .NET-GC-implementation-defined
7273
// (Framework reclaims post-finalize objects more eagerly than Core);
7374
// intentionally not asserted.

0 commit comments

Comments
 (0)