Skip to content

Commit

Permalink
Fix CollectionAssert.AreEqual fails for list of strings using IEquali…
Browse files Browse the repository at this point in the history
…tyComparer following (#3886)
  • Loading branch information
engyebrahim authored and Evangelink committed Sep 30, 2024
1 parent 50707f7 commit 36338bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
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

0 comments on commit 36338bd

Please sign in to comment.