A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections; 3using System.Diagnostics; 4using NUnit.Framework.Interfaces; 5using NUnit.Framework.Internal; 6using NUnit.Framework.Internal.Commands; 7using UnityEngine.TestRunner.NUnitExtensions.Runner; 8 9namespace UnityEngine.TestTools 10{ 11 internal class RepeatCommand : DelegatingTestCommand, IEnumerableTestMethodCommand 12 { 13 public RepeatCommand(TestCommand innerCommand) 14 : base(innerCommand) 15 { 16 } 17 18 public override TestResult Execute(ITestExecutionContext context) 19 { 20 throw new NotImplementedException("Use ExecuteEnumerable"); 21 } 22 23 public IEnumerable ExecuteEnumerable(ITestExecutionContext context) 24 { 25 var unityContext = (UnityTestExecutionContext)context; 26 if (unityContext.RetryRepeatState?.GetHashCode() == null) 27 { 28 unityContext.RetryRepeatState = new EnumerableTestState(); 29 } 30 31 while(unityContext.RetryRepeatState.Repeat < unityContext.RepeatCount + 1) 32 { 33 if (innerCommand is IEnumerableTestMethodCommand) 34 { 35 var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context); 36 foreach (var iterator in executeEnumerable) 37 { 38 yield return iterator; 39 } 40 } 41 else 42 { 43 context.CurrentResult = innerCommand.Execute(context); 44 } 45 46 if (context.CurrentResult.ResultState != ResultState.Success) 47 { 48 unityContext.RetryRepeatState.Repeat++; 49 break; 50 } 51 52 if (unityContext.RetryRepeatState.Repeat < unityContext.RepeatCount) 53 { 54 ReportTestFinishStartPair(unityContext); 55 } 56 unityContext.RetryRepeatState.Repeat++; 57 } 58 59 SetIterationProperty(unityContext, unityContext.RetryRepeatState.Repeat-1); 60 unityContext.RetryRepeatState.Repeat = 0; 61 } 62 63 private static void ReportTestFinishStartPair(UnityTestExecutionContext unityContext) 64 { 65 unityContext.CurrentResult.StartTime = unityContext.StartTime; 66 unityContext.CurrentResult.EndTime = DateTime.UtcNow; 67 long tickCount = Stopwatch.GetTimestamp() - unityContext.StartTicks; 68 double seconds = (double) tickCount / Stopwatch.Frequency; 69 unityContext.CurrentResult.Duration = seconds; 70 SetIterationProperty(unityContext, unityContext.RetryRepeatState.Repeat); 71 unityContext.Listener.TestFinished(unityContext.CurrentResult); 72 73 // Start new test iteration 74 unityContext.CurrentResult = unityContext.CurrentTest.MakeTestResult(); 75 unityContext.StartTime = DateTime.UtcNow; 76 unityContext.StartTicks = Stopwatch.GetTimestamp(); 77 unityContext.Listener.TestStarted(unityContext.CurrentTest); 78 } 79 80 private static void SetIterationProperty(UnityTestExecutionContext unityContext, int iteration) 81 { 82 unityContext.CurrentResult.Test.Properties.Set("repeatIteration", iteration); 83 } 84 } 85}