using DynamicExpresso; using OpenTap; using OpenTap.Addin; using System; namespace AddInPlugin.Util { public static class TestPlanRunAddIn { public static Interpreter GetInterpreter(this TestPlanRun run, ITestStep step) { var target = new Interpreter(); target.SetVariable("this", MethodUtil.Instance); target.SetVariable("FileGlobals", run.FileGlobalsRuntime); target.SetVariable("StationGlobals", run.StationGlobalsRuntime); target.SetVariable("Parameters", run.ParametersRuntime); target.SetVariable("Step", step); target.SetVariable("Locals", run.LocalsRuntime); var context = step?.GetParent(); if (context != null) { target.SetVariable("Context", context.GetContextVariables()); } return target; } public static dynamic Resolve(this TestPlanRun run, ITestStep step, string expr) { return Resolve(run, step, expr, typeof(void)); } public static T Resolve(this TestPlanRun run, ITestStep step, string expr) { return Resolve(run, step, expr, typeof(T)); } public static dynamic Resolve(this TestPlanRun run, ITestStep step, string expr, Type targetType) { if (expr == null) return null; //if ((expr.StartsWith("\"") && !expr.EndsWith("\"")) || (expr.EndsWith("\"") && !expr.StartsWith("\""))) //{ // throw new Exception("String Val Format Error"); //} var target = GetInterpreter(run, step); if (targetType == typeof(string)) { return target.Eval(expr)?.ToString(); } return target.Eval(expr, targetType); } public static void Update(this TestPlanRun run, ITestStep step, string expr) { var target = GetInterpreter(run, step); target.Eval(expr); } public static void Update(this TestPlanRun run, ITestStep step, string name, object value) { var target = GetInterpreter(run, step); var expr = $"{name} = __temp"; target.Eval(expr, new Parameter("__temp", value)); } } }