Skip to content

Update DictionaryContainsKeyConstraint modifiers #1046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,35 @@ Does.Not.ContainKey(object)
## Modifiers

```csharp
...IgnoreCase
...IgnoreWhiteSpace // From version 4.2
...Using(IEqualityComparer comparer)
...Using(IComparer comparer)
...Using<T>(IEqualityComparer<T> comparer)
...Using<T>(IComparer<T> comparer)
...Using<T>(Comparison<T> comparer)
...Using<T>(Func<T, T, bool> comparer)
...UsingPropertiesComparer() // From version 4.1
...UsingPropertiesComparer(
Func<PropertiesComparerConfiguration,
PropertiesComparerConfiguration> configure) // From version 4.4
...WithValue(object expectedValue)
```

As of version 4.4.0 of NUnit the other modifiers were removed as they were non-functional.
NUnit's `ContainKey` works the same as the `ContainsKey` method on `Dictionary`
It uses the comparer specified when the dictionary was created.

## Examples of Use

```csharp
IDictionary<int, int> idict = new IDictionary<int, int> { { 1, 4 }, { 2, 5 } };
var caseSensitiveDictionary = new Dictionary<string, string>(StringComparer.Ordinal)
{
["Hello"] = "World",
};

Assert.That(caseSensitiveDictionary, Does.ContainKey("Hello"));
Assert.That(caseSensitiveDictionary, Does.Not.ContainKey("hello"));

var caseInsensitiveDictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
["Hello"] = "World",
};

Assert.That(caseInsensitiveDictionary, Does.ContainKey("Hello"));
Assert.That(caseInsensitiveDictionary, Does.ContainKey("hello"));
Assert.That(caseInsensitiveDictionary, Does.Not.ContainKey("hallo"));

Assert.That(idict, Contains.Key(1));
Assert.That(idict, Does.ContainKey(2));
Assert.That(idict, Does.Not.ContainKey(3));
Assert.That(mydict, Contains.Key(myOwnObject).Using(myComparer));
Assert.That(mydict, Does.ContainKey("Hello").WithValue("World"));
// Note that the 'IgnoreCase' here is on the Value part, not the Key.
Assert.That(caseInsensitiveDictionary, Does.ContainKey("hello").WithValue("world").IgnoreCase);
```

## See also
Expand Down
Loading