Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
State to result, states in array, TOALTSTACK unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Feb 26, 2019
1 parent 437f4da commit df2c5a5
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 21 deletions.
19 changes: 15 additions & 4 deletions tests/neo-vm.Tests/Converters/StateConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using Neo.VM;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Neo.Test.Converters
{
Expand All @@ -13,13 +15,13 @@ public override bool CanConvert(Type objectType)

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (!(reader.Value is string str)) throw new FormatException();
if (reader.TokenType != JsonToken.StartArray) throw new FormatException();

VMState ret = VMState.NONE;

foreach (var split in str.Split("|", StringSplitOptions.RemoveEmptyEntries))
foreach (var split in JArray.ReadFrom(reader))
{
ret |= Enum.Parse<VMState>(split.Trim().ToUpperInvariant());
ret |= Enum.Parse<VMState>(split.Value<string>().Trim().ToUpperInvariant());
}

return ret;
Expand All @@ -29,7 +31,16 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
{
if (value is VMState data)
{
writer.WriteValue(data.ToString().Replace(" ", ""));
var list = new List<string>();

foreach(VMState item in Enum.GetValues(typeof(VMState)))
{
if (!data.HasFlag(item)) continue;

list.Add(item.ToString());
}

writer.WriteValue(list.ToArray());
}
else
{
Expand Down
88 changes: 88 additions & 0 deletions tests/neo-vm.Tests/Tests/OpCodes/Stack/TOALTSTACK.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"category": "Stack",
"name": "TOALTSTACK",
"tests":
[
{
"name": "Without push",
"script": "0x6B",
"trigger": "Application",
"steps":
[
{
"actions":
[
"StepInto"
],
"result":
{
"state":
[
"Fault",
"Break"
],
"invocationStack":
[
{
"scriptHash": "0xFFE12A1A4B38AA3A0F147BF4E57E41FC82A17206",
"instructionPointer": 1,
"nextInstruction": "RET"
}
]
}
}
]
},
{
"name": "Real test",
"script": "0x556B",
"trigger": "Application",
"steps":
[
{
"actions":
[
"StepInto",
"StepInto"
],
"result":
{
"state":
[
"Break"
],
"invocationStack":
[
{
"scriptHash": "0xF1B26DF541F9D4D406F78B2824E2F80DA54D96A5",
"instructionPointer": 2,
"nextInstruction": "RET",
"altStack":
[
{
"type": "Integer",
"value": 5
}
]
}
]
}
},
{
"actions":
[
"StepInto"
],
"result":
{
"state":
[
"Halt",
"Break"
]
}
}
]
}
]
}
15 changes: 11 additions & 4 deletions tests/neo-vm.Tests/Tests/Others/Init.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
"steps":
[
{
"state":
"result":
{
"state": "Break",
"state":
[
"Break"
],
"invocationStack":
[
{
Expand All @@ -36,9 +39,13 @@
[
"StepInto"
],
"state":
"result":
{
"state": "Break|Halt"
"state":
[
"Break",
"Halt"
]
}
}
]
Expand Down
36 changes: 26 additions & 10 deletions tests/neo-vm.Tests/Tests/Others/ScriptLogic.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
[
"StepInto"
],
"state":
"result":
{
"state": "Break",
"state":
[
"Break"
],
"invocationStack":
[
{
Expand All @@ -39,9 +42,12 @@
[
"StepInto"
],
"state":
"result":
{
"state": "Break",
"state":
[
"Break"
],
"invocationStack":
[
{
Expand All @@ -64,9 +70,12 @@
[
"StepInto"
],
"state":
"result":
{
"state": "Break",
"state":
[
"Break"
],
"invocationStack":
[
{
Expand All @@ -89,9 +98,12 @@
[
"StepInto"
],
"state":
"result":
{
"state": "Break",
"state":
[
"Break"
],
"invocationStack":
[
{
Expand All @@ -107,9 +119,13 @@
[
"StepInto"
],
"state":
"result":
{
"state": "Halt|Break"
"state":
[
"Halt",
"Break"
]
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion tests/neo-vm.Tests/Types/VMUTExecutionEngineState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Neo.Test.Types
{
public class VMUTExecutionEngineState
{
[JsonProperty,JsonConverter(typeof(StateConverter))]
[JsonProperty, JsonConverter(typeof(StateConverter))]
public VMState State { get; set; }

[JsonProperty]
Expand Down
2 changes: 1 addition & 1 deletion tests/neo-vm.Tests/Types/VMUTStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class VMUTStep
public VMUTActionType[] Actions { get; set; }

[JsonProperty]
public VMUTExecutionEngineState State { get; set; }
public VMUTExecutionEngineState Result { get; set; }
}
}
1 change: 1 addition & 0 deletions tests/neo-vm.Tests/UtVMJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class UtVMJson : VMJsonTestBase
{
[Theory]
[InlineData("./Tests/Others")]
[InlineData("./Tests/OpCodes/Stack")]
public void TestJson(string path)
{
foreach (var file in Directory.GetFiles(path, "*.json", SearchOption.AllDirectories))
Expand Down
2 changes: 1 addition & 1 deletion tests/neo-vm.Tests/VMJsonTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void ExecuteTest(VMUT ut)

var add = string.IsNullOrEmpty(step.Name) ? "" : "-" + step.Name;

AssertResult(engine, step.State, $"{ut.Category}-{ut.Name}{add}: ");
AssertResult(engine, step.Result, $"{ut.Category}-{ut.Name}{add}: ");
}
}
}
Expand Down

0 comments on commit df2c5a5

Please sign in to comment.