No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeforeAfterTestCommandBase.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using NUnit.Framework.Interfaces;
  7. using NUnit.Framework.Internal;
  8. using NUnit.Framework.Internal.Commands;
  9. using UnityEngine.TestRunner.NUnitExtensions;
  10. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  11. using UnityEngine.TestTools.Logging;
  12. using UnityEngine.TestTools.TestRunner;
  13. namespace UnityEngine.TestTools
  14. {
  15. internal abstract class BeforeAfterTestCommandBase<T> : DelegatingTestCommand, IEnumerableTestMethodCommand where T : class
  16. {
  17. private string m_BeforeErrorPrefix;
  18. private string m_AfterErrorPrefix;
  19. protected BeforeAfterTestCommandBase(TestCommand innerCommand, string beforeErrorPrefix, string afterErrorPrefix)
  20. : base(innerCommand)
  21. {
  22. m_BeforeErrorPrefix = beforeErrorPrefix;
  23. m_AfterErrorPrefix = afterErrorPrefix;
  24. }
  25. protected T[] BeforeActions = new T[0];
  26. protected T[] AfterActions = new T[0];
  27. protected static MethodInfo[] GetActions(IDictionary<Type, List<MethodInfo>> cacheStorage, Type fixtureType, Type attributeType, Type[] returnTypes)
  28. {
  29. if (cacheStorage.TryGetValue(fixtureType, out var result))
  30. {
  31. return result.ToArray();
  32. }
  33. cacheStorage[fixtureType] = GetMethodsWithAttributeFromFixture(fixtureType, attributeType, returnTypes);
  34. return cacheStorage[fixtureType].ToArray();
  35. }
  36. protected static T[] GetTestActions(IDictionary<MethodInfo, List<T>> cacheStorage, MethodInfo methodInfo)
  37. {
  38. if (cacheStorage.TryGetValue(methodInfo, out var result))
  39. {
  40. return result.ToArray();
  41. }
  42. var attributesForMethodInfo = new List<T>();
  43. var attributes = methodInfo.GetCustomAttributes(false);
  44. foreach (var attribute in attributes)
  45. {
  46. if (attribute is T attribute1)
  47. {
  48. attributesForMethodInfo.Add(attribute1);
  49. }
  50. }
  51. cacheStorage[methodInfo] = attributesForMethodInfo;
  52. return cacheStorage[methodInfo].ToArray();
  53. }
  54. private static List<MethodInfo> GetMethodsWithAttributeFromFixture(Type fixtureType, Type setUpType, Type[] returnTypes)
  55. {
  56. MethodInfo[] methodsWithAttribute = Reflect.GetMethodsWithAttribute(fixtureType, setUpType, true);
  57. var methodsInfo = new List<MethodInfo>();
  58. methodsInfo.AddRange(methodsWithAttribute.Where(method => returnTypes.Any(type => type == method.ReturnType)));
  59. return methodsInfo;
  60. }
  61. protected abstract IEnumerator InvokeBefore(T action, Test test, UnityTestExecutionContext context);
  62. protected abstract IEnumerator InvokeAfter(T action, Test test, UnityTestExecutionContext context);
  63. protected virtual bool MoveBeforeEnumerator(IEnumerator enumerator, Test test)
  64. {
  65. return enumerator.MoveNext();
  66. }
  67. protected virtual bool MoveAfterEnumerator(IEnumerator enumerator, Test test)
  68. {
  69. return enumerator.MoveNext();
  70. }
  71. protected abstract BeforeAfterTestCommandState GetState(UnityTestExecutionContext context);
  72. protected virtual bool AllowFrameSkipAfterAction(T action)
  73. {
  74. return true;
  75. }
  76. public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
  77. {
  78. var unityContext = (UnityTestExecutionContext)context;
  79. var state = GetState(unityContext);
  80. if (state == null)
  81. {
  82. throw new Exception($"No state in context for {GetType().Name}.");
  83. }
  84. if(state.ShouldRestore)
  85. {
  86. state.ApplyContext(unityContext);
  87. }
  88. while (state.NextBeforeStepIndex < BeforeActions.Length)
  89. {
  90. var action = BeforeActions[state.NextBeforeStepIndex];
  91. IEnumerator enumerator;
  92. try
  93. {
  94. enumerator = InvokeBefore(action, Test, unityContext);
  95. }
  96. catch (Exception ex)
  97. {
  98. state.TestHasRun = true;
  99. context.CurrentResult.RecordPrefixedException(m_BeforeErrorPrefix, ex);
  100. break;
  101. }
  102. ActivePcHelper.SetEnumeratorPC(enumerator, state.NextBeforeStepPc);
  103. using (var logScope = new LogScope())
  104. {
  105. while (true)
  106. {
  107. try
  108. {
  109. if (!enumerator.MoveNext())
  110. {
  111. logScope.EvaluateLogScope(true);
  112. break;
  113. }
  114. if (!AllowFrameSkipAfterAction(action)) // Evaluate the log scope right away for the commands where we do not yield
  115. {
  116. logScope.EvaluateLogScope(true);
  117. }
  118. if (unityContext.TestMode == TestPlatform.PlayMode && enumerator.Current is IEditModeTestYieldInstruction)
  119. {
  120. throw new Exception($"PlayMode test are not allowed to yield {enumerator.Current.GetType().Name}");
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. state.TestHasRun = true;
  126. context.CurrentResult.RecordPrefixedException(m_BeforeErrorPrefix, ex);
  127. state.StoreContext(unityContext);
  128. break;
  129. }
  130. state.NextBeforeStepPc = ActivePcHelper.GetEnumeratorPC(enumerator);
  131. state.StoreContext(unityContext);
  132. if (!AllowFrameSkipAfterAction(action))
  133. {
  134. break;
  135. }
  136. yield return enumerator.Current;
  137. }
  138. }
  139. state.NextBeforeStepIndex++;
  140. state.NextBeforeStepPc = 0;
  141. }
  142. if (!state.TestHasRun)
  143. {
  144. if (innerCommand is IEnumerableTestMethodCommand)
  145. {
  146. var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
  147. foreach (var iterator in executeEnumerable)
  148. {
  149. state.StoreContext(unityContext);
  150. yield return iterator;
  151. }
  152. }
  153. else
  154. {
  155. context.CurrentResult = innerCommand.Execute(context);
  156. state.StoreContext(unityContext);
  157. }
  158. state.TestHasRun = true;
  159. }
  160. while (state.NextAfterStepIndex < AfterActions.Length)
  161. {
  162. state.TestAfterStarted = true;
  163. var action = AfterActions[state.NextAfterStepIndex];
  164. IEnumerator enumerator;
  165. try
  166. {
  167. enumerator = InvokeAfter(action, Test, unityContext);
  168. }
  169. catch (Exception ex)
  170. {
  171. context.CurrentResult.RecordPrefixedException(m_AfterErrorPrefix, ex);
  172. state.StoreContext(unityContext);
  173. break;
  174. }
  175. ActivePcHelper.SetEnumeratorPC(enumerator, state.NextAfterStepPc);
  176. using (var logScope = new LogScope())
  177. {
  178. while (true)
  179. {
  180. try
  181. {
  182. if (!enumerator.MoveNext())
  183. {
  184. logScope.EvaluateLogScope(true);
  185. break;
  186. }
  187. if (!AllowFrameSkipAfterAction(action)) // Evaluate the log scope right away for the commands where we do not yield
  188. {
  189. logScope.EvaluateLogScope(true);
  190. }
  191. if (unityContext.TestMode == TestPlatform.PlayMode && enumerator.Current is IEditModeTestYieldInstruction)
  192. {
  193. throw new Exception($"PlayMode test are not allowed to yield {enumerator.Current.GetType().Name}");
  194. }
  195. }
  196. catch (Exception ex)
  197. {
  198. context.CurrentResult.RecordPrefixedException(m_AfterErrorPrefix, ex);
  199. state.StoreContext(unityContext);
  200. break;
  201. }
  202. state.NextAfterStepPc = ActivePcHelper.GetEnumeratorPC(enumerator);
  203. state.StoreContext(unityContext);
  204. if (!AllowFrameSkipAfterAction(action))
  205. {
  206. break;
  207. }
  208. yield return enumerator.Current;
  209. }
  210. }
  211. state.NextAfterStepIndex++;
  212. state.NextAfterStepPc = 0;
  213. }
  214. state.Reset();
  215. }
  216. public override TestResult Execute(ITestExecutionContext context)
  217. {
  218. throw new NotImplementedException("Use ExecuteEnumerable");
  219. }
  220. private static TestCommandPcHelper pcHelper;
  221. internal static TestCommandPcHelper ActivePcHelper
  222. {
  223. get
  224. {
  225. if (pcHelper == null)
  226. {
  227. pcHelper = new TestCommandPcHelper();
  228. }
  229. return pcHelper;
  230. }
  231. set
  232. {
  233. pcHelper = value;
  234. }
  235. }
  236. }
  237. }