2
using System.Collections;
3
using System.Reflection;
6
namespace NUnitLite.Runner
9
/// Static class used to create test fixtures from Types
11
public class TestFixtureBuilder
14
/// Determines whether this instance can build a fixture from the specified type.
16
/// <param name="type">The type to use as a fixture</param>
18
/// <c>true</c> if this instance can build from the specified type; otherwise, <c>false</c>.
20
public static bool CanBuildFrom(Type type)
22
if (Reflect.HasAttribute(type, typeof(TestFixtureAttribute)))
25
if (!type.IsPublic && !type.IsNestedPublic)
28
if (type.IsAbstract && !type.IsSealed)
31
foreach (MethodInfo method in type.GetMethods())
33
if (Reflect.HasAttribute(method, typeof(TestAttribute)) ||
34
Reflect.HasAttribute(method, typeof(TestCaseAttribute)))
42
/// Builds a fixture from the specified Type.
44
/// <param name="type">The type to use as a fixture.</param>
45
/// <returns></returns>
46
public static TestSuite BuildFrom(Type type)
48
TestSuite suite = new TestSuite(type);
50
object[] attrs = type.GetCustomAttributes( typeof(PropertyAttribute), true);
51
foreach (PropertyAttribute attr in attrs)
52
foreach( DictionaryEntry entry in attr.Properties )
53
suite.Properties[entry.Key] = entry.Value;
55
IgnoreAttribute ignore = (IgnoreAttribute)Reflect.GetAttribute(type, typeof(IgnoreAttribute));
58
suite.RunState = RunState.Ignored;
59
suite.IgnoreReason = ignore.Reason;
62
if (!Reflect.HasConstructor(type))
64
suite.RunState = RunState.NotRunnable;
65
suite.IgnoreReason = string.Format("Class {0} has no default constructor", type.Name);
69
foreach (MethodInfo method in type.GetMethods())
71
if (TestCaseBuilder.IsTestMethod(method))
72
suite.AddTest(TestCaseBuilder.BuildFrom(method));