~charlie.poole/nunit-3.0/net-3.5-support

« back to all changes in this revision

Viewing changes to src/framework/NUnitLite/Runner/TestFixtureBuilder.cs

  • Committer: Charlie Poole
  • Date: 2009-09-18 00:52:27 UTC
  • mfrom: (17.1.33 work)
  • Revision ID: charlie@nunit.com-20090918005227-cmvzhzqn4q1dq40o
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections;
 
3
using System.Reflection;
 
4
using NUnit.Framework;
 
5
 
 
6
namespace NUnitLite.Runner
 
7
{
 
8
    /// <summary>
 
9
    /// Static class used to create test fixtures from Types
 
10
    /// </summary>
 
11
    public class TestFixtureBuilder
 
12
    {
 
13
        /// <summary>
 
14
        /// Determines whether this instance can build a fixture from the specified type.
 
15
        /// </summary>
 
16
        /// <param name="type">The type to use as a fixture</param>
 
17
        /// <returns>
 
18
        ///     <c>true</c> if this instance can build from the specified type; otherwise, <c>false</c>.
 
19
        /// </returns>
 
20
        public static bool CanBuildFrom(Type type)
 
21
        {
 
22
            if (Reflect.HasAttribute(type, typeof(TestFixtureAttribute)))
 
23
                return true;
 
24
 
 
25
            if (!type.IsPublic && !type.IsNestedPublic)
 
26
                return false;
 
27
 
 
28
            if (type.IsAbstract && !type.IsSealed)
 
29
                return false;
 
30
 
 
31
            foreach (MethodInfo method in type.GetMethods())
 
32
            {
 
33
                if (Reflect.HasAttribute(method, typeof(TestAttribute)) ||
 
34
                    Reflect.HasAttribute(method, typeof(TestCaseAttribute)))
 
35
                        return true;
 
36
            }
 
37
 
 
38
            return false;
 
39
        }
 
40
 
 
41
        /// <summary>
 
42
        /// Builds a fixture from the specified Type.
 
43
        /// </summary>
 
44
        /// <param name="type">The type to use as a fixture.</param>
 
45
        /// <returns></returns>
 
46
        public static TestSuite BuildFrom(Type type)
 
47
        {
 
48
            TestSuite suite = new TestSuite(type);
 
49
 
 
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;
 
54
 
 
55
            IgnoreAttribute ignore = (IgnoreAttribute)Reflect.GetAttribute(type, typeof(IgnoreAttribute));
 
56
            if (ignore != null)
 
57
            {
 
58
                suite.RunState = RunState.Ignored;
 
59
                suite.IgnoreReason = ignore.Reason;
 
60
            }
 
61
 
 
62
            if (!Reflect.HasConstructor(type))
 
63
            {
 
64
                suite.RunState = RunState.NotRunnable;
 
65
                suite.IgnoreReason = string.Format("Class {0} has no default constructor", type.Name);
 
66
                return suite;
 
67
            }
 
68
 
 
69
            foreach (MethodInfo method in type.GetMethods())
 
70
            {
 
71
                if (TestCaseBuilder.IsTestMethod(method))
 
72
                    suite.AddTest(TestCaseBuilder.BuildFrom(method));
 
73
            }
 
74
 
 
75
            return suite;
 
76
        }
 
77
    }
 
78
}