TestMethodCommand
TestMethodCommand is the lowest level concrete command
used to run actual test cases.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Builders;
using NUnit.Framework.Internal.Extensions;
using System;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal.Commands
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class TestMethodCommand : TestCommand
{
private readonly TestMethod _testMethod;
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
private readonly object[] _arguments;
public TestMethodCommand(TestMethod testMethod)
: base(testMethod)
{
_testMethod = testMethod;
_arguments = testMethod.Arguments;
}
public override TestResult Execute(TestExecutionContext context)
{
object actual = RunTestMethod(context);
if (_testMethod.HasExpectedResult)
Assert.That(actual, Is.EqualTo(_testMethod.ExpectedResult), default(NUnitString), "result", "Is.EqualTo(_testMethod.ExpectedResult)");
if (context.MultipleAssertLevel > 0) {
TestResult currentResult = context.CurrentResult;
ResultState error = ResultState.Error;
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(45, 1);
defaultInterpolatedStringHandler.AppendLiteral("Test completed with ");
defaultInterpolatedStringHandler.AppendFormatted(context.MultipleAssertLevel);
defaultInterpolatedStringHandler.AppendLiteral(" active assertion scopes.");
currentResult.SetResult(error, defaultInterpolatedStringHandler.ToStringAndClear());
} else
context.CurrentResult.SetResult(ResultState.Success);
if (context.CurrentResult.AssertionResults.Count > 0)
context.CurrentResult.RecordTestCompletion();
return context.CurrentResult;
}
[return: System.Runtime.CompilerServices.Nullable(2)]
private object RunTestMethod(TestExecutionContext context)
{
MethodInfoCache.TestMethodMetadata testMethodMetadata = MethodInfoCache.Get(_testMethod.Method);
bool lastParameterAcceptsCancellationToken = testMethodMetadata.Parameters.LastParameterAcceptsCancellationToken();
if (testMethodMetadata.IsAsyncOperation)
return AsyncToSyncAdapter.Await(context, () => InvokeTestMethod(context, lastParameterAcceptsCancellationToken));
return InvokeTestMethod(context, lastParameterAcceptsCancellationToken);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
private object InvokeTestMethod(TestExecutionContext context, bool lastParameterAcceptsCancellationToken)
{
object[] array = _arguments;
if (lastParameterAcceptsCancellationToken && !array.LastArgumentIsCancellationToken()) {
array = new object[_arguments.Length + 1];
Array.Copy(_arguments, array, _arguments.Length);
array[_arguments.Length] = context.CancellationToken;
}
return _testMethod.Method.Invoke(context.TestObject, array);
}
}
}