/// <summary>
        /// Return an IEnumerable providing test cases for use in
        /// running a parameterized test.
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public IEnumerable GetTestCasesFor(MethodInfo method)
        {
            ArrayList list = new ArrayList();

            Attribute[] attrs = Reflect.GetAttributes(method, NUnitFramework.TestCaseAttribute, false);

            ParameterInfo[] parameters = method.GetParameters();
            int             argsNeeded = parameters.Length;

            foreach (Attribute attr in attrs)
            {
                ParameterSet parms;

                try
                {
                    parms = ParameterSet.FromDataSource(attr);

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

                    if (argsNeeded == 1 && method.GetParameters()[0].ParameterType == typeof(object[]))
                    {
                        if (parms.Arguments.Length > 1 ||
                            parms.Arguments.Length == 1 && parms.Arguments[0].GetType() != typeof(object[]))
                        {
                            parms.Arguments = new object[] { parms.Arguments };
                        }
                    }


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

                list.Add(parms);
            }

            return(list);
        }
        public IEnumerable GetTestCasesFor(MethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            Attribute[] rowAttributes = Reflect.GetAttributes(method, RowAttribute, false);
            ArrayList   parameterSets = new ArrayList();

            foreach (Attribute rowAttribute in rowAttributes)
            {
                ParameterSet parameterSet = ParameterSet.FromDataSource(rowAttribute);

                foreach (IParameterSetFilter filter in parameterSetFilters)
                {
                    filter.Filter(parameterSet, method);
                }

                parameterSets.Add(parameterSet);
            }

            return((ParameterSet[])parameterSets.ToArray(typeof(ParameterSet)));
        }
        /// <summary>
        /// Return an IEnumerable providing test cases for use in
        /// running a parameterized test.
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public IEnumerable GetTestCasesFor(MethodInfo method)
        {
            ArrayList list = new ArrayList();

            Attribute[] attrs = Reflect.GetAttributes(method, NUnitFramework.TestCaseAttribute, false);

            ParameterInfo[] parameters = method.GetParameters();
            int             argsNeeded = parameters.Length;

            foreach (Attribute attr in attrs)
            {
                ParameterSet parms;

                try
                {
                    parms = ParameterSet.FromDataSource(attr);
                    int argsProvided = parms.Arguments.Length;

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

                        if (lastParameterType.IsArray && lastParameter.IsDefined(typeof(ParamArrayAttribute), false))
                        {
                            object[] newArglist = new object[argsNeeded];
                            for (int i = 0; i < argsNeeded; i++)
                            {
                                newArglist[i] = parms.Arguments[i];
                            }

                            int   length = argsProvided - argsNeeded + 1;
                            Array array  = Array.CreateInstance(lastParameterType.GetElementType(), 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 ParameterSet(ex);
                }

                list.Add(parms);
            }

            return(list);
        }
        /// <summary>
        /// Return an IEnumerable providing test cases for use in
        /// running a parameterized test.
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public IEnumerable GetTestCasesFor(MethodInfo method, Test parentSuite)
        {
            ArrayList parameterList = new ArrayList();

            foreach (ProviderReference info in GetSourcesFor(method, parentSuite))
            {
                foreach (object source in info.GetInstance())
                {
                    ParameterSet parms;

                    if (source == null)
                    {
                        parms           = new ParameterSet();
                        parms.Arguments = new object[] { null };
                    }
                    else
                    {
                        parms = source as ParameterSet;
                    }

                    if (parms == null)
                    {
                        Type sourceType = source.GetType();

                        if (sourceType.GetInterface("NUnit.Framework.ITestCaseData") != null ||
                            sourceType.GetInterface("NUnit.Framework.Api.ITestCaseData") != null)
                        {
                            parms = ParameterSet.FromDataSource(source);
                        }
                        else
                        {
                            parms = new ParameterSet();

                            ParameterInfo[] parameters = method.GetParameters();

                            if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType))
                            {
                                parms.Arguments = new object[] { source }
                            }
                            ;
                            else if (source is object[])
                            {
                                parms.Arguments = (object[])source;
                            }
                            else if (source is Array)
                            {
                                Array array = (Array)source;
                                if (array.Rank == 1)
                                {
                                    parms.Arguments = new object[array.Length];
                                    for (int i = 0; i < array.Length; i++)
                                    {
                                        parms.Arguments[i] = (object)array.GetValue(i);
                                    }
                                }
                            }
                            else
                            {
                                parms.Arguments = new object[] { source }
                            };
                        }
                    }

                    if (info.Category != null)
                    {
                        foreach (string cat in info.Category.Split(new char[] { ',' }))
                        {
                            parms.Categories.Add(cat);
                        }
                    }

                    parameterList.Add(parms);
                }
            }

            return(parameterList);
        }
        /// <summary>
        /// Builds a ParameterizedMetodSuite containing individual
        /// test cases for each set of parameters provided for
        /// this method.
        /// </summary>
        /// <param name="method">The MethodInfo for which a test is to be built</param>
        /// <returns>A ParameterizedMethodSuite populated with test cases</returns>
        public static Test BuildParameterizedMethodSuite(MethodInfo method, Test parentSuite)
        {
            ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method);

            NUnitFramework.ApplyCommonAttributes(method, methodSuite);

            if (parentSuite != null)
            {
                if (parentSuite.RunState == RunState.NotRunnable && methodSuite.RunState != RunState.NotRunnable)
                {
                    methodSuite.RunState     = RunState.NotRunnable;
                    methodSuite.IgnoreReason = parentSuite.IgnoreReason;
                }

                if (parentSuite.RunState == RunState.Ignored && methodSuite.RunState != RunState.Ignored && methodSuite.RunState != RunState.NotRunnable)
                {
                    methodSuite.RunState     = RunState.Ignored;
                    methodSuite.IgnoreReason = parentSuite.IgnoreReason;
                }
            }

            foreach (object source in CoreExtensions.Host.TestCaseProviders.GetTestCasesFor(method, parentSuite))
            {
                ParameterSet parms;

                if (source == null)
                {
                    parms           = new ParameterSet();
                    parms.Arguments = new object[] { null };
                }
                else
                {
                    parms = source as ParameterSet;
                }

                if (parms == null)
                {
                    if (source.GetType().GetInterface("NUnit.Framework.ITestCaseData") != null)
                    {
                        parms = ParameterSet.FromDataSource(source);
                    }
                    else
                    {
                        parms = new ParameterSet();

                        ParameterInfo[] parameters = method.GetParameters();
                        Type            sourceType = source.GetType();

                        if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType))
                        {
                            parms.Arguments = new object[] { source }
                        }
                        ;
                        else if (source is object[])
                        {
                            parms.Arguments = (object[])source;
                        }
                        else if (source is Array)
                        {
                            Array array = (Array)source;
                            if (array.Rank == 1)
                            {
                                parms.Arguments = new object[array.Length];
                                for (int i = 0; i < array.Length; i++)
                                {
                                    parms.Arguments[i] = (object)array.GetValue(i);
                                }
                            }
                        }
                        else
                        {
                            parms.Arguments = new object[] { source }
                        };
                    }
                }

                TestMethod test = BuildSingleTestMethod(method, parentSuite, parms);

                methodSuite.Add(test);
            }

            return(methodSuite);
        }
        /// <summary>
        /// Return an IEnumerable providing test cases for use in
        /// running a parameterized test.
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public IEnumerable GetTestCasesFor(MethodInfo method, Test parentSuite)
        {
            ArrayList parameterList = new ArrayList();

            foreach (ProviderReference providerReference in GetSourcesFor(method, parentSuite))
            {
                foreach (object source in providerReference.GetInstance())
                {
                    ParameterSet parms;

                    if (source == null)
                    {
                        parms           = new ParameterSet();
                        parms.Arguments = new object[] { null };
                    }
                    else
                    {
                        parms = source as ParameterSet;
                    }

                    if (parms == null)
                    {
                        Type sourceType = source.GetType();

                        if (sourceType.GetInterface("NUnit.Framework.ITestCaseData") != null ||
                            sourceType.GetInterface("NUnit.Framework.Api.ITestCaseData") != null)
                        {
                            parms = ParameterSet.FromDataSource(source);
                        }
                        else
                        {
                            parms = new ParameterSet();

                            ParameterInfo[] parameters = method.GetParameters();

                            if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType))
                            {
                                parms.Arguments = new object[] { source }
                            }
                            ;
                            else if (source is object[])
                            {
                                parms.Arguments = (object[])source;
                            }
                            else if (source is Array)
                            {
                                Array array = (Array)source;
                                if (array.Rank == 1)
                                {
                                    parms.Arguments = new object[array.Length];
                                    for (int i = 0; i < array.Length; i++)
                                    {
                                        parms.Arguments[i] = (object)array.GetValue(i);
                                    }
                                }
                            }
                            else
                            {
                                parms.Arguments = new object[] { source }
                            };
                        }
                    }


                    // This is the only point we can easily check the individual returned
                    // ParameterSet items from a TestCaseSourceAttribute.
                    if (parms.ExpectedExceptionName != null)
                    {
                        Compatibility.Error(providerReference.ProviderLocation,
                                            "TestCaseSourceAttribute does not support ExpectedException in NUnit 3. Use Assert.Throws or ThrowsConstraint.");
                    }
                    if (parms.RunState == RunState.Ignored && string.IsNullOrEmpty(parms.IgnoreReason))
                    {
                        Compatibility.Error(providerReference.ProviderLocation,
                                            "TestCaseSourceAttribute requires a reason when case is ignored in NUnit 3.");
                    }

                    if (providerReference.ProviderCategory != null)
                    {
                        foreach (string cat in providerReference.ProviderCategory.Split(new char[] { ',' }))
                        {
                            parms.Categories.Add(cat);
                        }
                    }

                    parameterList.Add(parms);
                }
            }

            return(parameterList);
        }
