Files
ShrinkEventBus/Tests.PlayMode/ShrinkMonoEventScopePlayModeTests.cs
T
cneicy 1c921f5aec
Publish UPM package / publish (push) Failing after 1s
chore: initialize standalone UPM package
2026-08-26 02:50:18 +08:00

49 lines
1.5 KiB
C#

#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<MonoLifecycleTarget>();
gameObject.AddComponent<ShrinkMonoEventScope>();
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);
}
}
}