diff --git a/Runtime/GameObjectAdapter.cs b/Runtime/GameObjectAdapter.cs new file mode 100644 index 0000000..ff295d5 --- /dev/null +++ b/Runtime/GameObjectAdapter.cs @@ -0,0 +1,292 @@ +using System; +using System.Collections.Generic; +using Unity.Collections; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace SUL.Adapters { + +public interface IGameObjectAdapter : IObjectAdapter { + public bool activeInHierachy { get;} + public bool activeSelf { get;} + public bool isStatic { get; set; } + public int layer { get; set; } + public UnityEngine.SceneManagement.Scene scene { get; } + public ulong sceneCullingMask { get; } + public string tag { get; set; } + public Transform transform { get; } + + public Component AddComponent(Type componentType); + public void BroadCastMessage(string message, object parameter = null, SendMessageOptions options = SendMessageOptions.RequireReceiver); + public bool CompareTag(string tag); + public T GetComponent(); + public Component GetComponent(Type type); + public Component GetComponent(string type); + public T GetComponentInChildren(bool includeInactive = false); + public Component GetComponentInChildren(Type type); + public Component GetComponentInChildren(Type type, bool includeInactive); + public T GetComponentInParent(bool includeInactive= false); + public Component GetComponentInParent(Type type); + public Component GetComponentInParent(Type type, bool includeInactive); + public T[] GetComponents(); + public void GetComponents(List results); + public Component[] GetComponents(Type type); + public void GetComponents(Type type, List results); + public T[] GetComponentsInChildren(); + public T[] GetComponentsInChildren(bool includeInactive); + public void GetComponentsInChildren(List results); + public void GetComponentsInChildren(bool includeInactive, List results); + public Component[] GetComponentsInChildren (Type type, bool includeInactive= false); + public T[] GetComponentsInParent(); + public T[] GetComponentsInParent(bool includeInactive); + public void GetComponentsInParent(bool includeInactive, List results); + public Component[] GetComponentsInParent(Type type, bool includeInactive= false); + public void SendMessage(string methodName, object value= null, SendMessageOptions options= SendMessageOptions.RequireReceiver); + public void SendMessageUpwards(string methodName, object value= null, SendMessageOptions options= SendMessageOptions.RequireReceiver); + public void SetActive(bool value); + public bool TryGetComponent(out T component); + public bool TryGetComponent(Type type, out Component component); + + public IGameObjectAdapter CreatePrimitive(PrimitiveType type); + public IGameObjectAdapter Find(string name); + public IGameObjectAdapter[] FindGameObjectsWithTag(string tag); + public IGameObjectAdapter FindWithTag(string tag); + public UnityEngine.SceneManagement.Scene GetScene(int instanceID); + public void InstantiateGameObjects(int sourceInstanceID, int count, NativeArray newInstanceIDs, NativeArray newTransformInstanceIDs, + Scene destinationScene = default(Scene)); + public void SetGameObjectsActive(NativeArray instanceIDs, bool active); + public void SetGameObjectsActive(ReadOnlySpan instanceIDs, bool active); +} + +public class GameObjectAdapter : IGameObjectAdapter { + private GameObject adaptee; + + public GameObjectAdapter(GameObject adaptee) + => this.adaptee = adaptee; + + public bool activeInHierachy => adaptee.activeInHierarchy; + + public bool activeSelf => adaptee.activeSelf; + + public bool isStatic { get => adaptee.isStatic; set => adaptee.isStatic = value; } + public int layer { get => adaptee.layer; set => adaptee.layer = value; } + + public Scene scene => adaptee.scene; + + public ulong sceneCullingMask => adaptee.sceneCullingMask; + + public string tag { get => adaptee.tag; set => adaptee.tag = value; } + + public Transform transform => adaptee.transform; + + + public GameObjectAdapter() => this.adaptee = new GameObject(); + + public GameObjectAdapter(string name) => this.adaptee = new GameObject(name); + + public GameObjectAdapter(string name, params Type[] components) => this.adaptee = new GameObject(name, components); + + + public string name { get => adaptee.name; set => adaptee.name = value; } + public HideFlags hideFlags { get => adaptee.hideFlags; set => adaptee.hideFlags = value; } + + public void Destroy(IObjectAdapter objAdapter, float t = 0) { + if (objAdapter is GameObjectAdapter gameObjAdapter) + UnityEngine.Object.Destroy(gameObjAdapter.adaptee, t); + } + + public void DestroyImmediate(IObjectAdapter objAdapter, bool allowDestroyingAssets = false) { + if (objAdapter is GameObjectAdapter gameObjAdapter) + UnityEngine.Object.DestroyImmediate(gameObjAdapter.adaptee, allowDestroyingAssets); + } + + public void DontDestroyOnLoad(IObjectAdapter target) { + if (target is GameObjectAdapter gameObjAdapter) + UnityEngine.Object.DontDestroyOnLoad(gameObjAdapter.adaptee); + } + + public T FindObjectOfType() where T : UnityEngine.Object + => UnityEngine.Object.FindObjectOfType(); + + public T FindObjectOfType(bool includeInactive) where T : UnityEngine.Object + => UnityEngine.Object.FindObjectOfType(includeInactive); + + public IObjectAdapter FindObjectOfType(Type type) { + var obj = UnityEngine.Object.FindObjectOfType(type); + return obj is GameObject gameObj ? new GameObjectAdapter(gameObj) : null; + } + + public IObjectAdapter FindObjectOfType(Type type, bool includeInactive) { + var obj = UnityEngine.Object.FindObjectOfType(type, includeInactive); + return obj is GameObject gmaeObj ? new GameObjectAdapter(gmaeObj) : null; + } + + public IObjectAdapter[] FindObjectsOfType(Type type) { + var objs = UnityEngine.Object.FindObjectsOfType(type); + return ConvertToAdapters(objs); + } + + public IObjectAdapter[] FindObjectsOfType(Type type, bool includeInactive) { + var objs = UnityEngine.Object.FindObjectsOfType(type, includeInactive); + return ConvertToAdapters(objs); + } + + public T[] FindObjectsOfType(bool includeInactive) where T : UnityEngine.Object + => UnityEngine.Object.FindObjectsOfType(includeInactive); + + + public T[] FindObjectsOfType() where T : UnityEngine.Object + => UnityEngine.Object.FindObjectsOfType(); + + public int GetInstanceID() => adaptee.GetInstanceID(); + + public IObjectAdapter Instantiate(IObjectAdapter original) + => InstantiateInternal(original, null, false); + + public IObjectAdapter Instantiate(IObjectAdapter original, Transform parent) + => InstantiateInternal(original, parent, false); + + public IObjectAdapter Instantiate(IObjectAdapter original, Transform parent, bool instantiateInWorldSpace) + => InstantiateInternal(original, parent, instantiateInWorldSpace); + + public IObjectAdapter Instantiate(IObjectAdapter original, Vector3 position, Quaternion rotation) + => InstantiateInternal(original, null, false, position, rotation); + + public IObjectAdapter Instantiate(IObjectAdapter original, Vector3 position, Quaternion rotation, Transform parent) + => InstantiateInternal(original, parent, false, position, rotation); + + public Component AddComponent(Type componentType) => adaptee.AddComponent(componentType); + + public void BroadCastMessage(string methodName, object parameter = null, + SendMessageOptions options = SendMessageOptions.RequireReceiver) + => adaptee.BroadcastMessage(methodName, parameter, options); + + public bool CompareTag(string tag) => adaptee.CompareTag(tag); + + public T GetComponent() => adaptee.GetComponent(); + + public Component GetComponent(Type type) => adaptee.GetComponent(type); + + public Component GetComponent(string type) => adaptee.GetComponent(type); + + public T GetComponentInChildren(bool includeInactive = false) + => adaptee.GetComponentInChildren(includeInactive); + + public Component GetComponentInChildren(Type type) + => adaptee.GetComponentInChildren(type); + + public Component GetComponentInChildren(Type type, bool includeInactive) + => adaptee.GetComponentInChildren(type, includeInactive); + + public T GetComponentInParent(bool includeInactive = false) + => adaptee.GetComponentInParent(includeInactive); + + public Component GetComponentInParent(Type type) + => adaptee.GetComponentInParent(type); + + public Component GetComponentInParent(Type type, bool includeInactive) + => adaptee.GetComponentInParent(type, includeInactive); + + public T[] GetComponents() => adaptee.GetComponents(); + + public void GetComponents(List results) => adaptee.GetComponents(results); + + public Component[] GetComponents(Type type) => adaptee.GetComponents(type); + + public void GetComponents(Type type, List results) => adaptee.GetComponents(type, results); + + public T[] GetComponentsInChildren() => adaptee.GetComponentsInChildren(); + + public T[] GetComponentsInChildren(bool includeInactive) => adaptee.GetComponentsInChildren(includeInactive); + + public void GetComponentsInChildren(List results) => adaptee.GetComponentsInChildren(results); + + public void GetComponentsInChildren(bool includeInactive, List results) + => adaptee.GetComponentsInChildren(includeInactive, results); + + public Component[] GetComponentsInChildren(Type type, bool includeInactive = false) + => adaptee.GetComponentsInChildren(type, includeInactive); + + public T[] GetComponentsInParent() => adaptee.GetComponentsInParent(); + + public T[] GetComponentsInParent(bool includeInactive) => adaptee.GetComponentsInParent(includeInactive); + + public void GetComponentsInParent(bool includeInactive, List results) + => adaptee.GetComponentsInParent(includeInactive, results); + + public Component[] GetComponentsInParent(Type type, bool includeInactive = false) + => adaptee.GetComponentsInParent(type, includeInactive); + + public void SendMessage(string methodName, object value = null, + SendMessageOptions options = SendMessageOptions.RequireReceiver) + => adaptee.SendMessage(methodName, value, options); + + public void SendMessageUpwards(string methodName, object value = null, + SendMessageOptions options = SendMessageOptions.RequireReceiver) + => adaptee.SendMessageUpwards(methodName, value, options); + + public void SetActive(bool value) => adaptee.SetActive(value); + + public bool TryGetComponent(out T component) => adaptee.TryGetComponent(out component); + + public bool TryGetComponent(Type type, out Component component) => adaptee.TryGetComponent(type, out component); + + public IGameObjectAdapter CreatePrimitive(PrimitiveType type) + => new GameObjectAdapter(GameObject.CreatePrimitive(type)); + + public IGameObjectAdapter Find(string name) { + var gameObj = GameObject.Find(name); + return gameObj != null ? new GameObjectAdapter(gameObj) : null; + } + + public IGameObjectAdapter[] FindGameObjectsWithTag(string tag) { + var gameObjs = GameObject.FindGameObjectsWithTag(tag); + return Array.ConvertAll(gameObjs, gameObj => new GameObjectAdapter(gameObj) as IGameObjectAdapter); + } + + public IGameObjectAdapter FindWithTag(string tag) { + var gameObj = GameObject.FindWithTag(tag); + return gameObj != null ? new GameObjectAdapter(gameObj) : null; + } + + public Scene GetScene(int instanceID) => GameObject.GetScene(instanceID); + + public void InstantiateGameObjects(int sourceInstanceID, int count, NativeArray newInstanceIDs, + NativeArray newTransformInstanceIDs, Scene destinationScene = default) + => GameObject.InstantiateGameObjects(sourceInstanceID, count, newInstanceIDs, newTransformInstanceIDs, destinationScene); + + public void SetGameObjectsActive(NativeArray instanceIDs, bool active) + => GameObject.SetGameObjectsActive(instanceIDs, active); + + public void SetGameObjectsActive(ReadOnlySpan instanceIDs, bool active) + => GameObject.SetGameObjectsActive(instanceIDs, active); + + private IObjectAdapter[] ConvertToAdapters(UnityEngine.Object[] objs) { + var adapters = new IObjectAdapter[objs.Length]; + for (int i = 0; i < objs.Length; i++) { + if (objs[i] is GameObject gameObj) adapters[i] = new GameObjectAdapter(gameObj); + else adapters[i] = null; + } + + return adapters; + } + + private IObjectAdapter InstantiateInternal(IObjectAdapter original, Transform parent = null, bool instantiateInWorldSpace = false, + Vector3? position = null, Quaternion? rotation = null) { + if (original is GameObjectAdapter gameObjAdapter) { + GameObject instantiated; + if (position.HasValue && rotation.HasValue) + instantiated = parent != null ? + UnityEngine.Object.Instantiate(gameObjAdapter.adaptee, position.Value, rotation.Value, parent) : + UnityEngine.Object.Instantiate(gameObjAdapter.adaptee, position.Value, rotation.Value); + else + instantiated = parent != null ? + UnityEngine.Object.Instantiate(gameObjAdapter.adaptee, parent, instantiateInWorldSpace) : + UnityEngine.Object.Instantiate(gameObjAdapter.adaptee); + + return new GameObjectAdapter(instantiated); + } else return null; + } +} + +} // namespace diff --git a/Runtime/GameObjectAdapter.cs.meta b/Runtime/GameObjectAdapter.cs.meta new file mode 100644 index 0000000..733b015 --- /dev/null +++ b/Runtime/GameObjectAdapter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f499f6df89a7d4146ad9a5c3b3b6caab +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/ObjectAdapter.cs b/Runtime/ObjectAdapter.cs old mode 100755 new mode 100644 index 499484a..c2fae7e --- a/Runtime/ObjectAdapter.cs +++ b/Runtime/ObjectAdapter.cs @@ -5,8 +5,8 @@ namespace SUL.Adapters { public interface IObjectAdapter { - string Name { get; set; } - HideFlags HideFlags { get; set; } + string name { get; set; } + HideFlags hideFlags { get; set; } int GetInstanceID(); string ToString(); @@ -34,12 +34,12 @@ public class ObjectAdapter : IObjectAdapter { public ObjectAdapter(UnityEngine.Object adaptee) => this.adaptee = adaptee; - public string Name { + public string name { get => adaptee.name; set => adaptee.name = value; } - public HideFlags HideFlags { + public HideFlags hideFlags { get => adaptee.hideFlags; set => adaptee.hideFlags = value; } @@ -136,4 +136,4 @@ public IObjectAdapter Instantiate(IObjectAdapter original, Vector3 position, Qua } } -} // namespace +} // namespace \ No newline at end of file diff --git a/Tests/Editor/GameObjectAdapterEditorTests.cs b/Tests/Editor/GameObjectAdapterEditorTests.cs new file mode 100644 index 0000000..2b9a603 --- /dev/null +++ b/Tests/Editor/GameObjectAdapterEditorTests.cs @@ -0,0 +1,351 @@ +using System.Collections.Generic; +using NUnit.Framework; +using UnityEngine; +using UnityEditor.SceneManagement; +using System.Linq; + +namespace SUL.Adapters { + +[TestFixture] +[Category("Adapters")] +public class GameObjectAdapterEditorTests { + private const string TEST_GO_NAME = "TestObject"; + private const string NEW_GO_NAME = "NewObject"; + private const string PARENT_PREFIX = "(Parent)"; + private const string CHILD_PREFIX = "(Child)"; + + private IGameObjectAdapter goAdapter; + private GameObject testGo; + + [SetUp] + public void SetUp() { + EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); + + testGo = new GameObject(TEST_GO_NAME); + goAdapter = new GameObjectAdapter(testGo); + } + + [TearDown] + public void TearDown() { + if (testGo != null) Object.DestroyImmediate(testGo); + testGo = null; + goAdapter = null; + + EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); + } + + [Test] + public void G_GameObjectAdapter_W_GetBasicProperties_T_ReturnsCorrectValues() { + // Assert + Assert.AreEqual(TEST_GO_NAME, goAdapter.name); + Assert.AreEqual(testGo.activeInHierarchy, goAdapter.activeInHierachy); + Assert.AreEqual(testGo.activeSelf, goAdapter.activeSelf); + Assert.AreEqual(testGo.isStatic, goAdapter.isStatic); + Assert.AreEqual(testGo.layer, goAdapter.layer); + Assert.AreEqual(testGo.scene, goAdapter.scene); + Assert.AreEqual(testGo.sceneCullingMask, goAdapter.sceneCullingMask); + Assert.AreEqual(testGo.tag, goAdapter.tag); + Assert.AreEqual(testGo.transform, goAdapter.transform); + } + + [Test] + public void G_GameObjectAdapter_W_SetBasicProperties_T_PropertiesAreSet() { + // Act + goAdapter.name = NEW_GO_NAME; + goAdapter.isStatic = true; + goAdapter.layer = 5; + goAdapter.tag = "Player"; + + // Assert + Assert.AreEqual(NEW_GO_NAME, testGo.name); + Assert.IsTrue(testGo.isStatic); + Assert.AreEqual(5, testGo.layer); + Assert.AreEqual("Player", testGo.tag); + } + + [Test] + public void G_GameObjectAdapter_W_AddComponent_T_ComponentIsAdded() { + // Act + var component = goAdapter.AddComponent(typeof(BoxCollider)); + + // Assert + Assert.IsNotNull(component); + Assert.IsInstanceOf(component); + Assert.IsNotNull(testGo.GetComponent()); + } + + [Test] + public void G_GameObjectAdapter_W_GetComponent_T_ReturnsCorrectComponent() { + // Arrange + testGo.AddComponent(); + + // Act + var component1 = goAdapter.GetComponent(); + var component2 = goAdapter.GetComponent(typeof(BoxCollider)); + var component3 = goAdapter.GetComponent("BoxCollider"); + + // Assert + Assert.IsNotNull(component1); + Assert.IsNotNull(component2); + Assert.IsNotNull(component3); + Assert.IsInstanceOf(component1); + Assert.IsInstanceOf(component2); + Assert.IsInstanceOf(component3); + } + + [Test] + public void G_GameObjectAdapter_W_GetComponentInChildren_T_ReturnsCorrectComponent() { + // Arrange + var childObj = new GameObject(CHILD_PREFIX); + childObj.transform.SetParent(testGo.transform); + childObj.AddComponent(); + + // Act + var component1 = goAdapter.GetComponentInChildren(); + var component2 = goAdapter.GetComponentInChildren(typeof(BoxCollider)); + var component3 = goAdapter.GetComponentInChildren(typeof(BoxCollider), true); + + // Assert + Assert.IsNotNull(component1); + Assert.IsNotNull(component2); + Assert.IsNotNull(component3); + Assert.IsInstanceOf(component1); + Assert.IsInstanceOf(component2); + Assert.IsInstanceOf(component3); + } + + [Test] + public void G_GameObjectAdapter_W_GetComponentInParent_T_ReturnsCorrectComponent() { + // Arrange + var parentObj = new GameObject(PARENT_PREFIX); + testGo.transform.SetParent(parentObj.transform); + parentObj.AddComponent(); + var childAdapter = new GameObjectAdapter(testGo); + + // Act + var component1 = childAdapter.GetComponentInParent(); + var component2 = childAdapter.GetComponentInParent(typeof(BoxCollider)); + var component3 = childAdapter.GetComponentInParent(typeof(BoxCollider), true); + + // Assert + Assert.IsNotNull(component1); + Assert.IsNotNull(component2); + Assert.IsNotNull(component3); + Assert.IsInstanceOf(component1); + Assert.IsInstanceOf(component2); + Assert.IsInstanceOf(component3); + } + + [Test] + public void G_GameObjectAdapter_W_GetComponents_T_ReturnsCorrectComponents() { + // Arrange + testGo.AddComponent(); + testGo.AddComponent(); + + // Act + var components1 = goAdapter.GetComponents(); + var components2 = goAdapter.GetComponents(typeof(Collider)); + var componentsList = new List(); + goAdapter.GetComponents(componentsList); + + // Assert + Assert.AreEqual(2, components1.Length); + Assert.AreEqual(2, components2.Length); + Assert.AreEqual(2, componentsList.Count); + Assert.IsTrue(components1.Any(c => c is BoxCollider)); + Assert.IsTrue(components1.Any(c => c is SphereCollider)); + } + + [Test] + public void G_GameObjectAdapter_W_GetComponentsInChildren_T_ReturnsCorrectComponents() { + // Arrange + var childObj = new GameObject(CHILD_PREFIX); + childObj.transform.SetParent(testGo.transform); + testGo.AddComponent(); + childObj.AddComponent(); + + // Act + var components1 = goAdapter.GetComponentsInChildren(); + var components2 = goAdapter.GetComponentsInChildren(true); + var components3 = goAdapter.GetComponentsInChildren(typeof(Collider), true); + var componentsList = new List(); + goAdapter.GetComponentsInChildren(componentsList); + var componentsListInactive = new List(); + goAdapter.GetComponentsInChildren(true, componentsListInactive); + + // Assert + Assert.AreEqual(2, components1.Length); + Assert.AreEqual(2, components2.Length); + Assert.AreEqual(2, components3.Length); + Assert.AreEqual(2, componentsList.Count); + Assert.AreEqual(2, componentsListInactive.Count); + } + + [Test] + public void G_GameObjectAdapter_W_GetComponentsInParent_T_ReturnsCorrectComponents() { + // Arrange + var parentObj = new GameObject(PARENT_PREFIX); + testGo.transform.SetParent(parentObj.transform); + parentObj.AddComponent(); + testGo.AddComponent(); + var childAdapter = new GameObjectAdapter(testGo); + + // Act + var components1 = childAdapter.GetComponentsInParent(); + var components2 = childAdapter.GetComponentsInParent(true); + var components3 = childAdapter.GetComponentsInParent(typeof(Collider), true); + var componentsList = new List(); + childAdapter.GetComponentsInParent(true, componentsList); + + // Assert + Assert.AreEqual(2, components1.Length); + Assert.AreEqual(2, components2.Length); + Assert.AreEqual(2, components3.Length); + Assert.AreEqual(2, componentsList.Count); + } + + [Test] + public void G_GameObjectAdapter_W_TryGetComponent_T_ReturnsCorrectComponent() { + // Arrange + testGo.AddComponent(); + + // Act + bool success1 = goAdapter.TryGetComponent(out var component1); + bool success2 = goAdapter.TryGetComponent(typeof(BoxCollider), out var component2); + + // Assert + Assert.IsTrue(success1); + Assert.IsTrue(success2); + Assert.IsNotNull(component1); + Assert.IsNotNull(component2); + Assert.IsInstanceOf(component1); + Assert.IsInstanceOf(component2); + } + + [Test] + public void G_GameObjectAdapter_W_CompareTag_T_ReturnsCorrectResult() { + // Arrange + testGo.tag = "Player"; + + // Act + bool result = goAdapter.CompareTag("Player"); + + // Assert + Assert.IsTrue(result); + } + + [Test] + public void G_GameObjectAdapter_W_SetActive_T_GameObjectActiveStateChanges() { + // Act + goAdapter.SetActive(false); + + // Assert + Assert.IsFalse(testGo.activeSelf); + } + + [Test] + public void G_GameObjectAdapter_W_CreatePrimitive_T_ReturnsCorrectGameObjectAdapter() { + // Act + var primitiveAdapter = goAdapter.CreatePrimitive(PrimitiveType.Cube); + + // Assert + Assert.IsNotNull(primitiveAdapter); + Assert.IsInstanceOf(primitiveAdapter); + Assert.IsNotNull(primitiveAdapter.GetComponent()); + Assert.IsNotNull(primitiveAdapter.GetComponent()); + } + + [Test] + public void G_GameObjectAdapter_W_Find_T_ReturnsCorrectGameObjectAdapter() { + // Act + var foundAdapter = goAdapter.Find(TEST_GO_NAME); + + // Assert + Assert.IsNotNull(foundAdapter); + Assert.IsInstanceOf(foundAdapter); + Assert.AreEqual(TEST_GO_NAME, foundAdapter.name); + } + + [Test] + public void G_GameObjectAdapter_W_GetScene_T_ReturnsCorrectScene() { + // Arrange + int instanceID = testGo.GetInstanceID(); + + // Act + var scene = goAdapter.GetScene(instanceID); + + // Assert + Assert.AreEqual(testGo.scene, scene); + } + + [Test] + public void G_GameObjectAdapter_W_InstantiateGameObjects_T_GameObjectsAreInstantiated() { + // Arrange + int sourceInstanceID = testGo.GetInstanceID(); + int count = 3; + var newInstanceIDs = new Unity.Collections.NativeArray(count, Unity.Collections.Allocator.Temp); + var newTransformInstanceIDs = new Unity.Collections.NativeArray(count, Unity.Collections.Allocator.Temp); + int initialObjectCount = Object.FindObjectsOfType().Length; + + // Act + goAdapter.InstantiateGameObjects(sourceInstanceID, count, newInstanceIDs, newTransformInstanceIDs); + + // Assert + int newObjectCount = Object.FindObjectsOfType().Length; + Assert.AreEqual(initialObjectCount + count, newObjectCount, "Expected number of new GameObjects were not created"); + + for (int i = 0; i < count; i++) { + Assert.AreNotEqual(0, newInstanceIDs[i], $"Invalid GameObject instance ID at index {i}"); + Assert.AreNotEqual(0, newTransformInstanceIDs[i], $"Invalid Transform instance ID at index {i}"); + } + + // Clean up + newInstanceIDs.Dispose(); + newTransformInstanceIDs.Dispose(); + + var allObjects = Object.FindObjectsOfType(); + for (int i = initialObjectCount; i < allObjects.Length; i++) + Object.DestroyImmediate(allObjects[i]); + } + + [Test] + public void G_GameObjectAdapter_W_SetGameObjectsActive_T_GameObjectsActiveStateChanges() { + // Arrange + var obj1 = new GameObject("Obj1"); + var obj2 = new GameObject("Obj2"); + var instanceIDs = new int[] { obj1.GetInstanceID(), obj2.GetInstanceID() }; + + // Act + goAdapter.SetGameObjectsActive(instanceIDs, false); + + // Assert + Assert.IsFalse(obj1.activeSelf); + Assert.IsFalse(obj2.activeSelf); + + // Clean up + Object.DestroyImmediate(obj1); + Object.DestroyImmediate(obj2); + } + + [Test] + public void G_GameObjectAdapter_W_SetGameObjectsActiveWithNativeArray_T_GameObjectsActiveStateChanges() { + // Arrange + var obj1 = new GameObject("Obj1"); + var obj2 = new GameObject("Obj2"); + var instanceIDs = new Unity.Collections.NativeArray(new int[] { obj1.GetInstanceID(), obj2.GetInstanceID() }, Unity.Collections.Allocator.Temp); + + // Act + goAdapter.SetGameObjectsActive(instanceIDs, false); + + // Assert + Assert.IsFalse(obj1.activeSelf); + Assert.IsFalse(obj2.activeSelf); + + // Clean up + instanceIDs.Dispose(); + Object.DestroyImmediate(obj1); + Object.DestroyImmediate(obj2); + } +} + +} // namespace \ No newline at end of file diff --git a/Tests/Editor/GameObjectAdapterEditorTests.cs.meta b/Tests/Editor/GameObjectAdapterEditorTests.cs.meta new file mode 100644 index 0000000..5b447c3 --- /dev/null +++ b/Tests/Editor/GameObjectAdapterEditorTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: aa09df8bcb0abb842b3658d4025da656 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Editor/ObjectAdapterEditorTests.cs b/Tests/Editor/ObjectAdapterEditorTests.cs old mode 100755 new mode 100644 index e09664f..0812bd8 --- a/Tests/Editor/ObjectAdapterEditorTests.cs +++ b/Tests/Editor/ObjectAdapterEditorTests.cs @@ -1,11 +1,9 @@ -using SUL.Adapters; using NUnit.Framework; using UnityEditor.SceneManagement; using UnityEngine; -using UnityEngine.SceneManagement; using System.Linq; -namespace SUL_Tests.Adapters { +namespace SUL.Adapters { [TestFixture] [Category("Adapters")] @@ -18,6 +16,8 @@ public class ObjectAdapterEditorTests { [SetUp] public void SetUp() { + EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); + testObj = new GameObject(TEST_OBJ_NAME); adapter = new ObjectAdapter(testObj); } @@ -32,19 +32,18 @@ public void TearDown() { Object.DestroyImmediate(adapter as Object); adapter = null; - var testScene = SceneManager.GetActiveScene(); - EditorSceneManager.CloseScene(testScene, true); + EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); } [Test] public void G_ObjectAdapter_W_GetNameAndHideFlags_T_ReturnsCorrectValues() { // Act - adapter.Name = NEW_OBJ_NAME; - adapter.HideFlags = HideFlags.HideInHierarchy; + adapter.name = NEW_OBJ_NAME; + adapter.hideFlags = HideFlags.HideInHierarchy; // Assert - Assert.AreEqual(NEW_OBJ_NAME, adapter.Name); - Assert.AreEqual(HideFlags.HideInHierarchy, adapter.HideFlags); + Assert.AreEqual(NEW_OBJ_NAME, adapter.name); + Assert.AreEqual(HideFlags.HideInHierarchy, adapter.hideFlags); } [Test] diff --git a/Tests/Editor/Soobak.SoobakUnityLibrary.Editor.Tests.asmdef b/Tests/Editor/Soobak.SoobakUnityLibrary.Editor.Tests.asmdef index 6577134..8cc61ed 100644 --- a/Tests/Editor/Soobak.SoobakUnityLibrary.Editor.Tests.asmdef +++ b/Tests/Editor/Soobak.SoobakUnityLibrary.Editor.Tests.asmdef @@ -5,7 +5,8 @@ "GUID:27619889b8ba8c24980f49ee34dbb44a", "GUID:0acc523941302664db1f4e527237feb3", "GUID:c0c1ad89ce4ae974b96dcdc1ba2f06c6", - "GUID:2fd5810ee7bc8c442a957e228abad3c0" + "GUID:2fd5810ee7bc8c442a957e228abad3c0", + "GUID:962b7f9e2a1095f4f95f1bffb0271133" ], "includePlatforms": [ "Editor" diff --git a/Tests/Runtime/GameObjectAdapterPlayTests.cs b/Tests/Runtime/GameObjectAdapterPlayTests.cs new file mode 100644 index 0000000..2c252df --- /dev/null +++ b/Tests/Runtime/GameObjectAdapterPlayTests.cs @@ -0,0 +1,276 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; +using SUL.TestUtilities; +using NUnit.Framework.Internal; +using System.Linq; +using UnityEditor; + +namespace SUL.Adapters { + +[TestFixture] +[Category("Adapters")] +public class GameObjectAdapterPlayTests { + private const string TEST_GO_NAME = "TestObject"; + private const string TEST_OBJ_NAME = "TestObject"; + private const string TEST_TAG_NAME = "TestTag"; + private const string TEST_METHOD_NAME = "TestMethod"; + private const string TEST_PARAMETER_NAME = "TestParameter"; + private const string CLONE_SUFFIX = "(Clone)"; + private const string PARENT_PREFIX = "(Parent)"; + private const string CHILD_PREFIX = "(Child)"; + + public class MonobehaviorSpy : MonoBehaviour { + public bool MethodCalled { get; private set; } + public string LastMethodName { get; private set; } + public object LastParameter { get; private set; } + + public void TestMethod(object parameter = null) { + MethodCalled = true; + LastMethodName = nameof(TestMethod); + LastParameter = parameter; + } + } + + private IGameObjectAdapter goAdapter = null; + private GameObject testGo = null; + + [OneTimeSetUp] + public void OneTimeSetup() { + EditorSettings.enterPlayModeOptionsEnabled = true; + EditorSettings.enterPlayModeOptions = EnterPlayModeOptions.DisableDomainReload; + } + + [UnitySetUp] + public IEnumerator SetUp() { + yield return new EnterPlayMode(); + + testGo = new GameObject(TEST_OBJ_NAME); + goAdapter = new GameObjectAdapter(testGo); + } + + [UnityTearDown] + public IEnumerator TearDown() { + if (testGo != null) Object.Destroy(testGo); + testGo = null; + goAdapter = null; + + yield return new ExitPlayMode(); + } + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_DontDestroyOnLoad_T_GameObjectPersistsThroughSceneLoad() { + // Arrange + goAdapter.DontDestroyOnLoad(goAdapter); + + // Act + yield return TestHelper.SimulateSceneTransition(); + + // Assert + Assert.IsNotNull(GameObject.Find(TEST_OBJ_NAME)); + } + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_Instantiate_T_NewGameObjectIsCreated() { + // Act + var newAdapter = goAdapter.Instantiate(goAdapter); + yield return null; + + // Assert + Assert.IsNotNull(newAdapter); + Assert.AreNotEqual(goAdapter.GetInstanceID(), newAdapter.GetInstanceID()); + Assert.AreEqual(TEST_OBJ_NAME + CLONE_SUFFIX, newAdapter.name); + + // Clean up + newAdapter.Destroy(newAdapter); + } + + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_InstantiateWithParent_T_NewGameObjectIsCreatedWithParent() { + // Arrange + var parentObj = new GameObject(PARENT_PREFIX); + var parentAdapter = new GameObjectAdapter(parentObj); + + // Act + var newAdapter = goAdapter.Instantiate(goAdapter, parentAdapter.transform); + yield return null; + + // Assert + Assert.IsNotNull(newAdapter); + var newGameObject = GameObject.Find(TEST_OBJ_NAME + CLONE_SUFFIX); + Assert.IsNotNull(newGameObject, "New GameObject was not found in the scene"); + Assert.IsTrue(parentObj.transform == newGameObject.transform.parent); + + // Clean up + parentAdapter.Destroy(parentAdapter); + newAdapter.Destroy(newAdapter); + } + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_InstantiateWithPosition_T_NewGameObjectIsCreatedAtPosition() { + // Arrange + var position = new Vector3(1, 2, 3); + var rotation = Quaternion.Euler(30, 60, 90); + + // Act + var newAdapter = goAdapter.Instantiate(goAdapter, position, rotation); + yield return null; + + // Assert + Assert.IsNotNull(newAdapter); + var newGameObject = GameObject.Find(TEST_OBJ_NAME + CLONE_SUFFIX); + Assert.IsNotNull(newGameObject, "New GameObject was not found in the scene"); + Assert.IsTrue(position == newGameObject.transform.position); + Assert.IsTrue(rotation == newGameObject.transform.rotation); + + // Clean up + newAdapter.Destroy(newAdapter); + } + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_InstantiateWithPositionAndParent_T_NewGameObjectIsCreatedAtPositionWithParent() { + // Arrange + var parentObj = new GameObject(PARENT_PREFIX); + var parentAdapter = new GameObjectAdapter(parentObj); + var position = new Vector3(1, 2, 3); + var rotation = Quaternion.Euler(30, 60, 90); + + // Act + var newAdapter = goAdapter.Instantiate(goAdapter, position, rotation, parentAdapter.transform); + yield return null; + + // Assert + Assert.IsNotNull(newAdapter); + var newGameObject = GameObject.Find(TEST_OBJ_NAME + CLONE_SUFFIX); + Assert.IsNotNull(newGameObject, "New GameObject was not found in the scene"); + Assert.IsTrue(parentObj.transform == newGameObject.transform.parent); + Assert.IsTrue(position == newGameObject.transform.localPosition); + Assert.IsTrue(rotation == newGameObject.transform.localRotation); + + // Clean up + parentAdapter.Destroy(parentAdapter); + newAdapter.Destroy(newAdapter); + } + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_SendMessage_T_MessageIsSent() { + // Arrange + var mockBehaviour = testGo.AddComponent(); + + // Act + goAdapter.SendMessage(TEST_METHOD_NAME, TEST_PARAMETER_NAME, SendMessageOptions.RequireReceiver); + yield return null; // Wait for a frame to ensure the message is processed + + // Assert + Assert.IsTrue(mockBehaviour.MethodCalled); + Assert.AreEqual(TEST_METHOD_NAME, mockBehaviour.LastMethodName); + Assert.AreEqual(TEST_PARAMETER_NAME, mockBehaviour.LastParameter); + } + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_SendMessageUpwards_T_MessageIsSentUpwards() { + // Arrange + var parentObj = new GameObject(PARENT_PREFIX); + testGo.transform.SetParent(parentObj.transform); + var mockBehaviour = parentObj.AddComponent(); + + // Act + goAdapter.SendMessageUpwards(TEST_METHOD_NAME, TEST_PARAMETER_NAME, SendMessageOptions.RequireReceiver); + yield return null; // Wait for a frame to ensure the message is processed + + // Assert + Assert.IsTrue(mockBehaviour.MethodCalled); + Assert.AreEqual(TEST_METHOD_NAME, mockBehaviour.LastMethodName); + Assert.AreEqual(TEST_PARAMETER_NAME, mockBehaviour.LastParameter); + + // Clean up + Object.Destroy(parentObj); + } + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_BroadcastMessage_T_MessageIsBroadcasted() { + // Arrange + var childObj = new GameObject(CHILD_PREFIX); + childObj.transform.SetParent(testGo.transform); + var mockBehaviour = childObj.AddComponent(); + + // Act + goAdapter.BroadCastMessage(TEST_METHOD_NAME, TEST_PARAMETER_NAME, SendMessageOptions.RequireReceiver); + yield return null; // Wait for a frame to ensure the message is processed + + // Assert + Assert.IsTrue(mockBehaviour.MethodCalled); + Assert.AreEqual(TEST_METHOD_NAME, mockBehaviour.LastMethodName); + Assert.AreEqual(TEST_PARAMETER_NAME, mockBehaviour.LastParameter); + + // Clean up + Object.Destroy(childObj); + } + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_FindWithTag_T_ReturnsCorrectGameObjectAdapter() { + // Arrange + InitTagConfig(TEST_TAG_NAME); + testGo.tag = TEST_TAG_NAME; + + // Act + var foundAdapter = goAdapter.FindWithTag(TEST_TAG_NAME); + yield return null; + + // Assert + Assert.IsNotNull(foundAdapter); + Assert.IsInstanceOf(foundAdapter); + Assert.AreEqual(TEST_GO_NAME, foundAdapter.name); + } + + + [UnityTest] + public IEnumerator G_GameObjectAdapter_W_FindGameObjectsWithTag_T_ReturnsCorrectGameObjectAdapters() { + // Arrange + InitTagConfig(TEST_TAG_NAME); + testGo.tag = TEST_TAG_NAME; + var anotherObj = new GameObject("AnotherObject") { + tag = TEST_TAG_NAME + }; + + // Act + var foundAdapters = goAdapter.FindGameObjectsWithTag(TEST_TAG_NAME); + yield return null; + + // Assert + Assert.IsNotNull(foundAdapters); + Assert.AreEqual(2, foundAdapters.Length); + Assert.IsTrue(foundAdapters.All(a => a is IGameObjectAdapter)); + Assert.IsTrue(foundAdapters.Any(a => a.name == TEST_GO_NAME)); + Assert.IsTrue(foundAdapters.Any(a => a.name == "AnotherObject")); + + // Clean up + Object.DestroyImmediate(anotherObj); + } + + private void InitTagConfig(string tagName) { + var tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]); + var targetPropName = "tags"; + var tagsProp = tagManager.FindProperty(targetPropName); + + bool isFound = false; + for (int i = 0; i < tagsProp.arraySize; i++) { + SerializedProperty t = tagsProp.GetArrayElementAtIndex(i); + if (t.stringValue.Equals(tagName)) { + isFound = true; + break ; + } + } + + if (!isFound) { + tagsProp.InsertArrayElementAtIndex(0); + SerializedProperty newTag = tagsProp.GetArrayElementAtIndex(0); + newTag.stringValue = tagName; + tagManager.ApplyModifiedProperties(); + } + } +} + +} // namespace \ No newline at end of file diff --git a/Tests/Runtime/GameObjectAdapterPlayTests.cs.meta b/Tests/Runtime/GameObjectAdapterPlayTests.cs.meta new file mode 100644 index 0000000..90276b9 --- /dev/null +++ b/Tests/Runtime/GameObjectAdapterPlayTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4acd6474548793e4fbe353e387770443 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Runtime/ObjectAdapterPlayTests.cs b/Tests/Runtime/ObjectAdapterPlayTests.cs old mode 100755 new mode 100644 index d44d694..ccba8cd --- a/Tests/Runtime/ObjectAdapterPlayTests.cs +++ b/Tests/Runtime/ObjectAdapterPlayTests.cs @@ -1,19 +1,18 @@ -using SUL.Adapters; using NUnit.Framework; using UnityEngine; -using UnityEngine.SceneManagement; using UnityEngine.TestTools; using System; using System.Collections; using System.Linq; +using SUL.TestUtilities; -namespace SUL_Tests.Adapters { +namespace SUL.Adapters { [TestFixture] [Category("Adapters")] public class ObjectAdapterPlayTests { - const string INACTIVE_GOBJ_NAME = "InactiveObject"; - const string ACTIVE_GOBJ_NAME = "ActiveObject"; + const string INACTIVE_GAMEOBJ_NAME = "InactiveObject"; + const string ACTIVE_GAMEOBJ_NAME = "ActiveObject"; const string TEST_OBJ_NAME = "TestObject"; const string PARENT_OBJ_NAME = "Parent"; @@ -42,8 +41,8 @@ public IEnumerator TearDown() { UnityEngine.Object.DestroyImmediate(adapter as UnityEngine.Object); adapter = null; - var allObjects = UnityEngine.Object.FindObjectsOfType(); - foreach (var obj in allObjects) + var foundObjs = UnityEngine.Object.FindObjectsOfType(); + foreach (var obj in foundObjs) UnityEngine.Object.DestroyImmediate(obj); yield return null; @@ -52,8 +51,7 @@ public IEnumerator TearDown() { [UnityTest] public IEnumerator G_ObjectExists_W_DestroyCalledWithDelay_T_ObjectIsDestroyedAfterDelay() { // Arrange - const float delaySec = 0.1f; - const float afterDelaySec = 0.2f; + const float delaySec = 0.1f, afterDelaySec = 0.2f; // Act adapter.Destroy(adapter, delaySec); @@ -69,7 +67,7 @@ public IEnumerator G_ObjectExists_W_DestroyCalledWithDelay_T_ObjectIsDestroyedAf public IEnumerator G_ObjectExist_W_DontDestroyOnLoadCalledAndSceneTransitions_T_ObjectPersists() { // Act adapter.DontDestroyOnLoad(adapter); - yield return SimulateSceneTransition(); + yield return TestHelper.SimulateSceneTransition(); // Assert Assert.IsFalse(testObj == null); @@ -78,7 +76,7 @@ public IEnumerator G_ObjectExist_W_DontDestroyOnLoadCalledAndSceneTransitions_T_ [UnityTest] public IEnumerator G_InactiveObjectWithComponent_W_FindObjectOfTypeCalledWithIncludeInactive_T_ReturnsCorrectComponent() { // Arrange - var inactiveObj = new GameObject(INACTIVE_GOBJ_NAME); + var inactiveObj = new GameObject(INACTIVE_GAMEOBJ_NAME); inactiveObj.SetActive(false); var testCmp = inactiveObj.AddComponent(); @@ -97,7 +95,7 @@ public IEnumerator G_InactiveObjectWithComponent_W_FindObjectOfTypeCalledWithInc [UnityTest] public IEnumerator G_InactiveObjectWithComponent_W_FindObjectOfTypeCalledWithTypeAndIncludeInactive_T_ReturnsCorrectObjectAdapter() { // Arrange - var inactiveObj = new GameObject(INACTIVE_GOBJ_NAME); + var inactiveObj = new GameObject(INACTIVE_GAMEOBJ_NAME); inactiveObj.SetActive(false); inactiveObj.AddComponent(); yield return null; @@ -109,7 +107,7 @@ public IEnumerator G_InactiveObjectWithComponent_W_FindObjectOfTypeCalledWithTyp // Assert Assert.IsNotNull(foundObj); Assert.IsInstanceOf(foundObj); - Assert.AreEqual(inactiveObj.name, foundObj.Name); + Assert.AreEqual(inactiveObj.name, foundObj.name); // CleanUp UnityEngine.Object.Destroy(inactiveObj); @@ -118,12 +116,12 @@ public IEnumerator G_InactiveObjectWithComponent_W_FindObjectOfTypeCalledWithTyp [UnityTest] public IEnumerator G_ActiveAndInactiveObjectsWithComponent_W_FindObjectsOfTypeCalledWithIncludeInactive_T_ReturnsAllComponents() { // Arrange - var activeObj = new GameObject(ACTIVE_GOBJ_NAME); - var inactiveObj = new GameObject(INACTIVE_GOBJ_NAME); + var activeObj = new GameObject(ACTIVE_GAMEOBJ_NAME); + var inactiveObj = new GameObject(INACTIVE_GAMEOBJ_NAME); inactiveObj.SetActive(false); - var testCmp_1 = activeObj.AddComponent(); - var testCmp_2 = inactiveObj.AddComponent(); + var testCmpF = activeObj.AddComponent(); + var testCmpS = inactiveObj.AddComponent(); // Act var foundCmps = adapter.FindObjectsOfType(true); @@ -132,8 +130,8 @@ public IEnumerator G_ActiveAndInactiveObjectsWithComponent_W_FindObjectsOfTypeCa // Assert Assert.IsNotNull(foundCmps); Assert.AreEqual(2, foundCmps.Length); - Assert.IsTrue(foundCmps.Contains(testCmp_1)); - Assert.IsTrue(foundCmps.Contains(testCmp_2)); + Assert.IsTrue(foundCmps.Contains(testCmpF)); + Assert.IsTrue(foundCmps.Contains(testCmpS)); // CleanUp UnityEngine.Object.Destroy(activeObj); @@ -143,8 +141,8 @@ public IEnumerator G_ActiveAndInactiveObjectsWithComponent_W_FindObjectsOfTypeCa [UnityTest] public IEnumerator G_ActiveAndInactiveObjectsWithComponent_W_FindObjectsOfTypeCalledWithTypeAndIncludeInactive_T_ReturnsAllObjectAdapters() { // Arrange - var activeObj = new GameObject(ACTIVE_GOBJ_NAME); - var inactiveObj = new GameObject(INACTIVE_GOBJ_NAME); + var activeObj = new GameObject(ACTIVE_GAMEOBJ_NAME); + var inactiveObj = new GameObject(INACTIVE_GAMEOBJ_NAME); inactiveObj.SetActive(false); activeObj.AddComponent(); @@ -158,8 +156,8 @@ public IEnumerator G_ActiveAndInactiveObjectsWithComponent_W_FindObjectsOfTypeCa Assert.IsNotNull(foundObjs); Assert.AreEqual(2, foundObjs.Length); Assert.IsTrue(foundObjs.All(obj => obj is IObjectAdapter)); - Assert.IsTrue(foundObjs.Any(obj => obj.Name == ACTIVE_GOBJ_NAME)); - Assert.IsTrue(foundObjs.Any(obj => obj.Name == INACTIVE_GOBJ_NAME)); + Assert.IsTrue(foundObjs.Any(obj => obj.name == ACTIVE_GAMEOBJ_NAME)); + Assert.IsTrue(foundObjs.Any(obj => obj.name == INACTIVE_GAMEOBJ_NAME)); // CleanUp UnityEngine.Object.Destroy(activeObj); @@ -191,14 +189,14 @@ public IEnumerator G_ObjectAdapter_W_InstantiateCalledWithParent_T_ReturnsNewObj yield return null; // Assert - var newGameObject = GameObject.Find(newAdapter.Name); + var foundGameObj = GameObject.Find(newAdapter.name); Assert.IsNotNull(newAdapter); - Assert.AreEqual(parentObj.transform, newGameObject.transform.parent); + Assert.AreEqual(parentObj.transform, foundGameObj.transform.parent); // CleanUp UnityEngine.Object.Destroy(parentObj); adapter.Destroy(newAdapter); - GameObject.DestroyImmediate(newGameObject); + GameObject.DestroyImmediate(foundGameObj); } [UnityTest] @@ -210,14 +208,14 @@ public IEnumerator G_ObjectAdapter_W_InstantiateWithParentAndWorldSpace_T_Return // Assert Assert.IsNotNull(newAdapter); - var newGameObject = GameObject.Find(newAdapter.Name); - Assert.IsNotNull(newGameObject); - Assert.AreEqual(parentObj.transform, newGameObject.transform.parent); + var foundGameObj = GameObject.Find(newAdapter.name); + Assert.IsNotNull(foundGameObj); + Assert.AreEqual(parentObj.transform, foundGameObj.transform.parent); // CleanUp UnityEngine.Object.Destroy(parentObj); adapter.Destroy(newAdapter); - GameObject.DestroyImmediate(newGameObject); + GameObject.DestroyImmediate(foundGameObj); } [UnityTest] @@ -228,47 +226,38 @@ public IEnumerator G_ObjectAdapter_W_InstantiateWithPositionAndRotation_T_Return // Assert Assert.IsNotNull(newAdapter); - var newGameObject = GameObject.Find(newAdapter.Name); - Assert.IsNotNull(newGameObject); - Assert.AreEqual(INST_POS, newGameObject.transform.position); - Assert.AreEqual(INST_ROT.ToString(), newGameObject.transform.rotation.ToString()); + var foundGameObj = GameObject.Find(newAdapter.name); + Assert.IsNotNull(foundGameObj); + Assert.AreEqual(INST_POS, foundGameObj.transform.position); + Assert.AreEqual(INST_ROT.ToString(), foundGameObj.transform.rotation.ToString()); // CleanUp adapter.Destroy(newAdapter); - GameObject.DestroyImmediate(newGameObject); + GameObject.DestroyImmediate(foundGameObj); } [UnityTest] public IEnumerator G_ObjectAdapter_W_InstantiateWithPositionRotationAndParent_T_ReturnsNewObjectAdapterWithCorrectTransformAndParent() { // Arrange var parentObj = new GameObject(PARENT_OBJ_NAME); - + // Act var newAdapter = adapter.Instantiate(adapter, INST_POS, INST_ROT, parentObj.transform); yield return null; // Assert Assert.IsNotNull(newAdapter); - var newGameObject = GameObject.Find(newAdapter.Name); - Assert.IsNotNull(newGameObject); - Assert.AreEqual(INST_POS, newGameObject.transform.localPosition); - Assert.AreEqual(INST_ROT.ToString(), newGameObject.transform.localRotation.ToString()); - Assert.AreEqual(parentObj.transform, newGameObject.transform.parent); + var foundGameObj = GameObject.Find(newAdapter.name); + Assert.IsNotNull(foundGameObj); + Assert.AreEqual(INST_POS, foundGameObj.transform.localPosition); + Assert.AreEqual(INST_ROT.ToString(), foundGameObj.transform.localRotation.ToString()); + Assert.AreEqual(parentObj.transform, foundGameObj.transform.parent); // CleanUp UnityEngine.Object.Destroy(parentObj); adapter.Destroy(newAdapter); - GameObject.DestroyImmediate(newGameObject); - } - - private IEnumerator SimulateSceneTransition() { - var originScene = SceneManager.GetActiveScene(); - var tmpSceneName = "TempScene"; - var tmpScene = SceneManager.CreateScene(tmpSceneName); - yield return SceneManager.SetActiveScene(tmpScene); - yield return SceneManager.UnloadSceneAsync(tmpScene); - yield return SceneManager.SetActiveScene(originScene); + GameObject.DestroyImmediate(foundGameObj); } } -} // namespace +} // namespace \ No newline at end of file diff --git a/Tests/Runtime/Soobak.SoobakUnityLibrary.Tests.asmdef b/Tests/Runtime/Soobak.SoobakUnityLibrary.Tests.asmdef index a5b0066..df6dd72 100644 --- a/Tests/Runtime/Soobak.SoobakUnityLibrary.Tests.asmdef +++ b/Tests/Runtime/Soobak.SoobakUnityLibrary.Tests.asmdef @@ -4,7 +4,8 @@ "references": [ "GUID:27619889b8ba8c24980f49ee34dbb44a", "GUID:0acc523941302664db1f4e527237feb3", - "GUID:2fd5810ee7bc8c442a957e228abad3c0" + "GUID:2fd5810ee7bc8c442a957e228abad3c0", + "GUID:962b7f9e2a1095f4f95f1bffb0271133" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Tests/TestUtilities.meta b/Tests/TestUtilities.meta new file mode 100644 index 0000000..71f05bc --- /dev/null +++ b/Tests/TestUtilities.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c35cf06bf7943ca43b5b48892022e328 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/TestUtilities/Soobak.SoobakUnityLibrary.TestUtilities.asmdef b/Tests/TestUtilities/Soobak.SoobakUnityLibrary.TestUtilities.asmdef new file mode 100644 index 0000000..61b9b67 --- /dev/null +++ b/Tests/TestUtilities/Soobak.SoobakUnityLibrary.TestUtilities.asmdef @@ -0,0 +1,21 @@ +{ + "name": "SUL.Tests.TestUtilities", + "rootNamespace": "SUL_Tests.TestUtilities", + "references": [ + "UnityEngine.TestRunner", + "UnityEditor.TestRunner" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": true, + "precompiledReferences": [ + "nunit.framework.dll" + ], + "autoReferenced": false, + "defineConstraints": [ + "UNITY_INCLUDE_TESTS" + ], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Tests/TestUtilities/Soobak.SoobakUnityLibrary.TestUtilities.asmdef.meta b/Tests/TestUtilities/Soobak.SoobakUnityLibrary.TestUtilities.asmdef.meta new file mode 100644 index 0000000..52c7dea --- /dev/null +++ b/Tests/TestUtilities/Soobak.SoobakUnityLibrary.TestUtilities.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 962b7f9e2a1095f4f95f1bffb0271133 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/TestUtilities/TestHelper.cs b/Tests/TestUtilities/TestHelper.cs new file mode 100644 index 0000000..ba5205d --- /dev/null +++ b/Tests/TestUtilities/TestHelper.cs @@ -0,0 +1,16 @@ +using System.Collections; +using UnityEngine.SceneManagement; + +namespace SUL.TestUtilities { +public static class TestHelper { + public static IEnumerator SimulateSceneTransition() { + var originScene = SceneManager.GetActiveScene(); + var tmpSceneName = "TempScene"; + var tmpScene = SceneManager.CreateScene(tmpSceneName); + yield return SceneManager.SetActiveScene(tmpScene); + yield return SceneManager.UnloadSceneAsync(tmpScene); + yield return SceneManager.SetActiveScene(originScene); + } +} + +} // namespace \ No newline at end of file diff --git a/Tests/TestUtilities/TestHelper.cs.meta b/Tests/TestUtilities/TestHelper.cs.meta new file mode 100644 index 0000000..354808e --- /dev/null +++ b/Tests/TestUtilities/TestHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ec1b4fafce87134bbf2fa8677e21b73 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Utilities/Soobak.SoobakUnityLibrary.Utilities.Tests.asmdef b/Tests/Utilities/Soobak.SoobakUnityLibrary.Utilities.Tests.asmdef index 1a04f70..0a02fae 100644 --- a/Tests/Utilities/Soobak.SoobakUnityLibrary.Utilities.Tests.asmdef +++ b/Tests/Utilities/Soobak.SoobakUnityLibrary.Utilities.Tests.asmdef @@ -4,7 +4,8 @@ "references": [ "GUID:27619889b8ba8c24980f49ee34dbb44a", "GUID:0acc523941302664db1f4e527237feb3", - "GUID:c28f4d3c441669c4fbc0c6afce83433e" + "GUID:c28f4d3c441669c4fbc0c6afce83433e", + "GUID:962b7f9e2a1095f4f95f1bffb0271133" ], "includePlatforms": [ "Editor" diff --git a/Tests/Utilities/StringExtensionTests.cs b/Tests/Utilities/StringExtensionTests.cs index 457e0fc..6bc3220 100644 --- a/Tests/Utilities/StringExtensionTests.cs +++ b/Tests/Utilities/StringExtensionTests.cs @@ -1,38 +1,37 @@ using NUnit.Framework; -using SUL; -// XXX Consider namespace for classification structure in terms of hierarchy. -namespace SUL_Tests.Utilities { +namespace SUL.Utilities { [TestFixture] - [Category("StringManipulation")] - public class StringExtensionTests { +[Category("StringManipulation")] +public class StringExtensionTests { - [Test] - public void Reverse_WithNonEmptyString_ReturnsReversedString() { - // Arrange - var original = "soobak"; - var expected = "kaboos"; + [Test] + public void Reverse_WithNonEmptyString_ReturnsReversedString() { + // Arrange + var original = "soobak"; + var expected = "kaboos"; - // Act - string result = original.Reverse(); + // Act + string result = original.Reverse(); - // Assert - Assert.AreEqual(expected, result); - } - - [Test] - public void Reverse_WithEmptyString_ReturnsEmptyString() { - // Arrange - var original = string.Empty; - var expected = string.Empty; + // Assert + Assert.AreEqual(expected, result); + } - // Act - string result = original.Reverse(); + [Test] + public void Reverse_WithEmptyString_ReturnsEmptyString() { + // Arrange + var original = string.Empty; + var expected = string.Empty; - // Assert - Assert.AreEqual(expected, result); - } + // Act + string result = original.Reverse(); + // Assert + Assert.AreEqual(expected, result); } + } + +} // namespace diff --git a/Utilities/StringExtensions.cs b/Utilities/StringExtensions.cs index fb67afe..91778c2 100644 --- a/Utilities/StringExtensions.cs +++ b/Utilities/StringExtensions.cs @@ -1,17 +1,17 @@ using System; -namespace SUL { +namespace SUL.Utilities { - public static class StringExtensions { - public static string Reverse(this string str) { - if (string.IsNullOrEmpty(str)) - return str; +public static class StringExtensions { + public static string Reverse(this string str) { + if (string.IsNullOrEmpty(str)) + return str; - char[] charArr = str.ToCharArray(); - Array.Reverse(charArr); + char[] charArr = str.ToCharArray(); + Array.Reverse(charArr); - return new string(charArr); - } + return new string(charArr); } - } + +} // namespace