Skip to content

Commit

Permalink
fix ToStackItem issue (neo-project#1427)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenquanyu authored and Tommo-L committed Jun 22, 2020
1 parent 862aa7a commit 8fd6ade
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
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;
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

0 comments on commit 8fd6ade

Please sign in to comment.