using NUnit.Framework; using UnityEngine; using System.Collections; using UnityEngine.TestTools; namespace Tests.EditMode { public class BootstrapTests { private Bootstrap.CustomBootSettings settings; private GameObject prefab1; private GameObject prefab2; [SetUp] public void SetUp() { settings = ScriptableObject.CreateInstance(); settings.name = "TestSettings"; prefab1 = GameObject.CreatePrimitive(PrimitiveType.Cube); prefab1.name = "Prefab1"; prefab2 = GameObject.CreatePrimitive(PrimitiveType.Sphere); prefab2.name = "Prefab2"; } [TearDown] public void TearDown() { // Settings will clean up the container and instantiated prefabs settings.Cleanup(); // Clean up the prefab templates if (prefab1 != null) Object.DestroyImmediate(prefab1); if (prefab2 != null) Object.DestroyImmediate(prefab2); if (settings != null) Object.DestroyImmediate(settings); } [Test] public void InitialiseSync_CreatesContainerWithCorrectName() { // Arrange settings.BootPrefabs = new[] { prefab1 }; // Act settings.InitialiseSync(); // Access the RuntimeContainer through reflection since it's private var containerField = typeof(Bootstrap.CustomBootSettings).GetField("RuntimeContainer", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var container = containerField.GetValue(settings) as GameObject; // Assert Assert.IsNotNull(container, "RuntimeContainer should not be null"); Assert.AreEqual($"{settings.name}_Container", container.name, "Container should be named with the pattern {settings.name}_Container"); } [Test] public void InitialiseSync_InstantiatesAllPrefabs() { // Arrange settings.BootPrefabs = new[] { prefab1, prefab2 }; // Act settings.InitialiseSync(); // Access the RuntimeContainer and Instances through reflection var containerField = typeof(Bootstrap.CustomBootSettings).GetField("RuntimeContainer", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var container = containerField.GetValue(settings) as GameObject; var instancesField = typeof(Bootstrap.CustomBootSettings).GetField("Instances", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var instances = instancesField.GetValue(settings) as GameObject[]; // Assert Assert.IsNotNull(container, "RuntimeContainer should not be null"); Assert.AreEqual(2, container.transform.childCount, "Container should have 2 child objects"); Assert.IsNotNull(instances, "Instances array should not be null"); Assert.AreEqual(2, instances.Length, "Instances array should have 2 elements"); Assert.IsNotNull(instances[0], "First instance should not be null"); Assert.IsTrue(instances[0].name.StartsWith(prefab1.name), $"First instance name '{instances[0].name}' should start with prefab name '{prefab1.name}'"); Assert.AreEqual(container.transform, instances[0].transform.parent, "First instance should be a child of the container"); Assert.IsNotNull(instances[1], "Second instance should not be null"); Assert.IsTrue(instances[1].name.StartsWith(prefab2.name), $"Second instance name '{instances[1].name}' should start with prefab name '{prefab2.name}'"); Assert.AreEqual(container.transform, instances[1].transform.parent, "Second instance should be a child of the container"); } [Test] public void InitialiseSync_HandlesNullPrefabs() { // Arrange settings.BootPrefabs = new GameObject[] { prefab1, null, prefab2 }; // Act settings.InitialiseSync(); // Access the RuntimeContainer and Instances through reflection var containerField = typeof(Bootstrap.CustomBootSettings).GetField("RuntimeContainer", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var container = containerField.GetValue(settings) as GameObject; var instancesField = typeof(Bootstrap.CustomBootSettings).GetField("Instances", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var instances = instancesField.GetValue(settings) as GameObject[]; // Assert Assert.IsNotNull(container, "RuntimeContainer should not be null"); Assert.AreEqual(2, container.transform.childCount, "Container should have 2 child objects (skipping the null prefab)"); Assert.AreEqual(3, instances.Length, "Instances array should have 3 elements (including null)"); Assert.IsNotNull(instances[0], "First instance should not be null"); Assert.IsNull(instances[1], "Second instance should be null"); Assert.IsNotNull(instances[2], "Third instance should not be null"); } [Test] public void InitialiseSync_EmptyPrefabsArray_CreatesContainerOnly() { // Arrange settings.BootPrefabs = new GameObject[0]; // Act settings.InitialiseSync(); // Access the RuntimeContainer through reflection var containerField = typeof(Bootstrap.CustomBootSettings).GetField("RuntimeContainer", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var container = containerField.GetValue(settings) as GameObject; var instancesField = typeof(Bootstrap.CustomBootSettings).GetField("Instances", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var instances = instancesField.GetValue(settings) as GameObject[]; // Assert Assert.IsNotNull(container, "RuntimeContainer should be created even with empty prefabs array"); Assert.AreEqual(0, container.transform.childCount, "Container should have no children"); Assert.AreEqual(0, instances.Length, "Instances array should be empty"); } } }