CollectionSupersetConstraint
CollectionSupersetConstraint is used to determine whether
one collection is a superset 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 CollectionSupersetConstraint : CollectionItemsEqualConstraint
{
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class CollectionSupersetConstraintResult : ConstraintResult
{
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private readonly List<object> _missingItems;
public CollectionSupersetConstraintResult(IConstraint constraint, [System.Runtime.CompilerServices.Nullable(2)] object actualValue, bool isSuccess, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] List<object> missingItems)
: base(constraint, actualValue, isSuccess)
{
_missingItems = missingItems;
}
public override void WriteAdditionalLinesTo(MessageWriter writer)
{
List<object> missingItems = _missingItems;
if (missingItems != null && missingItems.Count > 0) {
string str = "Missing items: ";
str += MsgUtils.FormatCollection(_missingItems, 0, 10);
writer.WriteMessageLine(str, Array.Empty<object>());
}
}
}
private readonly IEnumerable _expected;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private List<object> _missingItems;
public override string DisplayName => "SupersetOf";
public override string Description => "superset of " + MsgUtils.FormatValue(_expected);
public CollectionSupersetConstraint(IEnumerable expected)
: base(expected)
{
_expected = expected;
}
protected override bool Matches(IEnumerable actual)
{
CollectionTally collectionTally = Tally(actual);
collectionTally.TryRemove(_expected);
_missingItems = collectionTally.Result.ExtraItems;
return _missingItems.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 CollectionSupersetConstraintResult(this, actual, isSuccess, _missingItems);
}
public CollectionSupersetConstraint Using<[System.Runtime.CompilerServices.Nullable(2)] TSupersetElement, [System.Runtime.CompilerServices.Nullable(2)] TSubsetElement>(Func<TSupersetElement, TSubsetElement, bool> comparison)
{
Func<TSubsetElement, TSupersetElement, bool> comparison2 = (TSubsetElement actual, TSupersetElement expected) => comparison(expected, actual);
Using(EqualityAdapter.For(comparison2));
return this;
}
}
}