コード例 #1
0
        private TestCaseParameters GetParametersForTestCase(IMethodInfo method)
        {
            TestCaseParameters parms;

            try
            {
                IParameterInfo[] parameters = method.GetParameters();
                int argsNeeded   = parameters.Length;
                int argsProvided = Arguments.Length;

                parms = new TestCaseParameters(this);

                // Special handling for params arguments
                if (argsNeeded > 0 && argsProvided >= argsNeeded - 1)
                {
                    IParameterInfo lastParameter     = parameters[argsNeeded - 1];
                    Type           lastParameterType = lastParameter.ParameterType;
                    Type           elementType       = lastParameterType.GetElementType();

                    if (lastParameterType.IsArray && lastParameter.IsDefined <ParamArrayAttribute>(false))
                    {
                        if (argsProvided == argsNeeded)
                        {
                            Type lastArgumentType = parms.Arguments[argsProvided - 1].GetType();
                            if (!lastParameterType.GetTypeInfo().IsAssignableFrom(lastArgumentType.GetTypeInfo()))
                            {
                                Array array = Array.CreateInstance(elementType, 1);
                                array.SetValue(parms.Arguments[argsProvided - 1], 0);
                                parms.Arguments[argsProvided - 1] = array;
                            }
                        }
                        else
                        {
                            object[] newArglist = new object[argsNeeded];
                            for (int i = 0; i < argsNeeded && i < argsProvided; i++)
                            {
                                newArglist[i] = parms.Arguments[i];
                            }

                            int   length = argsProvided - argsNeeded + 1;
                            Array array  = Array.CreateInstance(elementType, length);
                            for (int i = 0; i < length; i++)
                            {
                                array.SetValue(parms.Arguments[argsNeeded + i - 1], i);
                            }

                            newArglist[argsNeeded - 1] = array;
                            parms.Arguments            = newArglist;
                            argsProvided = argsNeeded;
                        }
                    }
                }

                //Special handling for optional parameters
                if (parms.Arguments.Length < argsNeeded)
                {
                    object[] newArgList = new object[parameters.Length];
                    Array.Copy(parms.Arguments, newArgList, parms.Arguments.Length);

                    //Fill with Type.Missing for remaining required parameters where optional
                    for (var i = parms.Arguments.Length; i < parameters.Length; i++)
                    {
                        if (parameters[i].IsOptional)
                        {
                            newArgList[i] = Type.Missing;
                        }
                        else
                        {
                            if (i < parms.Arguments.Length)
                            {
                                newArgList[i] = parms.Arguments[i];
                            }
                            else
                            {
                                throw new TargetParameterCountException(string.Format(
                                                                            "Method requires {0} arguments but TestCaseAttribute only supplied {1}",
                                                                            argsNeeded,
                                                                            argsProvided));
                            }
                        }
                    }
                    parms.Arguments = newArgList;
                }

                //if (method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(object[]))
                //    parms.Arguments = new object[]{parms.Arguments};

                // Special handling when sole argument is an object[]
                if (argsNeeded == 1 && method.GetParameters()[0].ParameterType == typeof(object[]))
                {
                    if (argsProvided > 1 ||
                        argsProvided == 1 && parms.Arguments[0].GetType() != typeof(object[]))
                    {
                        parms.Arguments = new object[] { parms.Arguments };
                    }
                }

                if (argsProvided == argsNeeded)
                {
                    PerformSpecialConversions(parms.Arguments, parameters);
                }
            }
            catch (Exception ex)
            {
                parms = new TestCaseParameters(ex);
            }

            return(parms);
        }
コード例 #2
0
        private TestCaseParameters GetParametersForTestCase(IMethodInfo method)
        {
            TestCaseParameters parms;

            try
            {
#if NETCF
                var tmethod = method.MakeGenericMethodEx(Arguments);
                if (tmethod == null)
                {
                    throw new NotSupportedException("Cannot determine generic types from probing");
                }
                method = tmethod;
#endif

                IParameterInfo[] parameters = method.GetParameters();
                int argsNeeded   = parameters.Length;
                int argsProvided = Arguments.Length;

                parms = new TestCaseParameters(this);

                // Special handling for params arguments
                if (argsNeeded > 0 && argsProvided >= argsNeeded - 1)
                {
                    IParameterInfo lastParameter     = parameters[argsNeeded - 1];
                    Type           lastParameterType = lastParameter.ParameterType;
                    Type           elementType       = lastParameterType.GetElementType();

                    if (lastParameterType.IsArray && lastParameter.IsDefined <ParamArrayAttribute>(false))
                    {
                        if (argsProvided == argsNeeded)
                        {
                            Type lastArgumentType = parms.Arguments[argsProvided - 1].GetType();
                            if (!lastParameterType.GetTypeInfo().IsAssignableFrom(lastArgumentType.GetTypeInfo()))
                            {
                                Array array = Array.CreateInstance(elementType, 1);
                                array.SetValue(parms.Arguments[argsProvided - 1], 0);
                                parms.Arguments[argsProvided - 1] = array;
                            }
                        }
                        else
                        {
                            object[] newArglist = new object[argsNeeded];
                            for (int i = 0; i < argsNeeded && i < argsProvided; i++)
                            {
                                newArglist[i] = parms.Arguments[i];
                            }

                            int   length = argsProvided - argsNeeded + 1;
                            Array array  = Array.CreateInstance(elementType, length);
                            for (int i = 0; i < length; i++)
                            {
                                array.SetValue(parms.Arguments[argsNeeded + i - 1], i);
                            }

                            newArglist[argsNeeded - 1] = array;
                            parms.Arguments            = newArglist;
                            argsProvided = argsNeeded;
                        }
                    }
                }

                //if (method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(object[]))
                //    parms.Arguments = new object[]{parms.Arguments};

                // Special handling when sole argument is an object[]
                if (argsNeeded == 1 && method.GetParameters()[0].ParameterType == typeof(object[]))
                {
                    if (argsProvided > 1 ||
                        argsProvided == 1 && parms.Arguments[0].GetType() != typeof(object[]))
                    {
                        parms.Arguments = new object[] { parms.Arguments };
                    }
                }

                if (argsProvided == argsNeeded)
                {
                    PerformSpecialConversions(parms.Arguments, parameters);
                }
            }
            catch (Exception ex)
            {
                parms = new TestCaseParameters(ex);
            }

            return(parms);
        }
 /// <summary>
 /// Determine whether any data is available for a parameter.
 /// </summary>
 /// <param name="parameter">A ParameterInfo representing one
 /// argument to a parameterized test</param>
 /// <returns>
 /// True if any data is available, otherwise false.
 /// </returns>
 public bool HasDataFor(IParameterInfo parameter)
 {
     return(parameter.IsDefined <IParameterDataSource>(false));
 }
