diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs index 849d9a4bac..8d05c96293 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs @@ -178,6 +178,22 @@ public static T ContainsSingle(Func predicate, IEnumerable collec public static void Contains(T expected, IEnumerable collection) => Contains(expected, collection, string.Empty, null); + /// + /// Tests whether the specified non-generic key/value pair collection contains the given key. + /// + /// The expected item. + /// The non-generic key/value pair collection (like Hashset). + public static void ContainsKey(object expectedKey, IEnumerable collection) + => ContainsKey(expectedKey, collection, string.Empty); + + /// + /// Tests whether the specified non-generic key/value pair collection contains the given value. + /// + /// The expected item. + /// The non-generic key/value pair collection (like Hashset). + public static void ContainsValue(object expectedValue, IEnumerable collection) + => ContainsValue(expectedValue, collection, string.Empty); + /// /// Tests whether the specified collection contains the given element. /// @@ -244,6 +260,66 @@ public static void Contains(T expected, IEnumerable collection, IEqualityC } } + /// + /// Tests whether the specified collection contains the given key. + /// Specifically for non-generic key/value pair collections like Hashtable. + /// + /// The expected key. + /// The non-generic key/value pair collection. + /// The message format to display when the assertion fails. + public static void ContainsKey(object expectedKey, IEnumerable collection, string? message) + { + bool isFound = false; + + foreach (object? item in collection) + { + if (item is DictionaryEntry dictEntry) + { + if (object.Equals(expectedKey, dictEntry.Key)) + { + isFound = true; + break; + } + } + } + + if (!isFound) + { + string userMessage = BuildUserMessage(message); + ThrowAssertContainsItemFailed(userMessage); + } + } + + /// + /// Tests whether the specified collection contains the given key. + /// Specifically for non-generic key/value pair collections like Hashtable. + /// + /// The expected value. + /// The non-generic key/value pair collection. + /// The message format to display when the assertion fails. + public static void ContainsValue(object expectedValue, IEnumerable collection, string? message) + { + bool isFound = false; + + foreach (object? item in collection) + { + if (item is DictionaryEntry dictEntry) + { + if (object.Equals(expectedValue, dictEntry.Value)) + { + isFound = true; + break; + } + } + } + + if (!isFound) + { + string userMessage = BuildUserMessage(message); + ThrowAssertContainsItemFailed(userMessage); + } + } + /// /// Tests whether the specified collection contains the given element. /// diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt index 7d27ebf1af..ae0a3304ae 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt @@ -1,5 +1,9 @@ #nullable enable Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.Field = 3 -> Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ContainsKey(object! expectedKey, System.Collections.IEnumerable! collection) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ContainsKey(object! expectedKey, System.Collections.IEnumerable! collection, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ContainsValue(object! expectedValue, System.Collections.IEnumerable! collection) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ContainsValue(object! expectedValue, System.Collections.IEnumerable! collection, string? message) -> void *REMOVED*static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Instance.get -> Microsoft.VisualStudio.TestTools.UnitTesting.Assert! *REMOVED*static Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.Instance.get -> Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert! *REMOVED*static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert.Instance.get -> Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert! diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs index b751807d44..a6eb5961fe 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -362,6 +362,99 @@ public void Contains_ValueExpected_ItemDoesNotExist_ThrowsException() action.Should().Throw().WithMessage("*20*"); } + /// + /// Tests the ContainsKey method (value overload) when the expected key is present. + /// + public void ContainsKey_InNonGenericKeyValueCollection_KeyExpected_KeyExists_DoesNotThrow() + { + // Arrange + var collection = new Hashtable { { "key1", "value1" }, { "key2", "value2" }, { 3, "value3" } }; + + // Act + Action action = () => Assert.ContainsKey("key2", collection, "No failure expected"); + + // Assert + action.Should().NotThrow(); + } + + /// + /// Tests the ContainsKey method (value overload) when the expected key is present. + /// + public void ContainsKey_InNonGenericKeyValueCollection_KeyExpected_KeyExists_DoesNotThrow2() + { + // Arrange + var collection = new Hashtable { { "key1", "value1" }, { "key2", "value2" }, { 3, "value3" } }; + + // Act + Action action = () => Assert.ContainsKey(3, collection, "No failure expected"); + + // Assert + action.Should().NotThrow(); + } + + /// + /// Tests the ContainsKey method (value overload) when the expected key is not present. + /// Expects an exception. + /// + public void ContainsKey_InNonGenericKeyValueCollection_KeyExpected_KeyDoesNotExists_ThrowException() + { + // Arrange + var collection = new Hashtable { { "key1", "value1" }, { "key2", "value2" }, { 3, "value3" } }; + object expectedKey = "key3"; + + // Act + Action action = () => Assert.ContainsKey(expectedKey, collection, $"Item {expectedKey} not found"); + + // Assert + action.Should().Throw().WithMessage($"*Item {expectedKey} not found*"); + } + + /// + /// Tests the ContainsValue method (value overload) when the expected value is present. + /// + public void ContainsValue_InNonGenericKeyValueCollection_ValueExpected_ValueExists_DoesNotThrow() + { + // Arrange + var collection = new Hashtable { { "key1", 1 }, { "key2", "value2" }, { 3, "value3" } }; + + // Act + Action action = () => Assert.ContainsValue("value2", collection, "No failure expected"); + + // Assert + action.Should().NotThrow(); + } + + /// + /// Tests the ContainsValue method (value overload) when the expected value is present. + /// + public void ContainsValue_InNonGenericKeyValueCollection_ValueExpected_ValueExists_DoesNotThrow2() + { + // Arrange + var collection = new Hashtable { { "key1", 1 }, { "key2", "value2" }, { 3, "value3" } }; + + // Act + Action action = () => Assert.ContainsValue(1, collection, "No failure expected"); + + // Assert + action.Should().NotThrow(); + } + + /// + /// Tests the ContainsValue method (value overload) when the expected value is not present. + /// + public void ContainsValue_InNonGenericKeyValueCollection_ValueExpected_ValueDoesNotExists_ThrowException() + { + // Arrange + var collection = new Hashtable { { "key1", 1 }, { "key2", "value2" }, { 3, "value3" } }; + object expectedValue = "value1"; + + // Act + Action action = () => Assert.ContainsValue(expectedValue, collection, $"Item {expectedValue} not found"); + + // Assert + action.Should().Throw().WithMessage($"*Item {expectedValue} not found*"); + } + /// /// Tests the Contains method with a comparer when the expected item is present. ///