public void It_should_allow_creating_step_descriptor_from_method() { var predefinedStepType = "stepType"; Action methodRef = Some_step; var rawName = methodRef.GetMethodInfo().Name; var parameters = new[] { ParameterDescriptor.FromConstant(ParameterInfoHelper.IntParameterInfo, 55), ParameterDescriptor.FromConstant(ParameterInfoHelper.IntParameterInfo, 32) }; var descriptor = new StepDescriptor(methodRef.GetMethodInfo(), SomeStepInvocation, parameters) { PredefinedStepType = predefinedStepType }; Assert.That(descriptor.PredefinedStepType, Is.EqualTo(predefinedStepType)); Assert.That(descriptor.RawName, Is.EqualTo(rawName)); Assert.That(descriptor.Parameters, Is.SameAs(parameters)); Assert.That(descriptor.StepInvocation, Is.SameAs(SomeStepInvocation)); Assert.That(descriptor.IsValid, Is.True); Assert.That(descriptor.CreationException, Is.Null); Assert.That(descriptor.IsNameFormattingRequired, Is.True); }
public static StepDescriptor Create <TArg>(Func <TArg, Task> step, TArg arg) { Func <object, object[], Task> stepInvocation = (ctx, args) => step.Invoke((TArg)args[0]); var parameter = ParameterDescriptor.FromConstant(step.GetMethodInfo().GetParameters()[0], arg); return(new StepDescriptor(step.GetMethodInfo().Name, stepInvocation, parameter)); }
public void FromConstant_should_allow_creating_constant() { var descriptor = ParameterDescriptor.FromConstant(ParameterInfoHelper.IntParameterInfo, 55); Assert.That(descriptor.IsConstant, Is.True); Assert.That(descriptor.ParameterInfo, Is.SameAs(ParameterInfoHelper.IntParameterInfo)); Assert.That(descriptor.RawName, Is.EqualTo(ParameterInfoHelper.IntParameterInfo.Name)); Assert.That(descriptor.ValueEvaluator, Is.Not.Null); Assert.That(descriptor.ValueEvaluator(null), Is.EqualTo(55)); }
public static StepDescriptor Create <TArg>(Func <TArg, Task> step, TArg arg) { Task <IStepResultDescriptor> StepInvocation(object ctx, object[] args) { step.Invoke((TArg)args[0]); return(Task.FromResult(DefaultStepResultDescriptor.Instance)); } var parameter = ParameterDescriptor.FromConstant(step.GetMethodInfo().GetParameters()[0], arg); return(new StepDescriptor(step.GetMethodInfo(), StepInvocation, parameter)); }
public static StepDescriptor CreateAsync <TArg>(Action <TArg> step, TArg arg) { Func <object, object[], Task> stepInvocation = async(ctx, args) => { await Task.Yield(); step.Invoke((TArg)args[0]); }; var parameter = ParameterDescriptor.FromConstant(step.GetMethodInfo().GetParameters()[0], arg); return(new StepDescriptor(step.GetMethodInfo().Name, stepInvocation, parameter)); }
public void GetStepName_should_capture_name_from_step_descriptor_but_leave_parameters_unknown() { var descriptor = new StepDescriptor( "given", nameof(Feature_type.Some_step_with_argument), (o, a) => Task.FromResult(0), ParameterDescriptor.FromConstant(ParameterInfoHelper.GetMethodParameter <int>(new Feature_type().Some_step_with_argument), 5)); var stepName = _metadataProvider.GetStepName(descriptor, null); Assert.That(stepName.StepTypeName.ToString(), Is.EqualTo("GIVEN")); Assert.That(stepName.NameFormat, Is.EqualTo("Some step with argument \"{0}\"")); Assert.That(stepName.ToString(), Is.EqualTo("GIVEN Some step with argument \"<?>\"")); }
public static StepDescriptor CreateAsync <TArg>(Action <TArg> step, TArg arg) { async Task <IStepResultDescriptor> StepInvocation(object ctx, object[] args) { await Task.Yield(); step.Invoke((TArg)args[0]); return(DefaultStepResultDescriptor.Instance); } var parameter = ParameterDescriptor.FromConstant(step.GetMethodInfo().GetParameters()[0], arg); return(new StepDescriptor(step.GetMethodInfo(), StepInvocation, parameter)); }
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 static StepDescriptor CreateAsync <TArg1, TArg2, TArg3>(Action <TArg1, TArg2, TArg3> step, TArg1 arg1, TArg2 arg2, TArg3 arg3) { async Task <IStepResultDescriptor> StepInvocation(object ctx, object[] args) { await Task.Yield(); step.Invoke((TArg1)args[0], (TArg2)args[1], (TArg3)args[2]); return(DefaultStepResultDescriptor.Instance); } var p1 = ParameterDescriptor.FromConstant(step.GetMethodInfo().GetParameters()[0], arg1); var p2 = ParameterDescriptor.FromConstant(step.GetMethodInfo().GetParameters()[1], arg2); var p3 = ParameterDescriptor.FromConstant(step.GetMethodInfo().GetParameters()[2], arg3); return(new StepDescriptor(step.GetMethodInfo(), StepInvocation, p1, p2, p3)); }
public void It_should_allow_creating_step_descriptor() { var predefinedStepType = "stepType"; var rawName = "rawName"; var parameters = new[] { ParameterDescriptor.FromConstant(ParameterInfoHelper.IntParameterInfo, 55), ParameterDescriptor.FromConstant(ParameterInfoHelper.IntParameterInfo, 32) }; var descriptor = new StepDescriptor(predefinedStepType, rawName, SomeStepInvocation, parameters); Assert.That(descriptor.PredefinedStepType, Is.EqualTo(predefinedStepType)); Assert.That(descriptor.RawName, Is.EqualTo(rawName)); Assert.That(descriptor.Parameters, Is.SameAs(parameters)); Assert.That(descriptor.StepInvocation, Is.SameAs(SomeStepInvocation)); }
private static IStepNameInfo CreateStepNameInfo(string type, string name, int args = 0) { Task <IStepResultDescriptor> Invocation(object ctx, object[] a) => Task.FromResult(DefaultStepResultDescriptor.Instance); void SomeFunction(int x) { } Action <int> fn = SomeFunction; var param = fn.GetMethodInfo().GetParameters().Single(); var descriptor = new StepDescriptor(name, Invocation, Enumerable.Range(0, args).Select(a => ParameterDescriptor.FromConstant(param, a)).ToArray()) { PredefinedStepType = type }; return(new TestMetadataProvider().GetStepName(descriptor, "")); }
public void FromConstant_should_throw_if_parameterInfo_is_null() { var ex = Assert.Throws <ArgumentNullException>(() => ParameterDescriptor.FromConstant(null, 0)); Assert.That(ex.ParamName, Is.EqualTo("parameterInfo")); }