123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using NUnit.Framework;
- using UnityEngine;
- using LLM.Editor.Commands;
- using LLM.Editor.Data;
- using LLM.Editor.Helper;
- namespace LLM.Editor.Tests.Unit
- {
- [TestFixture]
- public class SetHierarchyCommandTests
- {
- private GameObject _childObject;
- private GameObject _parentObject;
- private CommandContext _context;
- [SetUp]
- public void SetUp()
- {
- _childObject = new GameObject("ChildObject");
- _parentObject = new GameObject("ParentObject");
- _context = new CommandContext();
-
- // Register objects with the context using their instance IDs as identifiers
- _context.IdentifierMap[_childObject.GetInstanceID().ToString()] = _childObject.GetInstanceID().ToString();
- _context.IdentifierMap[_parentObject.GetInstanceID().ToString()] = _parentObject.GetInstanceID().ToString();
- }
- [TearDown]
- public void TearDown()
- {
- if (_childObject != null) Object.DestroyImmediate(_childObject);
- if (_parentObject != null) Object.DestroyImmediate(_parentObject);
- }
- private SetHierarchyCommand CreateCommand(string childId, string parentId, int siblingIndex = -1)
- {
- var parameters = new SetHierarchyParams
- {
- childIdentifier = childId,
- parentIdentifier = parentId,
- siblingIndex = siblingIndex
- };
- return new SetHierarchyCommand(parameters.ToJson());
- }
- [Test]
- public void Execute_WithValidParent_SetsParentCorrectly()
- {
- // Arrange
- var command = CreateCommand(_childObject.GetInstanceID().ToString(), _parentObject.GetInstanceID().ToString());
- // Act
- var outcome = command.Execute(_context);
- // Assert
- Assert.AreEqual(CommandOutcome.Success, outcome);
- Assert.AreEqual(_parentObject.transform, _childObject.transform.parent);
- }
- [Test]
- public void Execute_WithNullParent_MovesToRoot()
- {
- // Arrange
- _childObject.transform.SetParent(_parentObject.transform); // Pre-condition
- var command = CreateCommand(_childObject.GetInstanceID().ToString(), null);
- // Act
- var outcome = command.Execute(_context);
- // Assert
- Assert.AreEqual(CommandOutcome.Success, outcome);
- Assert.IsNull(_childObject.transform.parent);
- }
- [Test]
- public void Execute_WithSiblingIndex_SetsOrderCorrectly()
- {
- // Arrange
- // Create another child to verify sibling order
- var otherChild = new GameObject("OtherChild");
- otherChild.transform.SetParent(_parentObject.transform);
-
- var command = CreateCommand(_childObject.GetInstanceID().ToString(), _parentObject.GetInstanceID().ToString(), 0);
- // Act
- var outcome = command.Execute(_context);
- // Assert
- Assert.AreEqual(CommandOutcome.Success, outcome);
- Assert.AreEqual(0, _childObject.transform.GetSiblingIndex());
-
- Object.DestroyImmediate(otherChild);
- }
- [Test]
- public void Execute_WithInvalidChildId_ReturnsError()
- {
- // Arrange
- var command = CreateCommand("invalid_id", _parentObject.GetInstanceID().ToString());
- // Act
- var outcome = command.Execute(_context);
- // Assert
- Assert.AreEqual(CommandOutcome.Error, outcome);
- Assert.IsNotNull(_context.ErrorMessage);
- Assert.IsTrue(_context.ErrorMessage.Contains("Could not find a valid child GameObject"));
- }
- }
- }
|