1 // ***********************************************************************
2 // Copyright (c) 2012 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Reflection;
33 using NUnit.Compatibility;
39 namespace NUnit.Framework.Internal
43 internal class ActionsHelper
45 public static void ExecuteBeforeActions(IEnumerable<ITestAction> actions, ITest test)
47 ExecuteActions(ActionPhase.Before, actions, test);
50 public static void ExecuteAfterActions(IEnumerable<ITestAction> actions, ITest test)
52 ExecuteActions(ActionPhase.After, actions, test);
55 private static void ExecuteActions(ActionPhase phase, IEnumerable<ITestAction> actions, ITest test)
60 foreach (ITestAction action in GetFilteredAndSortedActions(actions, phase))
62 if(phase == ActionPhase.Before)
63 action.BeforeTest(test);
65 action.AfterTest(test);
70 //public static ITestAction[] GetActionsFromAttributeProvider(Assembly attributeProvider)
72 // if (attributeProvider == null)
73 // return new ITestAction[0];
75 // var actions = attributeProvider.GetAttributes<ITestAction>().ToList();
76 // actions.Sort(SortByTargetDescending);
78 // return actions.ToArray();
81 //public static ITestAction[] GetActionsFromAttributeProvider(MemberInfo attributeProvider)
83 // if (attributeProvider == null)
84 // return new ITestAction[0];
86 // var actions = attributeProvider.GetAttributes<ITestAction>(false).ToList();
87 // actions.Sort(SortByTargetDescending);
89 // return actions.ToArray();
92 public static ITestAction[] GetActionsFromAttributeProvider(ICustomAttributeProvider attributeProvider)
94 if (attributeProvider == null)
95 return new ITestAction[0];
97 var actions = new List<ITestAction>((ITestAction[])attributeProvider.GetCustomAttributes(typeof(ITestAction), false));
98 actions.Sort(SortByTargetDescending);
100 return actions.ToArray();
104 //public static ITestAction[] GetActionsFromTypesAttributes(Type type)
107 // return new ITestAction[0];
109 // if(type == typeof(object))
110 // return new ITestAction[0];
112 // var actions = new List<ITestAction>();
114 // actions.AddRange(GetActionsFromTypesAttributes(type.GetTypeInfo().BaseType));
116 // Type[] declaredInterfaces = GetDeclaredInterfaces(type);
118 // foreach(Type interfaceType in declaredInterfaces)
119 // actions.AddRange(GetActionsFromAttributeProvider(interfaceType.GetTypeInfo()));
121 // actions.AddRange(GetActionsFromAttributeProvider(type.GetTypeInfo()));
123 // return actions.ToArray();
126 private static Type[] GetDeclaredInterfaces(Type type)
128 List<Type> interfaces = new List<Type>(type.GetInterfaces());
130 if (type.GetTypeInfo().BaseType == typeof(object))
131 return interfaces.ToArray();
133 List<Type> baseInterfaces = new List<Type>(type.GetTypeInfo().BaseType.GetInterfaces());
134 List<Type> declaredInterfaces = new List<Type>();
136 foreach (Type interfaceType in interfaces)
138 if (!baseInterfaces.Contains(interfaceType))
139 declaredInterfaces.Add(interfaceType);
142 return declaredInterfaces.ToArray();
145 private static ITestAction[] GetFilteredAndSortedActions(IEnumerable<ITestAction> actions, ActionPhase phase)
147 var filteredActions = new List<ITestAction>();
148 foreach (var actionItem in actions)
150 if (filteredActions.Contains(actionItem) != true)
151 filteredActions.Add(actionItem);
154 if(phase == ActionPhase.After)
155 filteredActions.Reverse();
157 return filteredActions.ToArray();
160 private static int SortByTargetDescending(ITestAction x, ITestAction y)
162 return y.Targets.CompareTo(x.Targets);
165 private enum ActionPhase