PageRenderTime 32ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/Builders/TestCaseSourceProvider.cs

#
C# | 109 lines | 64 code | 14 blank | 31 comment | 4 complexity | 22f47e5f473a762b3074f926a798ed55 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;
  8using System.Collections;
  9using System.Reflection;
 10using NUnit.Core.Extensibility;
 11
 12namespace NUnit.Core.Builders
 13{
 14    /// <summary>
 15    /// TestCaseSourceProvider provides data for methods
 16    /// annotated with the TestCaseSourceAttribute.
 17    /// </summary>
 18    public class TestCaseSourceProvider : ITestCaseProvider2
 19    {
 20        #region Constants
 21        public const string SourceTypeProperty = "SourceType";
 22        public const string SourceNameProperty = "SourceName";
 23        #endregion
 24
 25        #region ITestCaseProvider Members
 26
 27        /// <summary>
 28        /// Determine whether any test cases are available for a parameterized method.
 29        /// </summary>
 30        /// <param name="method">A MethodInfo representing a parameterized test</param>
 31        /// <returns>True if any cases are available, otherwise false.</returns>
 32        public bool HasTestCasesFor(MethodInfo method)
 33        {
 34            return Reflect.HasAttribute(method, NUnitFramework.TestCaseSourceAttribute, false);
 35        }
 36
 37        /// <summary>
 38        /// Return an IEnumerable providing test cases for use in
 39        /// running a parameterized test.
 40        /// </summary>
 41        /// <param name="method"></param>
 42        /// <returns></returns>
 43        public IEnumerable GetTestCasesFor(MethodInfo method)
 44        {
 45            return GetTestCasesFor(method, null);
 46        }
 47        #endregion
 48
 49        #region ITestCaseProvider2 Members
 50
 51        /// <summary>
 52        /// Determine whether any test cases are available for a parameterized method.
 53        /// </summary>
 54        /// <param name="method">A MethodInfo representing a parameterized test</param>
 55        /// <returns>True if any cases are available, otherwise false.</returns>
 56        public bool HasTestCasesFor(MethodInfo method, Test suite)
 57        {
 58            return HasTestCasesFor(method);
 59        }
 60
 61        /// <summary>
 62        /// Return an IEnumerable providing test cases for use in
 63        /// running a parameterized test.
 64        /// </summary>
 65        /// <param name="method"></param>
 66        /// <returns></returns>
 67        public IEnumerable GetTestCasesFor(MethodInfo method, Test parentSuite)
 68        {
 69            ArrayList parameterList = new ArrayList();
 70
 71            foreach (ProviderReference info in GetSourcesFor(method, parentSuite))
 72            {
 73                foreach (object o in info.GetInstance())
 74                    parameterList.Add(o);
 75            }
 76
 77            return parameterList;
 78        }
 79        #endregion
 80
 81        #region Helper Methods
 82        private static IList GetSourcesFor(MethodInfo method, Test parent)
 83        {
 84            ArrayList sources = new ArrayList();
 85            TestFixture parentSuite = parent as TestFixture;
 86
 87            foreach (Attribute sourceAttr in Reflect.GetAttributes(method, NUnitFramework.TestCaseSourceAttribute, false))
 88            {
 89                Type sourceType = Reflect.GetPropertyValue(sourceAttr, SourceTypeProperty) as Type;
 90                string sourceName = Reflect.GetPropertyValue(sourceAttr, SourceNameProperty) as string;
 91
 92                if (sourceType == null)
 93                {
 94                    if (parentSuite != null)
 95                        sources.Add(new ProviderReference(parentSuite.FixtureType, parentSuite.arguments, sourceName));
 96                    else
 97                        sources.Add(new ProviderReference(method.ReflectedType, sourceName));
 98                }
 99                else
100                {
101                    sources.Add(new ProviderReference(sourceType, sourceName));
102                }
103
104            }
105            return sources;
106        }
107        #endregion
108    }
109}