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

RangeConstraint

public class RangeConstraint : Constraint
RangeConstraint tests whether two values are within a specified range.
using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace NUnit.Framework.Constraints { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class RangeConstraint : Constraint { private readonly object _from; private readonly object _to; private ComparisonAdapter _comparer = ComparisonAdapter.Default; public override string Description { get { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(12, 2); defaultInterpolatedStringHandler.AppendLiteral("in range ("); defaultInterpolatedStringHandler.AppendFormatted<object>(_from); defaultInterpolatedStringHandler.AppendLiteral(","); defaultInterpolatedStringHandler.AppendFormatted<object>(_to); defaultInterpolatedStringHandler.AppendLiteral(")"); return defaultInterpolatedStringHandler.ToStringAndClear(); } } public RangeConstraint(object from, object to) : base(from, to) { _from = from; _to = to; } public override ConstraintResult ApplyTo<[System.Runtime.CompilerServices.Nullable(2)] TActual>(TActual actual) { if (_from == null || _to == null || actual == null) throw new ArgumentException("Cannot compare using a null reference", "actual"); CompareFromAndTo(); bool isSuccess = _comparer.Compare(_from, actual) <= 0 && _comparer.Compare(_to, actual) >= 0; return new ConstraintResult(this, actual, isSuccess); } public RangeConstraint Using(IComparer comparer) { _comparer = ComparisonAdapter.For(comparer); return this; } public RangeConstraint Using<[System.Runtime.CompilerServices.Nullable(2)] T>(IComparer<T> comparer) { _comparer = ComparisonAdapter.For(comparer); return this; } public RangeConstraint Using<[System.Runtime.CompilerServices.Nullable(2)] T>(Comparison<T> comparer) { _comparer = ComparisonAdapter.For(comparer); return this; } private void CompareFromAndTo() { if (_comparer.Compare(_from, _to) > 0) throw new ArgumentException("The from value must be less than or equal to the to value."); } } }