Skip to content

Commit

Permalink
Permit DictionaryContainsKeyValuePairConstraint to take in a null val…
Browse files Browse the repository at this point in the history
…ue (fix CS8604) (#4689)

* Permit DictionaryContainsKeyValuePairConstraint to take in a null value, matching the nullability of DictionaryEntry.

* Fix up nullability of DictionaryContainsValueConstraint to permit `object?` instead of `object`.

* Add some basic tests that null values can be found in dictionaries by DictionaryContains(Key)ValueConstraint.

* Update DictionaryContainsKeyValueConstraintTests.cs to use [TestCase] instead of [Theory].

* Update DictionaryContainsValueConstraintTests.cs to replace use of [Theory] with [TestCase].

---------

Co-authored-by: Andrew McClement <andrew.mcclement@aveva.com>
  • Loading branch information
andrewimcclement and Andrew McClement committed Apr 18, 2024
1 parent 612dd8c commit 7b9497d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
Expand Up @@ -734,7 +734,7 @@ public DictionaryContainsKeyConstraint ContainKey(object expected)
/// presence of a particular value in the Dictionary value collection.
/// </summary>
/// <param name="expected">The value to be matched in the Dictionary value collection</param>
public DictionaryContainsValueConstraint ContainValue(object expected)
public DictionaryContainsValueConstraint ContainValue(object? expected)
{
return Append(new DictionaryContainsValueConstraint(expected));
}
Expand Down
Expand Up @@ -51,9 +51,9 @@ public DictionaryContainsKeyConstraint(object expected)
/// Returns a new DictionaryContainsKeyValuePairConstraint checking for the
/// presence of a particular key-value-pair in the dictionary.
/// </summary>
public DictionaryContainsKeyValuePairConstraint WithValue(object expectedValue)
public DictionaryContainsKeyValuePairConstraint WithValue(object? expectedValue)
{
return (DictionaryContainsKeyValuePairConstraint)Instead.Append(new DictionaryContainsKeyValuePairConstraint(Expected, expectedValue));
return Instead.Append(new DictionaryContainsKeyValuePairConstraint(Expected, expectedValue));
}

private bool Matches(object? actual)
Expand Down
Expand Up @@ -19,7 +19,7 @@ public sealed class DictionaryContainsKeyValuePairConstraint : CollectionItemsEq
/// <summary>
/// Construct a DictionaryContainsKeyValuePairConstraint
/// </summary>
public DictionaryContainsKeyValuePairConstraint(object key, object value)
public DictionaryContainsKeyValuePairConstraint(object key, object? value)
{
_expected = new DictionaryEntry(key, value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitFramework/framework/Contains.cs
Expand Up @@ -41,7 +41,7 @@ public static DictionaryContainsKeyConstraint Key(object expected)
/// Returns a new DictionaryContainsValueConstraint checking for the
/// presence of a particular value in the dictionary.
/// </summary>
public static DictionaryContainsValueConstraint Value(object expected)
public static DictionaryContainsValueConstraint Value(object? expected)
{
return new DictionaryContainsValueConstraint(expected);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitFramework/framework/Does.cs
Expand Up @@ -71,7 +71,7 @@ public static DictionaryContainsKeyConstraint ContainKey(object expected)
/// presence of a particular value in the Dictionary value collection.
/// </summary>
/// <param name="expected">The value to be matched in the Dictionary value collection</param>
public static DictionaryContainsValueConstraint ContainValue(object expected)
public static DictionaryContainsValueConstraint ContainValue(object? expected)
{
return Contains.Value(expected);
}
Expand Down
Expand Up @@ -10,12 +10,13 @@ namespace NUnit.Framework.Tests.Constraints
[TestFixture]
public class DictionaryContainsKeyValuePairConstraintTests
{
[Test]
public void SucceedsWhenKeyValuePairIsPresent()
[TestCase("Universe")]
[TestCase(null)]
public void SucceedsWhenKeyValuePairIsPresent(string? expectedValue)
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hi", "Universe" }, { "Hola", "Mundo" } };
var dictionary = new Dictionary<string, string?> { { "Hello", "World" }, { "Hi", expectedValue }, { "Hola", "Mundo" } };

Assert.That(dictionary, new DictionaryContainsKeyValuePairConstraint("Hi", "Universe"));
Assert.That(dictionary, new DictionaryContainsKeyValuePairConstraint("Hi", expectedValue));
}

[Test]
Expand All @@ -38,11 +39,12 @@ public void FailsWhenValueIsMissing()
Assert.That(act, Throws.Exception.TypeOf<AssertionException>());
}

[Test]
public void SucceedsWhenPairIsPresentUsingContainKeyWithValue()
[TestCase("Mundo")]
[TestCase(null)]
public void SucceedsWhenPairIsPresentUsingContainKeyWithValue(string? expectedValue)
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };
Assert.That(dictionary, Does.ContainKey("Hola").WithValue("Mundo"));
var dictionary = new Dictionary<string, string?> { { "Hello", "World" }, { "Hola", expectedValue } };
Assert.That(dictionary, Does.ContainKey("Hola").WithValue(expectedValue));
}

[Test]
Expand Down
Expand Up @@ -28,11 +28,12 @@ public void FailsWhenValueIsMissing()
Assert.That(act, Throws.Exception.TypeOf<AssertionException>());
}

[Test]
public void SucceedsWhenValueIsPresentUsingContainValue()
[TestCase("Mundo")]
[TestCase(null)]
public void SucceedsWhenValueIsPresentUsingContainValue(string? expectedValue)
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };
Assert.That(dictionary, Does.ContainValue("Mundo"));
var dictionary = new Dictionary<string, string?> { { "Hello", "World" }, { "Hola", expectedValue } };
Assert.That(dictionary, Does.ContainValue(expectedValue));
}

[Test]
Expand Down

0 comments on commit 7b9497d

Please sign in to comment.