AddComponentToAssetCommandTests.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using NUnit.Framework;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using LLM.Editor.Commands;
  6. using LLM.Editor.Data;
  7. using LLM.Editor.Helper;
  8. namespace LLM.Editor.Tests.Unit
  9. {
  10. public class MyTestCustomComponent : MonoBehaviour {}
  11. [TestFixture]
  12. public class AddComponentToAssetCommandTests
  13. {
  14. private const string TestPrefabPath = "Assets/LLM/Editor/Tests/Prefabs/TempTestPrefab.prefab";
  15. private GameObject _testSceneObject;
  16. private CommandContext _context;
  17. private string _sceneObjectLogicalName;
  18. private string _prefabLogicalName;
  19. [SetUp]
  20. public void SetUp()
  21. {
  22. // Create a scene object for scene-based tests, with a child
  23. _testSceneObject = new GameObject("TestSceneObject");
  24. var sceneChild = new GameObject("SceneChild");
  25. sceneChild.transform.SetParent(_testSceneObject.transform);
  26. // Create a prefab asset for prefab-based tests
  27. var prefabSourceObject = new GameObject("TestPrefab");
  28. var prefabChild = new GameObject("PrefabChild");
  29. prefabChild.transform.SetParent(prefabSourceObject.transform);
  30. Directory.CreateDirectory(Path.GetDirectoryName(TestPrefabPath));
  31. PrefabUtility.SaveAsPrefabAsset(prefabSourceObject, TestPrefabPath);
  32. Object.DestroyImmediate(prefabSourceObject);
  33. // Set up the context with identifiers for both the scene object and the prefab asset
  34. _context = new CommandContext();
  35. _sceneObjectLogicalName = "scene_object_1";
  36. _prefabLogicalName = "prefab_asset_1";
  37. var scenePid = new PersistentIdentifier
  38. {
  39. logicalName = _sceneObjectLogicalName,
  40. instanceID = _testSceneObject.GetInstanceID(),
  41. path = CommandUtility.GetGameObjectPath(_testSceneObject)
  42. };
  43. _context.IdentifierMap[_sceneObjectLogicalName] = scenePid;
  44. var prefabGuid = AssetDatabase.AssetPathToGUID(TestPrefabPath);
  45. var prefabPid = new PersistentIdentifier
  46. {
  47. logicalName = _prefabLogicalName,
  48. guid = prefabGuid
  49. };
  50. _context.IdentifierMap[_prefabLogicalName] = prefabPid;
  51. }
  52. [TearDown]
  53. public void TearDown()
  54. {
  55. if (_testSceneObject != null) Object.DestroyImmediate(_testSceneObject);
  56. AssetDatabase.DeleteAsset(TestPrefabPath);
  57. }
  58. private static AddComponentToAssetCommand CreateCommand(string targetId, string scriptName, string childPath = null)
  59. {
  60. var parameters = new AddComponentParams
  61. {
  62. targetIdentifier = targetId,
  63. scriptName = scriptName,
  64. childPath = childPath
  65. };
  66. return new AddComponentToAssetCommand(parameters.ToJson());
  67. }
  68. [Test]
  69. public void Execute_OnSceneObjectRoot_AddsComponentSuccessfully()
  70. {
  71. // Arrange
  72. var command = CreateCommand(_sceneObjectLogicalName, "UnityEngine.Rigidbody");
  73. // Act
  74. var outcome = command.Execute(_context);
  75. // Assert
  76. Assert.AreEqual(CommandOutcome.Success, outcome);
  77. Assert.IsNotNull(_testSceneObject.GetComponent<Rigidbody>());
  78. }
  79. [Test]
  80. public void Execute_OnSceneObjectWithValidChildPath_AddsComponentToChild()
  81. {
  82. // Arrange
  83. var command = CreateCommand(_sceneObjectLogicalName, "UnityEngine.BoxCollider", "SceneChild");
  84. // Act
  85. var outcome = command.Execute(_context);
  86. // Assert
  87. var childObject = _testSceneObject.transform.Find("SceneChild");
  88. Assert.AreEqual(CommandOutcome.Success, outcome);
  89. Assert.IsNotNull(childObject.GetComponent<BoxCollider>());
  90. Assert.IsNull(_testSceneObject.GetComponent<BoxCollider>()); // Ensure it wasn't added to the parent
  91. }
  92. [Test]
  93. public void Execute_OnPrefabRoot_AddsComponentToRootInAsset()
  94. {
  95. // Arrange
  96. var command = CreateCommand(_prefabLogicalName, "UnityEngine.SphereCollider");
  97. // Act
  98. var outcome = command.Execute(_context);
  99. // Assert
  100. Assert.AreEqual(CommandOutcome.Success, outcome);
  101. var loadedPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(TestPrefabPath);
  102. Assert.IsNotNull(loadedPrefab.GetComponent<SphereCollider>(), "Component should be added to the prefab asset's root.");
  103. }
  104. [Test]
  105. public void Execute_OnPrefabWithValidChildPath_AddsComponentToChildInAsset()
  106. {
  107. // Arrange
  108. var command = CreateCommand(_prefabLogicalName, "UnityEngine.CapsuleCollider", "PrefabChild");
  109. // Act
  110. var outcome = command.Execute(_context);
  111. // Assert
  112. Assert.AreEqual(CommandOutcome.Success, outcome);
  113. var loadedPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(TestPrefabPath);
  114. var childInPrefab = loadedPrefab.transform.Find("PrefabChild");
  115. Assert.IsNotNull(childInPrefab, "Child object should exist in prefab.");
  116. Assert.IsNotNull(childInPrefab.GetComponent<CapsuleCollider>(), "Component should be added to the child within the prefab asset.");
  117. }
  118. [Test]
  119. public void Execute_WithInvalidScriptName_ReturnsError()
  120. {
  121. // Arrange
  122. var command = CreateCommand(_sceneObjectLogicalName, "Invalid.NonExistent.Component");
  123. // Act
  124. var outcome = command.Execute(_context);
  125. // Assert
  126. Assert.AreEqual(CommandOutcome.Error, outcome);
  127. Assert.IsNotNull(_context.ErrorMessage);
  128. Assert.IsTrue(_context.ErrorMessage.Contains("Could not find script type"));
  129. }
  130. [Test]
  131. public void Execute_AddRigidbodyToSceneObject_Successfully()
  132. {
  133. // Arrange
  134. var command = CreateCommand(_sceneObjectLogicalName, "UnityEngine.Rigidbody");
  135. // Act
  136. var outcome = command.Execute(_context);
  137. // Assert
  138. Assert.AreEqual(CommandOutcome.Success, outcome);
  139. Assert.IsNotNull(_testSceneObject.GetComponent<Rigidbody>());
  140. }
  141. [Test]
  142. public void Execute_AddRigidbodyToSceneObjectWithExistingRigidbody_ReturnsError()
  143. {
  144. // Arrange
  145. _testSceneObject.AddComponent<Rigidbody>();
  146. var command = CreateCommand(_sceneObjectLogicalName, "UnityEngine.Rigidbody");
  147. // Act
  148. var outcome = command.Execute(_context);
  149. // Assert
  150. Assert.AreEqual(CommandOutcome.Error, outcome);
  151. Assert.IsNotNull(_context.ErrorMessage);
  152. Assert.IsTrue(_context.ErrorMessage.Contains("Failed to add component 'UnityEngine.Rigidbody'"));
  153. }
  154. }
  155. }