Skip to content

Commit

Permalink
Convert basic types in tests (neo-project#1031)
Browse files Browse the repository at this point in the history
* Convert basic types int tests

* clean using

* fix error

---------

Co-authored-by: Jim8y <[email protected]>
  • Loading branch information
shargon and Jim8y committed Apr 23, 2024
1 parent 875d412 commit dd17081
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
25 changes: 18 additions & 7 deletions src/Neo.SmartContract.Testing/Extensions/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,35 @@ _ when typeof(IInteroperable).IsAssignableFrom(type) => CreateInteroperable(stac

_ when stackItem is VM.Types.Array ar => type switch
{
_ when type == typeof(IList<object>) => new List<object>(ar.SubItems), // SubItems in StackItem type
_ when type == typeof(List<object>) => new List<object>(ar.SubItems), // SubItems in StackItem type
_ when type == typeof(IList<object>) => new List<object>(ar.SubItems.Select(ConvertToBaseValue)), // SubItems in StackItem type except bool, buffer and int
_ when type == typeof(List<object>) => new List<object>(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),
_ => throw new FormatException($"Impossible to convert {stackItem} to {type}"),
},
_ when stackItem is Map mp => type switch
{
_ when type == typeof(IDictionary<object, object>) => ToDictionary(mp), // SubItems in StackItem type
_ when type == typeof(Dictionary<object, object>) => ToDictionary(mp), // SubItems in StackItem type
_ when type == typeof(IDictionary<object, object>) => ToDictionary(mp), // SubItems in StackItem type except bool, buffer and int
_ when type == typeof(Dictionary<object, object>) => ToDictionary(mp), // SubItems in StackItem type except bool, buffer and int
_ => throw new FormatException($"Impossible to convert {stackItem} to {type}"),
},

_ => throw new FormatException($"Impossible to convert {stackItem} to {type}"),
};
}

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<StackItem> subItems, Type type)
{
var index = 0;
Expand Down Expand Up @@ -163,7 +174,7 @@ private static IDictionary<object, object> ToDictionary(Map map)

foreach (var entry in map)
{
dictionary.Add(entry.Key, entry.Value);
dictionary.Add(ConvertToBaseValue(entry.Key), ConvertToBaseValue(entry.Value));
}

return dictionary;
Expand Down Expand Up @@ -195,7 +206,7 @@ private static object CreateValueType(IEnumerable<StackItem> objects, Type value
return value;
}

private static object CreateTypeArray(IEnumerable<StackItem> objects, Type elementType)
private static System.Array CreateTypeArray(IEnumerable<StackItem> objects, Type elementType)
{
var obj = objects.ToArray();

Expand All @@ -214,7 +225,7 @@ private static object CreateTypeArray(IEnumerable<StackItem> 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);
Expand Down
12 changes: 6 additions & 6 deletions tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Lambda.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions tests/Neo.SmartContract.Framework.UnitTests/ListTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Json;
using Neo.SmartContract.Testing;
Expand Down Expand Up @@ -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]);
}
}

Expand Down

0 comments on commit dd17081

Please sign in to comment.