No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConstructDelegator.cs 4.1KB

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