Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CollectionAssert.AreEqual fails for list of strings using IEqualityComparer following #3886

Merged
merged 6 commits into from
Sep 30, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1641,8 +1641,11 @@ private static bool CompareIEnumerable(IEnumerable? expected, IEnumerable? actua

object? curExpected = expectedEnum.Current;
object? curActual = actualEnum.Current;

if (curExpected is IEnumerable curExpectedEnum && curActual is IEnumerable curActualEnum)
if (comparer.Compare(curExpected, curActual) == 0)
{
position++;
}
else if (curExpected is IEnumerable curExpectedEnum && curActual is IEnumerable curActualEnum)
{
stack.Push(new(expectedEnum, actualEnum, position + 1));
stack.Push(new(curExpectedEnum.GetEnumerator(), curActualEnum.GetEnumerator(), 0));
Expand All @@ -1656,8 +1659,6 @@ private static bool CompareIEnumerable(IEnumerable? expected, IEnumerable? actua
position);
return false;
}

position++;
}

if (actualEnum.MoveNext() && !expectedEnum.MoveNext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ public void CollectionAssertAreEqualComparerNullabilityPostConditions()
comparer.ToString(); // no warning
}

public void CollectionAssertAreEqual_WithIgnoreCaseComparer_DoesNotThrow()
{
List<string> expected = ["one", "two"];
List<string> actual = ["ONE", "tWo"];
CollectionAssert.AreEqual(expected, actual, StringComparer.OrdinalIgnoreCase);
}

public void CollectionAssertAreEqual_WithCaseSensetiveComparer_Fails()
{
List<string> expected = ["one", "two"];
List<string> actual = ["ONE", "tWo"];
VerifyThrows(() => CollectionAssert.AreEqual(expected, actual, StringComparer.Ordinal));
}

public void CollectionAssertAreEqualComparerMessageNullabilityPostConditions()
{
ICollection? collection1 = GetCollection();
Expand Down Expand Up @@ -264,6 +278,20 @@ public void CollectionAssertAreNotEqual_NotEqualNestedLists_Passes()
CollectionAssert.AreNotEqual(collection1, collection2);
}

public void CollectionAssertAreNotEqual_WithIgnoreCaseComparer_Fails()
{
List<string> expected = ["one", "two"];
List<string> actual = ["ONE", "tWo"];
VerifyThrows(() => CollectionAssert.AreNotEqual(expected, actual, StringComparer.OrdinalIgnoreCase));
}

public void CollectionAssertAreNotEqual_WithCaseSensitiveComparer_Passes()
{
List<string> expected = ["one", "two"];
List<string> actual = ["ONE", "tWo"];
CollectionAssert.AreNotEqual(expected, actual, StringComparer.Ordinal);
}

public void CollectionAssertAreNotEqual_EqualNestedLists_Fails()
{
ICollection? collection1 = GetNestedLists();
Expand Down