Ingen beskrivning
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

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