설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MonoBehaviourTest.cs 2.6KB

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