From 31062e4ec6b248b709095e13947400b49d507e53 Mon Sep 17 00:00:00 2001 From: LateApexEarlySpeed <72254037+lateapexearlyspeed@users.noreply.github.com> Date: Tue, 15 Dec 2020 00:15:59 +0800 Subject: [PATCH] =?UTF-8?q?Create=20more=20informational=20assert=20collec?= =?UTF-8?q?tion=20log=20for=20FunctionalTests=20o=E2=80=A6=20(#44350)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TestUtilities/System/AssertExtensions.cs | 64 +++++++++++++++++++ .../tests/FunctionalTests.cs | 25 ++------ 2 files changed, 69 insertions(+), 20 deletions(-) diff --git a/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs b/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs index ec648c7eab2c1..2b0d0379d4171 100644 --- a/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs +++ b/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs @@ -378,6 +378,58 @@ public static void Equal(HashSet expected, HashSet actual) } } + /// + /// 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 + /// + /// The collection that should contain same items as + /// + /// The comparer used to compare the items in two collections + public static void CollectionEqual(IEnumerable expected, IEnumerable actual, IEqualityComparer comparer) + { + var actualItemCountMapping = new Dictionary(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 expected1, T expected2, T value) { EqualityComparer comparer = EqualityComparer.Default; @@ -455,5 +507,17 @@ public static E Throws(string expectedParamName, Span 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; + } + } } } diff --git a/src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs b/src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs index fb919aaa943ec..b52e1cb94e925 100644 --- a/src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs +++ b/src/libraries/Microsoft.Extensions.FileSystemGlobbing/tests/FunctionalTests.cs @@ -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] @@ -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] @@ -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] @@ -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 GetFileList() @@ -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); } } }