EqualDateTimeOffsetConstraintWithSameOffset
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
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class EqualDateTimeOffsetConstraintWithSameOffset : Constraint
{
private readonly DateTimeOffset _expected;
public override string Description {
get {
StringBuilder stringBuilder = new StringBuilder(MsgUtils.FormatValue(_expected));
stringBuilder.Append(" with the same offset");
return stringBuilder.ToString();
}
}
public EqualDateTimeOffsetConstraintWithSameOffset(DateTimeOffset expected)
: base(expected)
{
_expected = expected;
}
public ConstraintResult ApplyTo(DateTimeOffset actual)
{
bool isSuccess = _expected.Equals(actual) && _expected.Offset == actual.Offset;
return new ConstraintResult(this, actual, isSuccess);
}
public override ConstraintResult ApplyTo<[System.Runtime.CompilerServices.Nullable(2)] TActual>(TActual actual)
{
if (((object)actual) is DateTimeOffset) {
DateTimeOffset actual2 = actual as DateTimeOffset;
return ApplyTo(actual2);
}
return new ConstraintResult(this, actual, false);
}
}
}