EnumerableTestMethodCommand.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using NUnit.Framework;
  5. using NUnit.Framework.Interfaces;
  6. using NUnit.Framework.Internal;
  7. using NUnit.Framework.Internal.Commands;
  8. using NUnit.Framework.Internal.Execution;
  9. using UnityEngine.TestRunner.NUnitExtensions;
  10. using Unity.Profiling;
  11. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  12. using UnityEngine.TestTools.TestRunner;
  13. namespace UnityEngine.TestTools
  14. {
  15. internal class EnumerableTestMethodCommand : TestCommand, IEnumerableTestMethodCommand
  16. {
  17. private readonly TestMethod testMethod;
  18. public EnumerableTestMethodCommand(TestMethod testMethod)
  19. : base(testMethod)
  20. {
  21. this.testMethod = testMethod;
  22. }
  23. public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
  24. {
  25. yield return null;
  26. IEnumerator currentExecutingTestEnumerator;
  27. try
  28. {
  29. currentExecutingTestEnumerator = new TestEnumeratorWrapper(testMethod).GetEnumerator(context);
  30. }
  31. catch (Exception ex)
  32. {
  33. context.CurrentResult.RecordException(ex);
  34. yield break;
  35. }
  36. if (currentExecutingTestEnumerator != null)
  37. {
  38. var testEnumeraterYieldInstruction = new TestEnumerator(context, currentExecutingTestEnumerator);
  39. yield return testEnumeraterYieldInstruction;
  40. var enumerator = testEnumeraterYieldInstruction.Execute();
  41. var executingEnumerator = ExecuteEnumerableAndRecordExceptions(enumerator, new EnumeratorContext(context));
  42. while (AdvanceEnumerator(executingEnumerator))
  43. {
  44. yield return executingEnumerator.Current;
  45. }
  46. }
  47. else
  48. {
  49. if (context.CurrentResult.ResultState != ResultState.Ignored)
  50. {
  51. context.CurrentResult.SetResult(ResultState.Success);
  52. }
  53. }
  54. }
  55. private bool AdvanceEnumerator(IEnumerator enumerator)
  56. {
  57. using (new ProfilerMarker(testMethod.MethodName).Auto())
  58. return enumerator.MoveNext();
  59. }
  60. private IEnumerator ExecuteEnumerableAndRecordExceptions(IEnumerator enumerator, EnumeratorContext context)
  61. {
  62. while (true)
  63. {
  64. if (context.ExceptionWasRecorded)
  65. {
  66. break;
  67. }
  68. try
  69. {
  70. if (!enumerator.MoveNext())
  71. {
  72. break;
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. context.RecordExceptionWithHint(ex);
  78. break;
  79. }
  80. if (enumerator.Current is IEnumerator nestedEnumerator)
  81. {
  82. yield return ExecuteEnumerableAndRecordExceptions(nestedEnumerator, context);
  83. }
  84. else
  85. {
  86. yield return enumerator.Current;
  87. }
  88. }
  89. }
  90. private class EnumeratorContext
  91. {
  92. private readonly ITestExecutionContext m_Context;
  93. public EnumeratorContext(ITestExecutionContext context)
  94. {
  95. m_Context = context;
  96. }
  97. public bool ExceptionWasRecorded
  98. {
  99. get;
  100. private set;
  101. }
  102. public void RecordExceptionWithHint(Exception ex)
  103. {
  104. if (ExceptionWasRecorded)
  105. {
  106. return;
  107. }
  108. m_Context.CurrentResult.RecordException(ex);
  109. ExceptionWasRecorded = true;
  110. }
  111. }
  112. public override TestResult Execute(ITestExecutionContext context)
  113. {
  114. throw new NotImplementedException("Use ExecuteEnumerable");
  115. }
  116. }
  117. }