SubstringConstraint
SubstringConstraint can test whether a string contains
the expected substring.
using System;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints
{
[NullableContext(1)]
[Nullable(0)]
public class SubstringConstraint : StringConstraint
{
private StringComparison? _comparisonType;
public override StringConstraint IgnoreCase {
get {
Using(StringComparison.CurrentCultureIgnoreCase);
return base.IgnoreCase;
}
}
public SubstringConstraint(string expected)
: base(expected)
{
descriptionText = "String containing";
}
[NullableContext(2)]
protected override bool Matches(string actual)
{
if (actual == null)
return false;
StringComparison valueOrDefault = _comparisonType.GetValueOrDefault();
return actual.IndexOf(expected, valueOrDefault) >= 0;
}
public SubstringConstraint Using(StringComparison comparisonType)
{
StringComparison? comparisonType2 = _comparisonType;
if (!comparisonType2.HasValue)
_comparisonType = comparisonType;
else {
comparisonType2 = _comparisonType;
if (!((comparisonType2.GetValueOrDefault() == comparisonType) & comparisonType2.HasValue))
throw new InvalidOperationException("A different comparison type was already set.");
}
return this;
}
}
}