NUnitTestCaseBuilder
NUnitTestCaseBuilder is a utility class used by attributes
that build test cases.
using NUnit.Framework.Interfaces;
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace NUnit.Framework.Internal.Builders
{
public class NUnitTestCaseBuilder
{
private readonly Randomizer randomizer = Randomizer.CreateRandomizer();
public TestMethod BuildTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms)
{
TestMethod testMethod = new TestMethod(method, parentSuite);
testMethod.Seed = randomizer.Next();
TestMethod testMethod2 = testMethod;
string fullName = method.ReflectedType.FullName;
if (parentSuite != null)
fullName = parentSuite.FullName;
if (CheckTestMethodSignature(testMethod2, parms) && (parms == null || parms.Arguments == null))
testMethod2.ApplyAttributesToTest(method);
if (parms != null) {
method = testMethod2.Method;
if (parms.TestName != null) {
testMethod2.Name = parms.TestName;
testMethod2.FullName = fullName + "." + parms.TestName;
} else if (parms.OriginalArguments != null) {
string str = testMethod2.Name = MethodHelper.GetDisplayName(method, parms.OriginalArguments);
testMethod2.FullName = fullName + "." + str;
}
parms.ApplyToTest(testMethod2);
}
return testMethod2;
}
private static bool CheckTestMethodSignature(TestMethod testMethod, ParameterSet parms)
{
if (testMethod.Method.IsAbstract)
return MarkAsNotRunnable(testMethod, "Method is abstract");
if (!testMethod.Method.IsPublic)
return MarkAsNotRunnable(testMethod, "Method is not public");
ParameterInfo[] parameters = testMethod.Method.GetParameters();
int num = parameters.Length;
object[] array = null;
int num2 = 0;
if (parms != null) {
testMethod.parms = parms;
testMethod.RunState = parms.RunState;
array = parms.Arguments;
if (array != null)
num2 = array.Length;
if (testMethod.RunState != RunState.Runnable)
return false;
}
Type returnType = testMethod.Method.ReturnType;
if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method)) {
if (returnType == 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 == 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 (num2 > 0 && num == 0)
return MarkAsNotRunnable(testMethod, "Arguments provided for method not taking any");
if (num2 == 0 && num > 0)
return MarkAsNotRunnable(testMethod, "No arguments were provided");
if (num2 != num)
return MarkAsNotRunnable(testMethod, "Wrong number of arguments provided");
if (testMethod.Method.IsGenericMethodDefinition) {
Type[] typeArgumentsForMethod = GetTypeArgumentsForMethod(testMethod.Method, array);
Type[] array2 = typeArgumentsForMethod;
foreach (object obj in array2) {
if (obj == null)
return MarkAsNotRunnable(testMethod, "Unable to determine type arguments for method");
}
testMethod.Method = testMethod.Method.MakeGenericMethod(typeArgumentsForMethod);
parameters = testMethod.Method.GetParameters();
}
if (array != null && parameters != null)
TypeHelper.ConvertArgumentList(array, parameters);
return true;
}
private static Type[] GetTypeArgumentsForMethod(MethodInfo method, object[] arglist)
{
Type[] genericArguments = method.GetGenericArguments();
Type[] array = new Type[genericArguments.Length];
ParameterInfo[] parameters = method.GetParameters();
for (int i = 0; i < array.Length; i++) {
Type o = genericArguments[i];
for (int j = 0; j < parameters.Length; j++) {
if (parameters[j].ParameterType.Equals(o)) {
Type type = (arglist[j] != null) ? arglist[j].GetType() : null;
array[i] = TypeHelper.BestCommonType(array[i], type);
}
}
}
return array;
}
private static bool MarkAsNotRunnable(TestMethod testMethod, string reason)
{
testMethod.RunState = RunState.NotRunnable;
testMethod.Properties.Set("_SKIPREASON", reason);
return false;
}
}
}