EnumerableSetUpTearDownCommand.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Reflection;
  5. using NUnit.Framework.Internal;
  6. using NUnit.Framework.Internal.Commands;
  7. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  8. namespace UnityEngine.TestTools
  9. {
  10. internal class EnumerableSetUpTearDownCommand : BeforeAfterTestCommandBase<MethodInfo>
  11. {
  12. public EnumerableSetUpTearDownCommand(TestCommand innerCommand)
  13. : base(innerCommand, "SetUp", "TearDown")
  14. {
  15. if (Test.TypeInfo.Type != null)
  16. {
  17. BeforeActions = GetMethodsWithAttributeFromFixture(Test.TypeInfo.Type, typeof(UnitySetUpAttribute));
  18. AfterActions = GetMethodsWithAttributeFromFixture(Test.TypeInfo.Type, typeof(UnityTearDownAttribute)).Reverse().ToArray();
  19. }
  20. }
  21. private static MethodInfo[] GetMethodsWithAttributeFromFixture(Type fixtureType, Type setUpType)
  22. {
  23. MethodInfo[] methodsWithAttribute = Reflect.GetMethodsWithAttribute(fixtureType, setUpType, true);
  24. return methodsWithAttribute.Where(x => x.ReturnType == typeof(IEnumerator)).ToArray();
  25. }
  26. protected override IEnumerator InvokeBefore(MethodInfo action, Test test, UnityTestExecutionContext context)
  27. {
  28. return (IEnumerator)Reflect.InvokeMethod(action, context.TestObject);
  29. }
  30. protected override IEnumerator InvokeAfter(MethodInfo action, Test test, UnityTestExecutionContext context)
  31. {
  32. return (IEnumerator)Reflect.InvokeMethod(action, context.TestObject);
  33. }
  34. protected override BeforeAfterTestCommandState GetState(UnityTestExecutionContext context)
  35. {
  36. return context.SetUpTearDownState;
  37. }
  38. }
  39. }