using System.Collections; using System.Text.RegularExpressions; using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; using BaseGames.Enemies.Abilities; namespace BaseGames.Tests.EditMode.Abilities { public class AbilityConfigResolveTests { // 测试专用 SO 子类型(不依赖真实 8 个子类,保持本 Task 自包含) private sealed class ProbeConfigSO : EnemyAbilitySO { } private sealed class OtherConfigSO : EnemyAbilitySO { } // 测试探针:跳过基类 Awake 依赖装配,暴露 protected 成员 private sealed class ProbeAbility : EnemyAbilityBase { protected override void Awake() { /* 跳过依赖装配,避免测试噪声 */ } protected override IEnumerator ExecuteCoroutine() { yield break; } public void SetConfig(EnemyAbilitySO c) => _config = c; public T Probe() where T : EnemyAbilitySO => ResolveConfig(); } private ProbeAbility Make() => new GameObject("probe").AddComponent(); [Test] public void ResolveConfig_CorrectType_ReturnsInstance() { var probe = Make(); var cfg = ScriptableObject.CreateInstance(); probe.SetConfig(cfg); Assert.AreSame(cfg, probe.Probe()); Object.DestroyImmediate(cfg); Object.DestroyImmediate(probe.gameObject); } [Test] public void ResolveConfig_WrongType_LogsError_ReturnsNull() { var probe = Make(); probe.SetConfig(ScriptableObject.CreateInstance()); LogAssert.Expect(LogType.Error, new Regex("_config 需为")); Assert.IsNull(probe.Probe()); Object.DestroyImmediate(probe.gameObject); } [Test] public void ResolveConfig_Null_LogsError_ReturnsNull() { var probe = Make(); probe.SetConfig(null); LogAssert.Expect(LogType.Error, new Regex("_config 需为")); Assert.IsNull(probe.Probe()); Object.DestroyImmediate(probe.gameObject); } } }