Example #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);
        }
Example #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());
        }