Skip to content

Commit fe165c5

Browse files
cursoragentipetrov
andcommitted
Refactor ListEncoderDecoderTests to use EvaluatePythonScript
Co-authored-by: ipetrov <[email protected]>
1 parent 6ad210a commit fe165c5

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed
Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System.Collections;
2-
using DSPythonNet3;
3-
using DSPythonNet3.Encoders;
42
using NUnit.Framework;
5-
using Python.Runtime;
3+
using DSPythonNet3;
64

75
namespace DSPythonNet3Tests
86
{
@@ -13,35 +11,46 @@ public void TryDecode_ConvertsNestedPythonListsToClrLists()
1311
{
1412
DSPythonNet3Evaluator.InitializePython();
1513

16-
using (Py.GIL())
17-
using (var scope = Py.CreateScope())
18-
{
19-
scope.Exec("value = [[1, [2, 3]], ['a', ['b']]]");
20-
using var pyList = scope.Get("value");
21-
22-
var decoder = new ListEncoderDecoder();
23-
var success = decoder.TryDecode(pyList, out IList result);
14+
string code = @"
15+
import clr
16+
clr.AddReference('DSCoreNodes')
17+
from DSCore import List
2418
25-
Assert.That(success, Is.True);
26-
Assert.That(result, Is.InstanceOf<IList>());
27-
28-
var first = result[0] as IList;
29-
Assert.That(first, Is.Not.Null);
30-
Assert.That(first[0], Is.EqualTo(1));
31-
32-
var secondLevel = first?[1] as IList;
33-
Assert.That(secondLevel, Is.Not.Null);
34-
Assert.That(secondLevel?[0], Is.EqualTo(2));
35-
Assert.That(secondLevel?[1], Is.EqualTo(3));
19+
data = [[1, 2, 3], [4, 5, 6]]
20+
OUT = data, List.Flatten(data, -1)
21+
";
22+
var empty = new ArrayList();
23+
var expected = new ArrayList
24+
{
25+
new ArrayList
26+
{
27+
new ArrayList { 1, 2, 3 },
28+
new ArrayList { 4, 5, 6 }
29+
},
30+
new ArrayList { 1, 2, 3, 4, 5, 6 }
31+
};
32+
33+
var result = DSPythonNet3Evaluator.EvaluatePythonScript(code, empty, empty);
34+
Assert.That(result, Is.InstanceOf<IEnumerable>());
35+
36+
var normalizedResult = NormalizeResult(result);
37+
CollectionAssert.AreEqual(expected, normalizedResult as IEnumerable);
38+
}
3639

37-
var second = result[1] as IList;
38-
Assert.That(second, Is.Not.Null);
39-
Assert.That(second?[0], Is.EqualTo("a"));
40+
private static object NormalizeResult(object value)
41+
{
42+
if (value is string || value is not IEnumerable enumerable)
43+
{
44+
return value;
45+
}
4046

41-
var thirdLevel = second?[1] as IList;
42-
Assert.That(thirdLevel, Is.Not.Null);
43-
Assert.That(thirdLevel?[0], Is.EqualTo("b"));
47+
var list = new ArrayList();
48+
foreach (var item in enumerable)
49+
{
50+
list.Add(NormalizeResult(item));
4451
}
52+
53+
return list;
4554
}
4655
}
4756
}

0 commit comments

Comments
 (0)