ActionsHelper
class ActionsHelper
using NUnit.Compatibility;
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
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(Assembly attributeProvider)
{
if ((object)attributeProvider == null)
return new ITestAction[0];
List<ITestAction> list = attributeProvider.GetAttributes<ITestAction>().ToList();
list.Sort(SortByTargetDescending);
return list.ToArray();
}
public static ITestAction[] GetActionsFromAttributeProvider(MemberInfo attributeProvider)
{
if ((object)attributeProvider == null)
return new ITestAction[0];
List<ITestAction> list = attributeProvider.GetAttributes<ITestAction>(false).ToList();
list.Sort(SortByTargetDescending);
return list.ToArray();
}
public static ITestAction[] GetActionsFromTypesAttributes(Type type)
{
if ((object)type == null)
return new ITestAction[0];
if ((object)type == typeof(object))
return new ITestAction[0];
List<ITestAction> list = new List<ITestAction>();
list.AddRange(GetActionsFromTypesAttributes(type.GetTypeInfo().get_BaseType()));
Type[] declaredInterfaces = GetDeclaredInterfaces(type);
foreach (Type type2 in declaredInterfaces) {
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>(TypeExtensions.GetInterfaces(type));
if ((object)type.GetTypeInfo().get_BaseType() == typeof(object))
return list.ToArray();
List<Type> list2 = new List<Type>(TypeExtensions.GetInterfaces(type.GetTypeInfo().get_BaseType()));
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);
}
}
}