~nunit-core/nunitv2/2.5

« back to all changes in this revision

Viewing changes to src/NUnitFramework/core/TestSuiteBuilder.cs

  • Committer: jnewkirk
  • Date: 2002-07-10 20:00:13 UTC
  • Revision ID: vcs-imports@canonical.com-20020710200013-j8us5mbi0usp7p02
initialĀ load

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Copyright (C) 2002. James W. Newkirk, Michael C. Two, Alexei A. Vorontsov. All Rights Reserved.
 
3
//
 
4
namespace Nunit.Core
 
5
{
 
6
        using System;
 
7
        using System.IO;
 
8
        using System.Reflection;
 
9
        using System.Collections;
 
10
 
 
11
        /// <summary>
 
12
        /// Summary description for TestSuiteBuilder.
 
13
        /// </summary>
 
14
        public class TestSuiteBuilder
 
15
        {
 
16
                Hashtable suites  = new Hashtable();
 
17
                TestSuite rootSuite;
 
18
 
 
19
                public static Assembly Load(string assemblyName)
 
20
                {
 
21
                        Assembly assembly = Assembly.LoadFrom(assemblyName);
 
22
                        return assembly;
 
23
                }
 
24
 
 
25
                private TestSuite BuildFromNameSpace(string nameSpace)
 
26
                {
 
27
                        if("".Equals(nameSpace)) return rootSuite;
 
28
                        TestSuite suite = (TestSuite)suites[nameSpace];
 
29
                        if(suite!=null) return suite;
 
30
                        int index = nameSpace.LastIndexOf(".");
 
31
                        if(index==-1)
 
32
                        {
 
33
                                suite = new TestSuite(nameSpace);
 
34
                                rootSuite.Add(suite);
 
35
                                suites[nameSpace]=suite;
 
36
                                return suite;
 
37
                        }
 
38
                        string parentNameSpace=nameSpace.Substring( 0,index);
 
39
                        TestSuite parent = BuildFromNameSpace(parentNameSpace);
 
40
                        string suiteName = nameSpace.Substring(index+1);
 
41
                        suite = new TestSuite(suiteName);
 
42
                        suites[nameSpace]=suite;
 
43
                        parent.Add(suite);
 
44
                        return suite;
 
45
                }
 
46
 
 
47
                public static TestSuite Build(string assemblyName)
 
48
                {
 
49
                        TestSuiteBuilder builder = new TestSuiteBuilder();
 
50
 
 
51
                        Assembly assembly = Load(assemblyName);
 
52
 
 
53
                        builder.rootSuite = new TestSuite(assemblyName);
 
54
                        int testFixtureCount = 0;
 
55
                        Type[] testTypes = assembly.GetExportedTypes();
 
56
                        foreach(Type testType in testTypes)
 
57
                        {
 
58
                                if(IsTestFixture(testType))
 
59
                                {
 
60
                                        testFixtureCount++;
 
61
                                        string namespaces = testType.Namespace;
 
62
                                        TestSuite suite = builder.BuildFromNameSpace(namespaces);
 
63
 
 
64
                                        try
 
65
                                        {
 
66
                                                object fixture = BuildTestFixture(testType);
 
67
                                                suite.Add(fixture);
 
68
                                        }
 
69
                                        catch(InvalidTestFixtureException exception)
 
70
                                        {
 
71
                                                InvalidFixture fixture = new InvalidFixture(testType, exception.Message);
 
72
                                                suite.Add(fixture);
 
73
                                        }
 
74
                                }
 
75
                        }
 
76
 
 
77
                        if(testFixtureCount == 0)
 
78
                                throw new NoTestFixturesException(assemblyName + " has no TestFixtures");
 
79
 
 
80
                        return builder.rootSuite;
 
81
                }
 
82
 
 
83
 
 
84
                public static TestSuite Build(string testName, string assemblyName)
 
85
                {
 
86
                        TestSuite suite = null;
 
87
 
 
88
                        Assembly assembly = Load(assemblyName);
 
89
 
 
90
                        if(assembly != null)
 
91
                        {
 
92
                                Type testType = assembly.GetType(testName);
 
93
                                if(testType != null)
 
94
                                {
 
95
                                        if(IsTestFixture(testType))
 
96
                                        {
 
97
                                                suite = MakeSuiteFromTestFixtureType(testType);
 
98
                                        }
 
99
                                        else if(IsTestSuiteProperty(testType))
 
100
                                        {
 
101
                                                suite = MakeSuiteFromProperty(testType);
 
102
                                        }
 
103
                                }
 
104
                        }
 
105
                        return suite;
 
106
                }
 
107
 
 
108
                private static bool IsTestFixture(Type type)
 
109
                {
 
110
                        if(type.IsAbstract) return false;
 
111
 
 
112
                        return type.IsDefined(typeof(Nunit.Framework.TestFixtureAttribute), true);
 
113
                }
 
114
 
 
115
                public static object BuildTestFixture(Type fixtureType)
 
116
                {
 
117
                        ConstructorInfo ctor = fixtureType.GetConstructor(Type.EmptyTypes);
 
118
                        if(ctor == null) throw new InvalidTestFixtureException(fixtureType.FullName + " does not have a valid constructor");
 
119
 
 
120
                        object testFixture = ctor.Invoke(Type.EmptyTypes);
 
121
                        if(testFixture == null) throw new InvalidTestFixtureException(ctor.Name + " cannot be invoked");
 
122
 
 
123
                        if(HasMultipleSetUpMethods(testFixture))
 
124
                        {
 
125
                                throw new InvalidTestFixtureException(ctor.Name + " has multiple SetUp methods");
 
126
                        }
 
127
                        if(HasMultipleTearDownMethods(testFixture))
 
128
                        {
 
129
                                throw new InvalidTestFixtureException(ctor.Name + " has multiple TearDown methods");
 
130
                        }
 
131
                        return testFixture;
 
132
                }
 
133
 
 
134
                private static int CountMethodWithGivenAttribute(object fixture, Type type)
 
135
                {
 
136
                        int count = 0;
 
137
                        foreach(MethodInfo method in fixture.GetType().GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.DeclaredOnly))
 
138
                        {
 
139
                                if(method.IsDefined(type,false)) 
 
140
                                        count++;
 
141
                        }
 
142
                        return count;
 
143
 
 
144
                }
 
145
                private static bool HasMultipleSetUpMethods(object fixture)
 
146
                {
 
147
                        return CountMethodWithGivenAttribute(fixture,typeof(Nunit.Framework.SetUpAttribute)) > 1;
 
148
                }
 
149
 
 
150
                private static bool HasMultipleTearDownMethods(object fixture)
 
151
                {
 
152
                        return CountMethodWithGivenAttribute(fixture,typeof(Nunit.Framework.TearDownAttribute)) > 1;
 
153
                }
 
154
 
 
155
                public static TestSuite MakeSuiteFromTestFixtureType(Type fixtureType)
 
156
                {
 
157
                        TestSuite suite = new TestSuite(fixtureType.Name);
 
158
                        try
 
159
                        {
 
160
                                object testFixture = BuildTestFixture(fixtureType);
 
161
                                suite.Add(testFixture);
 
162
                        }
 
163
                        catch(InvalidTestFixtureException exception)
 
164
                        {
 
165
                                InvalidFixture fixture = new InvalidFixture(fixtureType,exception.Message);
 
166
                                suite.ShouldRun = false;
 
167
                                suite.IgnoreReason = exception.Message;
 
168
                                suite.Add(fixture);
 
169
                        }
 
170
 
 
171
                        return suite;
 
172
                }
 
173
 
 
174
                private static bool IsTestSuiteProperty(Type testClass)
 
175
                {
 
176
                        return (GetSuiteProperty(testClass) != null);
 
177
                }
 
178
 
 
179
                /// <summary>
 
180
                /// Uses reflection to obtain the suite property for the Type
 
181
                /// </summary>
 
182
                /// <param name="testClass"></param>
 
183
                /// <returns>The Suite property of the Type, or null if the property 
 
184
                /// does not exist</returns>
 
185
                private static TestSuite MakeSuiteFromProperty(Type testClass) 
 
186
                {
 
187
                        TestSuite suite = null;
 
188
                        PropertyInfo suiteProperty = null;
 
189
                        try
 
190
                        {
 
191
                                suiteProperty=GetSuiteProperty(testClass);
 
192
                                suite = (TestSuite)suiteProperty.GetValue(null, new Object[0]);
 
193
                        }
 
194
                        catch(InvalidSuiteException)
 
195
                        {
 
196
                                return null;
 
197
                        }
 
198
                        return suite;
 
199
                }
 
200
 
 
201
                private static PropertyInfo GetSuiteProperty(Type testClass)
 
202
                {
 
203
                        if(testClass != null)
 
204
                        {
 
205
                                PropertyInfo[] properties = testClass.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly);
 
206
                                foreach(PropertyInfo property in properties)
 
207
                                {
 
208
                                        object[] attrributes = property.GetCustomAttributes(typeof(Nunit.Framework.SuiteAttribute),false);
 
209
                                        if(attrributes.Length>0)
 
210
                                        {
 
211
                                                try {
 
212
                                                        CheckSuiteProperty(property);
 
213
                                                }catch(InvalidSuiteException){
 
214
                                                        return null;
 
215
                                                }
 
216
                                                return property;
 
217
                                        }
 
218
                                }
 
219
                        }
 
220
                        return null;
 
221
                }
 
222
 
 
223
                private static void CheckSuiteProperty(PropertyInfo property)
 
224
                {
 
225
                        MethodInfo method = property.GetGetMethod(true);
 
226
                        if(method.ReturnType!=typeof(Nunit.Core.TestSuite))
 
227
                                throw new InvalidSuiteException("Invalid suite property method signature");
 
228
                        if(method.GetParameters().Length>0)
 
229
                                throw new InvalidSuiteException("Invalid suite property method signature");
 
230
                }
 
231
        }
 
232
}