TestCaseSourceAttribute
TestCaseSourceAttribute indicates the source to be used to
provide test cases for a test method.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Builders;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace NUnit.Framework
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class TestCaseSourceAttribute : TestCaseBuilderAttribute, ITestBuilder, IImplyFixture
{
private readonly object[] _sourceConstructorParameters;
private NUnitTestCaseBuilder _builder = new NUnitTestCaseBuilder();
public string SourceName { get; set; }
public Type SourceType { get; set; }
public string Category { get; set; }
public TestCaseSourceAttribute(string sourceName)
{
SourceName = sourceName;
}
public TestCaseSourceAttribute(Type sourceType, string sourceName, params object[] constructorParameters)
{
SourceType = sourceType;
SourceName = sourceName;
_sourceConstructorParameters = constructorParameters;
}
public TestCaseSourceAttribute(Type sourceType)
{
SourceType = sourceType;
}
public IEnumerable<ITestCaseData> GetTestCasesFor(MethodInfo method)
{
List<ITestCaseData> list = new List<ITestCaseData>();
IEnumerable testCaseSource = GetTestCaseSource(method);
if (testCaseSource != null)
try {
ParameterInfo[] parameters = method.GetParameters();
foreach (object item in testCaseSource) {
ITestCaseData testCaseData = item as ITestCaseData;
ParameterSet parameterSet;
if (testCaseData != null)
parameterSet = new ParameterSet(testCaseData);
else {
object[] array = item as object[];
if (array != null) {
if (array.Length != parameters.Length)
array = new object[1] {
item
};
} else if (item is Array) {
Array array2 = item as Array;
if (array2.Rank == 1 && array2.Length == parameters.Length) {
array = new object[array2.Length];
for (int i = 0; i < array2.Length; i++) {
array[i] = array2.GetValue(i);
}
} else
array = new object[1] {
item
};
} else {
array = new object[1] {
item
};
}
parameterSet = new ParameterSet(array);
}
if (Category != null) {
string[] array3 = Category.Split(new char[1] {
','
});
foreach (string value in array3) {
parameterSet.Properties.Add("Category", value);
}
}
list.Add(parameterSet);
}
return list;
} catch (Exception exception) {
list.Clear();
list.Add(new ParameterSet(exception));
return list;
}
return list;
}
private IEnumerable GetTestCaseSource(MethodInfo method)
{
IEnumerable result = null;
Type type = SourceType;
if (type == (Type)null)
type = method.ReflectedType;
if (SourceName == null)
return Reflect.Construct(type, _sourceConstructorParameters) as IEnumerable;
MemberInfo[] member = type.GetMember(SourceName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
if (member.Length == 1) {
MemberInfo memberInfo = member[0];
object obj = Reflect.Construct(type, _sourceConstructorParameters);
switch (memberInfo.MemberType) {
case MemberTypes.Field: {
FieldInfo fieldInfo = memberInfo as FieldInfo;
result = (IEnumerable)fieldInfo.GetValue(obj);
break;
}
case MemberTypes.Property: {
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
result = (IEnumerable)propertyInfo.GetValue(obj, null);
break;
}
case MemberTypes.Method: {
MethodInfo methodInfo = memberInfo as MethodInfo;
result = (IEnumerable)methodInfo.Invoke(obj, null);
break;
}
}
}
return result;
}
public IEnumerable<TestMethod> BuildFrom(MethodInfo method, Test suite)
{
List<TestMethod> list = new List<TestMethod>();
foreach (ParameterSet item in GetTestCasesFor(method)) {
list.Add(_builder.BuildTestMethod(method, suite, item));
}
return list;
}
}
}