Exemplo n.º 1
0
        /// <summary>
        /// Enumerates the commands representing the scenarios defined by the <paramref name="method"/>.
        /// </summary>
        /// <param name="method">The scenario method.</param>
        /// <returns>An instance of <see cref="IEnumerable{ITestCommand}"/> representing the scenarios defined by the <paramref name="method"/>.</returns>
        /// <remarks>This method may be overridden.</remarks>
        protected virtual IEnumerable <ICommand> EnumerateScenarioCommands(IMethodInfo method)
        {
            Guard.AgainstNullArgument("method", method);
            Guard.AgainstNullArgumentProperty("method", "MethodInfo", method.MethodInfo);

            var parameters = method.MethodInfo.GetParameters();

            if (!parameters.Any())
            {
                return(new[] { new Command(method) });
            }

            var commands = new List <ICommand>();

            foreach (var arguments in GetArgumentCollections(method.MethodInfo))
            {
                var    closedTypeMethod = method;
                Type[] typeArguments    = null;
                if (method.MethodInfo != null && method.MethodInfo.IsGenericMethodDefinition)
                {
                    typeArguments    = ResolveTypeArguments(method, arguments).ToArray();
                    closedTypeMethod = Reflector.Wrap(method.MethodInfo.MakeGenericMethod(typeArguments));
                }

                var generatedArguments = new List <Argument>();
                for (var missingArgumentIndex = arguments.Length; missingArgumentIndex < parameters.Length; ++missingArgumentIndex)
                {
                    var parameterType = parameters[missingArgumentIndex].ParameterType;
                    if (parameterType.IsGenericParameter)
                    {
                        Type concreteType   = null;
                        var  typeParameters = method.MethodInfo.GetGenericArguments();
                        for (var typeParameterIndex = 0; typeParameterIndex < typeParameters.Length; ++typeParameterIndex)
                        {
                            if (typeParameters[typeParameterIndex] == parameterType)
                            {
                                concreteType = typeArguments[typeParameterIndex];
                                break;
                            }
                        }

                        if (concreteType == null)
                        {
                            var message = string.Format(
                                CultureInfo.CurrentCulture, "The type of parameter \"{0}\" cannot be resolved.", parameters[missingArgumentIndex].Name);
                            throw new InvalidOperationException(message);
                        }

                        parameterType = concreteType;
                    }

                    generatedArguments.Add(new Argument(parameterType));
                }

                commands.Add(new Command(new MethodCall(closedTypeMethod, arguments.Concat(generatedArguments).ToArray(), typeArguments)));
            }

            return(commands);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Enumerates the commands representing the backgrounds associated with the <paramref name="method"/>.
        /// </summary>
        /// <param name="method">The scenario method.</param>
        /// <returns>An instance of <see cref="IEnumerable{ITestCommand}"/> representing the backgrounds associated with the <paramref name="method"/>.</returns>
        protected virtual IEnumerable <ITestCommand> EnumerateBackgroundCommands(IMethodInfo method)
        {
            Guard.AgainstNullArgument("method", method);
            Guard.AgainstNullArgumentProperty("method", "Class", method.Class);

            return(method.Class.GetMethods().SelectMany(
                       candidateMethod => candidateMethod.GetCustomAttributes(typeof(BackgroundAttribute))
                       .Select(attribute => attribute.GetInstance <BackgroundAttribute>())
                       .SelectMany(backgroundAttribute => backgroundAttribute.CreateBackgroundCommands(candidateMethod))).ToArray());
        }
Exemplo n.º 3
0
        public static IEnumerable <ITestCommand> ExtractCommands(
            MethodCall methodCall,
            IEnumerable <ITestCommand> commands,
            object continueOnFailureStepType,
            bool omitArgumentsFromScenarioNames)
        {
            Guard.AgainstNullArgument("methodCall", methodCall);
            Guard.AgainstNullArgument("commands", commands);

            try
            {
                try
                {
                    var feature = methodCall.Method.IsStatic ? null : methodCall.Method.CreateInstance();
                    foreach (var command in commands)
                    {
                        var result = command.Execute(feature);
                        if ((result as PassedResult) == null)
                        {
                            return(new ITestCommand[] { new ReplayCommand(methodCall.Method, result) });
                        }
                    }
                }
                catch (Exception ex)
                {
                    return(new ITestCommand[] { new ExceptionCommand(methodCall, ex, omitArgumentsFromScenarioNames) });
                }

                var contexts = new ContextFactory().CreateContexts(methodCall, Steps).ToArray();
                return(contexts.SelectMany((context, index) => context.CreateCommands(index + 1, continueOnFailureStepType, omitArgumentsFromScenarioNames)));
            }
            finally
            {
                steps = null;
            }
        }