TestEnumeratorWrapper.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections;
  3. using System.Reflection;
  4. using NUnit.Framework;
  5. using NUnit.Framework.Interfaces;
  6. using NUnit.Framework.Internal;
  7. namespace UnityEngine.TestTools.TestRunner
  8. {
  9. internal class TestEnumeratorWrapper
  10. {
  11. private readonly TestMethod m_TestMethod;
  12. public TestEnumeratorWrapper(TestMethod testMethod)
  13. {
  14. m_TestMethod = testMethod;
  15. }
  16. public IEnumerator GetEnumerator(ITestExecutionContext context)
  17. {
  18. if (m_TestMethod.Method.ReturnType.Type == typeof(IEnumerator))
  19. {
  20. return HandleEnumerableTest(context);
  21. }
  22. var message = string.Format("Return type {0} of {1} in {2} is not supported.",
  23. m_TestMethod.Method.ReturnType, m_TestMethod.Method.Name, m_TestMethod.Method.TypeInfo.FullName);
  24. if (m_TestMethod.Method.ReturnType.Type == typeof(IEnumerable))
  25. {
  26. message += "\nDid you mean IEnumerator?";
  27. }
  28. throw new InvalidSignatureException(message);
  29. }
  30. private IEnumerator HandleEnumerableTest(ITestExecutionContext context)
  31. {
  32. try
  33. {
  34. return m_TestMethod.Method.MethodInfo.Invoke(context.TestObject, m_TestMethod.parms != null ? m_TestMethod.parms.OriginalArguments : null) as IEnumerator;
  35. }
  36. catch (TargetInvocationException e)
  37. {
  38. if (e.InnerException is IgnoreException)
  39. {
  40. context.CurrentResult.SetResult(ResultState.Ignored, e.InnerException.Message);
  41. return null;
  42. }
  43. throw;
  44. }
  45. }
  46. }
  47. }