コード例 #4
0
 /// <summary>
 /// Determine whether any data is available for a parameter.
 /// </summary>
 /// <param name="parameter">A ParameterInfo representing one
 /// argument to a parameterized test</param>
 /// <returns>
 /// True if any data is available, otherwise false.
 /// </returns>
 public bool HasDataFor(IParameterInfo parameter)
 {
     return parameter.IsDefined<IParameterDataSource>(false);
 }
コード例 #5
0
        /// <summary>
        /// Builds the parameters
        /// </summary>
        protected TestCaseParameters GetParametersForTestCaseHelper(IParameterInfo[] parameters, IMethodInfo methodn)
        {
            TestCaseParameters parms;
            int argsNeeded   = parameters.Length;
            int argsProvided = Arguments.Length;

            parms = new TestCaseParameters(this);

            // Special handling for ExpectedResult (see if it needs to be converted into method return type)
            if (parms.HasExpectedResult &&
                ParamAttributeTypeConversions.TryConvert(parms.ExpectedResult, methodn.ReturnType.Type, out var expectedResultInTargetType))
            {
                parms.ExpectedResult = expectedResultInTargetType;
            }

            // Special handling for params arguments
            if (argsNeeded > 0 && argsProvided >= argsNeeded - 1)
            {
                IParameterInfo lastParameter     = parameters[argsNeeded - 1];
                Type           lastParameterType = lastParameter.ParameterType;
                Type           elementType       = lastParameterType.GetElementType();

                if (lastParameterType.IsArray && lastParameter.IsDefined <ParamArrayAttribute>(false))
                {
                    if (argsProvided == argsNeeded)
                    {
                        if (!lastParameterType.IsInstanceOfType(parms.Arguments[argsProvided - 1]))
                        {
                            Array array = Array.CreateInstance(elementType, 1);
                            array.SetValue(parms.Arguments[argsProvided - 1], 0);
                            parms.Arguments[argsProvided - 1] = array;
                        }
                    }
                    else
                    {
                        object[] newArglist = new object[argsNeeded];
                        for (int i = 0; i < argsNeeded && i < argsProvided; i++)
                        {
                            newArglist[i] = parms.Arguments[i];
                        }

                        int   length = argsProvided - argsNeeded + 1;
                        Array array  = Array.CreateInstance(elementType, length);
                        for (int i = 0; i < length; i++)
                        {
                            array.SetValue(parms.Arguments[argsNeeded + i - 1], i);
                        }

                        newArglist[argsNeeded - 1] = array;
                        parms.Arguments            = newArglist;
                        argsProvided = argsNeeded;
                    }
                }
            }

            //Special handling for optional parameters
            if (parms.Arguments.Length < argsNeeded)
            {
                object[] newArgList = new object[parameters.Length];
                Array.Copy(parms.Arguments, newArgList, parms.Arguments.Length);

                //Fill with Type.Missing for remaining required parameters where optional
                for (var i = parms.Arguments.Length; i < parameters.Length; i++)
                {
                    if (parameters[i].IsOptional)
                    {
                        newArgList[i] = Type.Missing;
                    }
                    else
                    {
                        if (i < parms.Arguments.Length)
                        {
                            newArgList[i] = parms.Arguments[i];
                        }
                        else
                        {
                            throw new TargetParameterCountException(string.Format(
                                                                        "Method requires {0} arguments but TestCaseAttribute only supplied {1}",
                                                                        argsNeeded,
                                                                        argsProvided));
                        }
                    }
                }
                parms.Arguments = newArgList;
            }

            // Special handling when sole argument is an object[]
            if (argsNeeded == 1 && parameters[0].ParameterType == typeof(object[]))
            {
                if (argsProvided > 1 ||
                    argsProvided == 1 && parms.Arguments[0].GetType() != typeof(object[]))
                {
                    parms.Arguments = new object[] { parms.Arguments };
                }
            }

            if (argsProvided == argsNeeded)
            {
                PerformSpecialConversions(parms.Arguments, parameters);
            }

            return(parms);
        }