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()];
68 /// Make a test case from a given fixture type and method
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
)
75 TestCase testCase
= null;
77 if( Reflect
.HasTestAttribute(method
) || Reflect
.IsObsoleteTestMethod( method
) )
79 if( Reflect
.IsTestMethodSignatureCorrect( method
) )
81 ITestBuilder builder
= GetBuilder(method
);
82 testCase
= builder
.Make(fixtureType
, method
);
84 if(Reflect
.HasIgnoreAttribute(method
))
86 testCase
.ShouldRun
= false;
87 testCase
.IgnoreReason
= Reflect
.GetIgnoreReason(method
);
90 if (Reflect
.HasCategoryAttribute(method
))
92 IList categories
= Reflect
.GetCategories(method
);
93 CategoryManager
.Add(categories
);
94 testCase
.Categories
= categories
;
97 testCase
.IsExplicit
= Reflect
.HasExplicitAttribute(method
);
99 testCase
.Description
= Reflect
.GetDescription(method
);
103 testCase
= new NotRunnableTestCase(method
);
110 #region Make Test Cases with pre-created fixtures
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.
115 public static TestCase
Make(object fixture
, MethodInfo method
)
117 TestCase testCase
= Make( fixture
.GetType(), method
);
118 testCase
.Fixture
= fixture
;
123 public static TestCase
Make(object fixture
, string methodName
)
125 MethodInfo method
= Reflect
.GetMethod( fixture
.GetType(), methodName
);
126 if ( method
!= null )
127 return Make(fixture
, method
);
135 internal interface ITestBuilder
137 TestCase
Make(Type fixtureType
, MethodInfo method
);
138 TestCase
Make(object fixture
, MethodInfo method
);
141 internal class ExpectedExceptionBuilder
: ITestBuilder
143 #region ITestBuilder Members
145 public TestCase
Make(Type fixtureType
, MethodInfo method
)
147 return new ExpectedExceptionTestCase( fixtureType
, method
);
150 public TestCase
Make(object fixture
, MethodInfo method
)
152 return new ExpectedExceptionTestCase( fixture
, method
);
158 internal class NormalBuilder
: ITestBuilder
160 #region ITestBuilder Members
162 public TestCase
Make(Type fixtureType
, MethodInfo method
)
164 return new NormalTestCase(fixtureType
, method
);
167 public TestCase
Make(object fixture
, MethodInfo method
)
169 return new NormalTestCase(fixture
, method
);