From b60b4e08b2f37bc07674041307c572f83072786a Mon Sep 17 00:00:00 2001 From: Muiz Atolagbe Date: Wed, 20 Aug 2025 07:29:45 +0100 Subject: [PATCH 1/6] playground implementation for non-generic collection on assert.contains --- .../Assertions/Assert.Contains.cs | 34 +++++++++++++++++++ .../Assertions/AssertTests.Contains.cs | 24 +++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs index 849d9a4bac..01cbc1320a 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 Contains(object expected, IEnumerable collection) + => Contains(expected, collection, string.Empty, null); + /// /// Tests whether the specified collection contains the given element. /// @@ -205,6 +213,32 @@ public static void Contains(T expected, IEnumerable collection, string? me } } + /// + /// Tests whether the specified collection contains the given element. + /// + /// The expected item. + /// The collection. + /// The message format to display when the assertion fails. + /// The parameters to format the message. + public static void Contains(object expected, IEnumerable collection, string? message, params object?[]? parameters) + { + bool isFound = false; + foreach (object? item in collection) + { + if (object.Equals(expected, item)) + { + isFound = true; + break; + } + } + + if (!isFound) + { + string userMessage = BuildUserMessage(message, parameters); + 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..79b322b6a6 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -346,6 +346,30 @@ public void Contains_ValueExpected_ItemExists_DoesNotThrow() action.Should().NotThrow(); } + /// + /// Tests the Contains method (value overload) when the expected item is present. + /// + public void Contains_ValueExpected_ItemExists_DoesNotThrow1() + { + // Arrange + // var collection = new List { 5, 10, 15 }; + // var collection = new Hashtable() + // { + // { "key1", "value1" }, { "key2", "value2" }, { 123, "numeric key" }, { "mixed", 456 }, + // }; + var collection = new Stack(); + + collection.Push("Hello"); + collection.Push(42); + collection.Push(true); + + // Act + Action action = () => Assert.Contains("Hello", collection, "No failure expected", null); + + // Assert + action.Should().NotThrow(); + } + /// /// Tests the Contains method (value overload) when the expected item is not present. /// Expects an exception. From cb398428d47ef9526f8bd0ecc3fc2f33755a1009 Mon Sep 17 00:00:00 2001 From: Muiz Atolagbe Date: Wed, 20 Aug 2025 21:40:14 +0100 Subject: [PATCH 2/6] added the assert.contain for non-generic collections into the publicapi.shipped --- src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt index 7954d442eb..7cc4ea2996 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt @@ -642,6 +642,8 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(string! subs static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(string! substring, string! value, System.StringComparison comparisonType, string? message) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(string! substring, string! value, System.StringComparison comparisonType) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(string! substring, string! value) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection, string? message, params object?[]? parameters) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(System.Func! predicate, System.Collections.Generic.IEnumerable! collection, string? message, params object?[]? parameters) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(System.Func! predicate, System.Collections.Generic.IEnumerable! collection, string? message) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(System.Func! predicate, System.Collections.Generic.IEnumerable! collection) -> void From 5951832b26efece8edc72d6a8fba286c9171f9be Mon Sep 17 00:00:00 2001 From: Muiz Atolagbe Date: Wed, 20 Aug 2025 22:15:30 +0100 Subject: [PATCH 3/6] added test cases to cover assert.contains for non-generic collections --- .../Assertions/AssertTests.Contains.cs | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs index 79b322b6a6..ac65fc1958 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -347,37 +347,28 @@ public void Contains_ValueExpected_ItemExists_DoesNotThrow() } /// - /// Tests the Contains method (value overload) when the expected item is present. + /// Tests the Contains method (value overload) when the expected item is not present. + /// Expects an exception. /// - public void Contains_ValueExpected_ItemExists_DoesNotThrow1() + public void Contains_ValueExpected_ItemDoesNotExist_ThrowsException() { // Arrange - // var collection = new List { 5, 10, 15 }; - // var collection = new Hashtable() - // { - // { "key1", "value1" }, { "key2", "value2" }, { 123, "numeric key" }, { "mixed", 456 }, - // }; - var collection = new Stack(); - - collection.Push("Hello"); - collection.Push(42); - collection.Push(true); + var collection = new List { 5, 10, 15 }; // Act - Action action = () => Assert.Contains("Hello", collection, "No failure expected", null); + Action action = () => Assert.Contains(20, collection, "Item {0} not found", 20); // Assert - action.Should().NotThrow(); + action.Should().Throw().WithMessage("*20*"); } /// - /// Tests the Contains method (value overload) when the expected item is not present. - /// Expects an exception. + /// Tests the Contains method (value overload) when the expected item is present. /// - public void Contains_ValueExpected_ItemDoesNotExist_ThrowsException() + public void Contains_InNonGenericCollection_ValueExpected_ItemDoesNotExist_ThrowsException() { // Arrange - var collection = new List { 5, 10, 15 }; + var collection = new ArrayList { 5, 10, "a" }; // Act Action action = () => Assert.Contains(20, collection, "Item {0} not found", 20); @@ -386,6 +377,21 @@ public void Contains_ValueExpected_ItemDoesNotExist_ThrowsException() action.Should().Throw().WithMessage("*20*"); } + /// + /// Tests the Contains method (value overload) when the expected item is present. + /// + public void Contains_InNonGenericCollection_ValueExpected_ItemExists_DoesNotThrow() + { + // Arrange + var collection = new ArrayList { 5, 10, "a" }; + + // Act + Action action = () => Assert.Contains("a", collection, "No failure expected", null); + + // Assert + action.Should().NotThrow(); + } + /// /// Tests the Contains method with a comparer when the expected item is present. /// From 859aae5b46679a465a6ef767f2da788bc4203ea4 Mon Sep 17 00:00:00 2001 From: Muiz Atolagbe Date: Thu, 21 Aug 2025 20:45:22 +0100 Subject: [PATCH 4/6] moved the declaration to publicAPI.Unshipped and also added addtional test coverage --- .../PublicAPI/PublicAPI.Shipped.txt | 2 -- .../PublicAPI/PublicAPI.Unshipped.txt | 2 ++ .../Assertions/AssertTests.Contains.cs | 30 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt index 7cc4ea2996..7954d442eb 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Shipped.txt @@ -642,8 +642,6 @@ static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(string! subs static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(string! substring, string! value, System.StringComparison comparisonType, string? message) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(string! substring, string! value, System.StringComparison comparisonType) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(string! substring, string! value) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection, string? message, params object?[]? parameters) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(System.Func! predicate, System.Collections.Generic.IEnumerable! collection, string? message, params object?[]? parameters) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(System.Func! predicate, System.Collections.Generic.IEnumerable! collection, string? message) -> void static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(System.Func! predicate, System.Collections.Generic.IEnumerable! collection) -> void diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt index 7d27ebf1af..ebc9f70160 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt @@ -1,5 +1,7 @@ #nullable enable Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.Field = 3 -> Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection, string? message, params object?[]? parameters) -> 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 ac65fc1958..9bad3a14ad 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -392,6 +392,21 @@ public void Contains_InNonGenericCollection_ValueExpected_ItemExists_DoesNotThro action.Should().NotThrow(); } + /// + /// Tests the Contains method (value overload) when the expected item is present. + /// + public void Contains_InNonGenericCollection_ValueExpected_ItemExists_DoesNotThrowException() + { + // Arrange + var collection = new ArrayList { 5, 10, "a" }; + + // Act + Action action = () => Assert.Contains(5, collection); + + // Assert + action.Should().NotThrow(); + } + /// /// Tests the Contains method with a comparer when the expected item is present. /// @@ -912,6 +927,21 @@ public void Contains_ItemNotFound_ShowsSpecificErrorMessage() action.Should().Throw().WithMessage("*Expected collection to contain the specified item*"); } + /// + /// Tests that Contains (item) failure shows specific error message. + /// + public void Contains_InNonGenericCollection_ItemNotFound_ShowsSpecificErrorMessage() + { + // Arrange + var collection = new ArrayList { 1, 2, 3 }; + + // Act + Action action = () => Assert.Contains(5, collection); + + // Assert + action.Should().Throw().WithMessage("*Expected collection to contain the specified item*"); + } + /// /// Tests that Contains (predicate) failure shows specific error message. /// From 87f82d15eb60014707e886fa379e6741f513e5f8 Mon Sep 17 00:00:00 2001 From: Muiz Atolagbe Date: Sat, 23 Aug 2025 17:33:04 +0100 Subject: [PATCH 5/6] made changes to by elimination the message params --- .../TestFramework/Assertions/Assert.Contains.cs | 7 +++---- .../TestFramework/PublicAPI/PublicAPI.Unshipped.txt | 2 +- .../Assertions/AssertTests.Contains.cs | 7 ++++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs index 01cbc1320a..79d870ee75 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs @@ -184,7 +184,7 @@ public static void Contains(T expected, IEnumerable collection) /// The expected item. /// The non-generic collection (like ArrayList). public static void Contains(object expected, IEnumerable collection) - => Contains(expected, collection, string.Empty, null); + => Contains(expected, collection, string.Empty); /// /// Tests whether the specified collection contains the given element. @@ -219,8 +219,7 @@ public static void Contains(T expected, IEnumerable collection, string? me /// The expected item. /// The collection. /// The message format to display when the assertion fails. - /// The parameters to format the message. - public static void Contains(object expected, IEnumerable collection, string? message, params object?[]? parameters) + public static void Contains(object expected, IEnumerable collection, string? message) { bool isFound = false; foreach (object? item in collection) @@ -234,7 +233,7 @@ public static void Contains(object expected, IEnumerable collection, string? mes if (!isFound) { - string userMessage = BuildUserMessage(message, parameters); + string userMessage = BuildUserMessage(message); ThrowAssertContainsItemFailed(userMessage); } } diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt index ebc9f70160..372d50ffa8 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt @@ -1,7 +1,7 @@ #nullable enable Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.Field = 3 -> Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection, string? message, params object?[]? parameters) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, 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 9bad3a14ad..87db25b20a 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -369,12 +369,13 @@ public void Contains_InNonGenericCollection_ValueExpected_ItemDoesNotExist_Throw { // Arrange var collection = new ArrayList { 5, 10, "a" }; + object expected = 20; // Act - Action action = () => Assert.Contains(20, collection, "Item {0} not found", 20); + Action action = () => Assert.Contains(expected, collection, $"Item {expected} not found"); // Assert - action.Should().Throw().WithMessage("*20*"); + action.Should().Throw().WithMessage($"*Item {expected} not found*"); } /// @@ -386,7 +387,7 @@ public void Contains_InNonGenericCollection_ValueExpected_ItemExists_DoesNotThro var collection = new ArrayList { 5, 10, "a" }; // Act - Action action = () => Assert.Contains("a", collection, "No failure expected", null); + Action action = () => Assert.Contains("a", collection, "No failure expected"); // Assert action.Should().NotThrow(); From 8f2f8faae78ae6dc6a746a6802ac394a38256f36 Mon Sep 17 00:00:00 2001 From: Muiz Atolagbe Date: Fri, 29 Aug 2025 23:50:33 +0100 Subject: [PATCH 6/6] make expected value nullable for non-generic Assert.Contains function --- .../Assertions/Assert.Contains.cs | 4 +-- .../PublicAPI/PublicAPI.Unshipped.txt | 4 +-- .../Assertions/AssertTests.Contains.cs | 32 +++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs index 79d870ee75..19d1159549 100644 --- a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs +++ b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs @@ -183,7 +183,7 @@ public static void Contains(T expected, IEnumerable collection) /// /// The expected item. /// The non-generic collection (like ArrayList). - public static void Contains(object expected, IEnumerable collection) + public static void Contains(object? expected, IEnumerable collection) => Contains(expected, collection, string.Empty); /// @@ -219,7 +219,7 @@ public static void Contains(T expected, IEnumerable collection, string? me /// The expected item. /// The collection. /// The message format to display when the assertion fails. - public static void Contains(object expected, IEnumerable collection, string? message) + public static void Contains(object? expected, IEnumerable collection, string? message) { bool isFound = false; foreach (object? item in collection) diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt index 372d50ffa8..3a91f2d09b 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt @@ -1,7 +1,7 @@ #nullable enable Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType.Field = 3 -> Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataSourceType -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection) -> void -static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object! expected, System.Collections.IEnumerable! collection, string? message) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object? expected, System.Collections.IEnumerable! collection) -> void +static Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Contains(object? expected, 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 87db25b20a..967796f1f2 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -408,6 +408,38 @@ public void Contains_InNonGenericCollection_ValueExpected_ItemExists_DoesNotThro action.Should().NotThrow(); } + /// + /// Tests the Contains method (value overload) when the expected item is not present. + /// + public void Contains_InNonGenericCollection_NullableValueExpected_ItemDoesNotExist_ThrowsException() + { + // Arrange + var collection = new ArrayList { 5, 10, "a" }; + object? expected = null; + + // Act + Action action = () => Assert.Contains(expected, collection, $"Item {expected} not found"); + + // Assert + action.Should().Throw().WithMessage($"*Item {expected} not found*"); + } + + /// + /// Tests the Contains method (value overload) when the expected item is present. + /// + public void Contains_InNonGenericCollection_NullableValueExpected_ItemExists_DoesNotThrow() + { + // Arrange + var collection = new ArrayList { null, 10, "a" }; + object? expected = null; + + // Act + Action action = () => Assert.Contains(expected, collection, "No failure expected"); + + // Assert + action.Should().NotThrow(); + } + /// /// Tests the Contains method with a comparer when the expected item is present. ///