SetHierarchyCommandTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using NUnit.Framework;
  2. using UnityEngine;
  3. using LLM.Editor.Commands;
  4. using LLM.Editor.Data;
  5. using LLM.Editor.Helper;
  6. namespace LLM.Editor.Tests.Unit
  7. {
  8. [TestFixture]
  9. public class SetHierarchyCommandTests
  10. {
  11. private GameObject _childObject;
  12. private GameObject _parentObject;
  13. private CommandContext _context;
  14. private string _childLogicalName;
  15. private string _parentLogicalName;
  16. [SetUp]
  17. public void SetUp()
  18. {
  19. _childObject = new GameObject("ChildObject");
  20. _parentObject = new GameObject("ParentObject");
  21. _context = new CommandContext();
  22. _childLogicalName = "child_1";
  23. _parentLogicalName = "parent_1";
  24. var childPid = new PersistentIdentifier
  25. {
  26. logicalName = _childLogicalName,
  27. instanceID = _childObject.GetInstanceID(),
  28. path = CommandUtility.GetGameObjectPath(_childObject)
  29. };
  30. _context.IdentifierMap[_childLogicalName] = childPid;
  31. var parentPid = new PersistentIdentifier
  32. {
  33. logicalName = _parentLogicalName,
  34. instanceID = _parentObject.GetInstanceID(),
  35. path = CommandUtility.GetGameObjectPath(_parentObject)
  36. };
  37. _context.IdentifierMap[_parentLogicalName] = parentPid;
  38. }
  39. [TearDown]
  40. public void TearDown()
  41. {
  42. if (_childObject != null) Object.DestroyImmediate(_childObject);
  43. if (_parentObject != null) Object.DestroyImmediate(_parentObject);
  44. }
  45. private SetHierarchyCommand CreateCommand(string childId, string parentId, int siblingIndex = -1)
  46. {
  47. var parameters = new SetHierarchyParams
  48. {
  49. childIdentifier = childId,
  50. parentIdentifier = parentId,
  51. siblingIndex = siblingIndex
  52. };
  53. return new SetHierarchyCommand(parameters.ToJson());
  54. }
  55. [Test]
  56. public void Execute_WithValidParent_SetsParentCorrectly()
  57. {
  58. // Arrange
  59. var command = CreateCommand(_childLogicalName, _parentLogicalName);
  60. // Act
  61. var outcome = command.Execute(_context);
  62. // Assert
  63. Assert.AreEqual(CommandOutcome.Success, outcome);
  64. Assert.AreEqual(_parentObject.transform, _childObject.transform.parent);
  65. }
  66. [Test]
  67. public void Execute_WithNullParent_MovesToRoot()
  68. {
  69. // Arrange
  70. _childObject.transform.SetParent(_parentObject.transform); // Pre-condition
  71. var command = CreateCommand(_childLogicalName, null);
  72. // Act
  73. var outcome = command.Execute(_context);
  74. // Assert
  75. Assert.AreEqual(CommandOutcome.Success, outcome);
  76. Assert.IsNull(_childObject.transform.parent);
  77. }
  78. [Test]
  79. public void Execute_WithSiblingIndex_SetsOrderCorrectly()
  80. {
  81. // Arrange
  82. // Create another child to verify sibling order
  83. var otherChild = new GameObject("OtherChild");
  84. otherChild.transform.SetParent(_parentObject.transform);
  85. var command = CreateCommand(_childLogicalName, _parentLogicalName, 0);
  86. // Act
  87. var outcome = command.Execute(_context);
  88. // Assert
  89. Assert.AreEqual(CommandOutcome.Success, outcome);
  90. Assert.AreEqual(0, _childObject.transform.GetSiblingIndex());
  91. Object.DestroyImmediate(otherChild);
  92. }
  93. [Test]
  94. public void Execute_WithInvalidChildId_ReturnsError()
  95. {
  96. // Arrange
  97. var command = CreateCommand("invalid_id", _parentLogicalName);
  98. // Act
  99. var outcome = command.Execute(_context);
  100. // Assert
  101. Assert.AreEqual(CommandOutcome.Error, outcome);
  102. Assert.IsNotNull(_context.ErrorMessage);
  103. Assert.IsTrue(_context.ErrorMessage.Contains("Could not find a valid child GameObject"));
  104. }
  105. }
  106. }