A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections;
3using NUnit.Framework.Internal;
4using UnityEngine.TestRunner.NUnitExtensions.Runner;
5using UnityEngine.TestTools.TestRunner;
6
7namespace UnityEngine.TestTools.Utils
8{
9 internal class CoroutineRunner
10 {
11 private bool m_Running;
12 private readonly MonoBehaviour m_Controller;
13 private readonly UnityTestExecutionContext m_Context;
14 private IEnumerator m_TestCoroutine;
15
16 public CoroutineRunner(MonoBehaviour playmodeTestsController, UnityTestExecutionContext context)
17 {
18 m_Controller = playmodeTestsController;
19 m_Context = context;
20 }
21
22 public IEnumerator HandleEnumerableTest(IEnumerator testEnumerator)
23 {
24 do
25 {
26 if (!m_Running)
27 {
28 m_Running = true;
29 m_TestCoroutine = ExMethod(WrapEnumeratorForChecks(testEnumerator));
30 m_Controller.StartCoroutine(m_TestCoroutine);
31 }
32 if (m_Context.ExecutionStatus == TestExecutionStatus.StopRequested || m_Context.ExecutionStatus == TestExecutionStatus.AbortRequested)
33 {
34 StopAllRunningCoroutines();
35 yield break;
36 }
37 yield return null;
38 }
39 while (m_Running);
40 }
41
42 private void StopAllRunningCoroutines()
43 {
44 if (m_TestCoroutine != null)
45 {
46 m_Controller.StopCoroutine(m_TestCoroutine);
47 }
48 }
49
50 private IEnumerator ExMethod(IEnumerator e)
51 {
52 yield return m_Controller.StartCoroutine(e);
53 m_Running = false;
54 }
55
56 private IEnumerator WrapEnumeratorForChecks(IEnumerator e)
57 {
58 while (e.MoveNext())
59 {
60 if (Application.isBatchMode && e.Current is WaitForEndOfFrame)
61 {
62 m_Running = false;
63 throw new Exception("UnityTest yielded WaitForEndOfFrame, which is not evoked in batchmode.");
64 }
65
66 yield return e.Current;
67 }
68 }
69 }
70}