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..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] @@ -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()); + } } }