Test
The Test abstract class represents a test within the framework.
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace NUnit.Framework.Internal
{
public abstract class Test : ITest, IXmlNodeBuilder, IComparable
{
private static int _nextID = 1000;
protected ITypeInfo DeclaringTypeInfo;
private IMethodInfo _method;
private ITestAction[] _actions;
public string Id { get; set; }
public string Name { get; set; }
public string FullName { get; set; }
public string ClassName {
get {
ITypeInfo typeInfo = TypeInfo;
if (Method != null) {
if (DeclaringTypeInfo == null)
DeclaringTypeInfo = new TypeWrapper(Method.MethodInfo.DeclaringType);
typeInfo = DeclaringTypeInfo;
}
if (typeInfo == null)
return null;
if (!typeInfo.IsGenericType)
return typeInfo.FullName;
return typeInfo.GetGenericTypeDefinition().FullName;
}
}
public virtual string MethodName => null;
public abstract object[] Arguments { get; }
public ITypeInfo TypeInfo { get; set; }
public IMethodInfo Method {
get {
return _method;
}
set {
DeclaringTypeInfo = null;
_method = value;
}
}
public RunState RunState { get; set; }
public abstract string XmlElementName { get; }
public virtual string TestType => GetType().Name;
public virtual int TestCaseCount => 1;
public IPropertyBag Properties { get; set; }
public bool IsSuite => this is TestSuite;
public abstract bool HasChildren { get; }
public ITest Parent { get; set; }
public abstract IList<ITest> Tests { get; }
public virtual object Fixture { get; set; }
public static string IdPrefix { get; set; }
public int Seed { get; set; }
public MethodInfo[] SetUpMethods { get; set; }
public MethodInfo[] TearDownMethods { get; set; }
internal bool RequiresThread { get; set; }
internal ITestAction[] Actions {
get {
if (_actions == null)
_actions = ((Method == null && TypeInfo != null) ? GetActionsForType(TypeInfo.Type) : GetCustomAttributes<ITestAction>(false));
return _actions;
}
}
protected Test(string name)
{
Guard.ArgumentNotNullOrEmpty(name, "name");
Initialize(name);
}
protected Test(string pathName, string name)
{
Initialize(name);
if (!string.IsNullOrEmpty(pathName))
FullName = pathName + "." + name;
}
protected Test(ITypeInfo typeInfo)
{
Initialize(typeInfo.GetDisplayName());
string namespace = typeInfo.Namespace;
if (namespace != null && namespace != "")
FullName = namespace + "." + Name;
TypeInfo = typeInfo;
}
protected Test(IMethodInfo method)
{
Initialize(method.Name);
Method = method;
TypeInfo = method.TypeInfo;
FullName = method.TypeInfo.FullName + "." + Name;
}
private void Initialize(string name)
{
string text3 = FullName = (Name = name);
Id = GetNextId();
Properties = new PropertyBag();
RunState = RunState.Runnable;
SetUpMethods = new MethodInfo[0];
TearDownMethods = new MethodInfo[0];
}
private static string GetNextId()
{
return IdPrefix + _nextID++;
}
public abstract TestResult MakeTestResult();
public void ApplyAttributesToTest(ICustomAttributeProvider provider)
{
object[] customAttributes = provider.GetCustomAttributes(typeof(IApplyToTest), true);
for (int i = 0; i < customAttributes.Length; i++) {
((IApplyToTest)customAttributes[i]).ApplyToTest(this);
}
}
public void MakeInvalid(string reason)
{
Guard.ArgumentNotNullOrEmpty(reason, "reason");
RunState = RunState.NotRunnable;
Properties.Add("_SKIPREASON", reason);
}
public virtual TAttr[] GetCustomAttributes<TAttr>(bool inherit) where TAttr : class
{
if (Method != null)
return (TAttr[])Method.MethodInfo.GetCustomAttributes(typeof(TAttr), inherit);
if (TypeInfo != null)
return TypeInfo.GetCustomAttributes<TAttr>(inherit).ToArray();
return new TAttr[0];
}
protected void PopulateTestNode(TNode thisNode, bool recursive)
{
thisNode.AddAttribute("id", Id.ToString());
thisNode.AddAttribute("name", Name);
thisNode.AddAttribute("fullname", FullName);
if (MethodName != null)
thisNode.AddAttribute("methodname", MethodName);
if (ClassName != null)
thisNode.AddAttribute("classname", ClassName);
thisNode.AddAttribute("runstate", RunState.ToString());
if (Properties.Keys.Count > 0)
Properties.AddToXml(thisNode, recursive);
}
private static ITestAction[] GetActionsForType(Type type)
{
List<ITestAction> list = new List<ITestAction>();
if (type != (Type)null && type != typeof(object)) {
list.AddRange(GetActionsForType(type.GetTypeInfo().BaseType));
Type[] declaredInterfaces = TypeHelper.GetDeclaredInterfaces(type);
foreach (Type type2 in declaredInterfaces) {
list.AddRange((ITestAction[])type2.GetTypeInfo().GetCustomAttributes(typeof(ITestAction), false));
}
list.AddRange((ITestAction[])type.GetTypeInfo().GetCustomAttributes(typeof(ITestAction), false));
}
return list.ToArray();
}
public TNode ToXml(bool recursive)
{
return AddToXml(new TNode("dummy"), recursive);
}
public abstract TNode AddToXml(TNode parentNode, bool recursive);
public int CompareTo(object obj)
{
Test test = obj as Test;
if (test == null)
return -1;
return FullName.CompareTo(test.FullName);
}
}
}