2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / nunit20 / core / Test.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 Philip A. Craig
6 '
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.
10
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:
14 '
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.
18 '
19 ' Portions Copyright © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 namespace NUnit.Core
31 {
32         using System;
33         using System.Collections;
34
35         /// <summary>
36         ///             Test Class.
37         /// </summary>
38         public abstract class Test : LongLivingMarshalByRefObject, ITest, IComparable
39         {
40                 #region Private Fields
41
42                 /// <summary>
43                 /// Name of the test
44                 /// </summary>
45                 private string testName;
46
47                 /// <summary>
48                 /// Full Name of the test
49                 /// </summary>
50                 private string fullName;
51                 
52                 /// <summary>
53                 /// Int used to distinguish suites of the same
54                 /// name across multiple assemblies.
55                 /// </summary>
56                 private int assemblyKey;
57
58                 /// <summary>
59                 /// The Type of the fixture associated with this test, or null
60                 /// </summary>
61                 protected Type fixtureType;
62
63                 /// <summary>
64                 /// The fixture object, to be used with this test, or null
65                 /// </summary>
66                 private object fixture;
67                 
68                 /// <summary>
69                 /// Whether or not the test should be run
70                 /// </summary>
71                 private bool shouldRun;
72                 
73                 /// <summary>
74                 /// Reason for not running the test, if applicable
75                 /// </summary>
76                 private string ignoreReason;
77                 
78                 /// <summary>
79                 /// Description for this test 
80                 /// </summary>
81                 private string description;
82                 
83                 /// <summary>
84                 /// Test suite containing this test, or null
85                 /// </summary>
86                 private TestSuite parent;
87                 
88                 /// <summary>
89                 /// List of categories applying to this test
90                 /// </summary>
91                 private IList categories;
92
93                 /// <summary>
94                 /// True if the test had the Explicit attribute
95                 /// </summary>
96                 private bool isExplicit;
97
98                 #endregion
99
100                 #region Constructors
101
102                 public Test( string name ) : this( name, 0 ) { }
103
104                 public Test( string name, int assemblyKey )
105                 {
106                         fullName = testName = name;
107                         this.assemblyKey = assemblyKey;
108                 }
109
110                 protected Test( string pathName, string testName ) 
111                         : this( pathName, testName, 0 ) { }
112
113                 protected Test( string pathName, string testName, int assemblyKey ) 
114                 { 
115                         fullName = pathName == null || pathName == string.Empty ? testName : pathName + "." + testName;
116                         this.testName = testName;
117                         this.assemblyKey = assemblyKey;
118                         shouldRun = true;
119                 }
120
121                 protected Test( Type fixtureType ) : this( fixtureType, 0 ) { }
122
123                 protected Test( Type fixtureType, int assemblyKey ) 
124                 {
125                         this.fixtureType = fixtureType;
126                         this.fullName = this.testName = fixtureType.FullName;
127                         this.assemblyKey = assemblyKey;
128                         this.shouldRun = true;
129
130                         if ( fixtureType.Namespace != null )
131                                 testName = testName.Substring( Name.LastIndexOf( '.' ) + 1 );
132                 }
133
134                 // TODO: Currently, these two are only used by our tests. Remove?
135                 protected Test( object fixture ) : this( fixture, 0 ) { }
136
137                 protected Test( object fixture, int assemblyKey ) : this( fixture.GetType(), assemblyKey )
138                 {
139                         this.fixture = fixture;
140                 }
141
142                 #endregion
143
144                 #region Properties
145
146                 public string Name
147                 {
148                         get { return testName; }
149                 }
150
151                 public string FullName 
152                 {
153                         get { return fullName; }
154                 }
155
156                 /// <summary>
157                 /// If the name is a path, this just returns the file part
158                 /// </summary>
159                 public string ShortName
160                 {
161                         get
162                         {
163                                 string name = Name;
164                                 int val = name.LastIndexOf("\\");
165                                 if(val != -1)
166                                         name = name.Substring(val+1);
167                                 return name;
168                         }
169                 }
170
171                 /// <summary>
172                 /// Int used to distinguish suites of the same
173                 /// name across multiple assemblies.
174                 /// </summary>
175                 public int AssemblyKey
176                 {
177                         get { return assemblyKey; }
178                         set { assemblyKey = value; }
179                 }
180
181                 /// <summary>
182                 /// Key used to look up a test in a hash table
183                 /// </summary>
184                 public string UniqueName
185                 {
186                         get { return string.Format( "[{0}]{1}", assemblyKey, fullName ); }
187                 }
188
189                 public object Fixture
190                 {
191                         get { return fixture; }
192                         set { fixture = value; }
193                 }
194
195                 /// <summary>
196                 /// Whether or not the test should be run
197                 /// </summary>
198                 public virtual bool ShouldRun
199                 {
200                         get { return shouldRun; }
201                         set { shouldRun = value; }
202                 }
203
204                 /// <summary>
205                 /// Reason for not running the test, if applicable
206                 /// </summary>
207                 public string IgnoreReason
208                 {
209                         get { return ignoreReason; }
210                         set { ignoreReason = value; }
211                 }
212
213                 public TestSuite Parent 
214                 {
215                         get { return parent; }
216                         set { parent = value; }
217                 }
218
219                 public string TestPath 
220                 {
221                         get
222                         {
223                                 string testPath = "";
224                                 if (parent != null)
225                                         testPath = parent.TestPath;
226                                 return testPath + FullName;
227                         }
228                 }
229
230                 public IList Categories 
231                 {
232                         get { return categories; }
233                         set { categories = value; }
234                 }
235
236                 public bool HasCategory( string name )
237                 {
238                         return categories != null && categories.Contains( name );
239                 }
240
241                 public bool HasCategory( IList names )
242                 {
243                         if ( categories == null )
244                                 return false;
245
246                         foreach( string name in names )
247                                 if ( categories.Contains( name ) )
248                                         return true;
249                         
250                         return false;
251                 }
252
253                 public bool IsDescendant(Test test) 
254                 {
255                         if (parent != null) 
256                         {
257                                 return parent == test || parent.IsDescendant(test);
258                         }
259
260                         return false;
261                 }
262
263                 public String Description
264                 {
265                         get { return description; }
266                         set { description = value; }
267                 }
268
269                 public bool IsExplicit
270                 {
271                         get { return isExplicit; }
272                         set { isExplicit = value; }
273                 }
274
275                 #endregion
276
277                 #region Abstract Methods and Properties
278
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);
284                 
285                 public abstract bool IsSuite { get; }
286                 public abstract bool IsFixture{ get; }
287                 public abstract bool IsTestCase{ get; }
288                 public abstract ArrayList Tests { get; }
289
290                 public abstract bool Filter(IFilter filter);
291
292                 public abstract TestResult Run( EventListener listener );
293                 public abstract TestResult Run(EventListener listener, IFilter filter);
294
295                 #endregion
296
297                 #region IComparable Members
298
299                 public int CompareTo(object obj)
300                 {
301                         Test other = obj as Test;
302                         
303                         if ( other == null )
304                                 return -1;
305
306                         return this.FullName.CompareTo( other.FullName );
307                 }
308
309                 #endregion
310         }
311 }