#nullable enable using System.Collections; using Cysharp.Threading.Tasks; using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; namespace ShrinkEventBus.PlayMode.Tests { internal readonly struct MonoLifecycleEvent : IShrinkEvent { public MonoLifecycleEvent(int value) => Value = value; public int Value { get; } } [ShrinkEventSubscriber(DefaultBus = "game")] internal sealed class MonoLifecycleTarget : MonoBehaviour { public int Sum { get; private set; } [ShrinkSubscribe] private void OnEvent(MonoLifecycleEvent value) => Sum += value.Value; } public sealed class ShrinkMonoEventScopePlayModeTests { [UnityTest] public IEnumerator OnEnableAttachesAndOnDisableReleases() { var gameObject = new GameObject("ShrinkMonoEventScope-PlayMode"); gameObject.SetActive(false); var target = gameObject.AddComponent(); gameObject.AddComponent(); gameObject.SetActive(true); yield return null; yield return EventBus.PostAsync(new MonoLifecycleEvent(3)).ToCoroutine(); Assert.AreEqual(3, target.Sum); gameObject.SetActive(false); yield return null; yield return EventBus.PostAsync(new MonoLifecycleEvent(5)).ToCoroutine(); Assert.AreEqual(3, target.Sum); Object.Destroy(gameObject); } } }