Skip to content

Commit

Permalink
Create more informational assert collection log for FunctionalTests o… (
Browse files Browse the repository at this point in the history
  • Loading branch information
lateapexearlyspeed authored Dec 14, 2020
1 parent 995bbae commit 31062e4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,58 @@ public static void Equal<T>(HashSet<T> expected, HashSet<T> actual)
}
}

/// <summary>
/// Validates that the actual collection contains same items as expected collection. If the test fails, this will display:
/// 1. Count if two collection count are different;
/// 2. Missed expected collection item when found
/// </summary>
/// <param name="expected">The collection that <paramref name="actual"/> should contain same items as</param>
/// <param name="actual"></param>
/// <param name="comparer">The comparer used to compare the items in two collections</param>
public static void CollectionEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, IEqualityComparer<T> comparer)
{
var actualItemCountMapping = new Dictionary<T, ItemCount>(comparer);
int actualCount = 0;
foreach (T actualItem in actual)
{
if (actualItemCountMapping.TryGetValue(actualItem, out ItemCount countInfo))
{
countInfo.Original++;
countInfo.Remain++;
}
else
{
actualItemCountMapping[actualItem] = new ItemCount(1, 1);
}

actualCount++;
}

var expectedArray = expected.ToArray();
var expectedCount = expectedArray.Length;

if (expectedCount != actualCount)
{
throw new XunitException($"Expected count: {expectedCount}{Environment.NewLine}Actual count: {actualCount}");
}

for (var i = 0; i < expectedCount; i++)
{
T currentExpectedItem = expectedArray[i];
if (!actualItemCountMapping.TryGetValue(currentExpectedItem, out ItemCount countInfo))
{
throw new XunitException($"Expected: {currentExpectedItem} but not found");
}

if (countInfo.Remain == 0)
{
throw new XunitException($"Collections are not equal.{Environment.NewLine}Totally {countInfo.Original} {currentExpectedItem} in actual collection but expect more {currentExpectedItem}");
}

countInfo.Remain--;
}
}

public static void AtLeastOneEquals<T>(T expected1, T expected2, T value)
{
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
Expand Down Expand Up @@ -455,5 +507,17 @@ public static E Throws<E, T>(string expectedParamName, Span<T> span, AssertThrow
Assert.Equal(expectedParamName, exception.ParamName);
return exception;
}

private class ItemCount
{
public int Original { get; set; }
public int Remain { get; set; }

public ItemCount(int original, int remain)
{
Original = original;
Remain = remain;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,7 @@ public void StemCorrectWithDifferentWildCards()
"compiler/shared/sub/sub/sharedsub.cs"
};

Assert.Equal(
expected.OrderBy(e => e),
actual.OrderBy(e => e),
StringComparer.OrdinalIgnoreCase);
AssertExtensions.CollectionEqual(expected, actual, StringComparer.OrdinalIgnoreCase);
}

[Fact]
Expand Down Expand Up @@ -398,10 +395,7 @@ public void StemCorrectWithDifferentWildCards_WithInMemory()
"compiler/shared/sub/sub/sharedsub.cs"
};

Assert.Equal(
expected.OrderBy(e => e),
actual.OrderBy(e => e),
StringComparer.OrdinalIgnoreCase);
AssertExtensions.CollectionEqual(expected, actual, StringComparer.OrdinalIgnoreCase);
}

[Fact]
Expand All @@ -424,10 +418,7 @@ public void MultipleSubDirsAfterFirstWildcardMatch_HasCorrectStem()
"shared/sub/sub/sharedsub.cs"
};

Assert.Equal(
expected.OrderBy(e => e),
actual.OrderBy(e => e),
StringComparer.OrdinalIgnoreCase);
AssertExtensions.CollectionEqual(expected, actual, StringComparer.OrdinalIgnoreCase);
}

[Fact]
Expand All @@ -451,10 +442,7 @@ public void MultipleSubDirsAfterFirstWildcardMatch_HasCorrectStem_WithInMemory()
"shared/sub/sub/sharedsub.cs"
};

Assert.Equal(
expected.OrderBy(e => e),
actual.OrderBy(e => e),
StringComparer.OrdinalIgnoreCase);
AssertExtensions.CollectionEqual(expected, actual, StringComparer.OrdinalIgnoreCase);
}

private List<string> GetFileList()
Expand Down Expand Up @@ -530,10 +518,7 @@ private void ExecuteAndVerify(Matcher matcher, string directoryPath, params stri
var actual = results.Files.Select(match => Path.GetFullPath(Path.Combine(_context.RootPath, directoryPath, match.Path)));
var expected = expectFiles.Select(relativePath => Path.GetFullPath(Path.Combine(_context.RootPath, relativePath)));

Assert.Equal(
expected.OrderBy(e => e),
actual.OrderBy(e => e),
StringComparer.OrdinalIgnoreCase);
AssertExtensions.CollectionEqual(expected, actual, StringComparer.OrdinalIgnoreCase);
}
}
}

0 comments on commit 31062e4

Please sign in to comment.