EnumerableSetUpTearDownCommand.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using NUnit.Framework.Internal;
  7. using NUnit.Framework.Internal.Commands;
  8. using Unity.Profiling;
  9. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  10. namespace UnityEngine.TestTools
  11. {
  12. internal class EnumerableSetUpTearDownCommand : BeforeAfterTestCommandBase<MethodInfo>
  13. {
  14. static readonly Dictionary<Type, List<MethodInfo>> m_BeforeActionsCache = new Dictionary<Type, List<MethodInfo>>();
  15. static readonly Dictionary<Type, List<MethodInfo>> m_AfterActionsCache = new Dictionary<Type, List<MethodInfo>>();
  16. public EnumerableSetUpTearDownCommand(TestCommand innerCommand)
  17. : base(innerCommand, "SetUp", "TearDown")
  18. {
  19. using (new ProfilerMarker(nameof(EnumerableSetUpTearDownCommand)).Auto())
  20. {
  21. if (Test.TypeInfo.Type != null)
  22. {
  23. BeforeActions = GetActions(m_BeforeActionsCache, Test.TypeInfo.Type, typeof(UnitySetUpAttribute), typeof(IEnumerator));
  24. AfterActions = GetActions(m_AfterActionsCache, Test.TypeInfo.Type, typeof(UnityTearDownAttribute), typeof(IEnumerator)).Reverse().ToArray();
  25. }
  26. }
  27. }
  28. protected override bool MoveAfterEnumerator(IEnumerator enumerator, Test test)
  29. {
  30. using (new ProfilerMarker(test.Name + ".TearDown").Auto())
  31. return base.MoveAfterEnumerator(enumerator, test);
  32. }
  33. protected override bool MoveBeforeEnumerator(IEnumerator enumerator, Test test)
  34. {
  35. using (new ProfilerMarker(test.Name + ".Setup").Auto())
  36. return base.MoveBeforeEnumerator(enumerator, test);
  37. }
  38. protected override IEnumerator InvokeBefore(MethodInfo action, Test test, UnityTestExecutionContext context)
  39. {
  40. return (IEnumerator)Reflect.InvokeMethod(action, context.TestObject);
  41. }
  42. protected override IEnumerator InvokeAfter(MethodInfo action, Test test, UnityTestExecutionContext context)
  43. {
  44. return (IEnumerator)Reflect.InvokeMethod(action, context.TestObject);
  45. }
  46. protected override BeforeAfterTestCommandState GetState(UnityTestExecutionContext context)
  47. {
  48. return context.SetUpTearDownState;
  49. }
  50. }
  51. }