GetDataFromPathProviderTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using NUnit.Framework;
  2. using UnityEngine;
  3. using LLM.Editor.Analysis;
  4. using LLM.Editor.Helper;
  5. using System.Collections.Generic;
  6. namespace LLM.Editor.Tests.Unit
  7. {
  8. public class GetDataTestComponent : MonoBehaviour
  9. {
  10. public string testField = "Hello";
  11. }
  12. [TestFixture]
  13. public class GetDataFromPathProviderTests
  14. {
  15. private GameObject _testObject;
  16. private GetDataFromPathProvider _provider;
  17. [SetUp]
  18. public void SetUp()
  19. {
  20. _provider = new GetDataFromPathProvider();
  21. _testObject = new GameObject("TestObject");
  22. _testObject.AddComponent<GetDataTestComponent>();
  23. }
  24. [TearDown]
  25. public void TearDown()
  26. {
  27. if (_testObject != null)
  28. {
  29. Object.DestroyImmediate(_testObject);
  30. }
  31. }
  32. private string CreatePathRequestJson(params PathStep[] steps)
  33. {
  34. var request = new PathRequest { steps = new List<PathStep>(steps) };
  35. return request.ToJson();
  36. }
  37. [Test]
  38. public void GetContext_ForSimpleProperty_ReturnsValue()
  39. {
  40. // Arrange
  41. _testObject.transform.position = new Vector3(1, 2, 3);
  42. var pathJson = CreatePathRequestJson(
  43. new PathStep { type = "component", name = "UnityEngine.Transform" },
  44. new PathStep { type = "property", name = "position" }
  45. );
  46. // Act
  47. var context = _provider.GetContext(_testObject, pathJson);
  48. // Assert
  49. Assert.IsInstanceOf<Vector3>(context);
  50. Assert.AreEqual(new Vector3(1, 2, 3), (Vector3)context);
  51. }
  52. [Test]
  53. public void GetContext_ForCustomField_ReturnsValue()
  54. {
  55. // Arrange
  56. var pathJson = CreatePathRequestJson(
  57. new PathStep { type = "component", name = "LLM.Editor.Tests.Unit.GetDataTestComponent" },
  58. new PathStep { type = "field", name = "testField" }
  59. );
  60. // Act
  61. var context = _provider.GetContext(_testObject, pathJson);
  62. // Assert
  63. Assert.IsInstanceOf<string>(context);
  64. Assert.AreEqual("Hello", (string)context);
  65. }
  66. [Test]
  67. public void GetContext_ForChainedPath_ReturnsChildComponent()
  68. {
  69. // Arrange
  70. var childObject = new GameObject("Child");
  71. childObject.transform.SetParent(_testObject.transform);
  72. childObject.AddComponent<BoxCollider>();
  73. var pathJson = CreatePathRequestJson(
  74. new PathStep { type = "child", name = "Child" },
  75. new PathStep { type = "component", name = "UnityEngine.BoxCollider" }
  76. );
  77. // Act
  78. var context = _provider.GetContext(_testObject, pathJson);
  79. // Assert
  80. // The provider returns a dictionary when the final object is a component
  81. Assert.IsInstanceOf<Dictionary<string, object>>(context);
  82. var data = (Dictionary<string, object>)context;
  83. Assert.IsInstanceOf<BoxCollider>(data["requestedComponentData"]);
  84. }
  85. [Test]
  86. public void GetContext_WithInvalidPath_ReturnsError()
  87. {
  88. // Arrange
  89. var pathJson = CreatePathRequestJson(
  90. new PathStep { type = "child", name = "NonExistentChild" },
  91. new PathStep { type = "component", name = "UnityEngine.BoxCollider" }
  92. );
  93. // Act
  94. var context = _provider.GetContext(_testObject, pathJson);
  95. // Assert
  96. Assert.IsInstanceOf<string>(context);
  97. Assert.IsTrue(((string)context).Contains("Path evaluation failed at a null object"));
  98. }
  99. }
  100. }