forked from ssannandeji/Zenject-2019
-
Notifications
You must be signed in to change notification settings - Fork 268
/
ZenjectTestUtil.cs
49 lines (44 loc) · 1.59 KB
/
ZenjectTestUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using ModestTree;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Zenject.Internal
{
public static class ZenjectTestUtil
{
public const string UnitTestRunnerGameObjectName = "Code-based tests runner";
public static void DestroyEverythingExceptTestRunner(bool immediate)
{
var testRunner = GameObject.Find(UnitTestRunnerGameObjectName);
Assert.IsNotNull(testRunner);
GameObject.DontDestroyOnLoad(testRunner);
// We want to clear all objects across all scenes to ensure the next test is not affected
// at all by previous tests
for (int i = 0; i < SceneManager.sceneCount; i++)
{
foreach (var obj in SceneManager.GetSceneAt(i).GetRootGameObjects())
{
GameObject.DestroyImmediate(obj);
}
}
if (ProjectContext.HasInstance)
{
var dontDestroyOnLoadRoots = ProjectContext.Instance.gameObject.scene
.GetRootGameObjects();
foreach (var rootObj in dontDestroyOnLoadRoots)
{
if (rootObj.name != UnitTestRunnerGameObjectName)
{
if (immediate)
{
GameObject.DestroyImmediate(rootObj);
}
else
{
GameObject.Destroy(rootObj);
}
}
}
}
}
}
}