Files
zeling_v2/Assets/Tests/PlayMode/RequiredFieldValidatorTests.cs
2026-05-25 13:21:41 +08:00

50 lines
1.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using NUnit.Framework;
using UnityEngine;
using BaseGames.Core;
namespace BaseGames.Tests.PlayMode
{
/// <summary>
/// RequiredFieldValidator 反射扫描测试。
/// </summary>
public class RequiredFieldValidatorTests
{
private class Sample : MonoBehaviour
{
[RequiredField] public GameObject Required;
[RequiredField("提示文本")] public string RequiredString;
public GameObject Optional;
}
[Test]
public void MissingField_LogsWarning()
{
var go = new GameObject("ReqHost");
var s = go.AddComponent<Sample>();
s.Required = null;
s.RequiredString = "";
// 期待两条警告Required + RequiredString
UnityEngine.TestTools.LogAssert.Expect(LogType.Warning,
new System.Text.RegularExpressions.Regex(@"\[RequiredField\] Sample\.Required.*"));
UnityEngine.TestTools.LogAssert.Expect(LogType.Warning,
new System.Text.RegularExpressions.Regex(@"\[RequiredField\] Sample\.RequiredString.*"));
RequiredFieldValidator.ValidateAll(s);
Object.DestroyImmediate(go);
}
[Test]
public void FilledField_NoWarning()
{
var go = new GameObject("ReqHost2");
var s = go.AddComponent<Sample>();
s.Required = go;
s.RequiredString = "ok";
RequiredFieldValidator.ValidateAll(s); // 不应有警告
Object.DestroyImmediate(go);
}
}
}