CollectionSubsetConstraint
CollectionSubsetConstraint is used to determine whether
one collection is a subset of another
using NUnit.Framework.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class CollectionSubsetConstraint : CollectionItemsEqualConstraint
{
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class CollectionSubsetConstraintResult : ConstraintResult
{
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private readonly List<object> _extraItems;
public CollectionSubsetConstraintResult(IConstraint constraint, [System.Runtime.CompilerServices.Nullable(2)] object actualValue, bool isSuccess, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] List<object> extraItems)
: base(constraint, actualValue, isSuccess)
{
_extraItems = extraItems;
}
public override void WriteAdditionalLinesTo(MessageWriter writer)
{
List<object> extraItems = _extraItems;
if (extraItems != null && extraItems.Count > 0) {
string str = "Extra items: ";
str += MsgUtils.FormatCollection(_extraItems, 0, 10);
writer.WriteMessageLine(str, Array.Empty<object>());
}
}
}
private readonly IEnumerable _expected;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private List<object> _extraItems;
public override string DisplayName => "SubsetOf";
public override string Description => "subset of " + MsgUtils.FormatValue(_expected);
public CollectionSubsetConstraint(IEnumerable expected)
: base(expected)
{
_expected = expected;
}
protected override bool Matches(IEnumerable actual)
{
CollectionTally collectionTally = Tally(_expected);
collectionTally.TryRemove(actual);
_extraItems = collectionTally.Result.ExtraItems;
return _extraItems.Count == 0;
}
public override ConstraintResult ApplyTo<[System.Runtime.CompilerServices.Nullable(2)] TActual>(TActual actual)
{
IEnumerable collection = ConstraintUtils.RequireActual<IEnumerable>(actual, "actual", false);
bool isSuccess = Matches(collection);
return new CollectionSubsetConstraintResult(this, actual, isSuccess, _extraItems);
}
public CollectionSubsetConstraint Using<[System.Runtime.CompilerServices.Nullable(2)] TSubsetElement, [System.Runtime.CompilerServices.Nullable(2)] TSupersetElement>(Func<TSubsetElement, TSupersetElement, bool> comparison)
{
Using(EqualityAdapter.For(comparison));
return this;
}
}
}