File size: 2,704 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | namespace UnityEngine.TestTools
{
/// <summary>
/// This is a wrapper that allows running tests on MonoBehaviour scripts. Inherits from <see cref="CustomYieldInstruction"/>.
/// </summary>
/// <typeparam name="T">A MonoBehaviour component created for the test and attached to the tests [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html).</typeparam>
public class MonoBehaviourTest<T> : CustomYieldInstruction where T : MonoBehaviour, IMonoBehaviourTest
{
/// <summary>A MonoBehaviour component created for the test and attached to the tests [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html).</summary>
public T component { get; }
/// <summary>
/// A `GameObject` created as a container for the test component.
/// </summary>
public GameObject gameObject { get { return component.gameObject; } }
/// <summary>
/// `MonoBehaviourTest` is a [coroutine](https://docs.unity3d.com/ScriptReference/Coroutine.html) and a helper for writing MonoBehaviour tests.
/// Yield a `MonoBehaviour`Test 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.
/// </summary>
/// <param name="dontDestroyOnLoad"></param>
/// <example>
/// <code>
/// [UnityTest]
/// public IEnumerator MonoBehaviourTest_Works()
/// {
/// yield return new MonoBehaviourTest<MyMonoBehaviourTest>();
/// }
///
/// public class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest
/// {
/// private int frameCount;
/// public bool IsTestFinished
/// {
/// get { return frameCount > 10; }
/// }
///
/// void Update()
/// {
/// frameCount++;
/// }
/// }
/// </code>
/// </example>
public MonoBehaviourTest(bool dontDestroyOnLoad = true)
{
var go = new GameObject("MonoBehaviourTest: " + typeof(T).FullName);
component = go.AddComponent<T>();
if (dontDestroyOnLoad)
{
Object.DontDestroyOnLoad(go);
}
}
/// <summary>
/// (Inherited) Returns `true`` if the test is not finished yet, which keeps the coroutine suspended
/// </summary>
public override bool keepWaiting
{
get { return !component.IsTestFinished; }
}
}
}
|