From bf3131ca2f140b4a87bd8a6c52cd365b6cb2656e Mon Sep 17 00:00:00 2001 From: Muiz Atolagbe Date: Sat, 23 Aug 2025 23:37:42 +0100 Subject: [PATCH 1/2] containskey Implementation in progress --- .../Assertions/Assert.Contains.cs | 38 +++++++++++++++++++ .../Assertions/AssertTests.Contains.cs | 32 ++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs index 849d9a4bac..3a136a0e60 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs @@ -178,6 +178,14 @@ 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 collection contains the given element. + /// + /// The expected item. + /// The non-generic collection (like ArrayList). + public static void ContainsKey(object expectedKey, IEnumerable collection) + => ContainsKey(expectedKey, collection, string.Empty); + /// /// Tests whether the specified collection contains the given element. /// @@ -244,6 +252,36 @@ 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 key/value 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 element. /// diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs index b751807d44..cf92b0c98a 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -362,6 +362,38 @@ 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 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 Contains method with a comparer when the expected item is present. /// From 4e99d81311c88c213d905e65df4ba34c924ae709 Mon Sep 17 00:00:00 2001 From: Muiz Atolagbe Date: Sun, 24 Aug 2025 00:58:07 +0100 Subject: [PATCH 2/2] implemented ContainsKey and ContainsValue for non generic key/value pair colllection --- .../Assertions/Assert.Contains.cs | 44 ++++++++++++- .../PublicAPI/PublicAPI.Unshipped.txt | 4 ++ .../Assertions/AssertTests.Contains.cs | 61 +++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs index 3a136a0e60..8d05c96293 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs @@ -179,13 +179,21 @@ public static void Contains(T expected, IEnumerable collection) => Contains(expected, collection, string.Empty, null); /// - /// Tests whether the specified non-generic collection contains the given element. + /// Tests whether the specified non-generic key/value pair collection contains the given key. /// /// The expected item. - /// The non-generic collection (like ArrayList). + /// 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. /// @@ -257,7 +265,7 @@ public static void Contains(T expected, IEnumerable collection, IEqualityC /// Specifically for non-generic key/value pair collections like Hashtable. /// /// The expected key. - /// The key/value collection. + /// 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) { @@ -282,6 +290,36 @@ public static void ContainsKey(object expectedKey, IEnumerable collection, strin } } + /// + /// 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 cf92b0c98a..a6eb5961fe 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -377,6 +377,21 @@ public void ContainsKey_InNonGenericKeyValueCollection_KeyExpected_KeyExists_Doe 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. @@ -394,6 +409,52 @@ public void ContainsKey_InNonGenericKeyValueCollection_KeyExpected_KeyDoesNotExi 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. ///