PropertyExistsConstraint
PropertyExistsConstraint tests that a named property
exists on the object provided through Match.
Originally, PropertyConstraint provided this feature
in addition to making optional tests on the value
of the property. The two constraints are now separate.
using NUnit.Framework.Internal;
using System;
using System.Reflection;
namespace NUnit.Framework.Constraints
{
public class PropertyExistsConstraint : Constraint
{
private readonly string name;
private Type actualType;
public override string Description => "property " + name;
public PropertyExistsConstraint(string name)
: base(name)
{
this.name = name;
}
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
Guard.ArgumentNotNull(actual, "actual");
actualType = (actual as Type);
if (actualType == (Type)null)
actualType = actual.GetType();
PropertyInfo ultimateShadowingProperty = Reflect.GetUltimateShadowingProperty(actualType, name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return new ConstraintResult(this, actualType, ultimateShadowingProperty != (PropertyInfo)null);
}
protected override string GetStringRepresentation()
{
return $"""{name}""";
}
}
}