50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|