SetComponentValueCommandTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using NUnit.Framework;
  2. using UnityEngine;
  3. using LLM.Editor.Commands;
  4. using LLM.Editor.Data;
  5. using LLM.Editor.Core;
  6. using Newtonsoft.Json.Linq;
  7. namespace LLM.Editor.Tests.Unit
  8. {
  9. // A helper component with various data types to test against.
  10. public class TestComponent : MonoBehaviour
  11. {
  12. public string stringValue;
  13. public int intValue;
  14. public float floatValue;
  15. public bool boolValue;
  16. public Vector3 vector3Value;
  17. }
  18. [TestFixture]
  19. public class SetComponentValueCommandTests
  20. {
  21. private GameObject _testObject;
  22. private TestComponent _testComponent;
  23. private CommandContext _context;
  24. [SetUp]
  25. public void SetUp()
  26. {
  27. _testObject = new GameObject("TestObject");
  28. _testComponent = _testObject.AddComponent<TestComponent>();
  29. _context = new CommandContext();
  30. // The most direct way to identify a scene object for testing is its instance ID.
  31. // We will use this as the "logical name" for the scope of these tests.
  32. var instanceId = _testObject.GetInstanceID().ToString();
  33. _context.IdentifierMap[instanceId] = instanceId;
  34. }
  35. [TearDown]
  36. public void TearDown()
  37. {
  38. if (_testObject != null)
  39. {
  40. Object.DestroyImmediate(_testObject);
  41. }
  42. }
  43. // A helper to create the command with proper JSON structure
  44. private SetComponentValueCommand CreateCommand(string memberName, JToken value)
  45. {
  46. var json = new JObject
  47. {
  48. ["subjectIdentifier"] = _testObject.GetInstanceID().ToString(),
  49. ["componentName"] = "LLM.Editor.Tests.Unit.TestComponent",
  50. ["memberName"] = memberName,
  51. ["value"] = value
  52. }.ToString();
  53. return new SetComponentValueCommand(json);
  54. }
  55. [Test]
  56. public void Execute_SetsStringValue_Successfully()
  57. {
  58. // Arrange
  59. var command = CreateCommand("stringValue", "Hello World");
  60. // Act
  61. var outcome = command.Execute(_context);
  62. // Assert
  63. Assert.AreEqual(CommandOutcome.Success, outcome);
  64. Assert.AreEqual("Hello World", _testComponent.stringValue);
  65. }
  66. [Test]
  67. public void Execute_SetsIntValue_Successfully()
  68. {
  69. // Arrange
  70. var command = CreateCommand("intValue", 42);
  71. // Act
  72. var outcome = command.Execute(_context);
  73. // Assert
  74. Assert.AreEqual(CommandOutcome.Success, outcome);
  75. Assert.AreEqual(42, _testComponent.intValue);
  76. }
  77. [Test]
  78. public void Execute_SetsFloatValue_Successfully()
  79. {
  80. // Arrange
  81. var command = CreateCommand("floatValue", 3.14f);
  82. // Act
  83. var outcome = command.Execute(_context);
  84. // Assert
  85. Assert.AreEqual(CommandOutcome.Success, outcome);
  86. Assert.AreEqual(3.14f, _testComponent.floatValue);
  87. }
  88. [Test]
  89. public void Execute_SetsBoolValue_Successfully()
  90. {
  91. // Arrange
  92. var command = CreateCommand("boolValue", true);
  93. // Act
  94. var outcome = command.Execute(_context);
  95. // Assert
  96. Assert.AreEqual(CommandOutcome.Success, outcome);
  97. Assert.IsTrue(_testComponent.boolValue);
  98. }
  99. [Test]
  100. public void Execute_SetsVector3Value_Successfully()
  101. {
  102. // Arrange
  103. var vec3Json = new JObject { ["x"] = 1, ["y"] = 2, ["z"] = 3 };
  104. var command = CreateCommand("vector3Value", vec3Json);
  105. // Act
  106. var outcome = command.Execute(_context);
  107. // Assert
  108. Assert.AreEqual(CommandOutcome.Success, outcome);
  109. Assert.AreEqual(new Vector3(1, 2, 3), _testComponent.vector3Value);
  110. }
  111. [Test]
  112. public void Execute_WithInvalidMemberName_ReturnsError()
  113. {
  114. // Arrange
  115. var command = CreateCommand("invalidMember", "someValue");
  116. // Act
  117. var outcome = command.Execute(_context);
  118. // Assert
  119. Assert.AreEqual(CommandOutcome.Error, outcome);
  120. Assert.IsNotNull(_context.ErrorMessage);
  121. Assert.IsTrue(_context.ErrorMessage.Contains("Could not find a writable serialized field or public property"));
  122. }
  123. }
  124. }