/// <summary> /// Execute this test /// </summary> /// <param name="ut">Test</param> public void ExecuteTest(VMUT ut) { foreach (var test in ut.Tests) { // Interop service IInteropService service = new InteropService(); // Message provider IScriptContainer scriptContainer = null; if (test.Message != null) { scriptContainer = new MessageProvider(test.Message); } // Create engine using (var engine = new ExecutionEngine(scriptContainer, Crypto.Default, service)) { Debugger debugger = new Debugger(engine); engine.LoadScript(test.Script); // Execute Steps if (test.Steps != null) { foreach (var step in test.Steps) { // Actions if (step.Actions != null) { foreach (var run in step.Actions) { switch (run) { case VMUTActionType.Execute: debugger.Execute(); break; case VMUTActionType.StepInto: debugger.StepInto(); break; case VMUTActionType.StepOut: debugger.StepOut(); break; case VMUTActionType.StepOver: debugger.StepOver(); break; } } } // Review results var add = string.IsNullOrEmpty(step.Name) ? "" : "-" + step.Name; AssertResult(engine, step.Result, $"{ut.Category}-{ut.Name}-{test.Name}{add}: "); } } } } }
/// <summary> /// Execute this test /// </summary> /// <param name="ut">Test</param> public void ExecuteTest(VMUT ut) { foreach (var test in ut.Tests) { Assert.IsFalse(string.IsNullOrEmpty(test.Name), "Name is required"); using (var engine = new TestEngine()) { Debugger debugger = new Debugger(engine); if (test.Script.Length > 0) { engine.LoadScript(test.Script); } // Execute Steps if (test.Steps != null) { foreach (var step in test.Steps) { // Actions if (step.Actions != null) { foreach (var run in step.Actions) { switch (run) { case VMUTActionType.Execute: debugger.Execute(); break; case VMUTActionType.StepInto: debugger.StepInto(); break; case VMUTActionType.StepOut: debugger.StepOut(); break; case VMUTActionType.StepOver: debugger.StepOver(); break; } } } // Review results var add = string.IsNullOrEmpty(step.Name) ? "" : "-" + step.Name; AssertResult(step.Result, engine, $"{ut.Category}-{ut.Name}-{test.Name}{add}: "); } } } } }
/// <summary> /// Execute this test /// </summary> /// <param name="factory">Factory</param> /// <param name="ut">Test</param> public void ExecuteTest(IVMFactory factory, VMUT ut) { foreach (var test in ut.Tests) { // Arguments var storages = new ManualStorage(); var interopService = new InteropService(); var contracts = new ManualContracts(); var state = new StateMachine ( null, null, null, contracts, storages, interopService, null, null, null, test.Trigger ); var args = new ExecutionEngineArgs() { Trigger = test.Trigger, InteropService = interopService, Logger = new ExecutionEngineLogger(ELogVerbosity.StepInto), ScriptTable = contracts, }; var log = new StringBuilder(); var logBag = new List <string>(); var notBag = new List <JToken>(); interopService.OnLog += (sender, e) => { logBag.Add(e.Message); }; interopService.OnNotify += (sender, e) => { notBag.Add(ItemToJson(e.State)); }; args.Logger.OnStepInto += (context) => { log.AppendLine(context.InstructionPointer.ToString("x6") + " - " + context.NextInstruction); }; interopService.OnSysCall += (o, e) => { // Remove last line log.Remove(log.Length - Environment.NewLine.Length, Environment.NewLine.Length); log.AppendLine($" [{e.MethodName} - {e.Result}]"); }; // Script table if (test.ScriptTable != null) { foreach (var script in test.ScriptTable) { var contract = new Contract() { Code = new Code() { Script = script.Script, ScriptHash = script.Script.ToScriptHash(), Metadata = ContractMetadata.NoProperty } }; if (script.HasDynamicInvoke) { contract.Code.Metadata |= ContractMetadata.HasDynamicInvoke; } if (script.HasStorage) { contract.Code.Metadata |= ContractMetadata.HasStorage; } if (script.Payable) { contract.Code.Metadata |= ContractMetadata.Payable; } contracts.Add(contract.ScriptHash, contract); } contracts.Commit(); } // Create engine using (var engine = factory.Create(args)) { engine.GasAmount = ulong.MaxValue; engine.LoadScript(test.Script); // Execute Steps if (test.Steps != null) { foreach (var step in test.Steps) { // Actions log.Clear(); logBag.Clear(); notBag.Clear(); foreach (var run in step.Actions) { switch (run) { case VMUTActionType.Execute: engine.Execute(); break; case VMUTActionType.StepInto: engine.StepInto(); break; case VMUTActionType.StepOut: engine.StepOut(); break; case VMUTActionType.StepOver: engine.StepOver(); break; case VMUTActionType.Clean: engine.Clean(); break; } } // Review results var add = string.IsNullOrEmpty(step.Name) ? "" : "-" + step.Name; AssertResult(engine, step.State, logBag, notBag, $"{ut.Category}-{ut.Name}{add}: "); } } } } }
/// <summary> /// Serialize UT to json /// </summary> /// <param name="ut">Unit test</param> /// <returns>Json</returns> public static string ToJson(this VMUT ut) { return(JsonConvert.SerializeObject(ut, _settings)); }