diff --git a/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs b/src/TestFramework/TestFramework/Assertions/Assert.Contains.cs index 849d9a4bac..19d1159549 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); + /// /// Tests whether the specified collection contains the given element. /// @@ -205,6 +213,31 @@ 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. + public static void Contains(object? expected, IEnumerable collection, string? message) + { + bool isFound = false; + foreach (object? item in collection) + { + if (object.Equals(expected, item)) + { + 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..3a91f2d09b 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) -> 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..967796f1f2 100644 --- a/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs +++ b/test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.Contains.cs @@ -362,6 +362,84 @@ 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_ItemDoesNotExist_ThrowsException() + { + // Arrange + var collection = new ArrayList { 5, 10, "a" }; + object expected = 20; + + // 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_ValueExpected_ItemExists_DoesNotThrow() + { + // Arrange + var collection = new ArrayList { 5, 10, "a" }; + + // Act + Action action = () => Assert.Contains("a", collection, "No failure expected"); + + // Assert + 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 (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. /// @@ -882,6 +960,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. ///