namespace UnityHelpers.Tests.Helper { using System.Collections; using JetBrains.Annotations; using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; using UnityHelpers.Core.Helper; [UsedImplicitly] public sealed class ObjectHelperComponent : MonoBehaviour { } public sealed class ObjectHelperTests { [UnityTest] public IEnumerator HasComponent() { GameObject go = new("Test SpriteRenderer", typeof(SpriteRenderer)); SpriteRenderer spriteRenderer = go.GetComponent(); Assert.IsTrue(go.HasComponent(typeof(SpriteRenderer))); Assert.IsTrue(go.HasComponent()); Assert.IsTrue(spriteRenderer.HasComponent()); Assert.IsTrue(spriteRenderer.HasComponent(typeof(SpriteRenderer))); Assert.IsFalse(go.HasComponent()); Assert.IsFalse(go.HasComponent(typeof(LineRenderer))); Assert.IsFalse(spriteRenderer.HasComponent()); Assert.IsFalse(spriteRenderer.HasComponent(typeof(LineRenderer))); Object obj = go; Assert.IsTrue(obj.HasComponent()); Assert.IsTrue(obj.HasComponent(typeof(SpriteRenderer))); Assert.IsFalse(obj.HasComponent()); Assert.IsFalse(obj.HasComponent(typeof(LineRenderer))); yield break; } [UnityTest] public IEnumerator EnableRendererRecursively() { GameObject one = New("1"); GameObject two = New("2"); two.transform.SetParent(one.transform); GameObject three = New("3"); three.transform.SetParent(two.transform); GameObject four = New("4"); four.transform.SetParent(three.transform); // Act two.transform.EnableRendererRecursively(false); Assert.IsTrue(one.GetComponent().enabled); Assert.IsFalse(two.GetComponent().enabled); Assert.IsFalse(three.GetComponent().enabled); Assert.IsFalse(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); // Act three.transform.EnableRendererRecursively(true); Assert.IsTrue(one.GetComponent().enabled); Assert.IsFalse(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); // Act one.transform.EnableRendererRecursively(true); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); // Act two.transform.EnableRendererRecursively( false, renderer => renderer.gameObject == three ); Assert.IsTrue(one.GetComponent().enabled); Assert.IsFalse(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsFalse(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); // Act one.transform.EnableRendererRecursively( true, renderer => renderer.gameObject == four ); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsFalse(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); yield break; GameObject New(string name) { return new GameObject(name, typeof(SpriteRenderer), typeof(CircleCollider2D)); } } [UnityTest] public IEnumerator EnableRecursively() { GameObject one = New("1"); GameObject two = New("2"); two.transform.SetParent(one.transform); GameObject three = New("3"); three.transform.SetParent(two.transform); GameObject four = New("4"); four.transform.SetParent(three.transform); // Act two.transform.EnableRecursively(false); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsFalse(two.GetComponent().enabled); Assert.IsFalse(three.GetComponent().enabled); Assert.IsFalse(four.GetComponent().enabled); // Act three.transform.EnableRecursively(true); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsFalse(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); // Act one.transform.EnableRecursively(true); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); // Act two.transform.EnableRecursively( false, collider => collider.gameObject == three ); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsFalse(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsFalse(four.GetComponent().enabled); // Act one.transform.EnableRecursively( true, collider => collider.gameObject == four ); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsTrue(four.GetComponent().enabled); Assert.IsTrue(one.GetComponent().enabled); Assert.IsTrue(two.GetComponent().enabled); Assert.IsTrue(three.GetComponent().enabled); Assert.IsFalse(four.GetComponent().enabled); yield break; GameObject New(string name) { return new GameObject(name, typeof(SpriteRenderer), typeof(CircleCollider2D)); } } [UnityTest] public IEnumerator DestroyAllChildGameObjects() { GameObject one = new("1"); GameObject two = new("2"); two.transform.SetParent(one.transform); GameObject three = new("3"); three.transform.SetParent(two.transform); GameObject four = new("4"); four.transform.SetParent(three.transform); // Act two.DestroyAllChildrenGameObjects(); yield return null; Assert.IsTrue(one != null); Assert.IsTrue(two != null); Assert.IsTrue(three == null); Assert.IsTrue(four == null); three = new GameObject("3"); three.transform.SetParent(two.transform); four = new GameObject("4"); four.transform.SetParent(three.transform); // Act one.DestroyAllChildrenGameObjects(); yield return null; Assert.IsTrue(one != null); Assert.IsTrue(two == null); Assert.IsTrue(three == null); Assert.IsTrue(four == null); } [UnityTest] public IEnumerator DestroyAllComponentsOfType() { GameObject one = New("1"); Assert.AreEqual(4, one.GetComponents().Length); GameObject two = New("2"); two.transform.SetParent(one.transform); one.DestroyAllComponentsOfType(); yield return null; Assert.AreEqual(0, one.GetComponents().Length); Assert.IsTrue(one.GetComponent() != null); Assert.AreEqual(4, two.GetComponents().Length); two.DestroyAllComponentsOfType(); yield return null; Assert.AreEqual(0, one.GetComponents().Length); Assert.IsTrue(one.GetComponent() != null); Assert.AreEqual(0, two.GetComponents().Length); Assert.IsTrue(two.GetComponent() != null); GameObject New(string name) { return new GameObject( name, typeof(SpriteRenderer), typeof(ObjectHelperComponent), typeof(ObjectHelperComponent), typeof(ObjectHelperComponent), typeof(ObjectHelperComponent) ); } } [UnityTest] public IEnumerator SmartDestroy() { GameObject one = new("1"); one.SmartDestroy(); yield return null; Assert.IsTrue(one == null); GameObject two = new("2"); two.SmartDestroy(1.5f); yield return null; Assert.IsTrue(two != null); yield return new WaitForSeconds(1.6f); Assert.IsTrue(two == null); } [UnityTest] public IEnumerator DestroyAllChildrenGameObjectsImmediatelyConditionally() { GameObject one = new("1"); GameObject two = new("2"); two.transform.SetParent(one.transform); GameObject three = new("3"); three.transform.SetParent(two.transform); GameObject four = new("4"); four.transform.SetParent(two.transform); two.DestroyAllChildrenGameObjectsImmediatelyConditionally(go => go == four); Assert.IsTrue(one != null); Assert.IsTrue(two != null); Assert.IsTrue(three != null); Assert.IsTrue(four == null); one.DestroyAllChildrenGameObjectsImmediatelyConditionally(go => go != two); Assert.IsTrue(one != null); Assert.IsTrue(two != null); Assert.IsTrue(three != null); Assert.IsTrue(four == null); one.DestroyAllChildrenGameObjectsImmediatelyConditionally(go => go == two); Assert.IsTrue(one != null); Assert.IsTrue(two == null); Assert.IsTrue(three == null); Assert.IsTrue(four == null); yield break; } [UnityTest] public IEnumerator DestroyAllChildGameObjectsConditionally() { GameObject one = new("1"); GameObject two = new("2"); two.transform.SetParent(one.transform); GameObject three = new("3"); three.transform.SetParent(two.transform); GameObject four = new("4"); four.transform.SetParent(two.transform); two.DestroyAllChildGameObjectsConditionally(go => go == four); yield return null; Assert.IsTrue(one != null); Assert.IsTrue(two != null); Assert.IsTrue(three != null); Assert.IsTrue(four == null); one.DestroyAllChildGameObjectsConditionally(go => go != two); yield return null; Assert.IsTrue(one != null); Assert.IsTrue(two != null); Assert.IsTrue(three != null); Assert.IsTrue(four == null); one.DestroyAllChildGameObjectsConditionally(go => go == two); yield return null; Assert.IsTrue(one != null); Assert.IsTrue(two == null); Assert.IsTrue(three == null); Assert.IsTrue(four == null); } [UnityTest] public IEnumerator DestroyAllChildrenGameObjectsImmediately() { GameObject one = new("1"); GameObject two = new("2"); two.transform.SetParent(one.transform); GameObject three = new("3"); three.transform.SetParent(two.transform); GameObject four = new("4"); four.transform.SetParent(two.transform); two.DestroyAllChildrenGameObjectsImmediately(); Assert.IsTrue(one != null); Assert.IsTrue(two != null); Assert.IsTrue(three == null); Assert.IsTrue(four == null); three = new GameObject("3"); three.transform.SetParent(two.transform); four = new("4"); four.transform.SetParent(two.transform); one.DestroyAllChildrenGameObjectsImmediately(); Assert.IsTrue(one != null); Assert.IsTrue(two == null); Assert.IsTrue(three == null); Assert.IsTrue(four == null); yield break; } } }