ActionsHelper
class ActionsHelper
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace NUnit.Framework.Internal
{
internal class ActionsHelper
{
private enum ActionPhase
{
Before,
After
}
public static void ExecuteBeforeActions(IEnumerable<ITestAction> actions, ITest test)
{
ExecuteActions(ActionPhase.Before, actions, test);
}
public static void ExecuteAfterActions(IEnumerable<ITestAction> actions, ITest test)
{
ExecuteActions(ActionPhase.After, actions, test);
}
private static void ExecuteActions(ActionPhase phase, IEnumerable<ITestAction> actions, ITest test)
{
if (actions != null) {
ITestAction[] filteredAndSortedActions = GetFilteredAndSortedActions(actions, phase);
foreach (ITestAction testAction in filteredAndSortedActions) {
if (phase == ActionPhase.Before)
testAction.BeforeTest(test);
else
testAction.AfterTest(test);
}
}
}
public static ITestAction[] GetActionsFromAttributeProvider(ICustomAttributeProvider attributeProvider)
{
if (attributeProvider == null)
return new ITestAction[0];
List<ITestAction> list = new List<ITestAction>((ITestAction[])attributeProvider.GetCustomAttributes(typeof(ITestAction), false));
list.Sort(SortByTargetDescending);
return list.ToArray();
}
public static ITestAction[] GetActionsFromTypesAttributes(Type type)
{
if (type == (Type)null)
return new ITestAction[0];
if (type == typeof(object))
return new ITestAction[0];
List<ITestAction> list = new List<ITestAction>();
list.AddRange(GetActionsFromTypesAttributes(type.GetTypeInfo().BaseType));
Type[] declaredInterfaces = GetDeclaredInterfaces(type);
Type[] array = declaredInterfaces;
foreach (Type type2 in array) {
list.AddRange(GetActionsFromAttributeProvider(type2.GetTypeInfo()));
}
list.AddRange(GetActionsFromAttributeProvider(type.GetTypeInfo()));
return list.ToArray();
}
private static Type[] GetDeclaredInterfaces(Type type)
{
List<Type> list = new List<Type>(type.GetInterfaces());
if (type.GetTypeInfo().BaseType == typeof(object))
return list.ToArray();
List<Type> list2 = new List<Type>(type.GetTypeInfo().BaseType.GetInterfaces());
List<Type> list3 = new List<Type>();
foreach (Type item in list) {
if (!list2.Contains(item))
list3.Add(item);
}
return list3.ToArray();
}
private static ITestAction[] GetFilteredAndSortedActions(IEnumerable<ITestAction> actions, ActionPhase phase)
{
List<ITestAction> list = new List<ITestAction>();
foreach (ITestAction action in actions) {
if (!list.Contains(action))
list.Add(action);
}
if (phase == ActionPhase.After)
list.Reverse();
return list.ToArray();
}
private static int SortByTargetDescending(ITestAction x, ITestAction y)
{
return y.Targets.CompareTo(x.Targets);
}
}
}