< Summary

Class:TestSceneIntegrityChecker
Assembly:TestHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Tests/TestSceneIntegrityChecker.cs
Covered lines:6
Uncovered lines:29
Coverable lines:35
Total lines:95
Line coverage:17.1% (6 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TestSceneIntegrityChecker()0%110100%
SaveSceneSnapshot()0%14.115028.57%
GetAllSceneComponents()0%42600%
TestSceneSnapshot()0%100.631109.52%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Tests/TestSceneIntegrityChecker.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEditor;
 4using UnityEngine;
 5
 6public class TestSceneIntegrityChecker
 7{
 8    private const bool DEBUG_PAUSE_ON_INTEGRITY_FAIL = false;
 5019    public bool enabled = true;
 10
 50111    protected List<Component> startingSceneComponents = new List<Component>();
 12
 13    public IEnumerator SaveSceneSnapshot()
 14    {
 50115        if (!enabled)
 50116            yield break;
 17
 18        //NOTE(Brian): to make it run faster in CI
 019        if (Application.isBatchMode)
 020            yield break;
 21
 022        yield return null;
 023        startingSceneComponents = GetAllSceneComponents();
 024    }
 25
 26    static List<Component> GetAllSceneComponents()
 27    {
 028        if (!(Resources.FindObjectsOfTypeAll(typeof(Component)) is Component[] components))
 029            return new List<Component>();
 30
 031        List<Component> result = new List<Component>();
 32
 033        foreach (Component go in components)
 34        {
 035            bool isPersistent = EditorUtility.IsPersistent(go.transform.root.gameObject);
 036            bool isEditableOrVisible = go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSav
 37
 038            if (!isPersistent && !isEditableOrVisible)
 39            {
 040                result.Add(go);
 41            }
 42        }
 43
 044        return result;
 45    }
 46
 47    public IEnumerator TestSceneSnapshot()
 48    {
 50149        if (!enabled)
 50150            yield break;
 51
 052        if (startingSceneComponents == null)
 53        {
 054            Debug.LogError("SceneIntegrityChecker fail. Check called without Saving snapshot?");
 055            yield break;
 56        }
 57
 58        //NOTE(Brian): If any Destroy() calls are pending, this will flush them.
 059        yield return null;
 60
 061        List<Component> currentObjects = GetAllSceneComponents();
 062        List<Component> newObjects = new List<Component>();
 63
 064        foreach (var o in currentObjects)
 65        {
 066            if (o.ToString().Contains("MainCamera"))
 67                continue;
 68
 069            if (!startingSceneComponents.Contains(o))
 70            {
 071                newObjects.Add(o);
 72            }
 73        }
 74
 075        if (newObjects.Count > 0)
 76        {
 077            Debug.LogError("Dangling components detected!. Look your TearDown code, you missed to destroy objects after 
 78
 79            //NOTE(Brian): Can't use asserts here because Unity Editor hangs for some reason.
 080            foreach (var o in newObjects)
 81            {
 82                if (DEBUG_PAUSE_ON_INTEGRITY_FAIL)
 83                    Debug.LogError($"Component - {o} (id: {o.GetInstanceID()}) (Click to highlight)", o.gameObject);
 84                else
 085                    Debug.LogError($"Component - {o}", o.gameObject);
 86            }
 87
 88            if (DEBUG_PAUSE_ON_INTEGRITY_FAIL)
 89            {
 90                Debug.Break();
 91                yield return null;
 92            }
 93        }
 094    }
 95}