using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
namespace Watermelon.Tests
{
///
/// Base class for all Pool module tests.
/// Creates a real PoolManager MonoBehaviour in [SetUp] and destroys it in [TearDown].
/// Always pass an explicit container to pool constructors to avoid the #if UNITY_EDITOR
/// default-container path; containers are tracked and destroyed alongside their children.
///
public abstract class PoolTestBase
{
private GameObject _poolManagerGO;
private readonly List _ownedObjects = new();
[SetUp]
public void PoolBaseSetUp()
{
_poolManagerGO = new GameObject("PoolManager");
// EditMode does not trigger Awake, so Init() must be called explicitly
// (same pattern as TweenTestBase → tweenGO.AddComponent().Init(...))
_poolManagerGO.AddComponent().Init();
}
[TearDown]
public void PoolBaseTearDown()
{
// Cache before destroying PoolManager (instance → null after OnDestroy)
Transform defaultContainer = PoolManager.DefaultContainer;
// Destroy tracked objects — containers bring down all pooled children with them
foreach (GameObject go in _ownedObjects)
if (go != null)
Object.DestroyImmediate(go);
_ownedObjects.Clear();
// Destroy PoolManager (triggers OnDestroy → instance = null)
if (_poolManagerGO != null)
Object.DestroyImmediate(_poolManagerGO);
// Destroy the default container spawned by GetContainer() in Editor mode
if (defaultContainer != null)
Object.DestroyImmediate(defaultContainer.gameObject);
}
// ─── Helpers ───────────────────────────────────────────────────────────
/// Creates a plain prefab GameObject tracked for TearDown cleanup.
protected GameObject CreatePrefab(string name = "TestPrefab")
{
var go = new GameObject(name);
_ownedObjects.Add(go);
return go;
}
/// Creates a prefab with component, tracked for cleanup.
protected GameObject CreatePrefab(string name = "TestPrefab") where T : Component
{
var go = CreatePrefab(name);
go.AddComponent();
return go;
}
///
/// Creates a container Transform tracked for TearDown cleanup.
/// All pool objects instantiated as children are destroyed with this container.
///
protected Transform CreateContainer(string name = "PoolContainer")
{
var go = new GameObject(name);
_ownedObjects.Add(go);
return go.transform;
}
}
// ─── Shared test component ─────────────────────────────────────────────────
/// Minimal MonoBehaviour used as a typed component in PoolGeneric tests.
internal class TestBehaviour : MonoBehaviour { }
}