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

EqualUsingConstraint<T>

public class EqualUsingConstraint<T> : Constraint
EqualUsingConstraint where the comparison is done by a user supplied comparer.
using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Text; namespace NUnit.Framework.Constraints { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class EqualUsingConstraint<[System.Runtime.CompilerServices.Nullable(2)] T> : Constraint { [System.Runtime.CompilerServices.Nullable(2)] private readonly T _expected; [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1 })] private readonly Func<T, T, bool> _comparer; [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1 })] private readonly Func<object, object, bool> _nonTypedComparer; public override string Description { get { StringBuilder stringBuilder = new StringBuilder(MsgUtils.FormatValue(_expected)); if (_comparer != null) stringBuilder.Append(", using strongly typed comparer"); if (_nonTypedComparer != null) stringBuilder.Append(", using untyped comparer"); return stringBuilder.ToString(); } } public EqualUsingConstraint([System.Runtime.CompilerServices.Nullable(2)] T expected, Func<T, T, bool> comparer) : base(expected) { _expected = expected; _comparer = comparer; } public EqualUsingConstraint([System.Runtime.CompilerServices.Nullable(2)] T expected, Func<object, object, bool> comparer) : base(expected) { _expected = expected; _nonTypedComparer = comparer; } [System.Runtime.CompilerServices.OverloadResolutionPriority(1)] public EqualUsingConstraint([System.Runtime.CompilerServices.Nullable(2)] T expected, IEqualityComparer<T> comparer) : this(expected, (Func<T, T, bool>)comparer.Equals) { } public EqualUsingConstraint([System.Runtime.CompilerServices.Nullable(2)] T expected, IComparer<T> comparer) : this(expected, (Func<T, T, bool>)((T x, T y) => comparer.Compare(x, y) == 0)) { } public EqualUsingConstraint([System.Runtime.CompilerServices.Nullable(2)] T expected, Comparison<T> comparer) : this(expected, (Func<T, T, bool>)((T x, T y) => comparer(x, y) == 0)) { } public EqualUsingConstraint([System.Runtime.CompilerServices.Nullable(2)] T expected, IEqualityComparer comparer) : this(expected, (Func<object, object, bool>)((object x, object y) => comparer.Equals(x, y))) { } public EqualUsingConstraint([System.Runtime.CompilerServices.Nullable(2)] T expected, IComparer comparer) : this(expected, (Func<object, object, bool>)((object x, object y) => comparer.Compare(x, y) == 0)) { } public virtual ConstraintResult ApplyTo([System.Runtime.CompilerServices.Nullable(2)] T actual) { bool hasSucceeded = (actual == null) ? (_expected == null) : (_expected != null && ((_comparer != null) ? _comparer(actual, _expected) : (_nonTypedComparer != null && _nonTypedComparer(actual, _expected)))); return ConstraintResult(actual, hasSucceeded); } public sealed override ConstraintResult ApplyTo<[System.Runtime.CompilerServices.Nullable(2)] TActual>(TActual actual) { bool hasSucceeded; if (actual == null) hasSucceeded = (this._expected == null); else if (this._expected == null) { hasSucceeded = false; } else if (this._nonTypedComparer != null) { hasSucceeded = this._nonTypedComparer(actual, this._expected); } else if (this._comparer != null && ((object)actual) is T) { T arg = actual as T; hasSucceeded = this._comparer(arg, this._expected); } else { hasSucceeded = false; } return ConstraintResult(actual, hasSucceeded); } private ConstraintResult ConstraintResult<[System.Runtime.CompilerServices.Nullable(2)] TActual>(TActual actual, bool hasSucceeded) { return new EqualConstraintResult(this, actual, hasSucceeded); } } }