1
// ***********************************************************************
2
// Copyright (c) 2009 Charlie Poole
4
// Permission is hereby granted, free of charge, to any person obtaining
5
// a copy of this software and associated documentation files (the
6
// "Software"), to deal in the Software without restriction, including
7
// without limitation the rights to use, copy, modify, merge, publish,
8
// distribute, sublicense, and/or sell copies of the Software, and to
9
// permit persons to whom the Software is furnished to do so, subject to
10
// the following conditions:
12
// The above copyright notice and this permission notice shall be
13
// included in all copies or substantial portions of the Software.
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
// ***********************************************************************
25
using System.Reflection;
26
using System.Collections;
27
using System.Collections.Specialized;
28
using System.Diagnostics;
29
using NUnit.Core.Extensibility;
30
using NUnit.Framework;
31
using NUnit.Framework.Api;
33
namespace NUnit.Framework.Internal
36
/// Static methods that implement aspects of the NUnit framework that cut
37
/// across individual test types, extensions, etc. Some of these use the
38
/// methods of the Reflect class to implement operations specific to the
41
public class NUnitFramework
44
private static Assembly frameworkAssembly;
45
private static bool frameworkAssemblyInitialized;
46
private static Assembly FrameworkAssembly
50
if (!frameworkAssemblyInitialized)
51
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
52
if (assembly.GetName().Name == "nunit.framework" ||
53
assembly.GetName().Name == "NUnitLite")
55
frameworkAssembly = assembly;
59
frameworkAssemblyInitialized = true;
61
return frameworkAssembly;
66
#region Check SetUp and TearDown methods
67
public static bool CheckSetUpTearDownMethods(Type fixtureType, Type attributeType, ref string reason)
69
foreach( MethodInfo theMethod in Reflect.GetMethodsWithAttribute(fixtureType, attributeType, true ))
70
if ( theMethod.IsAbstract ||
71
!theMethod.IsPublic && !theMethod.IsFamily ||
72
theMethod.GetParameters().Length > 0 ||
73
!theMethod.ReturnType.Equals(typeof(void)))
75
reason = string.Format( "Invalid signature for SetUp or TearDown method: {0}", theMethod.Name );
83
#region ApplyCommonAttributes
85
/// Modify a newly constructed test based on a type or method by
86
/// applying any of NUnit's common attributes.
88
/// <param name="member">The type or method from which the test was constructed</param>
89
/// <param name="test">The test to which the attributes apply</param>
90
public static void ApplyCommonAttributes(MemberInfo member, Test test)
92
ApplyCommonAttributes( Reflect.GetAttributes( member, false ), test );
96
/// Modify a newly constructed test based on an assembly by applying
97
/// any of NUnit's common attributes.
99
/// <param name="assembly">The assembly from which the test was constructed</param>
100
/// <param name="test">The test to which the attributes apply</param>
101
public static void ApplyCommonAttributes(Assembly assembly, Test test)
103
ApplyCommonAttributes( Reflect.GetAttributes( assembly, false ), test );
107
/// Modify a newly constructed test by applying any of NUnit's common
108
/// attributes, based on an input array of attributes. This method checks
109
/// for all attributes, relying on the fact that specific attributes can only
110
/// occur on those constructs on which they are allowed.
112
/// <param name="attributes">An array of attributes possibly including NUnit attributes
113
/// <param name="test">The test to which the attributes apply</param>
114
public static void ApplyCommonAttributes(Attribute[] attributes, Test test)
116
foreach (Attribute attribute in attributes)
118
Type attributeType = attribute.GetType();
119
string attributeName = attributeType.FullName;
120
bool isValid = test.RunState != RunState.NotRunnable;
122
if (attribute is TestFixtureAttribute)
124
if (test.Description == null)
125
test.Description = ((TestFixtureAttribute)attribute).Description;
127
else if (attribute is TestAttribute)
129
if (test.Description == null)
130
test.Description = ((TestAttribute)attribute).Description;
132
else if (attribute is ISetRunState)
136
ISetRunState irs = (ISetRunState)attribute;
137
test.RunState = irs.GetRunState();
138
test.IgnoreReason = irs.GetReason();
141
else if (attribute is CategoryAttribute)
143
test.Categories.Add(((CategoryAttribute)attribute).Name);
145
else if (attribute is PropertyAttribute)
147
IDictionary props = ((PropertyAttribute)attribute).Properties;
149
foreach (DictionaryEntry entry in props)
150
test.Properties.Add(entry.Key, entry.Value);
156
#region ApplyExpectedExceptionAttribute
158
/// Modify a newly constructed test by checking for ExpectedExceptionAttribute
159
/// and setting properties on the test accordingly.
161
/// <param name="method">The method being processed, possibly annotated with an ExpectedExceptionAttribute</param>
162
/// <param name="testMethod">The test to which the attributes apply</param>
163
public static void ApplyExpectedExceptionAttribute(MethodInfo method, TestMethod testMethod)
165
ExpectedExceptionAttribute[] attributes =
166
(ExpectedExceptionAttribute[])method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false);
168
if (attributes != null && attributes.Length > 0)
169
testMethod.ExceptionProcessor = new ExpectedExceptionProcessor(testMethod, attributes[0]);
174
#region GetResultState
176
/// Returns a result state for a special exception.
177
/// If the exception is not handled specially, returns
178
/// ResultState.Error.
180
/// <param name="ex">The exception to be examined</param>
181
/// <returns>A ResultState</returns>
182
public static ResultState GetResultState(Exception ex)
184
if (ex is System.Threading.ThreadAbortException)
185
return ResultState.Cancelled;
187
if (ex is AssertionException)
188
return ResultState.Failure;
190
if (ex is IgnoreException)
191
return ResultState.Ignored;
193
if (ex is InconclusiveException)
194
return ResultState.Inconclusive;
196
if (ex is SuccessException)
197
return ResultState.Success;
199
return ResultState.Error;