PageRenderTime 15ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Attributes/TestFixtureAttribute.cs

#
C# | 171 lines | 106 code | 20 blank | 45 comment | 18 complexity | 6a2f29671887262e93c836b479414b42 MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You
  3// may obtain a copy of the license as well as information regarding
  4// copyright ownership at http://nunit.org.
  5// ****************************************************************
  6
  7namespace NUnit.Framework
  8{
  9	using System;
 10    using System.Collections;
 11
 12	/// <example>
 13	/// [TestFixture]
 14	/// public class ExampleClass 
 15	/// {}
 16	/// </example>
 17	[AttributeUsage(AttributeTargets.Class, AllowMultiple=true, Inherited=true)]
 18	public class TestFixtureAttribute : Attribute
 19	{
 20		private string description;
 21
 22        private object[] arguments;
 23        private bool isIgnored;
 24        private string ignoreReason;
 25        private string category;
 26
 27#if NET_2_0
 28        private Type[] typeArgs;
 29        private bool argsSeparated;
 30#endif
 31
 32        /// <summary>
 33        /// Default constructor
 34        /// </summary>
 35        public TestFixtureAttribute() : this( null ) { }
 36        
 37        /// <summary>
 38        /// Construct with a object[] representing a set of arguments. 
 39        /// In .NET 2.0, the arguments may later be separated into
 40        /// type arguments and constructor arguments.
 41        /// </summary>
 42        /// <param name="arguments"></param>
 43        public TestFixtureAttribute(params object[] arguments)
 44        {
 45            this.arguments = arguments == null
 46                ? new object[0]
 47                : arguments;
 48
 49            for (int i = 0; i < this.arguments.Length; i++)
 50                if (arguments[i] is SpecialValue && (SpecialValue)arguments[i] == SpecialValue.Null)
 51                    arguments[i] = null;
 52        }
 53
 54        /// <summary>
 55        /// Descriptive text for this fixture
 56        /// </summary>
 57        public string Description
 58		{
 59			get { return description; }
 60			set { description = value; }
 61		}
 62
 63        /// <summary>
 64        /// Gets and sets the category for this fixture.
 65        /// May be a comma-separated list of categories.
 66        /// </summary>
 67        public string Category
 68        {
 69            get { return category; }
 70            set { category = value; }
 71        }
 72
 73        /// <summary>
 74        /// Gets a list of categories for this fixture
 75        /// </summary>
 76        public IList Categories
 77        {
 78            get { return category == null ? null : category.Split(','); }
 79        }
 80
 81        /// <summary>
 82        /// The arguments originally provided to the attribute
 83        /// </summary>
 84        public object[] Arguments
 85        {
 86            get 
 87            {
 88#if NET_2_0
 89                if (!argsSeparated)
 90                    SeparateArgs();
 91#endif
 92                return arguments; 
 93            }
 94        }
 95
 96        /// <summary>
 97        /// Gets or sets a value indicating whether this <see cref="TestFixtureAttribute"/> should be ignored.
 98        /// </summary>
 99        /// <value><c>true</c> if ignore; otherwise, <c>false</c>.</value>
100        public bool Ignore
101        {
102            get { return isIgnored; }
103            set { isIgnored = value; }
104        }
105
106        /// <summary>
107        /// Gets or sets the ignore reason. May set Ignored as a side effect.
108        /// </summary>
109        /// <value>The ignore reason.</value>
110        public string IgnoreReason
111        {
112            get { return ignoreReason; }
113            set
114            {
115                ignoreReason = value;
116                isIgnored = ignoreReason != null && ignoreReason != string.Empty;
117            }
118        }
119
120#if NET_2_0
121        /// <summary>
122        /// Get or set the type arguments. If not set
123        /// explicitly, any leading arguments that are
124        /// Types are taken as type arguments.
125        /// </summary>
126        public Type[] TypeArgs
127        {
128            get
129            {
130                if (!argsSeparated)
131                    SeparateArgs();
132
133                return typeArgs;
134            }
135            set 
136            { 
137                typeArgs = value;
138                argsSeparated = true;
139            }
140        }
141
142        private void SeparateArgs()
143        {
144            int cnt = 0;
145            if (arguments != null)
146            {
147                foreach (object o in arguments)
148                    if (o is Type) cnt++;
149                    else break;
150
151                typeArgs = new Type[cnt];
152                for (int i = 0; i < cnt; i++)
153                    typeArgs[i] = (Type)arguments[i];
154
155                if (cnt > 0)
156                {
157                    object[] args = new object[arguments.Length - cnt];
158                    for (int i = 0; i < args.Length; i++)
159                        args[i] = arguments[cnt + i];
160
161                    arguments = args;
162                }
163            }
164            else
165                typeArgs = new Type[0];
166
167            argsSeparated = true;
168        }
169#endif
170	}
171}