PageRenderTime 28ms CodeModel.GetById 20ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/Builders/SetUpFixtureBuilder.cs

#
C# | 70 lines | 51 code | 11 blank | 8 comment | 7 complexity | 2aeae0a0d4176d07c6d58c7b3cfa43d5 MD5 | raw file
 1// ****************************************************************
 2// This is free software licensed under the NUnit license. You
 3// may obtain a copy of the license as well as information regarding
 4// copyright ownership at http://nunit.org.
 5// ****************************************************************
 6
 7using System;
 8
 9namespace NUnit.Core.Builders
10{
11	/// <summary>
12	/// SetUpFixtureBuilder knows how to build a SetUpFixture.
13	/// </summary>
14	public class SetUpFixtureBuilder : Extensibility.ISuiteBuilder
15	{	
16		#region ISuiteBuilder Members
17		public Test BuildFrom(Type type)
18		{
19			SetUpFixture fixture = new SetUpFixture( type );
20
21            string reason = null;
22            if (!IsValidFixtureType(type, ref reason))
23            {
24                fixture.RunState = RunState.NotRunnable;
25                fixture.IgnoreReason = reason;
26            }
27
28            return fixture;
29		}
30
31		public bool CanBuildFrom(Type type)
32		{
33			return Reflect.HasAttribute( type, NUnitFramework.SetUpFixtureAttribute, false );
34		}
35		#endregion
36
37        private bool IsValidFixtureType(Type type, ref string reason)
38        {
39            if (type.IsAbstract)
40            {
41                reason = string.Format("{0} is an abstract class", type.FullName);
42                return false;
43            }
44
45            if (Reflect.GetConstructor(type) == null)
46            {
47                reason = string.Format("{0} does not have a valid constructor", type.FullName);
48                return false;
49            }
50
51            if (!NUnitFramework.CheckSetUpTearDownMethods(type, NUnitFramework.SetUpAttribute, ref reason) ||
52                !NUnitFramework.CheckSetUpTearDownMethods(type, NUnitFramework.TearDownAttribute, ref reason) )
53                    return false;
54
55            if ( Reflect.HasMethodWithAttribute(type, NUnitFramework.FixtureSetUpAttribute, true) )
56            {
57                reason = "TestFixtureSetUp method not allowed on a SetUpFixture";
58                return false;
59            }
60
61            if ( Reflect.HasMethodWithAttribute(type, NUnitFramework.FixtureTearDownAttribute, true) )
62            {
63                reason = "TestFixtureTearDown method not allowed on a SetUpFixture";
64                return false;
65            }
66
67            return true;
68        }
69	}
70}