Commit af571a61 authored by BlackAngle233's avatar BlackAngle233
Browse files

212

parent 1d9b5391
fileFormatVersion: 2
guid: c82a8473f4a8f7b42a004c91e06d2f2b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: c82a8473f4a8f7b42a004c91e06d2f2b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace UnityEngine.TestTools
{
internal class TestEnumerator
{
private readonly ITestExecutionContext m_Context;
private static IEnumerator m_TestEnumerator;
public static IEnumerator Enumerator { get { return m_TestEnumerator; } }
public TestEnumerator(ITestExecutionContext context, IEnumerator testEnumerator)
{
m_Context = context;
m_TestEnumerator = testEnumerator;
}
public IEnumerator Execute()
{
m_Context.CurrentResult.SetResult(ResultState.Success);
while (true)
{
object current = null;
try
{
if (!m_TestEnumerator.MoveNext())
{
yield break;
}
if (!m_Context.CurrentResult.ResultState.Equals(ResultState.Success))
{
yield break;
}
current = m_TestEnumerator.Current;
}
catch (Exception exception)
{
m_Context.CurrentResult.RecordException(exception);
yield break;
}
yield return current;
}
}
}
}
using System;
using System.Collections;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace UnityEngine.TestTools
{
internal class TestEnumerator
{
private readonly ITestExecutionContext m_Context;
private static IEnumerator m_TestEnumerator;
public static IEnumerator Enumerator { get { return m_TestEnumerator; } }
public TestEnumerator(ITestExecutionContext context, IEnumerator testEnumerator)
{
m_Context = context;
m_TestEnumerator = testEnumerator;
}
public IEnumerator Execute()
{
m_Context.CurrentResult.SetResult(ResultState.Success);
while (true)
{
object current = null;
try
{
if (!m_TestEnumerator.MoveNext())
{
yield break;
}
if (!m_Context.CurrentResult.ResultState.Equals(ResultState.Success))
{
yield break;
}
current = m_TestEnumerator.Current;
}
catch (Exception exception)
{
m_Context.CurrentResult.RecordException(exception);
yield break;
}
yield return current;
}
}
}
}
fileFormatVersion: 2
guid: 750aad009559b814dbc27001341fc1c3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 750aad009559b814dbc27001341fc1c3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
namespace UnityEngine.TestTools
{
/// <summary>
/// The presence of this attribute will cause the test runner to require that every single log is expected. By
/// default, the runner will only automatically fail on any error logs, so this adds warnings and infos as well.
/// It is the same as calling `LogAssert.NoUnexpectedReceived()` at the bottom of every affected test.
///
/// This attribute can be applied to test assemblies (will affect every test in the assembly), fixtures (will
/// affect every test in the fixture), or on individual test methods. It is also automatically inherited from base
/// fixtures.
///
/// The MustExpect property (on by default) lets you selectively enable or disable the higher level value. For
/// example when migrating an assembly to this more strict checking method, you might attach
/// `[assembly:TestMustExpectAllLogs]` to the assembly itself, but then whitelist failing fixtures and test methods
/// with `[TestMustExpectAllLogs(MustExpect=false)]` until they can be migrated. This also means new tests in that
/// assembly would be required to have the more strict checking.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class TestMustExpectAllLogsAttribute : Attribute
{
public TestMustExpectAllLogsAttribute(bool mustExpect = true)
=> MustExpect = mustExpect;
public bool MustExpect { get; }
}
}
using System;
namespace UnityEngine.TestTools
{
/// <summary>
/// The presence of this attribute will cause the test runner to require that every single log is expected. By
/// default, the runner will only automatically fail on any error logs, so this adds warnings and infos as well.
/// It is the same as calling `LogAssert.NoUnexpectedReceived()` at the bottom of every affected test.
///
/// This attribute can be applied to test assemblies (will affect every test in the assembly), fixtures (will
/// affect every test in the fixture), or on individual test methods. It is also automatically inherited from base
/// fixtures.
///
/// The MustExpect property (on by default) lets you selectively enable or disable the higher level value. For
/// example when migrating an assembly to this more strict checking method, you might attach
/// `[assembly:TestMustExpectAllLogs]` to the assembly itself, but then whitelist failing fixtures and test methods
/// with `[TestMustExpectAllLogs(MustExpect=false)]` until they can be migrated. This also means new tests in that
/// assembly would be required to have the more strict checking.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class TestMustExpectAllLogsAttribute : Attribute
{
public TestMustExpectAllLogsAttribute(bool mustExpect = true)
=> MustExpect = mustExpect;
public bool MustExpect { get; }
}
}
fileFormatVersion: 2
guid: 3803f736886e77842995ddbc3531afaa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 3803f736886e77842995ddbc3531afaa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Builders;
namespace UnityEngine.TestTools
{
internal class UnityCombinatorialStrategy : CombinatorialStrategy, ICombiningStrategy
{
public new IEnumerable<ITestCaseData> GetTestCases(IEnumerable[] sources)
{
var testCases = base.GetTestCases(sources);
foreach (var testCase in testCases)
{
testCase.GetType().GetProperty("ExpectedResult").SetValue(testCase, new object(), null);
}
return testCases;
}
}
}
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Builders;
namespace UnityEngine.TestTools
{
internal class UnityCombinatorialStrategy : CombinatorialStrategy, ICombiningStrategy
{
public new IEnumerable<ITestCaseData> GetTestCases(IEnumerable[] sources)
{
var testCases = base.GetTestCases(sources);
foreach (var testCase in testCases)
{
testCase.GetType().GetProperty("ExpectedResult").SetValue(testCase, new object(), null);
}
return testCases;
}
}
}
fileFormatVersion: 2
guid: 7af6ac3e6b51b8d4aab04adc85b8de2f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 7af6ac3e6b51b8d4aab04adc85b8de2f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace UnityEngine.TestTools
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class UnityPlatformAttribute : NUnitAttribute, IApplyToTest
{
public RuntimePlatform[] include { get; set; }
public RuntimePlatform[] exclude { get; set; }
private string m_skippedReason;
public UnityPlatformAttribute()
{
include = new List<RuntimePlatform>().ToArray();
exclude = new List<RuntimePlatform>().ToArray();
}
public UnityPlatformAttribute(params RuntimePlatform[] include)
: this()
{
this.include = include;
}
public void ApplyToTest(Test test)
{
if (test.RunState == RunState.NotRunnable || test.RunState == RunState.Ignored || IsPlatformSupported(Application.platform))
{
return;
}
test.RunState = RunState.Skipped;
test.Properties.Add("_SKIPREASON", m_skippedReason);
}
internal bool IsPlatformSupported(RuntimePlatform testTargetPlatform)
{
if (include.Any() && !include.Any(x => x == testTargetPlatform))
{
m_skippedReason = string.Format("Only supported on {0}", string.Join(", ", include.Select(x => x.ToString()).ToArray()));
return false;
}
if (exclude.Any(x => x == testTargetPlatform))
{
m_skippedReason = string.Format("Not supported on {0}", string.Join(", ", include.Select(x => x.ToString()).ToArray()));
return false;
}
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace UnityEngine.TestTools
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class UnityPlatformAttribute : NUnitAttribute, IApplyToTest
{
public RuntimePlatform[] include { get; set; }
public RuntimePlatform[] exclude { get; set; }
private string m_skippedReason;
public UnityPlatformAttribute()
{
include = new List<RuntimePlatform>().ToArray();
exclude = new List<RuntimePlatform>().ToArray();
}
public UnityPlatformAttribute(params RuntimePlatform[] include)
: this()
{
this.include = include;
}
public void ApplyToTest(Test test)
{
if (test.RunState == RunState.NotRunnable || test.RunState == RunState.Ignored || IsPlatformSupported(Application.platform))
{
return;
}
test.RunState = RunState.Skipped;
test.Properties.Add("_SKIPREASON", m_skippedReason);
}
internal bool IsPlatformSupported(RuntimePlatform testTargetPlatform)
{
if (include.Any() && !include.Any(x => x == testTargetPlatform))
{
m_skippedReason = string.Format("Only supported on {0}", string.Join(", ", include.Select(x => x.ToString()).ToArray()));
return false;
}
if (exclude.Any(x => x == testTargetPlatform))
{
m_skippedReason = string.Format("Not supported on {0}", string.Join(", ", include.Select(x => x.ToString()).ToArray()));
return false;
}
return true;
}
}
}
fileFormatVersion: 2
guid: 5440c1153b397e14c9c7b1d6eb83b9f9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 5440c1153b397e14c9c7b1d6eb83b9f9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using NUnit.Framework;
namespace UnityEngine.TestTools
{
[AttributeUsage(AttributeTargets.Method)]
public class UnitySetUpAttribute : NUnitAttribute
{
}
}
using System;
using NUnit.Framework;
namespace UnityEngine.TestTools
{
[AttributeUsage(AttributeTargets.Method)]
public class UnitySetUpAttribute : NUnitAttribute
{
}
}
fileFormatVersion: 2
guid: cc6401f13df54ba44bfd7cdc93c7d64d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: cc6401f13df54ba44bfd7cdc93c7d64d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using NUnit.Framework;
namespace UnityEngine.TestTools
{
[AttributeUsage(AttributeTargets.Method)]
public class UnityTearDownAttribute : NUnitAttribute
{
}
}
using System;
using NUnit.Framework;
namespace UnityEngine.TestTools
{
[AttributeUsage(AttributeTargets.Method)]
public class UnityTearDownAttribute : NUnitAttribute
{
}
}
fileFormatVersion: 2
guid: 600f4b74746dbf944901257f81a8af6d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 600f4b74746dbf944901257f81a8af6d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using NUnit.Framework;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Builders;
using UnityEngine.TestRunner.NUnitExtensions.Runner;
namespace UnityEngine.TestTools
{
[AttributeUsage(AttributeTargets.Method)]
public class UnityTestAttribute : CombiningStrategyAttribute, ISimpleTestBuilder, IImplyFixture
{
public UnityTestAttribute() : base(new UnityCombinatorialStrategy(), new ParameterDataSourceProvider()) {}
private readonly NUnitTestCaseBuilder _builder = new NUnitTestCaseBuilder();
TestMethod ISimpleTestBuilder.BuildFrom(IMethodInfo method, Test suite)
{
TestCaseParameters parms = new TestCaseParameters
{
ExpectedResult = new object(),
HasExpectedResult = true
};
var t = _builder.BuildTestMethod(method, suite, parms);
if (t.parms != null)
t.parms.HasExpectedResult = false;
return t;
}
}
}
using System;
using NUnit.Framework;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Builders;
using UnityEngine.TestRunner.NUnitExtensions.Runner;
namespace UnityEngine.TestTools
{
[AttributeUsage(AttributeTargets.Method)]
public class UnityTestAttribute : CombiningStrategyAttribute, ISimpleTestBuilder, IImplyFixture
{
public UnityTestAttribute() : base(new UnityCombinatorialStrategy(), new ParameterDataSourceProvider()) {}
private readonly NUnitTestCaseBuilder _builder = new NUnitTestCaseBuilder();
TestMethod ISimpleTestBuilder.BuildFrom(IMethodInfo method, Test suite)
{
TestCaseParameters parms = new TestCaseParameters
{
ExpectedResult = new object(),
HasExpectedResult = true
};
var t = _builder.BuildTestMethod(method, suite, parms);
if (t.parms != null)
t.parms.HasExpectedResult = false;
return t;
}
}
}
fileFormatVersion: 2
guid: fedb0f9e5006b1943abae52f52f08a1a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: fedb0f9e5006b1943abae52f52f08a1a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Threading;
using NUnit.Framework.Internal;
namespace UnityEngine.TestTools.NUnitExtensions
{
internal abstract class BaseDelegator
{
protected ManualResetEvent m_Signal = new ManualResetEvent(false);
protected object m_Result;
protected Exception m_Exception;
protected ITestExecutionContext m_Context;
protected bool m_Aborted;
protected object HandleResult()
{
SetCurrentTestContext();
if (m_Exception != null)
{
var temp = m_Exception;
m_Exception = null;
throw temp;
}
var tempResult = m_Result;
m_Result = null;
return tempResult;
}
protected void WaitForSignal()
{
while (!m_Signal.WaitOne(100))
{
if (m_Aborted)
{
m_Aborted = false;
Reflect.MethodCallWrapper = null;
throw new Exception();
}
}
}
public void Abort()
{
m_Aborted = true;
}
protected void SetCurrentTestContext()
{
var prop = typeof(TestExecutionContext).GetProperty("CurrentContext");
if (prop != null)
{
prop.SetValue(null, m_Context, null);
}
}
}
}
using System;
using System.Threading;
using NUnit.Framework.Internal;
namespace UnityEngine.TestTools.NUnitExtensions
{
internal abstract class BaseDelegator
{
protected ManualResetEvent m_Signal = new ManualResetEvent(false);
protected object m_Result;
protected Exception m_Exception;
protected ITestExecutionContext m_Context;
protected bool m_Aborted;
protected object HandleResult()
{
SetCurrentTestContext();
if (m_Exception != null)
{
var temp = m_Exception;
m_Exception = null;
throw temp;
}
var tempResult = m_Result;
m_Result = null;
return tempResult;
}
protected void WaitForSignal()
{
while (!m_Signal.WaitOne(100))
{
if (m_Aborted)
{
m_Aborted = false;
Reflect.MethodCallWrapper = null;
throw new Exception();
}
}
}
public void Abort()
{
m_Aborted = true;
}
protected void SetCurrentTestContext()
{
var prop = typeof(TestExecutionContext).GetProperty("CurrentContext");
if (prop != null)
{
prop.SetValue(null, m_Context, null);
}
}
}
}
fileFormatVersion: 2
guid: 37cea569bfefafe49a1513c4d7f0e9eb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 37cea569bfefafe49a1513c4d7f0e9eb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 6b72875690e0f7343911e06af3145bd5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 6b72875690e0f7343911e06af3145bd5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections;
using System.Linq;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using UnityEngine.TestRunner.NUnitExtensions;
using UnityEngine.TestRunner.NUnitExtensions.Runner;
using UnityEngine.TestTools.Logging;
using UnityEngine.TestTools.TestRunner;
namespace UnityEngine.TestTools
{
internal abstract class BeforeAfterTestCommandBase<T> : DelegatingTestCommand, IEnumerableTestMethodCommand
{
private string m_BeforeErrorPrefix;
private string m_AfterErrorPrefix;
private bool m_SkipYieldAfterActions;
protected BeforeAfterTestCommandBase(TestCommand innerCommand, string beforeErrorPrefix, string afterErrorPrefix, bool skipYieldAfterActions = false)
: base(innerCommand)
{
m_BeforeErrorPrefix = beforeErrorPrefix;
m_AfterErrorPrefix = afterErrorPrefix;
m_SkipYieldAfterActions = skipYieldAfterActions;
}
protected T[] BeforeActions = new T[0];
protected T[] AfterActions = new T[0];
protected abstract IEnumerator InvokeBefore(T action, Test test, UnityTestExecutionContext context);
protected abstract IEnumerator InvokeAfter(T action, Test test, UnityTestExecutionContext context);
protected abstract BeforeAfterTestCommandState GetState(UnityTestExecutionContext context);
public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
{
var unityContext = (UnityTestExecutionContext)context;
var state = GetState(unityContext);
if (state == null)
{
// We do not expect a state to exist in playmode
state = ScriptableObject.CreateInstance<BeforeAfterTestCommandState>();
}
state.ApplyTestResult(context.CurrentResult);
while (state.NextBeforeStepIndex < BeforeActions.Length)
{
var action = BeforeActions[state.NextBeforeStepIndex];
var enumerator = InvokeBefore(action, Test, unityContext);
ActivePcHelper.SetEnumeratorPC(enumerator, state.NextBeforeStepPc);
using (var logScope = new LogScope())
{
while (true)
{
try
{
if (!enumerator.MoveNext())
{
break;
}
}
catch (Exception ex)
{
state.TestHasRun = true;
context.CurrentResult.RecordPrefixedException(m_BeforeErrorPrefix, ex);
state.StoreTestResult(context.CurrentResult);
break;
}
state.NextBeforeStepPc = ActivePcHelper.GetEnumeratorPC(enumerator);
state.StoreTestResult(context.CurrentResult);
if (m_SkipYieldAfterActions)
{
break;
}
else
{
yield return enumerator.Current;
}
}
if (logScope.AnyFailingLogs())
{
state.TestHasRun = true;
context.CurrentResult.RecordPrefixedError(m_BeforeErrorPrefix, new UnhandledLogMessageException(logScope.FailingLogs.First()).Message);
state.StoreTestResult(context.CurrentResult);
}
}
state.NextBeforeStepIndex++;
state.NextBeforeStepPc = 0;
}
if (!state.TestHasRun)
{
if (innerCommand is IEnumerableTestMethodCommand)
{
var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
foreach (var iterator in executeEnumerable)
{
state.StoreTestResult(context.CurrentResult);
yield return iterator;
}
}
else
{
context.CurrentResult = innerCommand.Execute(context);
state.StoreTestResult(context.CurrentResult);
}
state.TestHasRun = true;
}
while (state.NextAfterStepIndex < AfterActions.Length)
{
state.TestAfterStarted = true;
var action = AfterActions[state.NextAfterStepIndex];
var enumerator = InvokeAfter(action, Test, unityContext);
ActivePcHelper.SetEnumeratorPC(enumerator, state.NextAfterStepPc);
using (var logScope = new LogScope())
{
while (true)
{
try
{
if (!enumerator.MoveNext())
{
break;
}
}
catch (Exception ex)
{
context.CurrentResult.RecordPrefixedException(m_AfterErrorPrefix, ex);
state.StoreTestResult(context.CurrentResult);
break;
}
state.NextAfterStepPc = ActivePcHelper.GetEnumeratorPC(enumerator);
state.StoreTestResult(context.CurrentResult);
if (m_SkipYieldAfterActions)
{
break;
}
else
{
yield return enumerator.Current;
}
}
if (logScope.AnyFailingLogs())
{
state.TestHasRun = true;
context.CurrentResult.RecordPrefixedError(m_AfterErrorPrefix, new UnhandledLogMessageException(logScope.FailingLogs.First()).Message);
state.StoreTestResult(context.CurrentResult);
}
}
state.NextAfterStepIndex++;
state.NextAfterStepPc = 0;
}
state.Reset();
}
public override TestResult Execute(ITestExecutionContext context)
{
throw new NotImplementedException("Use ExecuteEnumerable");
}
private static TestCommandPcHelper pcHelper;
internal static TestCommandPcHelper ActivePcHelper
{
get
{
if (pcHelper == null)
{
pcHelper = new TestCommandPcHelper();
}
return pcHelper;
}
set
{
pcHelper = value;
}
}
}
}
using System;
using System.Collections;
using System.Linq;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using UnityEngine.TestRunner.NUnitExtensions;
using UnityEngine.TestRunner.NUnitExtensions.Runner;
using UnityEngine.TestTools.Logging;
using UnityEngine.TestTools.TestRunner;
namespace UnityEngine.TestTools
{
internal abstract class BeforeAfterTestCommandBase<T> : DelegatingTestCommand, IEnumerableTestMethodCommand
{
private string m_BeforeErrorPrefix;
private string m_AfterErrorPrefix;
private bool m_SkipYieldAfterActions;
protected BeforeAfterTestCommandBase(TestCommand innerCommand, string beforeErrorPrefix, string afterErrorPrefix, bool skipYieldAfterActions = false)
: base(innerCommand)
{
m_BeforeErrorPrefix = beforeErrorPrefix;
m_AfterErrorPrefix = afterErrorPrefix;
m_SkipYieldAfterActions = skipYieldAfterActions;
}
protected T[] BeforeActions = new T[0];
protected T[] AfterActions = new T[0];
protected abstract IEnumerator InvokeBefore(T action, Test test, UnityTestExecutionContext context);
protected abstract IEnumerator InvokeAfter(T action, Test test, UnityTestExecutionContext context);
protected abstract BeforeAfterTestCommandState GetState(UnityTestExecutionContext context);
public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
{
var unityContext = (UnityTestExecutionContext)context;
var state = GetState(unityContext);
if (state == null)
{
// We do not expect a state to exist in playmode
state = ScriptableObject.CreateInstance<BeforeAfterTestCommandState>();
}
state.ApplyTestResult(context.CurrentResult);
while (state.NextBeforeStepIndex < BeforeActions.Length)
{
var action = BeforeActions[state.NextBeforeStepIndex];
var enumerator = InvokeBefore(action, Test, unityContext);
ActivePcHelper.SetEnumeratorPC(enumerator, state.NextBeforeStepPc);
using (var logScope = new LogScope())
{
while (true)
{
try
{
if (!enumerator.MoveNext())
{
break;
}
}
catch (Exception ex)
{
state.TestHasRun = true;
context.CurrentResult.RecordPrefixedException(m_BeforeErrorPrefix, ex);
state.StoreTestResult(context.CurrentResult);
break;
}
state.NextBeforeStepPc = ActivePcHelper.GetEnumeratorPC(enumerator);
state.StoreTestResult(context.CurrentResult);
if (m_SkipYieldAfterActions)
{
break;
}
else
{
yield return enumerator.Current;
}
}
if (logScope.AnyFailingLogs())
{
state.TestHasRun = true;
context.CurrentResult.RecordPrefixedError(m_BeforeErrorPrefix, new UnhandledLogMessageException(logScope.FailingLogs.First()).Message);
state.StoreTestResult(context.CurrentResult);
}
}
state.NextBeforeStepIndex++;
state.NextBeforeStepPc = 0;
}
if (!state.TestHasRun)
{
if (innerCommand is IEnumerableTestMethodCommand)
{
var executeEnumerable = ((IEnumerableTestMethodCommand)innerCommand).ExecuteEnumerable(context);
foreach (var iterator in executeEnumerable)
{
state.StoreTestResult(context.CurrentResult);
yield return iterator;
}
}
else
{
context.CurrentResult = innerCommand.Execute(context);
state.StoreTestResult(context.CurrentResult);
}
state.TestHasRun = true;
}
while (state.NextAfterStepIndex < AfterActions.Length)
{
state.TestAfterStarted = true;
var action = AfterActions[state.NextAfterStepIndex];
var enumerator = InvokeAfter(action, Test, unityContext);
ActivePcHelper.SetEnumeratorPC(enumerator, state.NextAfterStepPc);
using (var logScope = new LogScope())
{
while (true)
{
try
{
if (!enumerator.MoveNext())
{
break;
}
}
catch (Exception ex)
{
context.CurrentResult.RecordPrefixedException(m_AfterErrorPrefix, ex);
state.StoreTestResult(context.CurrentResult);
break;
}
state.NextAfterStepPc = ActivePcHelper.GetEnumeratorPC(enumerator);
state.StoreTestResult(context.CurrentResult);
if (m_SkipYieldAfterActions)
{
break;
}
else
{
yield return enumerator.Current;
}
}
if (logScope.AnyFailingLogs())
{
state.TestHasRun = true;
context.CurrentResult.RecordPrefixedError(m_AfterErrorPrefix, new UnhandledLogMessageException(logScope.FailingLogs.First()).Message);
state.StoreTestResult(context.CurrentResult);
}
}
state.NextAfterStepIndex++;
state.NextAfterStepPc = 0;
}
state.Reset();
}
public override TestResult Execute(ITestExecutionContext context)
{
throw new NotImplementedException("Use ExecuteEnumerable");
}
private static TestCommandPcHelper pcHelper;
internal static TestCommandPcHelper ActivePcHelper
{
get
{
if (pcHelper == null)
{
pcHelper = new TestCommandPcHelper();
}
return pcHelper;
}
set
{
pcHelper = value;
}
}
}
}
fileFormatVersion: 2
guid: cbbca1d8a0434be4bbc7f165523763ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: cbbca1d8a0434be4bbc7f165523763ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment