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