|
| 1 | +namespace Python.EmbeddingTest { |
| 2 | + using System; |
| 3 | + using System.Collections.Generic; |
| 4 | + using System.Text; |
| 5 | + using NUnit.Framework; |
| 6 | + using Python.Runtime; |
| 7 | + using Python.Runtime.Codecs; |
| 8 | + |
| 9 | + public class Codecs { |
| 10 | + [SetUp] |
| 11 | + public void SetUp() { |
| 12 | + PythonEngine.Initialize(); |
| 13 | + } |
| 14 | + |
| 15 | + [TearDown] |
| 16 | + public void Dispose() { |
| 17 | + PythonEngine.Shutdown(); |
| 18 | + } |
| 19 | + |
| 20 | + [Test] |
| 21 | + public void ConversionsGeneric() { |
| 22 | + ConversionsGeneric<ValueTuple<int, string, object>, ValueTuple>(); |
| 23 | + } |
| 24 | + |
| 25 | + static void ConversionsGeneric<T, TTuple>() { |
| 26 | + TupleCodec<TTuple>.Register(); |
| 27 | + var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object()); |
| 28 | + T restored = default; |
| 29 | + using (Py.GIL()) |
| 30 | + using (var scope = Py.CreateScope()) { |
| 31 | + void Accept(T value) => restored = value; |
| 32 | + var accept = new Action<T>(Accept).ToPython(); |
| 33 | + scope.Set(nameof(tuple), tuple); |
| 34 | + scope.Set(nameof(accept), accept); |
| 35 | + scope.Exec($"{nameof(accept)}({nameof(tuple)})"); |
| 36 | + Assert.AreEqual(expected: tuple, actual: restored); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void ConversionsObject() { |
| 42 | + ConversionsObject<ValueTuple<int, string, object>, ValueTuple>(); |
| 43 | + } |
| 44 | + static void ConversionsObject<T, TTuple>() { |
| 45 | + TupleCodec<TTuple>.Register(); |
| 46 | + var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object()); |
| 47 | + T restored = default; |
| 48 | + using (Py.GIL()) |
| 49 | + using (var scope = Py.CreateScope()) { |
| 50 | + void Accept(object value) => restored = (T)value; |
| 51 | + var accept = new Action<object>(Accept).ToPython(); |
| 52 | + scope.Set(nameof(tuple), tuple); |
| 53 | + scope.Set(nameof(accept), accept); |
| 54 | + scope.Exec($"{nameof(accept)}({nameof(tuple)})"); |
| 55 | + Assert.AreEqual(expected: tuple, actual: restored); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public void TupleRoundtripObject() { |
| 61 | + TupleRoundtripObject<ValueTuple<int, string, object>, ValueTuple>(); |
| 62 | + } |
| 63 | + static void TupleRoundtripObject<T, TTuple>() { |
| 64 | + var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object()); |
| 65 | + using (Py.GIL()) { |
| 66 | + var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple); |
| 67 | + Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out object restored)); |
| 68 | + Assert.AreEqual(expected: tuple, actual: restored); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + public void TupleRoundtripGeneric() { |
| 74 | + TupleRoundtripGeneric<ValueTuple<int, string, object>, ValueTuple>(); |
| 75 | + } |
| 76 | + |
| 77 | + static void TupleRoundtripGeneric<T, TTuple>() { |
| 78 | + var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object()); |
| 79 | + using (Py.GIL()) { |
| 80 | + var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple); |
| 81 | + Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out T restored)); |
| 82 | + Assert.AreEqual(expected: tuple, actual: restored); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments