RenameGameObjectCommandTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using UnityEngine;
  2. using NUnit.Framework;
  3. using LLM.Editor.Commands;
  4. using LLM.Editor.Data;
  5. using LLM.Editor.Helper;
  6. using UnityEngine.TestTools; // Required for LogAssert
  7. namespace LLM.Editor.Tests.Unit
  8. {
  9. [TestFixture]
  10. public class RenameGameObjectCommandTests
  11. {
  12. private GameObject _testObject;
  13. private CommandContext _context;
  14. [SetUp]
  15. public void SetUp()
  16. {
  17. // Create a new GameObject before each test
  18. _testObject = new GameObject("TestObject");
  19. // Create a fresh context for each test
  20. _context = new CommandContext();
  21. }
  22. [TearDown]
  23. public void TearDown()
  24. {
  25. // Clean up the GameObject after each test
  26. if (_testObject != null)
  27. {
  28. Object.DestroyImmediate(_testObject);
  29. }
  30. }
  31. [Test]
  32. public void Execute_WithValidParams_RenamesObjectSuccessfully()
  33. {
  34. // Arrange
  35. var parameters = new RenameGameObjectParams
  36. {
  37. targetIdentifier = _testObject.GetInstanceID(),
  38. newName = "RenamedObject"
  39. };
  40. var command = new RenameGameObjectCommand(parameters.ToJson());
  41. // Act
  42. var outcome = command.Execute(_context);
  43. // Assert
  44. Assert.AreEqual(CommandOutcome.Success, outcome, "Command should execute successfully.");
  45. Assert.AreEqual("RenamedObject", _testObject.name, "GameObject name should be updated.");
  46. Assert.IsNull(_context.ErrorMessage, "Error message should be null on success.");
  47. }
  48. [Test]
  49. public void Execute_WithNonExistentTarget_ReturnsError()
  50. {
  51. // Arrange
  52. var nonExistentId = -12345;
  53. var parameters = new RenameGameObjectParams
  54. {
  55. targetIdentifier = nonExistentId,
  56. newName = "WontBeUsed"
  57. };
  58. var command = new RenameGameObjectCommand(parameters.ToJson());
  59. // We expect a specific error log message. This tells the test runner that the log is part of the test.
  60. LogAssert.Expect(LogType.Error, $"[RenameGameObjectCommand] Could not find GameObject with ID: {nonExistentId}");
  61. // Act
  62. var outcome = command.Execute(_context);
  63. // Assert
  64. Assert.AreEqual(CommandOutcome.Error, outcome, "Command should return an error.");
  65. Assert.IsNotNull(_context.ErrorMessage, "Error message should be set on failure.");
  66. Assert.IsTrue(_context.ErrorMessage.Contains($"Could not find GameObject with ID: {nonExistentId}"));
  67. }
  68. [Test]
  69. public void Execute_WithEmptyNewName_ReturnsError()
  70. {
  71. // Arrange
  72. var parameters = new RenameGameObjectParams
  73. {
  74. targetIdentifier = _testObject.GetInstanceID(),
  75. newName = "" // Invalid empty name
  76. };
  77. var command = new RenameGameObjectCommand(parameters.ToJson());
  78. // We expect a specific error log message.
  79. LogAssert.Expect(LogType.Error, "[RenameGameObjectCommand] Invalid parameters: targetIdentifier and newName are required.");
  80. // Act
  81. var outcome = command.Execute(_context);
  82. // Assert
  83. Assert.AreEqual(CommandOutcome.Error, outcome, "Command should return an error for empty name.");
  84. Assert.AreEqual("TestObject", _testObject.name, "GameObject name should not be changed.");
  85. Assert.IsNotNull(_context.ErrorMessage, "Error message should be set for invalid parameters.");
  86. Assert.IsTrue(_context.ErrorMessage.Contains("Invalid parameters"));
  87. }
  88. [Test]
  89. public void Execute_WithNullJson_ReturnsError()
  90. {
  91. // Arrange
  92. var command = new RenameGameObjectCommand(null);
  93. // We expect a specific error log message.
  94. LogAssert.Expect(LogType.Error, "[RenameGameObjectCommand] Invalid parameters: targetIdentifier and newName are required.");
  95. // Act
  96. var outcome = command.Execute(_context);
  97. // Assert
  98. Assert.AreEqual(CommandOutcome.Error, outcome, "Command should return an error for null JSON.");
  99. Assert.IsNotNull(_context.ErrorMessage, "Error message should be set for invalid parameters.");
  100. Assert.IsTrue(_context.ErrorMessage.Contains("Invalid parameters"));
  101. }
  102. }
  103. }