EnumerableTestMethodCommand.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Runner;
  10. using UnityEngine.TestTools.TestRunner;
  11. namespace UnityEngine.TestTools
  12. {
  13. internal class EnumerableTestMethodCommand : TestCommand, IEnumerableTestMethodCommand
  14. {
  15. private readonly TestMethod testMethod;
  16. public EnumerableTestMethodCommand(TestMethod testMethod)
  17. : base(testMethod)
  18. {
  19. this.testMethod = testMethod;
  20. }
  21. public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
  22. {
  23. yield return null;
  24. IEnumerator currentExecutingTestEnumerator;
  25. try
  26. {
  27. currentExecutingTestEnumerator = new TestEnumeratorWrapper(testMethod).GetEnumerator(context);
  28. }
  29. catch (Exception ex)
  30. {
  31. context.CurrentResult.RecordException(ex);
  32. yield break;
  33. }
  34. if (currentExecutingTestEnumerator != null)
  35. {
  36. var testEnumeraterYieldInstruction = new TestEnumerator(context, currentExecutingTestEnumerator);
  37. yield return testEnumeraterYieldInstruction;
  38. var enumerator = testEnumeraterYieldInstruction.Execute();
  39. var executingEnumerator = ExecuteEnumerableAndRecordExceptions(enumerator, context);
  40. while (executingEnumerator.MoveNext())
  41. {
  42. yield return executingEnumerator.Current;
  43. }
  44. }
  45. else
  46. {
  47. if (context.CurrentResult.ResultState != ResultState.Ignored)
  48. {
  49. context.CurrentResult.SetResult(ResultState.Success);
  50. }
  51. }
  52. }
  53. private static IEnumerator ExecuteEnumerableAndRecordExceptions(IEnumerator enumerator, ITestExecutionContext context)
  54. {
  55. while (true)
  56. {
  57. try
  58. {
  59. if (!enumerator.MoveNext())
  60. {
  61. break;
  62. }
  63. }
  64. catch (Exception ex)
  65. {
  66. context.CurrentResult.RecordException(ex);
  67. break;
  68. }
  69. if (enumerator.Current is IEnumerator)
  70. {
  71. var current = (IEnumerator)enumerator.Current;
  72. yield return ExecuteEnumerableAndRecordExceptions(current, context);
  73. }
  74. else
  75. {
  76. yield return enumerator.Current;
  77. }
  78. }
  79. }
  80. public override TestResult Execute(ITestExecutionContext context)
  81. {
  82. throw new NotImplementedException("Use ExecuteEnumerable");
  83. }
  84. }
  85. }