PageRenderTime 14ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Attributes/ValuesAttribute.cs

#
C# | 130 lines | 67 code | 17 blank | 46 comment | 23 complexity | f97034591d7c0f104d2da7a77039a3fb 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;
 10
 11namespace NUnit.Framework
 12{
 13    /// <summary>
 14    /// Abstract base class for attributes that apply to parameters 
 15    /// and supply data for the parameter.
 16    /// </summary>
 17    [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
 18    public abstract class ParameterDataAttribute : Attribute
 19    {
 20        /// <summary>
 21        /// Gets the data to be provided to the specified parameter
 22        /// </summary>
 23        public abstract IEnumerable GetData(ParameterInfo parameter);
 24    }
 25
 26    /// <summary>
 27    /// ValuesAttribute is used to provide literal arguments for
 28    /// an individual parameter of a test.
 29    /// </summary>
 30    [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
 31    public class ValuesAttribute : ParameterDataAttribute
 32    {
 33        /// <summary>
 34        /// The collection of data to be returned. Must
 35        /// be set by any derived attribute classes.
 36        /// We use an object[] so that the individual
 37        /// elements may have their type changed in GetData
 38        /// if necessary.
 39        /// </summary>
 40        // TODO: This causes a lot of boxing so we should eliminate it.
 41        protected object[] data;
 42
 43        /// <summary>
 44        /// Construct with one argument
 45        /// </summary>
 46        /// <param name="arg1"></param>
 47        public ValuesAttribute(object arg1)
 48        {
 49            data = new object[] { arg1 };
 50        }
 51
 52        /// <summary>
 53        /// Construct with two arguments
 54        /// </summary>
 55        /// <param name="arg1"></param>
 56        /// <param name="arg2"></param>
 57        public ValuesAttribute(object arg1, object arg2)
 58        {
 59            data = new object[] { arg1, arg2 };
 60        }
 61
 62        /// <summary>
 63        /// Construct with three arguments
 64        /// </summary>
 65        /// <param name="arg1"></param>
 66        /// <param name="arg2"></param>
 67        /// <param name="arg3"></param>
 68        public ValuesAttribute(object arg1, object arg2, object arg3)
 69        {
 70            data = new object[] { arg1, arg2, arg3 };
 71        }
 72
 73        /// <summary>
 74        /// Construct with an array of arguments
 75        /// </summary>
 76        /// <param name="args"></param>
 77        public ValuesAttribute(params object[] args)
 78        {
 79            data = args;
 80        }
 81
 82        /// <summary>
 83        /// Get the collection of values to be used as arguments
 84        /// </summary>
 85        public override IEnumerable GetData(ParameterInfo parameter)
 86        {
 87            Type targetType = parameter.ParameterType;
 88
 89            for (int i = 0; i < data.Length; i++)
 90            {
 91                object arg = data[i];
 92
 93                if (arg == null) 
 94                    continue;
 95
 96                if (arg.GetType().FullName == "NUnit.Framework.SpecialValue" &&
 97                    arg.ToString() == "Null")
 98                {
 99                    data[i] = null;
100                    continue;
101                }
102
103                if (targetType.IsAssignableFrom(arg.GetType()))
104                    continue;
105
106                if (arg is DBNull)
107                {
108                    data[i] = null;
109                    continue;
110                }
111
112                bool convert = false;
113
114                if (targetType == typeof(short) || targetType == typeof(byte) || targetType == typeof(sbyte))
115                    convert = arg is int;
116                else
117                    if (targetType == typeof(decimal))
118                        convert = arg is double || arg is string || arg is int;
119                    else
120                        if (targetType == typeof(DateTime) || targetType == typeof(TimeSpan))
121                            convert = arg is string;
122
123                if (convert)
124                    data[i] = Convert.ChangeType(arg, targetType, System.Globalization.CultureInfo.InvariantCulture);
125            }
126
127			return data;
128        }
129    }
130}