Пример #1
0
        private void AddStep(Expression <Action <TScenario> > stepAction, string stepTextTemplate, bool asserts, ExecutionOrder executionOrder, bool reports = true, bool includeInputsInStepTitle = true)
        {
            var methodInfo     = GetMethodInfo(stepAction);
            var inputArguments = new object[0];

            if (includeInputsInStepTitle)
            {
                inputArguments = stepAction.ExtractConstants().ToArray();
            }

            var flatInputArray = inputArguments.FlattenArrays();
            var stepTitle      = NetToString.Convert(methodInfo.Name);

            if (!string.IsNullOrEmpty(stepTextTemplate))
            {
                stepTitle = string.Format(stepTextTemplate, flatInputArray);
            }
            else if (includeInputsInStepTitle)
            {
                var stringFlatInputs = flatInputArray.Select(i => i.ToString()).ToArray();
                stepTitle = stepTitle + " " + string.Join(", ", stringFlatInputs);
            }

            stepTitle = stepTitle.Trim();
            var action = stepAction.Compile();

            _steps.Add(new Step(StepActionFactory.GetStepAction(action), stepTitle, asserts, executionOrder, reports));
        }
        static Func <object, object> GetStepAction(MethodInfo method, object[] inputs, bool returnsItsText)
        {
            if (returnsItsText)
            {
                // Note: Count() is a silly trick to enumerate over the method and make sure it returns because it is an IEnumerable method and runs lazily otherwise
                return(o => InvokeIEnumerableMethod(method, o, inputs).Count());
            }

            return(StepActionFactory.GetStepAction(method, inputs));
        }
        public IEnumerable <Step> Scan(ITestContext testContext, MethodInfo candidateMethod)
        {
            var executableAttribute = (ExecutableAttribute)candidateMethod.GetCustomAttributes(typeof(ExecutableAttribute), true).FirstOrDefault();

            if (executableAttribute == null)
            {
                yield break;
            }

            var stepTitle = new StepTitle(executableAttribute.StepTitle);

            if (string.IsNullOrEmpty(stepTitle))
            {
                stepTitle = new StepTitle(Configurator.Scanners.Humanize(candidateMethod.Name));
            }

            var stepAsserts  = IsAssertingByAttribute(candidateMethod);
            var shouldReport = executableAttribute.ShouldReport;

            var runStepWithArgsAttributes = (RunStepWithArgsAttribute[])candidateMethod.GetCustomAttributes(typeof(RunStepWithArgsAttribute), true);

            if (runStepWithArgsAttributes.Length == 0)
            {
                var stepAction = StepActionFactory.GetStepAction(candidateMethod, new object[0]);
                yield return(new Step(stepAction, stepTitle, stepAsserts, executableAttribute.ExecutionOrder, shouldReport, new List <StepArgument>())
                {
                    ExecutionSubOrder = executableAttribute.Order
                });
            }

            foreach (var runStepWithArgsAttribute in runStepWithArgsAttributes)
            {
                var inputArguments   = runStepWithArgsAttribute.InputArguments;
                var flatInput        = inputArguments.FlattenArrays();
                var stringFlatInputs = flatInput.Select(i => i.ToString()).ToArray();
                var methodName       = stepTitle + " " + string.Join(", ", stringFlatInputs);

                if (!string.IsNullOrEmpty(runStepWithArgsAttribute.StepTextTemplate))
                {
                    methodName = string.Format(runStepWithArgsAttribute.StepTextTemplate, flatInput);
                }
                else if (!string.IsNullOrEmpty(executableAttribute.StepTitle))
                {
                    methodName = string.Format(executableAttribute.StepTitle, flatInput);
                }

                var stepAction = StepActionFactory.GetStepAction(candidateMethod, inputArguments);
                yield return(new Step(stepAction, new StepTitle(methodName), stepAsserts,
                                      executableAttribute.ExecutionOrder, shouldReport, new List <StepArgument>())
                {
                    ExecutionSubOrder = executableAttribute.Order
                });
            }
        }
        public IEnumerable <Step> Scan(object testObject, MethodInfo candidateMethod)
        {
            var executableAttribute = (ExecutableAttribute)candidateMethod.GetCustomAttributes(typeof(ExecutableAttribute), false).FirstOrDefault();

            if (executableAttribute == null)
            {
                yield break;
            }

            string stepTitle = executableAttribute.StepTitle;

            if (string.IsNullOrEmpty(stepTitle))
            {
                stepTitle = NetToString.Convert(candidateMethod.Name);
            }

            var stepAsserts = IsAssertingByAttribute(candidateMethod);

            var runStepWithArgsAttributes = (RunStepWithArgsAttribute[])candidateMethod.GetCustomAttributes(typeof(RunStepWithArgsAttribute), false);

            if (runStepWithArgsAttributes.Length == 0)
            {
                yield return
                    (new Step(StepActionFactory.GetStepAction(candidateMethod, new object[0]), stepTitle, stepAsserts, executableAttribute.ExecutionOrder, true)
                {
                    ExecutionSubOrder = executableAttribute.Order
                });
            }

            foreach (var runStepWithArgsAttribute in runStepWithArgsAttributes)
            {
                var inputArguments   = runStepWithArgsAttribute.InputArguments;
                var flatInput        = inputArguments.FlattenArrays();
                var stringFlatInputs = flatInput.Select(i => i.ToString()).ToArray();
                var methodName       = stepTitle + " " + string.Join(", ", stringFlatInputs);

                if (!string.IsNullOrEmpty(runStepWithArgsAttribute.StepTextTemplate))
                {
                    methodName = string.Format(runStepWithArgsAttribute.StepTextTemplate, flatInput);
                }
                else if (!string.IsNullOrEmpty(executableAttribute.StepTitle))
                {
                    methodName = string.Format(executableAttribute.StepTitle, flatInput);
                }

                yield return
                    (new Step(StepActionFactory.GetStepAction(candidateMethod, inputArguments), methodName, stepAsserts,
                              executableAttribute.ExecutionOrder, true)
                {
                    ExecutionSubOrder = executableAttribute.Order
                });
            }
        }
