PageRenderTime 31ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/Extensibility/TestCaseProviders.cs

#
C# | 133 lines | 88 code | 16 blank | 29 comment | 10 complexity | 646321c8b8963a8a9397735cb1cc8151 MD5 | raw file
  1// ****************************************************************
  2// Copyright 2008, Charlie Poole
  3// This is free software licensed under the NUnit license. You may
  4// obtain a copy of the license at http://nunit.org.
  5// ****************************************************************
  6
  7using System.Collections;
  8using System.Reflection;
  9
 10namespace NUnit.Core.Extensibility
 11{
 12    class TestCaseProviders : ExtensionPoint, ITestCaseProvider2
 13    {
 14        public TestCaseProviders(IExtensionHost host) : base( "TestCaseProviders", host ) { }
 15
 16        #region ITestCaseProvider Members
 17
 18        /// <summary>
 19        /// Determine whether any test cases are available for a parameterized method.
 20        /// </summary>
 21        /// <param name="method">A MethodInfo representing a parameterized test</param>
 22        /// <returns>True if any cases are available, otherwise false.</returns>
 23        public bool HasTestCasesFor(MethodInfo method)
 24        {
 25            foreach (ITestCaseProvider provider in Extensions)
 26                if (provider.HasTestCasesFor(method))
 27                    return true;
 28
 29            return false;
 30        }
 31
 32        /// <summary>
 33        /// Return an enumeration providing test cases for use in
 34        /// running a paramterized test.
 35        /// </summary>
 36        /// <param name="method"></param>
 37        /// <returns></returns>
 38        public IEnumerable GetTestCasesFor(MethodInfo method)
 39        {
 40            ArrayList testcases = new ArrayList();
 41
 42            foreach (ITestCaseProvider provider in Extensions)
 43                try
 44                {
 45                    if (provider.HasTestCasesFor(method))
 46                        foreach (object o in provider.GetTestCasesFor(method))
 47                            testcases.Add(o);
 48                }
 49                catch (System.Reflection.TargetInvocationException ex)
 50                {
 51                    testcases.Add(new ParameterSet(ex.InnerException));
 52                }
 53                catch (System.Exception ex)
 54                {
 55                    testcases.Add(new ParameterSet(ex));
 56                }
 57
 58            return testcases;
 59        }
 60
 61        #endregion
 62
 63        #region ITestCaseProvider2 Members
 64
 65        /// <summary>
 66        /// Determine whether any test cases are available for a parameterized method.
 67        /// </summary>
 68        /// <param name="method">A MethodInfo representing a parameterized test</param>
 69        /// <param name="suite">The suite for which the test case is being built</param>
 70        /// <returns>True if any cases are available, otherwise false.</returns>
 71        public bool HasTestCasesFor(MethodInfo method, Test suite)
 72        {
 73            foreach (ITestCaseProvider provider in Extensions)
 74            {
 75                if (provider is ITestCaseProvider2)
 76                {
 77                    ITestCaseProvider2 provider2 = (ITestCaseProvider2)provider;
 78                    if (provider2.HasTestCasesFor(method, suite))
 79                        return true;
 80                }
 81                else if (provider.HasTestCasesFor(method))
 82                    return true;
 83            }
 84
 85            return false;
 86        }
 87
 88        /// <summary>
 89        /// Return an enumeration providing test cases for use in
 90        /// running a paramterized test.
 91        /// </summary>
 92        /// <param name="method">A MethodInfo representing a parameterized test</param>
 93        /// <param name="suite">The suite for which the test case is being built</param>
 94        /// <returns></returns>
 95        public IEnumerable GetTestCasesFor(MethodInfo method, Test suite)
 96        {
 97            ArrayList testcases = new ArrayList();
 98
 99            foreach (ITestCaseProvider provider in Extensions)
100                try
101                {
102                    if (provider is ITestCaseProvider2)
103                    {
104                        ITestCaseProvider2 provider2 = (ITestCaseProvider2)provider;
105                        if (provider2.HasTestCasesFor(method, suite))
106                            foreach (object o in provider2.GetTestCasesFor(method, suite))
107                                testcases.Add(o);
108                    }
109                    else if (provider.HasTestCasesFor(method))
110                        foreach (object o in provider.GetTestCasesFor(method))
111                            testcases.Add(o);
112                }
113                catch (System.Reflection.TargetInvocationException ex)
114                {
115                    testcases.Add(new ParameterSet(ex.InnerException));
116                }
117                catch (System.Exception ex)
118                {
119                    testcases.Add(new ParameterSet(ex));
120                }
121
122            return testcases;
123        }
124        #endregion
125
126        #region IsValidExtension
127        protected override bool IsValidExtension(object extension)
128        {
129            return extension is ITestCaseProvider;
130        }
131        #endregion
132    }
133}