A game about forced loneliness, made by TACStudios
1using System; 2 3namespace UnityEngine.TestTools 4{ 5 /// <summary> 6 /// This is a wrapper that allows running tests on MonoBehaviour scripts. Inherits from <see cref="CustomYieldInstruction"/> `MonoBehaviourTest` is a [coroutine](https://docs.unity3d.com/ScriptReference/Coroutine.html) and a helper for writing MonoBehaviour tests. 7 /// Yield a `MonoBehaviourTest` when using the `UnityTest` attribute to instantiate the `MonoBehaviour` you wish to test and wait for it to finish running. Implement the `IMonoBehaviourTest` interface on the `MonoBehaviour` to state when the test completes. 8 /// </summary> 9 /// <typeparam name="T">A MonoBehaviour component created for the test and attached to the tests [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html).</typeparam> 10 public class MonoBehaviourTest<T> : CustomYieldInstruction where T : MonoBehaviour, IMonoBehaviourTest 11 { 12 /// <summary> 13 /// A MonoBehaviour component created for the test and attached to the test's [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html). 14 /// </summary> 15 public T component { get; } 16 /// <summary> 17 /// A `GameObject` created as a container for the test component. 18 /// </summary> 19 public GameObject gameObject { get { return component.gameObject; } } 20 /// <summary> 21 /// Initializes and returns an instance of MonoBehaviourTest. 22 /// </summary> 23 /// <param name="dontDestroyOnLoad"></param> 24 /// <example> 25 /// <code> 26 /// [UnityTest] 27 /// public IEnumerator MonoBehaviourTest_Works() 28 /// { 29 /// yield return new MonoBehaviourTest&lt;MyMonoBehaviourTest&gt;(); 30 /// } 31 /// 32 /// public class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest 33 /// { 34 /// private int frameCount; 35 /// public bool IsTestFinished 36 /// { 37 /// get { return frameCount &gt; 10; } 38 /// } 39 /// 40 /// void Update() 41 /// { 42 /// frameCount++; 43 /// } 44 /// } 45 /// </code> 46 /// </example> 47 public MonoBehaviourTest(bool dontDestroyOnLoad = true) 48 { 49 var go = new GameObject("MonoBehaviourTest: " + typeof(T).FullName); 50 component = go.AddComponent<T>(); 51 if (dontDestroyOnLoad) 52 { 53 Object.DontDestroyOnLoad(go); 54 } 55 } 56 57 /// <summary> 58 /// (Inherited) Returns `true`` if the test is not finished yet, which keeps the coroutine suspended 59 /// </summary> 60 public override bool keepWaiting 61 { 62 get { return !component.IsTestFinished; } 63 } 64 } 65}