Skip to content

Commit

Permalink
Merge pull request #6 from simplitech/test-engine-json-input
Browse files Browse the repository at this point in the history
Add json test support to the TestEngine
  • Loading branch information
melanke authored Oct 26, 2020
2 parents 8c44910 + d172427 commit 9bda72e
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 4 deletions.
61 changes: 58 additions & 3 deletions src/TestEngine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ static int Main(string[] args)

public static JObject Run(string[] args)
{
if (args.Length == 1)
{
// verifies if the parameter is a json string
try
{
var input = JObject.Parse(args[0]);
return RunWithJson(input);
}
catch
{
// if the first input is not a json, verifies if the arguments are: nef path, method name, method args
}

}

JObject result;
if (args.Length >= 2)
{
Expand All @@ -40,7 +55,10 @@ public static JObject Run(string[] args)
}
else
{
result = BuildJsonException("One or more arguments are missing");
result = BuildJsonException(
"One or more arguments are missing\n" +
"Expected arguments: <nef path> <method name> <method arguments as json>"
);
}

return result;
Expand All @@ -50,8 +68,8 @@ public static JObject Run(string[] args)
/// Runs a nef script given a method name and its arguments
/// </summary>
/// <param name="path">Absolute path of the script</param>
/// <param name="method">The name of the targeted method</param>
/// <param name="parameters">Json string representing the arguments of the method</param>
/// <param name="methodName">The name of the targeted method</param>
/// <param name="jsonParams">Json string representing the arguments of the method</param>
/// <returns>Returns a json with the engine state after executing the script</returns>
public static JObject RunWithMethodName(string path, string methodName, string jsonParams)
{
Expand Down Expand Up @@ -79,6 +97,43 @@ public static JObject RunWithMethodName(string path, string methodName, string j
}
}

/// <summary>
/// Runs a nef script given a json with test engine fake values for testing.
/// </summary>
/// <param name="json">json object with fake values and execution arguments</param>
/// <returns>Returns a json with the engine state after executing the script</returns>
public static JObject RunWithJson(JObject json)
{
var missigFieldMessage = "Missing field: '{0}'";
if (!json.ContainsProperty("path"))
{
return BuildJsonException(string.Format(missigFieldMessage, "path"));
}

if (!json.ContainsProperty("method"))
{
return BuildJsonException(string.Format(missigFieldMessage, "method"));
}

if (!json.ContainsProperty("arguments"))
{
json["arguments"] = new JArray();
}

try
{
var path = json["path"].AsString();
var methodName = json["method"].AsString();
var parameters = (JArray)json["arguments"];

return Run(path, methodName, parameters);
}
catch (Exception e)
{
return BuildJsonException(e.Message);
}
}

/// <summary>
/// Runs the given method from a nef script
/// </summary>
Expand Down
85 changes: 84 additions & 1 deletion tests/TestEngine.UnitTests/UnitTest_Invoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public void Test_Missing_Arguments()
var result = Program.Run(args);

Assert.IsTrue(result.ContainsProperty("error"));
Assert.AreEqual(result["error"].AsString(), "One or more arguments are missing");
Assert.AreEqual(
"One or more arguments are missing\nExpected arguments: <nef path> <method name> <method arguments as json>",
result["error"].AsString()
);
}

[TestMethod]
Expand Down Expand Up @@ -188,5 +191,85 @@ public void Test_Invalid_File()
Assert.IsTrue(result.ContainsProperty("error"));
Assert.AreEqual(result["error"].AsString(), "Invalid file. A .nef file required.");
}

[TestMethod]
public void Test_Json_Missing_Fields()
{
var json = new JObject();
json["path"] = "./TestClasses/Contract1.nef";

var args = new string[] {
json.AsString()
};
var result = Program.Run(args);

Assert.IsTrue(result.ContainsProperty("error"));
Assert.AreEqual("Missing field: 'method'", result["error"].AsString());
}

[TestMethod]
public void Test_Json()
{
var json = new JObject();
json["path"] = "./TestClasses/Contract1.nef";
json["method"] = "unitTest_001";

var args = new string[] {
json.AsString()
};
var result = Program.Run(args);

// mustn't have errors
Assert.IsTrue(result.ContainsProperty("error"));
Assert.IsNull(result["error"]);

// test state
Assert.IsTrue(result.ContainsProperty("vm_state"));
Assert.AreEqual(result["vm_state"].AsString(), VMState.HALT.ToString());

// test result
StackItem wantresult = new byte[] { 1, 2, 3, 4 };
Assert.IsTrue(result.ContainsProperty("result_stack"));
Assert.IsInstanceOfType(result["result_stack"], typeof(JArray));

var resultStack = result["result_stack"] as JArray;
Assert.IsTrue(resultStack.Count == 1);
Assert.IsTrue(resultStack[0].ContainsProperty("value"));
Assert.AreEqual(resultStack[0]["value"].AsString(), wantresult.ToJson()["value"].AsString());
}

[TestMethod]
public void Test_Json_With_Parameters()
{
StackItem arguments = 16;

var json = new JObject();
json["path"] = "./TestClasses/Contract1.nef";
json["method"] = "testArgs1";
json["arguments"] = new JArray() { arguments.ToParameter().ToJson() };

var args = new string[] {
json.AsString()
};
var result = Program.Run(args);

// mustn't have errors
Assert.IsTrue(result.ContainsProperty("error"));
Assert.IsNull(result["error"]);

// test state
Assert.IsTrue(result.ContainsProperty("vm_state"));
Assert.AreEqual(result["vm_state"].AsString(), VMState.HALT.ToString());

// test result
StackItem wantresult = new byte[] { 1, 2, 3, 16 };
Assert.IsTrue(result.ContainsProperty("result_stack"));
Assert.IsInstanceOfType(result["result_stack"], typeof(JArray));

var resultStack = result["result_stack"] as JArray;
Assert.IsTrue(resultStack.Count == 1);
Assert.IsTrue(resultStack[0].ContainsProperty("value"));
Assert.AreEqual(resultStack[0]["value"].AsString(), wantresult.ToJson()["value"].AsString());
}
}
}

0 comments on commit 9bda72e

Please sign in to comment.