A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.Diagnostics; 5using System.Linq; 6using NUnit.Framework; 7using NUnit.Framework.Interfaces; 8using NUnit.Framework.Internal; 9using NUnit.Framework.Internal.Execution; 10 11namespace UnityEngine.TestRunner.NUnitExtensions.Runner 12{ 13 internal abstract class UnityWorkItem 14 { 15 protected readonly WorkItemFactory m_Factory; 16 protected bool m_ExecuteTestStartEvent; 17 protected bool m_DontRunRestoringResult; 18 protected const int k_DefaultTimeout = 1000 * 180; 19 public event EventHandler Completed; 20 21 public bool ResultedInDomainReload { get; internal set; } 22 23 public UnityTestExecutionContext Context { get; private set; } 24 25 public Test Test { get; private set; } 26 27 public TestResult Result { get; protected set; } 28 29 public WorkItemState State { get; private set; } 30 31 public List<ITestAction> Actions { get; private set; } 32 33 protected UnityWorkItem(Test test, WorkItemFactory factory) 34 { 35 m_Factory = factory; 36 Test = test; 37 Actions = new List<ITestAction>(); 38 Result = test.MakeTestResult(); 39 State = WorkItemState.Ready; 40 m_ExecuteTestStartEvent = ShouldExecuteStartEvent(); 41 m_DontRunRestoringResult = ShouldRestore(test); 42 } 43 44 protected static bool ShouldRestore(ITest loadedTest) 45 { 46 return UnityWorkItemDataHolder.alreadyExecutedTests != null && 47 UnityWorkItemDataHolder.alreadyExecutedTests.Contains(loadedTest.GetUniqueName()); 48 } 49 50 protected bool ShouldExecuteStartEvent() 51 { 52 return UnityWorkItemDataHolder.alreadyStartedTests != null && 53 UnityWorkItemDataHolder.alreadyStartedTests.All(x => x != Test.GetUniqueName()) && 54 !ShouldRestore(Test); 55 } 56 57 protected abstract IEnumerable PerformWork(); 58 59 public void InitializeContext(UnityTestExecutionContext context) 60 { 61 Context = context; 62 63 if (Test is TestAssembly) 64 Actions.AddRange(ActionsHelper.GetActionsFromTestAssembly((TestAssembly)Test)); 65 else if (Test is ParameterizedMethodSuite) 66 Actions.AddRange(ActionsHelper.GetActionsFromTestMethodInfo(Test.Method)); 67 else if (Test.TypeInfo != null) 68 Actions.AddRange(ActionsHelper.GetActionsFromTypesAttributes(Test.TypeInfo.Type)); 69 } 70 71 public virtual IEnumerable Execute() 72 { 73 Context.CurrentTest = Test; 74 Context.CurrentResult = Result; 75 76 if (m_ExecuteTestStartEvent) 77 { 78 Context.Listener.TestStarted(Test); 79 } 80 81 Context.StartTime = DateTime.UtcNow; 82 Context.StartTicks = Stopwatch.GetTimestamp(); 83 84 State = WorkItemState.Running; 85 86 return PerformWork(); 87 } 88 89 protected void WorkItemComplete() 90 { 91 State = WorkItemState.Complete; 92 93 Result.StartTime = Context.StartTime; 94 Result.EndTime = DateTime.UtcNow; 95 96 long tickCount = Stopwatch.GetTimestamp() - Context.StartTicks; 97 double seconds = (double)tickCount / Stopwatch.Frequency; 98 Result.Duration = seconds; 99 100 //Result.AssertCount += Context.AssertCount; 101 102 Context.Listener.TestFinished(Result); 103 104 if (Completed != null) 105 Completed(this, EventArgs.Empty); 106 107 Context.TestObject = null; 108 Test.Fixture = null; 109 110 // Reset the states, in case of errors in the middle of their execution. E.g. Timeout. 111 if (!Test.IsSuite) 112 { 113 Context.SetUpTearDownState.Reset(); 114 Context.OuterUnityTestActionState.Reset(); 115 } 116 } 117 118 public virtual void Cancel(bool force) 119 { 120 Result.SetResult(ResultState.Cancelled, "Cancelled by user"); 121 Context.Listener.TestFinished(Result); 122 } 123 } 124}