PredicateConstraint<T>
Predicate constraint wraps a Predicate in a constraint,
returning success if the predicate is true.
using System;
namespace NUnit.Framework.Constraints
{
public class PredicateConstraint<T> : Constraint
{
private readonly Predicate<T> predicate;
public override string Description {
get {
string name = predicate.Method.Name;
if (!name.StartsWith("<"))
return "value matching " + name;
return "value matching lambda expression";
}
}
public PredicateConstraint(Predicate<T> predicate)
{
this.predicate = predicate;
}
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
if (!(((object)actual) is T))
throw new ArgumentException("The actual value is not of type " + typeof(T).Name, "actual");
return new ConstraintResult(this, actual, this.predicate((T)(object)actual));
}
}
}