AttributeExistsConstraint
AttributeExistsConstraint tests for the presence of a
specified attribute on a Type.
using NUnit.Compatibility;
using System;
namespace NUnit.Framework.Constraints
{
public class AttributeExistsConstraint : Constraint
{
private Type expectedType;
public override string Description => "type with attribute " + MsgUtils.FormatValue(expectedType);
public AttributeExistsConstraint(Type type)
: base(type)
{
expectedType = type;
if (!typeof(Attribute).GetTypeInfo().IsAssignableFrom(expectedType.GetTypeInfo()))
throw new ArgumentException($"""{expectedType}""", "type");
}
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
Guard.ArgumentNotNull(actual, "actual");
Attribute[] customAttributes = AttributeHelper.GetCustomAttributes(actual, expectedType, true);
return new ConstraintResult(this, actual) {
Status = ((customAttributes.Length != 0) ? ConstraintStatus.Success : ConstraintStatus.Failure)
};
}
}
}