A game about forced loneliness, made by TACStudios
1using System;
2using NUnit.Framework.Internal;
3using UnityEngine.TestRunner.NUnitExtensions.Runner;
4using UnityEngine.TestTools.Logging;
5
6namespace UnityEngine.TestTools.NUnitExtensions
7{
8 /// <summary>
9 /// Specialization of BaseDelegator that makes sure objects are created on the MainThread.
10 /// It also deals with ScriptableObjects so that tests can survive assembly reload.
11 /// </summary>
12 internal class ConstructDelegator
13 {
14 private Type m_RequestedType;
15 private object[] m_Arguments;
16
17 protected ScriptableObject m_CurrentRunningTest;
18 private readonly IStateSerializer m_StateSerializer;
19
20 protected Exception m_Exception;
21 protected object m_Result;
22 protected ITestExecutionContext m_Context;
23
24 public ConstructDelegator(IStateSerializer stateSerializer)
25 {
26 m_StateSerializer = stateSerializer;
27 }
28
29 protected object HandleResult()
30 {
31 SetCurrentTestContext();
32 if (m_Exception != null)
33 {
34 var temp = m_Exception;
35 m_Exception = null;
36 throw temp;
37 }
38 var tempResult = m_Result;
39 m_Result = null;
40 return tempResult;
41 }
42
43 protected void SetCurrentTestContext()
44 {
45 var prop = typeof(UnityTestExecutionContext).GetProperty("CurrentContext");
46 if (prop != null)
47 {
48 prop.SetValue(null, m_Context, null);
49 }
50 }
51
52 public object Delegate(Type type, object[] arguments)
53 {
54 AssertState();
55 m_Context = UnityTestExecutionContext.CurrentContext;
56
57 m_RequestedType = type;
58 m_Arguments = arguments;
59
60 using (var logScope = new LogScope())
61 {
62 Execute(logScope);
63 }
64
65 return HandleResult();
66 }
67
68 private void AssertState()
69 {
70 if (m_RequestedType != null)
71 {
72 throw new Exception("Constructor not executed yet");
73 }
74 }
75
76 public bool HasAction()
77 {
78 return m_RequestedType != null;
79 }
80
81 public void Execute(LogScope logScope)
82 {
83 try
84 {
85 if (typeof(ScriptableObject).IsAssignableFrom(m_RequestedType))
86 {
87 if (m_CurrentRunningTest != null && m_RequestedType != m_CurrentRunningTest.GetType())
88 {
89 DestroyCurrentTestObjectIfExists();
90 }
91 if (m_CurrentRunningTest == null)
92 {
93 if (m_StateSerializer.CanRestoreFromScriptableObject(m_RequestedType))
94 {
95 m_CurrentRunningTest = m_StateSerializer.RestoreScriptableObjectInstance();
96 }
97 else
98 {
99 m_CurrentRunningTest = ScriptableObject.CreateInstance(m_RequestedType);
100 }
101 }
102 m_Result = m_CurrentRunningTest;
103 }
104 else
105 {
106 DestroyCurrentTestObjectIfExists();
107 m_Result = Activator.CreateInstance(m_RequestedType, m_Arguments);
108 if (m_StateSerializer.CanRestoreFromJson(m_RequestedType))
109 {
110 m_StateSerializer.RestoreClassFromJson(ref m_Result);
111 }
112 }
113 logScope.EvaluateLogScope(true);
114 }
115 catch (Exception e)
116 {
117 m_Exception = e;
118 }
119 finally
120 {
121 m_RequestedType = null;
122 m_Arguments = null;
123 }
124 }
125
126 public void DestroyCurrentTestObjectIfExists()
127 {
128 if (m_CurrentRunningTest != null)
129 {
130 Object.DestroyImmediate(m_CurrentRunningTest);
131 }
132 }
133 }
134}