**** Merged from MCS ****
[mono-project.git] / mcs / nunit20 / core / Test.cs
blob58b7a0aa96061ef9fb1cd89742e568ce5c10af20
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
9 ' software.
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
28 #endregion
30 namespace NUnit.Core
32 using System;
33 using System.Collections;
35 /// <summary>
36 /// Test Class.
37 /// </summary>
38 public abstract class Test : LongLivingMarshalByRefObject, ITest, IComparable
40 #region Private Fields
42 /// <summary>
43 /// Name of the test
44 /// </summary>
45 private string testName;
47 /// <summary>
48 /// Full Name of the test
49 /// </summary>
50 private string fullName;
52 /// <summary>
53 /// Int used to distinguish suites of the same
54 /// name across multiple assemblies.
55 /// </summary>
56 private int assemblyKey;
58 /// <summary>
59 /// The Type of the fixture associated with this test, or null
60 /// </summary>
61 protected Type fixtureType;
63 /// <summary>
64 /// The fixture object, to be used with this test, or null
65 /// </summary>
66 private object fixture;
68 /// <summary>
69 /// Whether or not the test should be run
70 /// </summary>
71 private bool shouldRun;
73 /// <summary>
74 /// Reason for not running the test, if applicable
75 /// </summary>
76 private string ignoreReason;
78 /// <summary>
79 /// Description for this test
80 /// </summary>
81 private string description;
83 /// <summary>
84 /// Test suite containing this test, or null
85 /// </summary>
86 private TestSuite parent;
88 /// <summary>
89 /// List of categories applying to this test
90 /// </summary>
91 private IList categories;
93 /// <summary>
94 /// True if the test had the Explicit attribute
95 /// </summary>
96 private bool isExplicit;
98 #endregion
100 #region Constructors
102 public Test( string name ) : this( name, 0 ) { }
104 public Test( string name, int assemblyKey )
106 fullName = testName = name;
107 this.assemblyKey = assemblyKey;
110 protected Test( string pathName, string testName )
111 : this( pathName, testName, 0 ) { }
113 protected Test( string pathName, string testName, int assemblyKey )
115 fullName = pathName == null || pathName == string.Empty ? testName : pathName + "." + testName;
116 this.testName = testName;
117 this.assemblyKey = assemblyKey;
118 shouldRun = true;
121 protected Test( Type fixtureType ) : this( fixtureType, 0 ) { }
123 protected Test( Type fixtureType, int assemblyKey )
125 this.fixtureType = fixtureType;
126 this.fullName = this.testName = fixtureType.FullName;
127 this.assemblyKey = assemblyKey;
128 this.shouldRun = true;
130 if ( fixtureType.Namespace != null )
131 testName = testName.Substring( Name.LastIndexOf( '.' ) + 1 );
134 // TODO: Currently, these two are only used by our tests. Remove?
135 protected Test( object fixture ) : this( fixture, 0 ) { }
137 protected Test( object fixture, int assemblyKey ) : this( fixture.GetType(), assemblyKey )
139 this.fixture = fixture;
142 #endregion
144 #region Properties
146 public string Name
148 get { return testName; }
151 public string FullName
153 get { return fullName; }
156 /// <summary>
157 /// If the name is a path, this just returns the file part
158 /// </summary>
159 public string ShortName
163 string name = Name;
164 int val = name.LastIndexOf("\\");
165 if(val != -1)
166 name = name.Substring(val+1);
167 return name;
171 /// <summary>
172 /// Int used to distinguish suites of the same
173 /// name across multiple assemblies.
174 /// </summary>
175 public int AssemblyKey
177 get { return assemblyKey; }
178 set { assemblyKey = value; }
181 /// <summary>
182 /// Key used to look up a test in a hash table
183 /// </summary>
184 public string UniqueName
186 get { return string.Format( "[{0}]{1}", assemblyKey, fullName ); }
189 public object Fixture
191 get { return fixture; }
192 set { fixture = value; }
195 /// <summary>
196 /// Whether or not the test should be run
197 /// </summary>
198 public virtual bool ShouldRun
200 get { return shouldRun; }
201 set { shouldRun = value; }
204 /// <summary>
205 /// Reason for not running the test, if applicable
206 /// </summary>
207 public string IgnoreReason
209 get { return ignoreReason; }
210 set { ignoreReason = value; }
213 public TestSuite Parent
215 get { return parent; }
216 set { parent = value; }
219 public string TestPath
223 string testPath = "";
224 if (parent != null)
225 testPath = parent.TestPath;
226 return testPath + FullName;
230 public IList Categories
232 get { return categories; }
233 set { categories = value; }
236 public bool HasCategory( string name )
238 return categories != null && categories.Contains( name );
241 public bool HasCategory( IList names )
243 if ( categories == null )
244 return false;
246 foreach( string name in names )
247 if ( categories.Contains( name ) )
248 return true;
250 return false;
253 public bool IsDescendant(Test test)
255 if (parent != null)
257 return parent == test || parent.IsDescendant(test);
260 return false;
263 public String Description
265 get { return description; }
266 set { description = value; }
269 public bool IsExplicit
271 get { return isExplicit; }
272 set { isExplicit = value; }
275 #endregion
277 #region Abstract Methods and Properties
279 /// <summary>
280 /// Count of the test cases ( 1 if this is a test case )
281 /// </summary>
282 public abstract int CountTestCases();
283 public abstract int CountTestCases(IFilter filter);
285 public abstract bool IsSuite { get; }
286 public abstract bool IsFixture{ get; }
287 public abstract bool IsTestCase{ get; }
288 public abstract ArrayList Tests { get; }
290 public abstract bool Filter(IFilter filter);
292 public abstract TestResult Run( EventListener listener );
293 public abstract TestResult Run(EventListener listener, IFilter filter);
295 #endregion
297 #region IComparable Members
299 public int CompareTo(object obj)
301 Test other = obj as Test;
303 if ( other == null )
304 return -1;
306 return this.FullName.CompareTo( other.FullName );
309 #endregion