Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/TestFramework/TestFramework/Assertions/Assert.Contains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ public static T ContainsSingle<T>(Func<T, bool> predicate, IEnumerable<T> collec
public static void Contains<T>(T expected, IEnumerable<T> collection)
=> Contains(expected, collection, string.Empty, null);

/// <summary>
/// Tests whether the specified non-generic collection contains the given element.
/// </summary>
/// <param name="expected">The expected item.</param>
/// <param name="collection">The non-generic collection (like ArrayList).</param>
public static void Contains(object? expected, IEnumerable collection)
=> Contains(expected, collection, string.Empty);

/// <summary>
/// Tests whether the specified collection contains the given element.
/// </summary>
Expand Down Expand Up @@ -205,6 +213,31 @@ public static void Contains<T>(T expected, IEnumerable<T> collection, string? me
}
}

/// <summary>
/// Tests whether the specified collection contains the given element.
/// </summary>
/// <param name="expected">The expected item.</param>
/// <param name="collection">The collection.</param>
/// <param name="message">The message format to display when the assertion fails.</param>
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);
}
}

/// <summary>
/// Tests whether the specified collection contains the given element.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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!
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,84 @@ public void Contains_ValueExpected_ItemDoesNotExist_ThrowsException()
action.Should().Throw<AssertFailedException>().WithMessage("*20*");
}

/// <summary>
/// Tests the Contains method (value overload) when the expected item is present.
/// </summary>
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<AssertFailedException>().WithMessage($"*Item {expected} not found*");
}

/// <summary>
/// Tests the Contains method (value overload) when the expected item is present.
/// </summary>
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<AssertFailedException>();
}

/// <summary>
/// Tests the Contains method (value overload) when the expected item is present.
/// </summary>
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<AssertFailedException>();
}

/// <summary>
/// Tests the Contains method (value overload) when the expected item is not present.
/// </summary>
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<AssertFailedException>().WithMessage($"*Item {expected} not found*");
}

/// <summary>
/// Tests the Contains method (value overload) when the expected item is present.
/// </summary>
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<AssertFailedException>();
}

/// <summary>
/// Tests the Contains method with a comparer when the expected item is present.
/// </summary>
Expand Down Expand Up @@ -882,6 +960,21 @@ public void Contains_ItemNotFound_ShowsSpecificErrorMessage()
action.Should().Throw<AssertFailedException>().WithMessage("*Expected collection to contain the specified item*");
}

/// <summary>
/// Tests that Contains (item) failure shows specific error message.
/// </summary>
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<AssertFailedException>().WithMessage("*Expected collection to contain the specified item*");
}

/// <summary>
/// Tests that Contains (predicate) failure shows specific error message.
/// </summary>
Expand Down