Пример #5
0
        public void AddStep(Expression <Func <TScenario, Task> > stepAction, string stepTextTemplate, bool includeInputsInStepTitle, bool reports, ExecutionOrder executionOrder, bool asserts, string stepPrefix)
        {
            var action         = stepAction.Compile();
            var inputArguments = new StepArgument[0];

            if (includeInputsInStepTitle)
            {
                inputArguments = stepAction.ExtractArguments(_testObject).ToArray();
            }

            var args = inputArguments.Where(s => !string.IsNullOrEmpty(s.Name)).ToList();

            _steps.Add(new Step(stepAction, StepActionFactory.GetStepAction(action), _createTitle, stepTextTemplate, stepPrefix, includeInputsInStepTitle, GetMethodInfo, _testObject, null, FixAsserts(asserts, executionOrder), FixConsecutiveStep(executionOrder), reports, args));
        }
Пример #6
0
        private void AddStep(Action <TScenario> action, LambdaExpression stepAction, string stepTextTemplate, bool includeInputsInStepTitle,
                             bool reports, ExecutionOrder executionOrder, bool asserts, string stepPrefix)
        {
            var inputArguments = new StepArgument[0];

            if (includeInputsInStepTitle)
            {
                inputArguments = stepAction.ExtractArguments(_testObject).ToArray();
            }

            var title = CreateTitle(stepTextTemplate, includeInputsInStepTitle, GetMethodInfo(stepAction), inputArguments,
                                    stepPrefix);
            var args = inputArguments.Where(s => !string.IsNullOrEmpty(s.Name)).ToList();

            _steps.Add(new Step(StepActionFactory.GetStepAction(action), title, FixAsserts(asserts, executionOrder),
                                FixConsecutiveStep(executionOrder), reports, args));
        }
        public IEnumerable <Step> Scan(ITestContext testContext, MethodInfo method, Example example)
        {
            var executableAttribute = (ExecutableAttribute)method.GetCustomAttributes(typeof(ExecutableAttribute), true).FirstOrDefault();

            if (executableAttribute == null)
            {
                yield break;
            }

            string stepTitle = executableAttribute.StepTitle;

            if (string.IsNullOrEmpty(stepTitle))
            {
                stepTitle = Configurator.Scanners.Humanize(method.Name);
            }

            var stepAsserts      = IsAssertingByAttribute(method);
            var shouldReport     = executableAttribute.ShouldReport;
            var methodParameters = method.GetParameters();

            var inputs            = new List <object>();
            var inputPlaceholders = Regex.Matches(stepTitle, " <(\\w+)> ");

            for (int i = 0; i < inputPlaceholders.Count; i++)
            {
                var placeholder = inputPlaceholders[i].Groups[1].Value;

                for (int j = 0; j < example.Headers.Length; j++)
                {
                    if (example.Values.ElementAt(j).MatchesName(placeholder))
                    {
                        inputs.Add(example.GetValueOf(j, methodParameters[inputs.Count].ParameterType));
                        break;
                    }
                }
            }

            var stepAction = StepActionFactory.GetStepAction(method, inputs.ToArray());

            yield return(new Step(stepAction, new StepTitle(stepTitle), stepAsserts, executableAttribute.ExecutionOrder, shouldReport, new List <StepArgument>()));
        }
Пример #8
0
        public void AddStep(Func <Task> stepAction, string title, bool reports, ExecutionOrder executionOrder, bool asserts, string stepPrefix)
        {
            var action = StepActionFactory.GetStepAction <object>(o => stepAction());

            _steps.Add(new Step(action, new StepTitle(AppendPrefix(title, stepPrefix)), FixAsserts(asserts, executionOrder), FixConsecutiveStep(executionOrder), reports, new List <StepArgument>()));
        }