public static StepDescriptor Create <TArg>(Func <TArg, Task> step, Func <TArg> argEvaluator) { Func <object, object[], Task> stepInvocation = (ctx, args) => step.Invoke((TArg)args[0]); var parameter = ParameterDescriptor.FromInvocation(step.GetMethodInfo().GetParameters()[0], ctx => argEvaluator.Invoke()); return(new StepDescriptor(step.GetMethodInfo().Name, stepInvocation, parameter)); }
public static StepDescriptor Create <TArg>(Func <TArg, Task> step, Func <TArg> argEvaluator) { Task <IStepResultDescriptor> StepInvocation(object ctx, object[] args) { step.Invoke((TArg)args[0]); return(Task.FromResult(DefaultStepResultDescriptor.Instance)); } var parameter = ParameterDescriptor.FromInvocation(step.GetMethodInfo().GetParameters()[0], ctx => argEvaluator.Invoke()); return(new StepDescriptor(step.GetMethodInfo(), StepInvocation, parameter)); }
public static StepDescriptor CreateAsync <TArg>(Action <TArg> step, Func <TArg> argEvaluator) { async Task <IStepResultDescriptor> StepInvocation(object ctx, object[] args) { await Task.Yield(); step.Invoke((TArg)args[0]); return(DefaultStepResultDescriptor.Instance); } var parameter = ParameterDescriptor.FromInvocation(step.GetMethodInfo().GetParameters()[0], ctx => argEvaluator.Invoke()); return(new StepDescriptor(step.GetMethodInfo(), StepInvocation, parameter)); }
public void FromInvocation_should_allow_creating_mutable_parameter() { var descriptor = ParameterDescriptor.FromInvocation(ParameterInfoHelper.IntParameterInfo, i => ((Incrementer)i).GetNext()); Assert.That(descriptor.IsConstant, Is.False); Assert.That(descriptor.ParameterInfo, Is.SameAs(ParameterInfoHelper.IntParameterInfo)); Assert.That(descriptor.RawName, Is.EqualTo(ParameterInfoHelper.IntParameterInfo.Name)); Assert.That(descriptor.ValueEvaluator, Is.Not.Null); var incrementer = new Incrementer(); Assert.That(descriptor.ValueEvaluator(incrementer), Is.EqualTo(1)); Assert.That(descriptor.ValueEvaluator(incrementer), Is.EqualTo(2)); }
private ParameterDescriptor CompileArgument(Expression argumentExpression, ParameterExpression contextParameter, ParameterInfo parameterInfo) { if (argumentExpression is ConstantExpression expression) { return(ParameterDescriptor.FromConstant(parameterInfo, expression.Value)); } var function = Expression.Lambda <Func <TContext, object> >(Expression.Convert(argumentExpression, typeof(object)), contextParameter).Compile(); var targetParam = Expression.Parameter(typeof(object), "target"); var genericStepArgFunction = Expression.Lambda <Func <object, object> >(Expression.Invoke(Expression.Constant(function), Expression.Convert(targetParam, contextParameter.Type)), targetParam) .Compile(); return(ParameterDescriptor.FromInvocation(parameterInfo, genericStepArgFunction)); }
public void FromInvocation_should_throw_if_valueEvaluator_is_null() { var ex = Assert.Throws <ArgumentNullException>(() => ParameterDescriptor.FromInvocation(ParameterInfoHelper.IntParameterInfo, null)); Assert.That(ex.ParamName, Is.EqualTo("valueEvaluator")); }
public void FromInvocation_should_throw_if_parameterInfo_is_null() { var ex = Assert.Throws <ArgumentNullException>(() => ParameterDescriptor.FromInvocation(null, o => 5)); Assert.That(ex.ParamName, Is.EqualTo("parameterInfo")); }
public void Runner_should_instantiate_context_and_use_it_in_step_and_parameter_execution() { var someParameterInfo = GetType().GetMethod(nameof(Given_step_two), BindingFlags.NonPublic | BindingFlags.Instance).GetParameters()[0]; var list = new List <object>(); _runner.Test() .WithContext(() => new object(), false) .TestScenario( new StepDescriptor("test1", (ctx, args) => { list.Add(ctx); return(Task.FromResult(DefaultStepResultDescriptor.Instance)); }, ParameterDescriptor.FromInvocation(someParameterInfo, ctx => { list.Add(ctx); return(ctx); })), new StepDescriptor("test1", (ctx, args) => { list.Add(ctx); return(Task.FromResult(DefaultStepResultDescriptor.Instance)); }, ParameterDescriptor.FromInvocation(someParameterInfo, ctx => { list.Add(ctx); return(ctx); }))); Assert.That(list.Count, Is.EqualTo(4)); Assert.That(list.Distinct().Count, Is.EqualTo(1), "Only one instance of object should be created"); }