DictionaryContainsValueConstraint
DictionaryContainsValueConstraint is used to test whether a dictionary
contains an expected object as a value.
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 DictionaryContainsValueConstraint : CollectionItemsEqualConstraint
{
public override string DisplayName => "ContainsValue";
public override string Description => "dictionary containing value " + MsgUtils.FormatValue(Expected);
[System.Runtime.CompilerServices.Nullable(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
protected object Expected {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public DictionaryContainsValueConstraint(object expected)
: base(expected)
{
Expected = expected;
}
protected override bool Matches(IEnumerable actual)
{
IDictionary dictionary = ConstraintUtils.RequireActual<IDictionary>(actual, "actual", false);
foreach (object value in dictionary.Values) {
if (ItemsEqual(value, Expected))
return true;
}
return false;
}
public DictionaryContainsValueConstraint Using<[System.Runtime.CompilerServices.Nullable(2)] TCollectionType, [System.Runtime.CompilerServices.Nullable(2)] TMemberType>(Func<TCollectionType, TMemberType, bool> comparison)
{
Func<TMemberType, TCollectionType, bool> comparison2 = (TMemberType actual, TCollectionType expected) => comparison(expected, actual);
Using(EqualityAdapter.For(comparison2));
return this;
}
}
}