PageRenderTime 29ms CodeModel.GetById 24ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/Builders/ValueSourceProvider.cs

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