NUnitTestCaseBuilder
NUnitTestCaseBuilder is a utility class used by attributes
that build test cases.
using NUnit.Framework.Interfaces;
using System;
using System.Threading.Tasks;
namespace NUnit.Framework.Internal.Builders
{
public class NUnitTestCaseBuilder
{
private const string DEFAULT_TEST_NAME_PATTERN = "{m}{a:40}";
private readonly Randomizer _randomizer = Randomizer.CreateRandomizer();
private readonly TestNameGenerator _nameGenerator;
public NUnitTestCaseBuilder()
{
_nameGenerator = new TestNameGenerator("{m}{a:40}");
}
public TestMethod BuildTestMethod(IMethodInfo method, Test parentSuite, TestCaseParameters parms)
{
TestMethod testMethod = new TestMethod(method, parentSuite) {
Seed = _randomizer.Next()
};
CheckTestMethodSignature(testMethod, parms);
if (parms == null || parms.Arguments == null)
testMethod.ApplyAttributesToTest(method.MethodInfo);
if (parms != null) {
method = testMethod.Method;
string fullName = method.TypeInfo.FullName;
if (parentSuite != null)
fullName = parentSuite.FullName;
if (parms.TestName != null)
testMethod.Name = (parms.TestName.Contains("{") ? new TestNameGenerator(parms.TestName).GetDisplayName(testMethod, parms.OriginalArguments) : parms.TestName);
else if (parms.OriginalArguments != null) {
testMethod.Name = _nameGenerator.GetDisplayName(testMethod, parms.OriginalArguments);
}
testMethod.FullName = fullName + "." + testMethod.Name;
parms.ApplyToTest(testMethod);
}
return testMethod;
}
private static bool CheckTestMethodSignature(TestMethod testMethod, TestCaseParameters parms)
{
if (testMethod.Method.IsAbstract)
return MarkAsNotRunnable(testMethod, "Method is abstract");
if (!testMethod.Method.IsPublic)
return MarkAsNotRunnable(testMethod, "Method is not public");
IParameterInfo[] parameters = testMethod.Method.GetParameters();
int num = 0;
IParameterInfo[] array = parameters;
for (int i = 0; i < array.Length; i++) {
if (!array[i].IsOptional)
num++;
}
int num2 = parameters.Length;
object[] array2 = null;
int num3 = 0;
if (parms != null) {
testMethod.parms = parms;
testMethod.RunState = parms.RunState;
array2 = parms.Arguments;
if (array2 != null)
num3 = array2.Length;
if (testMethod.RunState != RunState.Runnable)
return false;
}
ITypeInfo returnType = testMethod.Method.ReturnType;
if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method.MethodInfo)) {
if (returnType.IsType(typeof(void)))
return MarkAsNotRunnable(testMethod, "Async test method must have non-void return type");
bool flag = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>);
if (flag && (parms == null || !parms.HasExpectedResult))
return MarkAsNotRunnable(testMethod, "Async test method must have non-generic Task return type when no result is expected");
if (!flag && parms != null && parms.HasExpectedResult)
return MarkAsNotRunnable(testMethod, "Async test method must have Task<T> return type when a result is expected");
} else if (returnType.IsType(typeof(void))) {
if (parms != null && parms.HasExpectedResult)
return MarkAsNotRunnable(testMethod, "Method returning void cannot have an expected result");
} else if (parms == null || !parms.HasExpectedResult) {
return MarkAsNotRunnable(testMethod, "Method has non-void return value, but no result is expected");
}
if (num3 > 0 && num2 == 0)
return MarkAsNotRunnable(testMethod, "Arguments provided for method not taking any");
if (num3 == 0 && num > 0)
return MarkAsNotRunnable(testMethod, "No arguments were provided");
if (num3 < num)
return MarkAsNotRunnable(testMethod, $"""{num}""");
if (num3 > num2)
return MarkAsNotRunnable(testMethod, $"""{num2}""");
if (testMethod.Method.IsGenericMethodDefinition && array2 != null) {
Type[] typeArguments = new GenericMethodHelper(testMethod.Method.MethodInfo).GetTypeArguments(array2);
Type[] array3 = typeArguments;
foreach (Type left in array3) {
if (left == (Type)null || left == TypeHelper.NonmatchingType)
return MarkAsNotRunnable(testMethod, "Unable to determine type arguments for method");
}
testMethod.Method = testMethod.Method.MakeGenericMethod(typeArguments);
parameters = testMethod.Method.GetParameters();
}
if (array2 != null && parameters != null)
TypeHelper.ConvertArgumentList(array2, parameters);
return true;
}
private static bool MarkAsNotRunnable(TestMethod testMethod, string reason)
{
testMethod.RunState = RunState.NotRunnable;
testMethod.Properties.Set("_SKIPREASON", reason);
return false;
}
}
}