using System;
namespace UnityEngine.TestTools
{
///
/// This is a wrapper that allows running tests on MonoBehaviour scripts. Inherits from `MonoBehaviourTest` is a [coroutine](https://docs.unity3d.com/ScriptReference/Coroutine.html) and a helper for writing MonoBehaviour tests.
/// 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.
///
/// A MonoBehaviour component created for the test and attached to the tests [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html).
public class MonoBehaviourTest : CustomYieldInstruction where T : MonoBehaviour, IMonoBehaviourTest
{
///
/// A MonoBehaviour component created for the test and attached to the test's [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html).
///
public T component { get; }
///
/// A `GameObject` created as a container for the test component.
///
public GameObject gameObject { get { return component.gameObject; } }
///
/// Initializes and returns an instance of MonoBehaviourTest.
///
///
///
///
/// [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++;
/// }
/// }
///
///
public MonoBehaviourTest(bool dontDestroyOnLoad = true)
{
var go = new GameObject("MonoBehaviourTest: " + typeof(T).FullName);
component = go.AddComponent();
if (dontDestroyOnLoad)
{
Object.DontDestroyOnLoad(go);
}
}
///
/// (Inherited) Returns `true`` if the test is not finished yet, which keeps the coroutine suspended
///
public override bool keepWaiting
{
get { return !component.IsTestFinished; }
}
}
}