Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ToStackItem issue #1427

Merged
merged 15 commits into from
Feb 26, 2020
7 changes: 4 additions & 3 deletions src/neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public static ContractParameter ToParameter(this StackItem item)

private static ContractParameter ToParameter(StackItem item, List<(StackItem, ContractParameter)> context)
{
if (item is null) throw new ArgumentNullException();
ContractParameter parameter = null;
switch (item)
{
Expand Down Expand Up @@ -286,7 +287,7 @@ private static ContractParameter ToParameter(StackItem item, List<(StackItem, Co
};
break;
default:
throw new ArgumentException();
throw new ArgumentException($"StackItemType({item.Type}) is not supported to ContractParameter.");
}
return parameter;
}
Expand All @@ -298,6 +299,8 @@ public static StackItem ToStackItem(this ContractParameter parameter)

private static StackItem ToStackItem(ContractParameter parameter, List<(StackItem, ContractParameter)> context)
{
if (parameter is null) throw new ArgumentNullException();
if (parameter.Value is null) return StackItem.Null;
shargon marked this conversation as resolved.
Show resolved Hide resolved
StackItem stackItem = null;
switch (parameter.Type)
{
Expand Down Expand Up @@ -348,8 +351,6 @@ private static StackItem ToStackItem(ContractParameter parameter, List<(StackIte
case ContractParameterType.String:
stackItem = (string)parameter.Value;
break;
case ContractParameterType.InteropInterface:
break;
default:
throw new ArgumentException($"ContractParameterType({parameter.Type}) is not supported to StackItem.");
}
Expand Down
24 changes: 17 additions & 7 deletions tests/neo.UnitTests/VM/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public void TestToParameter()
StackItem intItem = new BigInteger(1000);
Assert.AreEqual(1000, (BigInteger)intItem.ToParameter().Value);

StackItem interopItem = new VM.Types.InteropInterface("test");
Assert.AreEqual(null, interopItem.ToParameter().Value);
StackItem interopItem = new InteropInterface("test");
Assert.AreEqual(ContractParameterType.InteropInterface, interopItem.ToParameter().Type);

StackItem arrayItem = new VM.Types.Array(new[] { byteItem, boolItem, intItem, interopItem });
Assert.AreEqual(1000, (BigInteger)(arrayItem.ToParameter().Value as List<ContractParameter>)[2].Value);
Expand All @@ -125,6 +125,9 @@ public void TestToParameter()
[TestMethod]
public void TestToStackItem()
{
ContractParameter parameter = null;
Assert.ThrowsException<ArgumentNullException>(() => parameter.ToStackItem());

ContractParameter byteParameter = new ContractParameter { Type = ContractParameterType.ByteArray, Value = "00e057eb481b".HexToBytes() };
Assert.AreEqual(30000000000000L, (long)byteParameter.ToStackItem().GetBigInteger());

Expand All @@ -146,10 +149,13 @@ public void TestToStackItem()
ContractParameter strParameter = new ContractParameter { Type = ContractParameterType.String, Value = "test😂👍" };
Assert.AreEqual("test😂👍", strParameter.ToStackItem().GetString());

ContractParameter interopParameter = new ContractParameter { Type = ContractParameterType.InteropInterface };
Assert.AreEqual(null, interopParameter.ToStackItem());
ContractParameter interopParameter = new ContractParameter { Type = ContractParameterType.InteropInterface, Value = new object() };
Assert.ThrowsException<ArgumentException>(() => interopParameter.ToStackItem());

ContractParameter interopParameter2 = new ContractParameter { Type = ContractParameterType.InteropInterface };
Assert.AreEqual(StackItem.Null, interopParameter2.ToStackItem());

ContractParameter arrayParameter = new ContractParameter { Type = ContractParameterType.Array, Value = new[] { byteParameter, boolParameter, intParameter, h160Parameter, h256Parameter, pkParameter, strParameter, interopParameter }.ToList() };
ContractParameter arrayParameter = new ContractParameter { Type = ContractParameterType.Array, Value = new[] { byteParameter, boolParameter, intParameter, h160Parameter, h256Parameter, pkParameter, strParameter }.ToList() };
Assert.AreEqual(1000, ((VM.Types.Array)arrayParameter.ToStackItem())[2].GetBigInteger());

ContractParameter mapParameter = new ContractParameter { Type = ContractParameterType.Map, Value = new[] { new KeyValuePair<ContractParameter, ContractParameter>(byteParameter, pkParameter) } };
Expand Down Expand Up @@ -486,9 +492,13 @@ public void TestToParameter2()
TestToParameter2ByteArray();
TestToParameter2Integer();
TestToParameter2InteropInterface();
TestToParameterNull();
}

Action action = () => VM.Helper.ToParameter(null);
action.Should().Throw<ArgumentException>();
private void TestToParameterNull()
{
StackItem item = null;
Assert.ThrowsException<ArgumentNullException>(() => item.ToParameter());
}

private void TestToParameter2InteropInterface()
Expand Down