SetComponentValueCommandTests.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. public class TestComponent : MonoBehaviour
  10. {
  11. public string stringValue;
  12. public int intValue;
  13. public float floatValue;
  14. public bool boolValue;
  15. public Vector3 vector3Value;
  16. }
  17. [TestFixture]
  18. public class SetComponentValueCommandTests
  19. {
  20. private GameObject _testObject;
  21. private TestComponent _testComponent;
  22. private CommandContext _context;
  23. private string _logicalName;
  24. [SetUp]
  25. public void SetUp()
  26. {
  27. _testObject = new GameObject("TestObject");
  28. _testComponent = _testObject.AddComponent<TestComponent>();
  29. _testObject.AddComponent<Rigidbody>();
  30. _context = new CommandContext();
  31. _logicalName = "test_object_1";
  32. var pid = new PersistentIdentifier
  33. {
  34. logicalName = _logicalName,
  35. instanceID = _testObject.GetInstanceID(),
  36. path = CommandUtility.GetGameObjectPath(_testObject)
  37. };
  38. _context.IdentifierMap[_logicalName] = pid;
  39. }
  40. [TearDown]
  41. public void TearDown()
  42. {
  43. if (_testObject != null)
  44. {
  45. Object.DestroyImmediate(_testObject);
  46. }
  47. }
  48. private SetComponentValueCommand CreateCommand(string componentName, JArray members)
  49. {
  50. var json = new JObject
  51. {
  52. ["subjectIdentifier"] = _logicalName,
  53. ["componentName"] = componentName,
  54. ["members"] = members
  55. }.ToString();
  56. return new SetComponentValueCommand(json);
  57. }
  58. [Test]
  59. public void Execute_SetsStringValue_Successfully()
  60. {
  61. var members = new JArray { new JObject { ["memberName"] = "stringValue", ["value"] = "Hello World" } };
  62. var command = CreateCommand("LLM.Editor.Tests.Unit.TestComponent", members);
  63. var outcome = command.Execute(_context);
  64. Assert.AreEqual(CommandOutcome.Success, outcome);
  65. Assert.AreEqual("Hello World", _testComponent.stringValue);
  66. }
  67. [Test]
  68. public void Execute_SetsIntValue_Successfully()
  69. {
  70. var members = new JArray { new JObject { ["memberName"] = "intValue", ["value"] = 42 } };
  71. var command = CreateCommand("LLM.Editor.Tests.Unit.TestComponent", members);
  72. var outcome = command.Execute(_context);
  73. Assert.AreEqual(CommandOutcome.Success, outcome);
  74. Assert.AreEqual(42, _testComponent.intValue);
  75. }
  76. [Test]
  77. public void Execute_SetsFloatValue_Successfully()
  78. {
  79. var members = new JArray { new JObject { ["memberName"] = "floatValue", ["value"] = 3.14f } };
  80. var command = CreateCommand("LLM.Editor.Tests.Unit.TestComponent", members);
  81. var outcome = command.Execute(_context);
  82. Assert.AreEqual(CommandOutcome.Success, outcome);
  83. Assert.AreEqual(3.14f, _testComponent.floatValue);
  84. }
  85. [Test]
  86. public void Execute_SetsBoolValue_Successfully()
  87. {
  88. var members = new JArray { new JObject { ["memberName"] = "boolValue", ["value"] = true } };
  89. var command = CreateCommand("LLM.Editor.Tests.Unit.TestComponent", members);
  90. var outcome = command.Execute(_context);
  91. Assert.AreEqual(CommandOutcome.Success, outcome);
  92. Assert.IsTrue(_testComponent.boolValue);
  93. }
  94. [Test]
  95. public void Execute_SetsVector3Value_Successfully()
  96. {
  97. var vec3Json = new JObject { ["x"] = 1, ["y"] = 2, ["z"] = 3 };
  98. var members = new JArray { new JObject { ["memberName"] = "vector3Value", ["value"] = vec3Json } };
  99. var command = CreateCommand("LLM.Editor.Tests.Unit.TestComponent", members);
  100. var outcome = command.Execute(_context);
  101. Assert.AreEqual(CommandOutcome.Success, outcome);
  102. Assert.AreEqual(new Vector3(1, 2, 3), _testComponent.vector3Value);
  103. }
  104. [Test]
  105. public void Execute_WithInvalidMemberName_ReturnsError()
  106. {
  107. var members = new JArray { new JObject { ["memberName"] = "invalidMember", ["value"] = "someValue" } };
  108. var command = CreateCommand("LLM.Editor.Tests.Unit.TestComponent", members);
  109. var outcome = command.Execute(_context);
  110. Assert.AreEqual(CommandOutcome.Error, outcome);
  111. Assert.IsNotNull(_context.ErrorMessage);
  112. Assert.IsTrue(_context.ErrorMessage.Contains("Could not find a writable serialized field or public property"));
  113. }
  114. [Test]
  115. public void Execute_SetTransformLocalPositionValue_Successfully()
  116. {
  117. var members = new JArray { new JObject { ["memberName"] = "position", ["value"] = new JObject { ["x"] = 1, ["y"] = 2, ["z"] = 3 } } };
  118. var command = CreateCommand("UnityEngine.Transform", members);
  119. var outcome = command.Execute(_context);
  120. Assert.AreEqual(CommandOutcome.Success, outcome);
  121. Assert.AreEqual(new Vector3(1, 2, 3), _testComponent.transform.position);
  122. }
  123. [Test]
  124. public void Execute_SetTransformLocalRotationValue_Successfully()
  125. {
  126. var members = new JArray { new JObject { ["memberName"] = "eulerAngles", ["value"] = new JObject { ["x"] = 1, ["y"] = 2, ["z"] = 3 } } };
  127. var command = CreateCommand("UnityEngine.Transform", members);
  128. var outcome = command.Execute(_context);
  129. Assert.AreEqual(CommandOutcome.Success, outcome);
  130. Assert.IsTrue(IsSame(_testComponent.transform.eulerAngles, new Vector3(1, 2, 3)));
  131. bool IsSame(Vector3 a, Vector3 b) => Mathf.Approximately(a.x, b.x) && Mathf.Approximately(a.y, b.y) && Mathf.Approximately(a.z, b.z);
  132. }
  133. [Test]
  134. public void Execute_SetTransformLocalScaleValue_Successfully()
  135. {
  136. var members = new JArray { new JObject { ["memberName"] = "localScale", ["value"] = new JObject { ["x"] = 1, ["y"] = 2, ["z"] = 3 } } };
  137. var command = CreateCommand("UnityEngine.Transform", members);
  138. var outcome = command.Execute(_context);
  139. Assert.AreEqual(CommandOutcome.Success, outcome);
  140. Assert.AreEqual(new Vector3(1, 2, 3), _testComponent.transform.localScale);
  141. }
  142. [Test]
  143. public void Execute_DisableBoxCollider_Successfully()
  144. {
  145. if (!_testComponent.TryGetComponent(out BoxCollider boxCollider))
  146. {
  147. boxCollider = _testObject.AddComponent<BoxCollider>();
  148. }
  149. var members = new JArray { new JObject { ["memberName"] = "enabled", ["value"] = false } };
  150. var command = CreateCommand("UnityEngine.BoxCollider", members);
  151. var outcome = command.Execute(_context);
  152. Assert.AreEqual(CommandOutcome.Success, outcome);
  153. Assert.IsFalse(boxCollider.enabled);
  154. }
  155. [Test]
  156. public void Execute_SetsMultipleValues_Successfully()
  157. {
  158. var members = new JArray
  159. {
  160. new JObject { ["memberName"] = "stringValue", ["value"] = "Multi-set" },
  161. new JObject { ["memberName"] = "intValue", ["value"] = 123 },
  162. new JObject { ["memberName"] = "boolValue", ["value"] = true }
  163. };
  164. var command = CreateCommand("LLM.Editor.Tests.Unit.TestComponent", members);
  165. var outcome = command.Execute(_context);
  166. Assert.AreEqual(CommandOutcome.Success, outcome);
  167. Assert.AreEqual("Multi-set", _testComponent.stringValue);
  168. Assert.AreEqual(123, _testComponent.intValue);
  169. Assert.IsTrue(_testComponent.boolValue);
  170. }
  171. [Test]
  172. public void Execute_SetsMultipleTransformValues_Successfully()
  173. {
  174. var members = new JArray
  175. {
  176. new JObject
  177. {
  178. ["memberName"] = "position",
  179. ["value"] = new JObject { ["x"] = 5, ["y"] = 6, ["z"] = 7 }
  180. },
  181. new JObject
  182. {
  183. ["memberName"] = "localScale",
  184. ["value"] = new JObject { ["x"] = 2, ["y"] = 2, ["z"] = 2 }
  185. }
  186. };
  187. var command = CreateCommand("UnityEngine.Transform", members);
  188. var outcome = command.Execute(_context);
  189. Assert.AreEqual(CommandOutcome.Success, outcome);
  190. Assert.AreEqual(new Vector3(5, 6, 7), _testObject.transform.position);
  191. Assert.AreEqual(new Vector3(2, 2, 2), _testObject.transform.localScale);
  192. }
  193. }
  194. }