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

StringConstraint

public abstract class StringConstraint : Constraint
StringConstraint is the abstract base for constraints that operate on strings. It supports the IgnoreCase modifier for string operations.
using NUnit.Framework.Internal; using System; using System.Globalization; using System.Runtime.CompilerServices; namespace NUnit.Framework.Constraints { [NullableContext(1)] [Nullable(0)] public abstract class StringConstraint : Constraint { protected readonly string expected; protected bool caseInsensitive; protected string descriptionText = string.Empty; private StringComparison? _comparisonType; [Nullable(2)] private CultureInfo _cultureInfo; public virtual StringConstraint IgnoreCase { get { caseInsensitive = true; return this; } } public override string Description { get { string text = descriptionText + " " + MsgUtils.FormatValue(expected); if (caseInsensitive) text += ", ignoring case"; StringComparison? comparisonType = _comparisonType; if (comparisonType.HasValue) { string str = text; DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(23, 1); defaultInterpolatedStringHandler.AppendLiteral(", with comparison type "); defaultInterpolatedStringHandler.AppendFormatted(_comparisonType); text = str + defaultInterpolatedStringHandler.ToStringAndClear(); } else if (_cultureInfo != null) { text = text + ", with culture: " + _cultureInfo.Name; } return text; } } protected StringConstraint() : base(Array.Empty<object>()) { expected = string.Empty; } protected StringConstraint(string expected) : base(expected) { this.expected = expected; } public virtual StringConstraint Using(StringComparison comparisonType) { StringComparison? comparisonType2 = _comparisonType; if (comparisonType2.HasValue || _cultureInfo != null) throw new InvalidOperationException("Only one Using modifier may be used"); _comparisonType = comparisonType; return this; } public virtual StringConstraint Using(CultureInfo culture) { StringComparison? comparisonType = _comparisonType; if (comparisonType.HasValue || _cultureInfo != null) throw new InvalidOperationException("Only one Using modifier may be used"); _cultureInfo = culture; return this; } [NullableContext(2)] protected abstract bool Matches(string actual); [NullableContext(2)] protected virtual bool Matches(string actual, StringComparison stringComparison) { return Matches(actual); } protected virtual bool Matches([Nullable(2)] string actual, CultureInfo cultureInfo) { return Matches(actual); } public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(TActual actual) { string actual2 = ConstraintUtils.RequireActual<string>(actual, "actual", true); bool isSuccess; if (_cultureInfo != null) isSuccess = Matches(actual2, _cultureInfo); else { StringComparison? comparisonType = _comparisonType; isSuccess = (comparisonType.HasValue ? Matches(actual2, caseInsensitive ? GetIgnoreCaseEquivalent(_comparisonType.Value) : _comparisonType.Value) : ((!caseInsensitive) ? Matches(actual2) : Matches(actual2, StringComparison.CurrentCultureIgnoreCase))); } return new ConstraintResult(this, actual, isSuccess); } private static StringComparison GetIgnoreCaseEquivalent(StringComparison? currentComparison) { if (currentComparison.HasValue) { switch (currentComparison.GetValueOrDefault()) { case StringComparison.CurrentCulture: return StringComparison.CurrentCultureIgnoreCase; case StringComparison.CurrentCultureIgnoreCase: return StringComparison.CurrentCultureIgnoreCase; case StringComparison.InvariantCulture: return StringComparison.InvariantCultureIgnoreCase; case StringComparison.InvariantCultureIgnoreCase: return StringComparison.InvariantCultureIgnoreCase; case StringComparison.Ordinal: return StringComparison.OrdinalIgnoreCase; case StringComparison.OrdinalIgnoreCase: return StringComparison.OrdinalIgnoreCase; default: { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(36, 1); defaultInterpolatedStringHandler.AppendLiteral("Unsupported StringComparison value: "); defaultInterpolatedStringHandler.AppendFormatted(currentComparison); throw new InvalidOperationException(defaultInterpolatedStringHandler.ToStringAndClear()); } } } return StringComparison.CurrentCultureIgnoreCase; } } }