~adam-rpconnelly/nunit-3.0/bug-483845

« back to all changes in this revision

Viewing changes to src/framework/Internal/NUnitFramework.cs

  • Committer: Charlie Poole
  • Date: 2010-01-08 17:21:03 UTC
  • Revision ID: charlie@nunit.com-20100108172103-o065jps8sbft4hhq
Further simplification of API and internals: Removing TestInfo and NUnitFramework classes, eliminating all references to the framework from the test runner and using xml to communicate results back to the runner.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ***********************************************************************
2
 
// Copyright (c) 2009 Charlie Poole
3
 
//
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:
11
 
// 
12
 
// The above copyright notice and this permission notice shall be
13
 
// included in all copies or substantial portions of the Software.
14
 
// 
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
 
// ***********************************************************************
23
 
 
24
 
using System;
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;
32
 
 
33
 
namespace NUnit.Framework.Internal
34
 
{
35
 
        /// <summary>
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 
39
 
        /// NUnit Framework.
40
 
        /// </summary>
41
 
        public class NUnitFramework
42
 
        {
43
 
        #region Properties
44
 
        private static Assembly frameworkAssembly;
45
 
        private static bool frameworkAssemblyInitialized;
46
 
        private static Assembly FrameworkAssembly
47
 
        {
48
 
            get
49
 
            {
50
 
                if (!frameworkAssemblyInitialized)
51
 
                    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
52
 
                        if (assembly.GetName().Name == "nunit.framework" ||
53
 
                            assembly.GetName().Name == "NUnitLite")
54
 
                        {
55
 
                            frameworkAssembly = assembly;
56
 
                            break;
57
 
                        }
58
 
 
59
 
                frameworkAssemblyInitialized = true;
60
 
 
61
 
                return frameworkAssembly;
62
 
            }
63
 
        }
64
 
        #endregion
65
 
 
66
 
        #region Check SetUp and TearDown methods
67
 
        public static bool CheckSetUpTearDownMethods(Type fixtureType, Type attributeType, ref string reason)
68
 
        {
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)))
74
 
                {
75
 
                    reason = string.Format( "Invalid signature for SetUp or TearDown method: {0}", theMethod.Name );
76
 
                    return false;
77
 
                }
78
 
 
79
 
            return true;
80
 
        }
81
 
        #endregion
82
 
 
83
 
                #region ApplyCommonAttributes
84
 
        /// <summary>
85
 
        /// Modify a newly constructed test based on a type or method by 
86
 
        /// applying any of NUnit's common attributes.
87
 
        /// </summary>
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)
91
 
        {
92
 
            ApplyCommonAttributes( Reflect.GetAttributes( member, false ), test );
93
 
        }
94
 
 
95
 
        /// <summary>
96
 
        /// Modify a newly constructed test based on an assembly by applying 
97
 
        /// any of NUnit's common attributes.
98
 
        /// </summary>
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)
102
 
        {
103
 
            ApplyCommonAttributes( Reflect.GetAttributes( assembly, false ), test );
104
 
        }
105
 
 
106
 
        /// <summary>
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.
111
 
        /// </summary>
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)
115
 
        {
116
 
            foreach (Attribute attribute in attributes)
117
 
            {
118
 
                                Type attributeType = attribute.GetType();
119
 
                                string attributeName = attributeType.FullName;
120
 
                bool isValid = test.RunState != RunState.NotRunnable;
121
 
 
122
 
                if (attribute is TestFixtureAttribute)
123
 
                {
124
 
                    if (test.Description == null)
125
 
                        test.Description = ((TestFixtureAttribute)attribute).Description;
126
 
                }
127
 
                else if (attribute is TestAttribute)
128
 
                {
129
 
                    if (test.Description == null)
130
 
                        test.Description = ((TestAttribute)attribute).Description;
131
 
                }
132
 
                else if (attribute is ISetRunState)
133
 
                {
134
 
                    if (isValid)
135
 
                    {
136
 
                        ISetRunState irs = (ISetRunState)attribute;
137
 
                        test.RunState = irs.GetRunState();
138
 
                        test.IgnoreReason = irs.GetReason();
139
 
                    }
140
 
                }
141
 
                else if (attribute is CategoryAttribute)
142
 
                {
143
 
                    test.Categories.Add(((CategoryAttribute)attribute).Name);
144
 
                }
145
 
                else if (attribute is PropertyAttribute)
146
 
                {
147
 
                    IDictionary props = ((PropertyAttribute)attribute).Properties;
148
 
                    if (props != null)
149
 
                        foreach (DictionaryEntry entry in props)
150
 
                            test.Properties.Add(entry.Key, entry.Value);
151
 
                }
152
 
            }
153
 
        }
154
 
                #endregion
155
 
 
156
 
        #region ApplyExpectedExceptionAttribute
157
 
        /// <summary>
158
 
        /// Modify a newly constructed test by checking for ExpectedExceptionAttribute
159
 
        /// and setting properties on the test accordingly.
160
 
        /// </summary>
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)
164
 
        {
165
 
            ExpectedExceptionAttribute[] attributes = 
166
 
                (ExpectedExceptionAttribute[])method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false);
167
 
 
168
 
            if (attributes != null && attributes.Length > 0)
169
 
                testMethod.ExceptionProcessor = new ExpectedExceptionProcessor(testMethod, attributes[0]);
170
 
        }
171
 
 
172
 
        #endregion
173
 
 
174
 
        #region GetResultState
175
 
        /// <summary>
176
 
        /// Returns a result state for a special exception.
177
 
        /// If the exception is not handled specially, returns
178
 
        /// ResultState.Error.
179
 
        /// </summary>
180
 
        /// <param name="ex">The exception to be examined</param>
181
 
        /// <returns>A ResultState</returns>
182
 
        public static ResultState GetResultState(Exception ex)
183
 
        {
184
 
            if (ex is System.Threading.ThreadAbortException)
185
 
                return ResultState.Cancelled;
186
 
 
187
 
            if (ex is AssertionException)
188
 
                return ResultState.Failure;
189
 
 
190
 
            if (ex is IgnoreException)
191
 
                return ResultState.Ignored;
192
 
 
193
 
            if (ex is InconclusiveException)
194
 
                return ResultState.Inconclusive;
195
 
 
196
 
            if (ex is SuccessException)
197
 
                return ResultState.Success;
198
 
 
199
 
            return ResultState.Error;
200
 
        }
201
 
        #endregion
202
 
    }
203
 
}