CommandBuilder
A utility class to create TestCommands
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Commands;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace NUnit.Framework.Internal.Execution
{
public static class CommandBuilder
{
public static TestCommand MakeOneTimeSetUpCommand(TestSuite suite, List<SetUpTearDownItem> setUpTearDown, List<TestActionItem> actions)
{
if (suite.RunState != RunState.Runnable && suite.RunState != RunState.Explicit)
return new SkipCommand(suite);
TestCommand testCommand = new OneTimeSetUpCommand(suite, setUpTearDown, actions);
if (suite.FixtureType != (Type)null) {
IApplyToContext[] array = (IApplyToContext[])suite.FixtureType.GetCustomAttributes(typeof(IApplyToContext), true);
if (array.Length != 0)
testCommand = new ApplyChangesToContextCommand(testCommand, array);
}
return testCommand;
}
public static TestCommand MakeOneTimeTearDownCommand(TestSuite suite, List<SetUpTearDownItem> setUpTearDownItems, List<TestActionItem> actions)
{
TestCommand testCommand = new OneTimeTearDownCommand(suite, setUpTearDownItems, actions);
if (suite.TestType == "Theory")
testCommand = new TheoryResultCommand(testCommand);
return testCommand;
}
public static TestCommand MakeTestCommand(TestMethod test)
{
if (test.RunState != RunState.Runnable && test.RunState != RunState.Explicit)
return new SkipCommand(test);
TestCommand testCommand = new TestMethodCommand(test);
object[] customAttributes = test.Method.GetCustomAttributes(typeof(IWrapTestMethod), true);
for (int i = 0; i < customAttributes.Length; i++) {
testCommand = ((IWrapTestMethod)customAttributes[i]).Wrap(testCommand);
}
testCommand = new TestActionCommand(testCommand);
testCommand = new SetUpTearDownCommand(testCommand);
customAttributes = test.Method.GetCustomAttributes(typeof(IWrapSetUpTearDown), true);
for (int i = 0; i < customAttributes.Length; i++) {
testCommand = ((ICommandWrapper)customAttributes[i]).Wrap(testCommand);
}
IApplyToContext[] array = (IApplyToContext[])test.Method.GetCustomAttributes(typeof(IApplyToContext), true);
if (array.Length != 0)
testCommand = new ApplyChangesToContextCommand(testCommand, array);
return testCommand;
}
public static List<SetUpTearDownItem> BuildSetUpTearDownList(Type fixtureType, Type setUpType, Type tearDownType)
{
MethodInfo[] methodsWithAttribute = Reflect.GetMethodsWithAttribute(fixtureType, setUpType, true);
MethodInfo[] methodsWithAttribute2 = Reflect.GetMethodsWithAttribute(fixtureType, tearDownType, true);
List<SetUpTearDownItem> list = new List<SetUpTearDownItem>();
while (fixtureType != (Type)null && fixtureType != typeof(object)) {
SetUpTearDownItem setUpTearDownItem = BuildNode(fixtureType, methodsWithAttribute, methodsWithAttribute2);
if (setUpTearDownItem.HasMethods)
list.Add(setUpTearDownItem);
fixtureType = fixtureType.BaseType;
}
return list;
}
private static SetUpTearDownItem BuildNode(Type fixtureType, IList<MethodInfo> setUpMethods, IList<MethodInfo> tearDownMethods)
{
List<MethodInfo> setUpMethods2 = SelectMethodsByDeclaringType(fixtureType, setUpMethods);
List<MethodInfo> tearDownMethods2 = SelectMethodsByDeclaringType(fixtureType, tearDownMethods);
return new SetUpTearDownItem(setUpMethods2, tearDownMethods2);
}
private static List<MethodInfo> SelectMethodsByDeclaringType(Type type, IList<MethodInfo> methods)
{
List<MethodInfo> list = new List<MethodInfo>();
foreach (MethodInfo method in methods) {
if (method.DeclaringType == type)
list.Add(method);
}
return list;
}
}
}