Files
vampire-like/Assets/Tests/Gameplay/EditMode/ChildrenMapTests.cs
T

126 lines
4.0 KiB
C#

using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;
using SepCore.Components;
using UnityEngine;
using UnityEngine.TestTools;
namespace VampireLike.Tests.Gameplay.EditMode
{
[TestFixture]
public class ChildrenMapTests
{
private GameObject _rootObject;
private ChildrenMap _childrenMap;
private GameObject _childA;
private GameObject _childB;
[SetUp]
public void SetUp()
{
GameFramework.GameFrameworkLog.SetLogHelper(new UnityGameFramework.Runtime.DefaultLogHelper());
_rootObject = new GameObject("RootTestObject");
_childrenMap = _rootObject.AddComponent<ChildrenMap>();
_childA = new GameObject("ChildA");
_childA.transform.SetParent(_rootObject.transform);
_childB = new GameObject("ChildB");
_childB.transform.SetParent(_rootObject.transform);
}
[TearDown]
public void TearDown()
{
if (_rootObject != null)
{
Object.DestroyImmediate(_rootObject);
}
}
private void SetEntries(List<ChildrenMap.Entry> entries)
{
FieldInfo field = typeof(ChildrenMap).GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.IsNotNull(field);
field.SetValue(_childrenMap, entries);
}
[Test]
public void Get_WithValidKey_ReturnsGameObject()
{
SetEntries(new List<ChildrenMap.Entry>
{
new ChildrenMap.Entry("Socket_A", _childA),
new ChildrenMap.Entry("Socket_B", _childB)
});
GameObject resultA = _childrenMap.Get("Socket_A");
GameObject resultB = _childrenMap.Get("Socket_B");
Assert.AreEqual(_childA, resultA);
Assert.AreEqual(_childB, resultB);
}
[Test]
public void Get_WithNonExistingKey_ReturnsNull()
{
SetEntries(new List<ChildrenMap.Entry>
{
new ChildrenMap.Entry("Socket_A", _childA)
});
LogAssert.Expect(LogType.Error, new System.Text.RegularExpressions.Regex("Key 'NonExisting' not found"));
GameObject result = _childrenMap.Get("NonExisting");
Assert.IsNull(result);
}
[Test]
public void Get_WithNullOrEmptyKey_ReturnsNull()
{
SetEntries(new List<ChildrenMap.Entry>
{
new ChildrenMap.Entry("Socket_A", _childA)
});
LogAssert.Expect(LogType.Error, new System.Text.RegularExpressions.Regex("Key is null or empty"));
GameObject resultNull = _childrenMap.Get(null);
Assert.IsNull(resultNull);
LogAssert.Expect(LogType.Error, new System.Text.RegularExpressions.Regex("Key is null or empty"));
GameObject resultEmpty = _childrenMap.Get(string.Empty);
Assert.IsNull(resultEmpty);
}
[Test]
public void Get_WithConfiguredNullTarget_ReturnsNull()
{
SetEntries(new List<ChildrenMap.Entry>
{
new ChildrenMap.Entry("NullTargetKey", null)
});
LogAssert.Expect(LogType.Error, new System.Text.RegularExpressions.Regex("Target GameObject for key 'NullTargetKey' is null"));
GameObject result = _childrenMap.Get("NullTargetKey");
Assert.IsNull(result);
}
[Test]
public void InitMap_WithDuplicateKey_LogsErrorAndKeepsFirst()
{
SetEntries(new List<ChildrenMap.Entry>
{
new ChildrenMap.Entry("SameKey", _childA),
new ChildrenMap.Entry("SameKey", _childB)
});
LogAssert.Expect(LogType.Error, new System.Text.RegularExpressions.Regex("Duplicate key 'SameKey'"));
GameObject result = _childrenMap.Get("SameKey");
Assert.AreEqual(_childA, result);
}
}
}