InstantiatePrefabCommandTests.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. [TestFixture]
  11. public class InstantiatePrefabCommandTests
  12. {
  13. private const string TestPrefabName = "TempTestPrefab.prefab";
  14. private string _testPrefabPath;
  15. private GameObject _prefabObject;
  16. private CommandContext _context;
  17. private GameObject _instantiatedObject;
  18. [SetUp]
  19. public void SetUp()
  20. {
  21. _context = new CommandContext();
  22. // Create a dummy prefab asset
  23. _testPrefabPath = Path.Combine("Assets", TestPrefabName);
  24. var sourceObject = new GameObject("MyPrefab");
  25. _prefabObject = PrefabUtility.SaveAsPrefabAsset(sourceObject, _testPrefabPath);
  26. Object.DestroyImmediate(sourceObject);
  27. Assert.IsNotNull(_prefabObject, "Setup failed: Could not create test prefab.");
  28. // Register the prefab asset with the context
  29. var guid = AssetDatabase.AssetPathToGUID(_testPrefabPath);
  30. _context.IdentifierMap["my_prefab_1"] = guid;
  31. }
  32. [TearDown]
  33. public void TearDown()
  34. {
  35. // Clean up the instantiated object and the prefab asset
  36. if (_instantiatedObject != null)
  37. {
  38. Object.DestroyImmediate(_instantiatedObject);
  39. }
  40. if (File.Exists(_testPrefabPath))
  41. {
  42. AssetDatabase.DeleteAsset(_testPrefabPath);
  43. }
  44. }
  45. [Test]
  46. public void Execute_WithValidPrefabId_InstantiatesObjectAndCreatesNewLogicalName()
  47. {
  48. // Arrange
  49. var parameters = new InstantiatePrefabParams { prefabIdentifier = "my_prefab_1" };
  50. var command = new InstantiatePrefabCommand(parameters.ToJson());
  51. // Act
  52. var outcome = command.Execute(_context);
  53. // Assert
  54. Assert.AreEqual(CommandOutcome.Success, outcome);
  55. // Check that a *new* logical name for the instance was created
  56. Assert.IsNotNull(_context.CurrentSubject);
  57. var newLogicalName = (string)_context.CurrentSubject;
  58. Assert.AreNotEqual("my_prefab_1", newLogicalName);
  59. Assert.IsTrue(newLogicalName.StartsWith("my_prefab_1_instance_"));
  60. Assert.IsTrue(_context.IdentifierMap.ContainsKey(newLogicalName));
  61. // Find the instantiated object using the path registered by the command itself
  62. var instancePath = _context.IdentifierMap[newLogicalName];
  63. _instantiatedObject = GameObject.Find(instancePath);
  64. Assert.IsNotNull(_instantiatedObject, "Prefab should be instantiated and found via its registered path.");
  65. }
  66. [Test]
  67. public void Execute_WithInvalidPrefabId_ReturnsError()
  68. {
  69. // Arrange
  70. var parameters = new InstantiatePrefabParams { prefabIdentifier = "invalid_prefab_id" };
  71. var command = new InstantiatePrefabCommand(parameters.ToJson());
  72. // Act
  73. var outcome = command.Execute(_context);
  74. // Assert
  75. Assert.AreEqual(CommandOutcome.Error, outcome);
  76. Assert.IsNotNull(_context.ErrorMessage);
  77. Assert.IsTrue(_context.ErrorMessage.Contains("Could not resolve prefab"));
  78. }
  79. }
  80. }