CollectionEquivalentConstraint
CollectionEquivalentConstraint is used to determine whether two
collections are equivalent.
using NUnit.Framework.Internal;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class CollectionEquivalentConstraint : CollectionItemsEqualConstraint
{
private readonly IEnumerable _expected;
public override string DisplayName => "Equivalent";
public override string Description => "equivalent to " + MsgUtils.FormatValue(_expected);
public CollectionEquivalentConstraint(IEnumerable expected)
: base(expected)
{
_expected = expected;
}
private bool Matches(IEnumerable actual, out CollectionTally.CollectionTallyResult tallyResult)
{
CollectionTally collectionTally = Tally(_expected);
collectionTally.TryRemove(actual);
tallyResult = collectionTally.Result;
if (tallyResult.ExtraItems.Count == 0)
return tallyResult.MissingItems.Count == 0;
return false;
}
protected override bool Matches(IEnumerable actual)
{
CollectionTally.CollectionTallyResult tallyResult;
return Matches(actual, out tallyResult);
}
public override ConstraintResult ApplyTo<[System.Runtime.CompilerServices.Nullable(2)] TActual>(TActual actual)
{
IEnumerable actual2 = ConstraintUtils.RequireActual<IEnumerable>(actual, "actual", false);
CollectionTally.CollectionTallyResult tallyResult;
bool isSuccess = Matches(actual2, out tallyResult);
return new CollectionEquivalentConstraintResult(this, tallyResult, actual, isSuccess);
}
public CollectionEquivalentConstraint Using<[System.Runtime.CompilerServices.Nullable(2)] TActualElement, [System.Runtime.CompilerServices.Nullable(2)] TExpectedElement>(Func<TActualElement, TExpectedElement, bool> comparison)
{
Using(EqualityAdapter.For(comparison));
return this;
}
}
}