NUnitTestFixtureBuilder
NUnitTestFixtureBuilder is able to build a fixture given
a class marked with a TestFixtureAttribute or an unmarked
class containing test methods. In the first case, it is
called by the attribute and in the second directly by
NUnitSuiteBuilder.
using NUnit.Framework.Interfaces;
using System;
using System.Collections;
using System.Reflection;
namespace NUnit.Framework.Internal.Builders
{
public class NUnitTestFixtureBuilder : IFixtureBuilder
{
private static readonly string NO_TYPE_ARGS_MSG = "Fixture type contains generic parameters. You must either provide Type arguments or specify constructor arguments that allow NUnit to deduce the Type arguments.";
private TestFixture fixture;
private ITestCaseBuilder testBuilder = new DefaultTestCaseBuilder();
public TestSuite BuildFrom(Type type)
{
fixture = new TestFixture(type);
if (fixture.RunState != 0)
CheckTestFixtureIsValid(fixture);
fixture.ApplyAttributesToTest(type);
AddTestCases(type);
return fixture;
}
public TestSuite BuildFrom(Type type, TestFixtureAttribute attr)
{
object[] array = null;
if (attr != null) {
array = attr.Arguments;
if (type.ContainsGenericParameters) {
Type[] typeArgsOut = attr.TypeArgs;
if (typeArgsOut.Length == 0) {
int num = 0;
object[] array2 = array;
foreach (object obj in array2) {
if (!(obj is Type))
break;
num++;
}
typeArgsOut = new Type[num];
for (int j = 0; j < num; j++) {
typeArgsOut[j] = (Type)array[j];
}
if (num > 0) {
object[] array3 = new object[array.Length - num];
for (int k = 0; k < array3.Length; k++) {
array3[k] = array[num + k];
}
array = array3;
}
}
if (typeArgsOut.Length > 0 || TypeHelper.CanDeduceTypeArgsFromArgs(type, array, ref typeArgsOut))
type = TypeHelper.MakeGenericType(type, typeArgsOut);
}
}
fixture = new TestFixture(type);
if (array != null) {
string text = fixture.Name = TypeHelper.GetDisplayName(type, array);
string text2 = text;
string namespace = type.Namespace;
fixture.FullName = ((namespace != null && namespace != "") ? (namespace + "." + text2) : text2);
fixture.Arguments = array;
}
if (fixture.RunState != 0)
CheckTestFixtureIsValid(fixture);
fixture.ApplyAttributesToTest(type);
AddTestCases(type);
return fixture;
}
private void AddTestCases(Type fixtureType)
{
IList methods = fixtureType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
foreach (MethodInfo item in methods) {
Test test = BuildTestCase(item, fixture);
if (test != null)
fixture.Add(test);
}
}
private Test BuildTestCase(MethodInfo method, TestSuite suite)
{
if (!testBuilder.CanBuildFrom(method, suite))
return null;
return testBuilder.BuildFrom(method, suite);
}
private void CheckTestFixtureIsValid(TestFixture fixture)
{
Type fixtureType = fixture.FixtureType;
if (fixtureType.ContainsGenericParameters) {
fixture.RunState = RunState.NotRunnable;
fixture.Properties.Set("_SKIPREASON", NO_TYPE_ARGS_MSG);
} else if (!IsStaticClass(fixtureType)) {
object[] arguments = fixture.Arguments;
Type[] array;
if (arguments == null)
array = new Type[0];
else {
array = new Type[arguments.Length];
int num = 0;
object[] array2 = arguments;
foreach (object obj in array2) {
array[num++] = obj.GetType();
}
}
ConstructorInfo constructor = fixtureType.GetConstructor(array);
if (constructor == (ConstructorInfo)null) {
fixture.RunState = RunState.NotRunnable;
fixture.Properties.Set("_SKIPREASON", "No suitable constructor was found");
}
}
}
private static bool IsStaticClass(Type type)
{
if (type.IsAbstract)
return type.IsSealed;
return false;
}
}
}