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
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 '***********************************************************************************/
33 using System.Reflection;
34 using System.Collections;
37 /// Summary description for TestCaseBuilder.
39 public class TestCaseBuilder
41 private static Hashtable builders;
42 private static ITestBuilder normalBuilder = new NormalBuilder();
44 private static void InitBuilders()
46 builders = new Hashtable();
47 builders[typeof(NUnit.Framework.ExpectedExceptionAttribute)] = new ExpectedExceptionBuilder();
50 private static ITestBuilder GetBuilder(MethodInfo method)
55 object[] attributes = method.GetCustomAttributes(false);
57 foreach (object attribute in attributes)
59 ITestBuilder builder = (ITestBuilder) builders[attribute.GetType()];
67 public static TestCase Make(object fixture, MethodInfo method)
69 TestCase testCase = null;
71 if(HasTestAttribute(method) || HasObsoleteTestName(method) && !HasAnySetUpOrTearDownAttribute(method) )
73 if(IsTestMethodSignatureCorrect(method))
75 ITestBuilder builder = GetBuilder(method);
76 testCase = builder.Make(fixture, method);
78 if(HasIgnoreAttribute(method))
80 testCase.ShouldRun = false;
81 testCase.IgnoreReason = GetIgnoreReason(method);
84 if (HasCategoryAttribute(method))
86 IList categories = GetCategories(method);
87 CategoryManager.Add(categories);
88 testCase.Categories = categories;
91 testCase.IsExplicit = HasExplicitAttribute(method);
93 testCase.Description = GetDescription(method);
97 testCase = new NotRunnableTestCase(method);
104 public static TestCase Make(object fixture, string methodName)
106 MethodInfo [] methods = fixture.GetType().GetMethods(BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance);
107 foreach(MethodInfo method in methods)
109 if(method.Name.Equals(methodName))
110 return Make(fixture, method);
116 private static bool IsExpectedException(MethodInfo method)
118 Type exceptionAttr = typeof(NUnit.Framework.ExpectedExceptionAttribute);
119 object[] attributes = method.GetCustomAttributes(exceptionAttr, false);
120 return attributes.Length == 1;
123 private static NUnit.Framework.ExpectedExceptionAttribute GetExpectedExceptions(MethodInfo method)
125 Type exceptionAttr = typeof(NUnit.Framework.ExpectedExceptionAttribute);
126 object[] attributes = method.GetCustomAttributes(exceptionAttr, false);
128 NUnit.Framework.ExpectedExceptionAttribute expectedAttr = null;
130 if(attributes.Length == 1)
132 expectedAttr = (NUnit.Framework.ExpectedExceptionAttribute)attributes[0];
138 private static string GetDescription(MethodInfo method)
140 Type testAttr = typeof(NUnit.Framework.TestAttribute);
141 object[] attributes = method.GetCustomAttributes(testAttr, false);
143 string description = null;
145 if(attributes.Length == 1)
147 NUnit.Framework.TestAttribute attribute =
148 (NUnit.Framework.TestAttribute)attributes[0];
149 description = attribute.Description;
156 public static int CountTestCases(object fixture)
160 MethodInfo [] methods = fixture.GetType().GetMethods();
161 foreach(MethodInfo method in methods)
163 if(IsTestMethod(method))
171 public static bool IsTestMethod(MethodInfo methodToCheck)
174 (HasTestAttribute(methodToCheck) || HasObsoleteTestName(methodToCheck))
175 && IsTestMethodSignatureCorrect(methodToCheck);
178 private static bool IsTestMethodSignatureCorrect(MethodInfo methodToCheck)
181 !methodToCheck.IsStatic
182 && !methodToCheck.IsAbstract
183 && methodToCheck.IsPublic
184 && methodToCheck.GetParameters().Length == 0
185 && methodToCheck.ReturnType.Equals(typeof(void));
188 private static bool HasTestAttribute(MethodInfo methodToCheck)
190 return methodToCheck.IsDefined(typeof(NUnit.Framework.TestAttribute),false);
193 private static bool HasObsoleteTestName(MethodInfo methodToCheck)
195 return methodToCheck.Name.ToLower().StartsWith("test");
198 private static bool HasAnySetUpOrTearDownAttribute( MethodInfo methodToCheck )
200 object[] attributes = methodToCheck.GetCustomAttributes( false );
201 foreach( Attribute attribute in attributes )
202 if ( attribute is NUnit.Framework.SetUpAttribute ||
203 attribute is NUnit.Framework.TestFixtureSetUpAttribute ||
204 attribute is NUnit.Framework.TearDownAttribute ||
205 attribute is NUnit.Framework.TestFixtureTearDownAttribute )
211 private static bool HasIgnoreAttribute(MethodInfo methodToCheck)
213 Type ignoreMethodAttribute = typeof(NUnit.Framework.IgnoreAttribute);
214 object[] attributes = methodToCheck.GetCustomAttributes(ignoreMethodAttribute, false);
215 return attributes.Length == 1;
218 private static string GetIgnoreReason(MethodInfo methodToCheck)
220 Type ignoreMethodAttribute = typeof(NUnit.Framework.IgnoreAttribute);
221 NUnit.Framework.IgnoreAttribute[] attributes = (NUnit.Framework.IgnoreAttribute[])methodToCheck.GetCustomAttributes(ignoreMethodAttribute, false);
222 string result = "no reason";
223 if(attributes.Length > 0)
224 result = attributes[0].Reason;
229 private static bool HasCategoryAttribute(MethodInfo methodToCheck)
231 object[] attributes = methodToCheck.GetCustomAttributes(typeof(NUnit.Framework.CategoryAttribute), false);
232 return attributes.Length > 0;
235 private static bool HasExplicitAttribute(MethodInfo methodToCheck)
237 object[] attributes = methodToCheck.GetCustomAttributes(typeof(NUnit.Framework.ExplicitAttribute), false);
238 return attributes.Length > 0;
241 private static IList GetCategories(MethodInfo methodToCheck)
243 object[] attributes = methodToCheck.GetCustomAttributes(typeof(NUnit.Framework.CategoryAttribute), false);
244 ArrayList list = new ArrayList();
245 foreach (NUnit.Framework.CategoryAttribute attrib in attributes)
247 list.Add(attrib.Name);
254 internal interface ITestBuilder
256 TestCase Make(object fixture, MethodInfo method);
259 internal class ExpectedExceptionBuilder : ITestBuilder
261 #region ITestBuilder Members
263 public TestCase Make(object fixture, MethodInfo method)
265 NUnit.Framework.ExpectedExceptionAttribute expectedException = GetExpectedExceptions(method);
266 TestCase testCase = new ExpectedExceptionTestCase(fixture, method, expectedException.ExceptionType, expectedException.ExpectedMessage);
273 private static NUnit.Framework.ExpectedExceptionAttribute GetExpectedExceptions(MethodInfo method)
275 Type exceptionAttr = typeof(NUnit.Framework.ExpectedExceptionAttribute);
276 object[] attributes = method.GetCustomAttributes(exceptionAttr, false);
278 NUnit.Framework.ExpectedExceptionAttribute expectedAttr = null;
280 if(attributes.Length == 1)
282 expectedAttr = (NUnit.Framework.ExpectedExceptionAttribute)attributes[0];
290 internal class NormalBuilder : ITestBuilder
292 #region ITestBuilder Members
294 public TestCase Make(object fixture, MethodInfo method)
296 return new NormalTestCase(fixture, method);