From 71b59ffcf7a98d993d1865f0887de3731e4e448d Mon Sep 17 00:00:00 2001 From: Mirella de Medeiros Date: Mon, 26 Oct 2020 12:16:12 -0300 Subject: [PATCH 1/2] add json test support to the TestEngine --- src/TestEngine/Program.cs | 61 +++++++++++++- tests/TestEngine.UnitTests/UnitTest_Invoke.cs | 80 +++++++++++++++++++ 2 files changed, 138 insertions(+), 3 deletions(-) diff --git a/src/TestEngine/Program.cs b/src/TestEngine/Program.cs index 4f1ef9f30..61a3050a8 100644 --- a/src/TestEngine/Program.cs +++ b/src/TestEngine/Program.cs @@ -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) { @@ -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: " + ); } return result; @@ -50,8 +68,8 @@ public static JObject Run(string[] args) /// Runs a nef script given a method name and its arguments /// /// Absolute path of the script - /// The name of the targeted method - /// Json string representing the arguments of the method + /// The name of the targeted method + /// Json string representing the arguments of the method /// Returns a json with the engine state after executing the script public static JObject RunWithMethodName(string path, string methodName, string jsonParams) { @@ -79,6 +97,43 @@ public static JObject RunWithMethodName(string path, string methodName, string j } } + /// + /// Runs a nef script given a json with test engine fake values for testing. + /// + /// json object with fake values and execution arguments + /// Returns a json with the engine state after executing the script + 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); + } + } + /// /// Runs the given method from a nef script /// diff --git a/tests/TestEngine.UnitTests/UnitTest_Invoke.cs b/tests/TestEngine.UnitTests/UnitTest_Invoke.cs index ac1286378..52f4d0d9f 100644 --- a/tests/TestEngine.UnitTests/UnitTest_Invoke.cs +++ b/tests/TestEngine.UnitTests/UnitTest_Invoke.cs @@ -188,5 +188,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(result["error"].AsString(), "One or more arguments are missing"); + } + + [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"] = arguments.ToParameter().ToJson().ToString(); + + 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()); + } } } From d17242723bb341211eb5f39388cb5593b8aac097 Mon Sep 17 00:00:00 2001 From: Mirella de Medeiros Date: Mon, 26 Oct 2020 13:40:24 -0300 Subject: [PATCH 2/2] fix unit tests --- tests/TestEngine.UnitTests/UnitTest_Invoke.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/TestEngine.UnitTests/UnitTest_Invoke.cs b/tests/TestEngine.UnitTests/UnitTest_Invoke.cs index 52f4d0d9f..15509a255 100644 --- a/tests/TestEngine.UnitTests/UnitTest_Invoke.cs +++ b/tests/TestEngine.UnitTests/UnitTest_Invoke.cs @@ -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: ", + result["error"].AsString() + ); } [TestMethod] @@ -201,7 +204,7 @@ public void Test_Json_Missing_Fields() var result = Program.Run(args); Assert.IsTrue(result.ContainsProperty("error")); - Assert.AreEqual(result["error"].AsString(), "One or more arguments are missing"); + Assert.AreEqual("Missing field: 'method'", result["error"].AsString()); } [TestMethod] @@ -243,7 +246,7 @@ public void Test_Json_With_Parameters() var json = new JObject(); json["path"] = "./TestClasses/Contract1.nef"; json["method"] = "testArgs1"; - json["arguments"] = arguments.ToParameter().ToJson().ToString(); + json["arguments"] = new JArray() { arguments.ToParameter().ToJson() }; var args = new string[] { json.AsString()