EqualTimeBasedConstraintWithTimeSpanTolerance<T>
public class EqualTimeBasedConstraintWithTimeSpanTolerance<T> : Constraint where T : IEquatable<T>, IComparable<T>
EqualConstraint is able to compare an actual value with the
expected value provided in its constructor. Two objects are
considered equal if both are null, or if both have the same
value. NUnit has special semantics for some object types.
using System;
using System.Runtime.CompilerServices;
using System.Text;
namespace NUnit.Framework.Constraints
{
[NullableContext(1)]
[Nullable(0)]
public class EqualTimeBasedConstraintWithTimeSpanTolerance<T> : Constraint where T : IEquatable<T>, IComparable<T>
{
private readonly T _expected;
private readonly Func<T, long> _getTicks;
private readonly TimeSpan _tolerance;
public override string Description {
get {
StringBuilder stringBuilder = new StringBuilder(MsgUtils.FormatValue(_expected));
stringBuilder.Append(" +/- ");
stringBuilder.Append(MsgUtils.FormatValue(_tolerance));
return stringBuilder.ToString();
}
}
public EqualTimeBasedConstraintWithTimeSpanTolerance(T expected, Func<T, long> getTicks, TimeSpan tolerance)
: base(expected)
{
_expected = expected;
_getTicks = getTicks;
_tolerance = tolerance;
}
public ConstraintResult ApplyTo(T actual)
{
long num = _getTicks(_expected);
long num2 = _getTicks(actual);
bool isSuccess = Math.Abs(num - num2) <= _tolerance.Ticks;
return new ConstraintResult(this, actual, isSuccess);
}
public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(TActual actual)
{
if (((object)actual) is T) {
T actual2 = actual as T;
return this.ApplyTo(actual2);
}
return new ConstraintResult(this, actual, false);
}
}
}