Files
zeling_v2/Assets/Tests/EditMode/Abilities/AbilityConfigResolveTests.cs
T

59 lines
2.2 KiB
C#

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<T>() where T : EnemyAbilitySO => ResolveConfig<T>();
}
private ProbeAbility Make() => new GameObject("probe").AddComponent<ProbeAbility>();
[Test]
public void ResolveConfig_CorrectType_ReturnsInstance()
{
var probe = Make();
var cfg = ScriptableObject.CreateInstance<ProbeConfigSO>();
probe.SetConfig(cfg);
Assert.AreSame(cfg, probe.Probe<ProbeConfigSO>());
Object.DestroyImmediate(cfg);
Object.DestroyImmediate(probe.gameObject);
}
[Test]
public void ResolveConfig_WrongType_LogsError_ReturnsNull()
{
var probe = Make();
probe.SetConfig(ScriptableObject.CreateInstance<OtherConfigSO>());
LogAssert.Expect(LogType.Error, new Regex("_config 需为"));
Assert.IsNull(probe.Probe<ProbeConfigSO>());
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<ProbeConfigSO>());
Object.DestroyImmediate(probe.gameObject);
}
}
}