diff --git a/src/Neo.SmartContract.Testing/Extensions/TestExtensions.cs b/src/Neo.SmartContract.Testing/Extensions/TestExtensions.cs index a95043eee..ee06d8edd 100644 --- a/src/Neo.SmartContract.Testing/Extensions/TestExtensions.cs +++ b/src/Neo.SmartContract.Testing/Extensions/TestExtensions.cs @@ -84,8 +84,8 @@ _ when typeof(IInteroperable).IsAssignableFrom(type) => CreateInteroperable(stac _ when stackItem is VM.Types.Array ar => type switch { - _ when type == typeof(IList) => new List(ar.SubItems), // SubItems in StackItem type - _ when type == typeof(List) => new List(ar.SubItems), // SubItems in StackItem type + _ when type == typeof(IList) => new List(ar.SubItems.Select(ConvertToBaseValue)), // SubItems in StackItem type except bool, buffer and int + _ when type == typeof(List) => new List(ar.SubItems.Select(ConvertToBaseValue)), // SubItems in StackItem type except bool, buffer and int _ when type.IsArray => CreateTypeArray(ar.SubItems, type.GetElementType()!), _ when type.IsClass => CreateObject(ar.SubItems, type), _ when type.IsValueType => CreateValueType(ar.SubItems, type), @@ -93,8 +93,8 @@ _ when typeof(IInteroperable).IsAssignableFrom(type) => CreateInteroperable(stac }, _ when stackItem is Map mp => type switch { - _ when type == typeof(IDictionary) => ToDictionary(mp), // SubItems in StackItem type - _ when type == typeof(Dictionary) => ToDictionary(mp), // SubItems in StackItem type + _ when type == typeof(IDictionary) => ToDictionary(mp), // SubItems in StackItem type except bool, buffer and int + _ when type == typeof(Dictionary) => ToDictionary(mp), // SubItems in StackItem type except bool, buffer and int _ => throw new FormatException($"Impossible to convert {stackItem} to {type}"), }, @@ -102,6 +102,17 @@ _ when typeof(IInteroperable).IsAssignableFrom(type) => CreateInteroperable(stac }; } + private static object ConvertToBaseValue(StackItem u) + { + // if (u is Null) return null; // it require nullable return + if (u is Integer i) return i.GetInteger(); + if (u is VM.Types.Boolean b) return b.GetBoolean(); + if (u is VM.Types.Buffer bf) return bf.GetSpan().ToArray(); + // if (u is ByteString s) return s.GetString(); // it could be a byte[] + + return u; + } + private static object CreateObject(IEnumerable subItems, Type type) { var index = 0; @@ -163,7 +174,7 @@ private static IDictionary ToDictionary(Map map) foreach (var entry in map) { - dictionary.Add(entry.Key, entry.Value); + dictionary.Add(ConvertToBaseValue(entry.Key), ConvertToBaseValue(entry.Value)); } return dictionary; @@ -195,7 +206,7 @@ private static object CreateValueType(IEnumerable objects, Type value return value; } - private static object CreateTypeArray(IEnumerable objects, Type elementType) + private static System.Array CreateTypeArray(IEnumerable objects, Type elementType) { var obj = objects.ToArray(); @@ -214,7 +225,7 @@ private static object CreateTypeArray(IEnumerable objects, Type eleme return obj; } - private static object CreateInteroperable(StackItem stackItem, Type type) + private static IInteroperable CreateInteroperable(StackItem stackItem, Type type) { var interoperable = (IInteroperable)Activator.CreateInstance(type)!; interoperable.FromStackItem(stackItem); diff --git a/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Lambda.cs b/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Lambda.cs index af00c451b..2e137f784 100644 --- a/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Lambda.cs +++ b/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Lambda.cs @@ -1,8 +1,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.SmartContract.Testing; using Neo.SmartContract.Testing.TestingStandards; -using Neo.VM.Types; using System.Collections.Generic; +using System.Numerics; namespace Neo.Compiler.CSharp.UnitTests { @@ -67,9 +67,9 @@ public void Test_WhereGreaterThanZero() result = Contract.WhereGreaterThanZero(array); Assert.AreEqual(3, result!.Count); - Assert.AreEqual(1, (result[0] as Integer)!.GetInteger()); - Assert.AreEqual(100, (result[1] as Integer)!.GetInteger()); - Assert.AreEqual(56, (result[2] as Integer)!.GetInteger()); + Assert.AreEqual(new BigInteger(1), result[0]); + Assert.AreEqual(new BigInteger(100), result[1]); + Assert.AreEqual(new BigInteger(56), result[2]); } [TestMethod] @@ -83,7 +83,7 @@ public void Test_ForEachVar() }; var result = Contract.ForEachVar(array); Assert.AreEqual(array.Count, result!.Count); - Assert.AreEqual(-100, (result[0] as Integer)!.GetInteger()); + Assert.AreEqual(new BigInteger(-100), result[0]); } [TestMethod] @@ -97,7 +97,7 @@ public void Test_ForVar() }; var result = Contract.ForVar(array); Assert.AreEqual(array.Count, result!.Count); - Assert.AreEqual(-100, (result[0] as Integer)!.GetInteger()); + Assert.AreEqual(new BigInteger(-100), result[0]); } [TestMethod] diff --git a/tests/Neo.SmartContract.Framework.UnitTests/ListTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/ListTest.cs index 44a08af72..bf45f233a 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/ListTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/ListTest.cs @@ -1,3 +1,4 @@ +using System.Numerics; using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Json; using Neo.SmartContract.Testing; @@ -65,8 +66,7 @@ public void TestArrayConvert() Assert.AreEqual(4, array.Count); for (int i = 0; i < 4; i++) { - Assert.IsTrue(array[i] is Integer); - Assert.AreEqual(i, ((Integer)array[i]).GetInteger()); + Assert.AreEqual(new BigInteger(i), array[i]); } }