ContainsConstraint
ContainsConstraint tests a whether a string contains a substring
or a collection contains an object. It postpones the decision of
which test to use until the type of the actual argument is known.
This allows testing whether a string is contained in a collection
or as a substring of another string using the same syntax.
namespace NUnit.Framework.Constraints
{
public class ContainsConstraint : Constraint
{
private readonly object _expected;
private Constraint _realConstraint;
private bool _ignoreCase;
public override string Description => _realConstraint.Description;
public ContainsConstraint IgnoreCase {
get {
_ignoreCase = true;
return this;
}
}
public ContainsConstraint(object expected)
{
_expected = expected;
}
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
if (((object)actual) is string) {
StringConstraint stringConstraint = new SubstringConstraint((string)_expected);
if (_ignoreCase)
stringConstraint = stringConstraint.IgnoreCase;
_realConstraint = stringConstraint;
} else
_realConstraint = new CollectionContainsConstraint(_expected);
return _realConstraint.ApplyTo(actual);
}
}
}