<PackageReference Include="NUnit" Version="4.3.2" />

AttributeConstraint

AttributeConstraint tests that a specified attribute is present on a Type or other provider and that the value of the attribute satisfies some other constraint.
using NUnit.Compatibility; using System; using System.Runtime.CompilerServices; namespace NUnit.Framework.Constraints { [NullableContext(1)] [Nullable(0)] public class AttributeConstraint : PrefixConstraint { private readonly Type _expectedType; public AttributeConstraint(Type type, IConstraint baseConstraint) : base(baseConstraint, "attribute " + type.FullName) { _expectedType = type; if (!typeof(Attribute).IsAssignableFrom(_expectedType)) { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(25, 1); defaultInterpolatedStringHandler.AppendLiteral("Type "); defaultInterpolatedStringHandler.AppendFormatted(_expectedType); defaultInterpolatedStringHandler.AppendLiteral(" is not an attribute"); throw new ArgumentException(defaultInterpolatedStringHandler.ToStringAndClear(), "type"); } } public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(TActual actual) { Guard.ArgumentNotNull(actual, "actual"); Attribute[] customAttributes = AttributeHelper.GetCustomAttributes(actual, _expectedType, true); if (customAttributes.Length == 0) { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(24, 1); defaultInterpolatedStringHandler.AppendLiteral("Attribute "); defaultInterpolatedStringHandler.AppendFormatted(_expectedType); defaultInterpolatedStringHandler.AppendLiteral(" was not found"); throw new ArgumentException(defaultInterpolatedStringHandler.ToStringAndClear(), "actual"); } Attribute actual2 = customAttributes[0]; return base.BaseConstraint.ApplyTo(actual2); } protected override string GetStringRepresentation() { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(13, 2); defaultInterpolatedStringHandler.AppendLiteral("<attribute "); defaultInterpolatedStringHandler.AppendFormatted(_expectedType); defaultInterpolatedStringHandler.AppendLiteral(" "); defaultInterpolatedStringHandler.AppendFormatted(base.BaseConstraint); defaultInterpolatedStringHandler.AppendLiteral(">"); return defaultInterpolatedStringHandler.ToStringAndClear(); } } }