示例#7
0
        /// <summary>
        /// Get Testcase with Test case attributes
        /// </summary>
        /// <param name="method">The MethodInfo object.</param>
        public ArrayList GetTestCasesWithTestCaseAttribute(MethodInfo method)
        {
            ArrayList testCaseCollection = new ArrayList();

            testMethods = Reflect.IsAsyncMethod(method) ? new NUnitAsyncTestMethod(method) : new NUnitTestMethod(method);
            ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method);

            NUnitFramework.ApplyCommonAttributes(method, methodSuite);
            IEnumerable ParameterList = ParamProvider.GetTestCasesFor(method);

            testCaseCollection.Add(testMethods.FixtureType.FullName + "." + method.Name);
            foreach (object source in ParameterList)
            {
                ParameterSet parms;
                if (source == null)
                {
                    parms           = new ParameterSet();
                    parms.Arguments = new object[] { null };
                }
                else
                {
                    parms = source as ParameterSet;
                }
                if (parms == null)
                {
                    if (source.GetType().GetInterface("NUnit.Framework.ITestCaseData") != null)
                    {
                        parms = ParameterSet.FromDataSource(source);
                    }
                    else
                    {
                        parms = new ParameterSet();
                        ParameterInfo[] parameters = method.GetParameters();
                        Type            sourceType = source.GetType();
                        if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType))
                        {
                            parms.Arguments = new object[] { source }
                        }
                        ;
                        else if (source is object[])
                        {
                            parms.Arguments = (object[])source;
                        }
                        else if (source is Array)
                        {
                            Array array = (Array)source;
                            if (array.Rank == 1)
                            {
                                parms.Arguments = new object[array.Length];
                                for (int i = 0; i < array.Length; i++)
                                {
                                    parms.Arguments[i] = (object)array.GetValue(i);
                                }
                            }
                        }
                        else
                        {
                            parms.Arguments = new object[] { source }
                        };
                    }
                }
                TestMethod testMethod = NUnit.Core.Builders.NUnitTestCaseBuilder.BuildSingleTestMethod(method, null, parms);
                testCaseCollection.Add(testMethod.TestName.FullName);
            }
            return(testCaseCollection);
        }