TestEnumerator.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections;
  3. using NUnit.Framework;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. using UnityEngine.TestRunner.NUnitExtensions;
  7. namespace UnityEngine.TestTools
  8. {
  9. internal class TestEnumerator
  10. {
  11. private readonly ITestExecutionContext m_Context;
  12. private static IEnumerator m_TestEnumerator;
  13. public static IEnumerator Enumerator { get { return m_TestEnumerator; } }
  14. public static void Reset()
  15. {
  16. m_TestEnumerator = null;
  17. }
  18. public TestEnumerator(ITestExecutionContext context, IEnumerator testEnumerator)
  19. {
  20. m_Context = context;
  21. m_TestEnumerator = testEnumerator;
  22. }
  23. public IEnumerator Execute()
  24. {
  25. m_Context.CurrentResult.SetResult(ResultState.Success);
  26. return Execute(m_TestEnumerator, new EnumeratorContext(m_Context));
  27. }
  28. private IEnumerator Execute(IEnumerator enumerator, EnumeratorContext context)
  29. {
  30. while (true)
  31. {
  32. if (context.ExceptionWasRecorded)
  33. {
  34. break;
  35. }
  36. try
  37. {
  38. if (!enumerator.MoveNext())
  39. {
  40. break;
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. context.RecordExceptionWithHint(ex);
  46. break;
  47. }
  48. if (enumerator.Current is IEnumerator nestedEnumerator)
  49. {
  50. yield return Execute(nestedEnumerator, context);
  51. }
  52. else
  53. {
  54. yield return enumerator.Current;
  55. }
  56. }
  57. }
  58. private class EnumeratorContext
  59. {
  60. private readonly ITestExecutionContext m_Context;
  61. public EnumeratorContext(ITestExecutionContext context)
  62. {
  63. m_Context = context;
  64. }
  65. public bool ExceptionWasRecorded
  66. {
  67. get;
  68. private set;
  69. }
  70. public void RecordExceptionWithHint(Exception ex)
  71. {
  72. if (ExceptionWasRecorded)
  73. {
  74. return;
  75. }
  76. m_Context.CurrentResult.RecordException(ex);
  77. ExceptionWasRecorded = true;
  78. }
  79. }
  80. }
  81. }