2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / nunit20 / core / TestCaseBuilder.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.Reflection;
34         using System.Collections;
35
36         /// <summary>
37         /// Summary description for TestCaseBuilder.
38         /// </summary>
39         public class TestCaseBuilder
40         {
41                 private static Hashtable builders;
42                 private static ITestBuilder normalBuilder = new NormalBuilder();
43
44                 private static void InitBuilders() 
45                 {
46                         builders = new Hashtable();
47                         builders[typeof(NUnit.Framework.ExpectedExceptionAttribute)] = new ExpectedExceptionBuilder();
48                 }
49
50                 private static ITestBuilder GetBuilder(MethodInfo method) 
51                 {
52                         if (builders == null)
53                                 InitBuilders();
54
55                         object[] attributes = method.GetCustomAttributes(false);
56                         
57                         foreach (object attribute in attributes) 
58                         {
59                                 ITestBuilder builder = (ITestBuilder) builders[attribute.GetType()];
60                                 if (builder != null)
61                                         return builder;
62                         }
63
64                         return normalBuilder;
65                 }
66
67                 /// <summary>
68                 /// Make a test case from a given fixture type and method
69                 /// </summary>
70                 /// <param name="fixtureType">The fixture type</param>
71                 /// <param name="method">MethodInfo for the particular method</param>
72                 /// <returns>A test case or null</returns>
73                 public static TestCase Make(Type fixtureType, MethodInfo method)
74                 {
75                         TestCase testCase = null;
76
77                         if( Reflect.HasTestAttribute(method) || Reflect.IsObsoleteTestMethod( method ) )
78                         {
79                                 if( Reflect.IsTestMethodSignatureCorrect( method ) )
80                                 {
81                                         ITestBuilder builder = GetBuilder(method);
82                                         testCase = builder.Make(fixtureType, method);
83
84                                         if(Reflect.HasIgnoreAttribute(method))
85                                         {
86                                                 testCase.ShouldRun = false;
87                                                 testCase.IgnoreReason = Reflect.GetIgnoreReason(method);
88                                         }
89
90                                         if (Reflect.HasCategoryAttribute(method)) 
91                                         {
92                                                 IList categories = Reflect.GetCategories(method);
93                                                 CategoryManager.Add(categories);
94                                                 testCase.Categories = categories;
95                                         }
96
97                                         testCase.IsExplicit = Reflect.HasExplicitAttribute(method);
98
99                                         testCase.Description = Reflect.GetDescription(method);
100                                 }
101                                 else
102                                 {
103                                         testCase = new NotRunnableTestCase(method);
104                                 }
105                         }
106
107                         return testCase;
108                 }
109
110                 #region Make Test Cases with pre-created fixtures
111
112                 // TODO: These methods are only used by our tests, since we no longer
113                 // create the fixture in advance. They should be phased out.
114
115                 public static TestCase Make(object fixture, MethodInfo method)
116                 {
117                         TestCase testCase = Make( fixture.GetType(), method );
118                         testCase.Fixture = fixture;
119
120                         return testCase;
121                 }
122
123                 public static TestCase Make(object fixture, string methodName)
124                 {
125                         MethodInfo method = Reflect.GetMethod( fixture.GetType(), methodName );
126                         if ( method != null )
127                                 return Make(fixture, method);
128
129                         return null;
130                 }
131
132                 #endregion
133         }
134
135         internal interface ITestBuilder 
136         {
137                 TestCase Make(Type fixtureType, MethodInfo method);
138                 TestCase Make(object fixture, MethodInfo method);
139         }
140
141         internal class ExpectedExceptionBuilder : ITestBuilder
142         {
143                 #region ITestBuilder Members
144
145                 public TestCase Make(Type fixtureType, MethodInfo method)
146                 {
147                         return new ExpectedExceptionTestCase( fixtureType, method );
148                 }
149
150                 public TestCase Make(object fixture, MethodInfo method)
151                 {
152                         return new ExpectedExceptionTestCase( fixture, method );
153                 }
154
155                 #endregion
156         }
157
158         internal class NormalBuilder : ITestBuilder
159         {
160                 #region ITestBuilder Members
161
162                 public TestCase Make(Type fixtureType, MethodInfo method)
163                 {
164                         return new NormalTestCase(fixtureType, method);
165                 }
166
167                 public TestCase Make(object fixture, MethodInfo method)
168                 {
169                         return new NormalTestCase(fixture, method);
170                 }
171
172                 #endregion
173         }
174
175
176 }
177