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

RangeConstraint

public class RangeConstraint : Constraint
RangeConstraint tests whether two values are within a specified range.
using System; using System.Collections; using System.Collections.Generic; namespace NUnit.Framework.Constraints { public class RangeConstraint : Constraint { private readonly object from; private readonly object to; private ComparisonAdapter comparer = ComparisonAdapter.Default; public override string Description => $"""{from}""{to}"""; public RangeConstraint(object from, object to) : base(from, to) { this.from = from; this.to = to; } public override ConstraintResult ApplyTo<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) { this.comparer = ComparisonAdapter.For(comparer); return this; } public RangeConstraint Using<T>(IComparer<T> comparer) { this.comparer = ComparisonAdapter.For(comparer); return this; } public RangeConstraint Using<T>(Comparison<T> comparer) { this.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."); } } }