Skip to content

Commit

Permalink
Squashed 'src/Microsoft.DotNet.XUnitAssert/src/' changes from 46dfaa9…
Browse files Browse the repository at this point in the history
…2..28ab73fb

28ab73fb xunit/xunit#2795: Collection equality regression with custom comparer on sequence type
30b56ac2 Update README.md
f125ad27 Formatting
03aec0e5 xunit/xunit#2755: Another embedded collection regression
380afc7e Enable ability to build source with all .NET 6 analyzer rules enabled
a5a79156 Add null argument guards to exception factories

git-subtree-dir: src/Microsoft.DotNet.XUnitAssert/src
git-subtree-split: 28ab73fb04b7813b22b1bd9930e275fca1a08f7f
  • Loading branch information
agocke committed Oct 19, 2023
1 parent 9856e56 commit 2b57770
Show file tree
Hide file tree
Showing 49 changed files with 306 additions and 235 deletions.
13 changes: 12 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,15 @@ dotnet_naming_style.begins_with_i.capitalization = pascal_case

#### Roslyn diagnostics ####

dotnet_diagnostic.IDE1006.severity = none
dotnet_diagnostic.CA1000.severity = none # Do not declare static members on generic types
dotnet_diagnostic.CA1031.severity = none # Modify method to catch a more specific allowed exception type
dotnet_diagnostic.CA1032.severity = none # Add missing constructor to exception
dotnet_diagnostic.CA1034.severity = none # Do not nest type
dotnet_diagnostic.CA1040.severity = none # Avoid empty interfaces
dotnet_diagnostic.CA1052.severity = none # Type is a static holder type but is not static
dotnet_diagnostic.CA1063.severity = none # Ensure that Dispose is declared as public and sealed
dotnet_diagnostic.CA1200.severity = none # Avoid using cref tags with a prefix
dotnet_diagnostic.CA1707.severity = none # Remove the underscores from type name
dotnet_diagnostic.CA1720.severity = none # Identifier contains type name
dotnet_diagnostic.CA1810.severity = none # Do not use static constructors
dotnet_diagnostic.CA2007.severity = none # Consider calling ConfigureAwait on the awaited task
213 changes: 112 additions & 101 deletions CollectionAsserts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,30 @@ public static void Collection<T>(
GuardArgumentNotNull(nameof(collection), collection);
GuardArgumentNotNull(nameof(elementInspectors), elementInspectors);

var tracker = collection.AsTracker();
var index = 0;

foreach (var item in tracker)
using (var tracker = collection.AsTracker())
{
try
{
if (index < elementInspectors.Length)
elementInspectors[index](item);
}
catch (Exception ex)
var index = 0;

foreach (var item in tracker)
{
int? pointerIndent;
var formattedCollection = tracker.FormatIndexedMismatch(index, out pointerIndent);
throw CollectionException.ForMismatchedItem(ex, index, pointerIndent, formattedCollection);
try
{
if (index < elementInspectors.Length)
elementInspectors[index](item);
}
catch (Exception ex)
{
int? pointerIndent;
var formattedCollection = tracker.FormatIndexedMismatch(index, out pointerIndent);
throw CollectionException.ForMismatchedItem(ex, index, pointerIndent, formattedCollection);
}

index++;
}

index++;
if (tracker.IterationCount != elementInspectors.Length)
throw CollectionException.ForMismatchedItemCount(elementInspectors.Length, tracker.IterationCount, tracker.FormatStart());
}

if (tracker.IterationCount != elementInspectors.Length)
throw CollectionException.ForMismatchedItemCount(elementInspectors.Length, tracker.IterationCount, tracker.FormatStart());
}

#if XUNIT_VALUETASK
Expand All @@ -191,28 +193,30 @@ public static async ValueTask CollectionAsync<T>(
GuardArgumentNotNull(nameof(collection), collection);
GuardArgumentNotNull(nameof(elementInspectors), elementInspectors);

var tracker = collection.AsTracker();
var index = 0;

foreach (var item in tracker)
using (var tracker = collection.AsTracker())
{
try
{
if (index < elementInspectors.Length)
await elementInspectors[index](item);
}
catch (Exception ex)
var index = 0;

foreach (var item in tracker)
{
int? pointerIndent;
var formattedCollection = tracker.FormatIndexedMismatch(index, out pointerIndent);
throw CollectionException.ForMismatchedItem(ex, index, pointerIndent, formattedCollection);
try
{
if (index < elementInspectors.Length)
await elementInspectors[index](item);
}
catch (Exception ex)
{
int? pointerIndent;
var formattedCollection = tracker.FormatIndexedMismatch(index, out pointerIndent);
throw CollectionException.ForMismatchedItem(ex, index, pointerIndent, formattedCollection);
}

index++;
}

index++;
if (tracker.IterationCount != elementInspectors.Length)
throw CollectionException.ForMismatchedItemCount(elementInspectors.Length, tracker.IterationCount, tracker.FormatStart());
}

if (tracker.IterationCount != elementInspectors.Length)
throw CollectionException.ForMismatchedItemCount(elementInspectors.Length, tracker.IterationCount, tracker.FormatStart());
}
#endif

Expand Down Expand Up @@ -254,10 +258,9 @@ public static void Contains<T>(
GuardArgumentNotNull(nameof(collection), collection);
GuardArgumentNotNull(nameof(comparer), comparer);

var tracker = collection.AsTracker();

if (!tracker.Contains(expected, comparer))
throw ContainsException.ForCollectionItemNotFound(ArgumentFormatter.Format(expected), tracker.FormatStart());
using (var tracker = collection.AsTracker())
if (!tracker.Contains(expected, comparer))
throw ContainsException.ForCollectionItemNotFound(ArgumentFormatter.Format(expected), tracker.FormatStart());
}

/// <summary>
Expand All @@ -274,13 +277,14 @@ public static void Contains<T>(
GuardArgumentNotNull(nameof(collection), collection);
GuardArgumentNotNull(nameof(filter), filter);

var tracker = collection.AsTracker();

foreach (var item in tracker)
if (filter(item))
return;
using (var tracker = collection.AsTracker())
{
foreach (var item in tracker)
if (filter(item))
return;

throw ContainsException.ForCollectionFilterNotMatched(tracker.FormatStart());
throw ContainsException.ForCollectionFilterNotMatched(tracker.FormatStart());
}
}

/// <summary>
Expand All @@ -306,12 +310,14 @@ public static void Distinct<T>(
GuardArgumentNotNull(nameof(collection), collection);
GuardArgumentNotNull(nameof(comparer), comparer);

var tracker = collection.AsTracker();
var set = new HashSet<T>(comparer);
using (var tracker = collection.AsTracker())
{
var set = new HashSet<T>(comparer);

foreach (var item in tracker)
if (!set.Add(item))
throw DistinctException.ForDuplicateItem(ArgumentFormatter.Format(item), tracker.FormatStart());
foreach (var item in tracker)
if (!set.Add(item))
throw DistinctException.ForDuplicateItem(ArgumentFormatter.Format(item), tracker.FormatStart());
}
}

/// <summary>
Expand Down Expand Up @@ -352,25 +358,27 @@ public static void DoesNotContain<T>(
GuardArgumentNotNull(nameof(collection), collection);
GuardArgumentNotNull(nameof(comparer), comparer);

var tracker = collection.AsTracker();
var index = 0;

foreach (var item in tracker)
using (var tracker = collection.AsTracker())
{
if (comparer.Equals(item, expected))
var index = 0;

foreach (var item in tracker)
{
int? pointerIndent;
var formattedCollection = tracker.FormatIndexedMismatch(index, out pointerIndent);

throw DoesNotContainException.ForCollectionItemFound(
ArgumentFormatter.Format(expected),
index,
pointerIndent,
formattedCollection
);
if (comparer.Equals(item, expected))
{
int? pointerIndent;
var formattedCollection = tracker.FormatIndexedMismatch(index, out pointerIndent);

throw DoesNotContainException.ForCollectionItemFound(
ArgumentFormatter.Format(expected),
index,
pointerIndent,
formattedCollection
);
}

++index;
}

++index;
}
}

Expand All @@ -388,24 +396,26 @@ public static void DoesNotContain<T>(
GuardArgumentNotNull(nameof(collection), collection);
GuardArgumentNotNull(nameof(filter), filter);

var tracker = collection.AsTracker();
var index = 0;

foreach (var item in tracker)
using (var tracker = collection.AsTracker())
{
if (filter(item))
var index = 0;

foreach (var item in tracker)
{
int? pointerIndent;
var formattedCollection = tracker.FormatIndexedMismatch(index, out pointerIndent);

throw DoesNotContainException.ForCollectionFilterMatched(
index,
pointerIndent,
formattedCollection
);
if (filter(item))
{
int? pointerIndent;
var formattedCollection = tracker.FormatIndexedMismatch(index, out pointerIndent);

throw DoesNotContainException.ForCollectionFilterMatched(
index,
pointerIndent,
formattedCollection
);
}

++index;
}

++index;
}
}

Expand All @@ -419,8 +429,7 @@ public static void Empty(IEnumerable collection)
{
GuardArgumentNotNull(nameof(collection), collection);

var tracker = collection.AsTracker();

using (var tracker = collection.AsTracker())
using (var enumerator = tracker.GetEnumerator())
if (enumerator.MoveNext())
throw EmptyException.ForNonEmptyCollection(tracker.FormatStart());
Expand Down Expand Up @@ -652,33 +661,35 @@ static T GetSingleResult<T>(
var index = 0;
var matchIndices = new List<int>();
var result = default(T);
var tracker = collection.AsTracker();

foreach (var item in tracker)
using (var tracker = collection.AsTracker())
{
if (predicate == null || predicate(item))
foreach (var item in tracker)
{
if (++count == 1)
result = item;
if (predicate != null)
matchIndices.Add(index);
if (predicate == null || predicate(item))
{
if (++count == 1)
result = item;
if (predicate != null)
matchIndices.Add(index);
}

++index;
}

++index;
}

switch (count)
{
case 0:
throw SingleException.Empty(expected, tracker.FormatStart());
case 1:
switch (count)
{
case 0:
throw SingleException.Empty(expected, tracker.FormatStart());
case 1:
#if XUNIT_NULLABLE
return result!;
return result!;
#else
return result;
return result;
#endif
default:
throw SingleException.MoreThanOne(count, expected, tracker.FormatStart(), matchIndices);
default:
throw SingleException.MoreThanOne(count, expected, tracker.FormatStart(), matchIndices);
}
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions EqualityAsserts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Xunit.Sdk;
Expand Down Expand Up @@ -316,8 +317,8 @@ public static void Equal(

if (!(object.Equals(expected, actual) || Math.Abs(expected - actual) <= tolerance))
throw EqualException.ForMismatchedValues(
expected.ToString("G17"),
actual.ToString("G17"),
expected.ToString("G17", CultureInfo.CurrentCulture),
actual.ToString("G17", CultureInfo.CurrentCulture),
$"Values are not within tolerance {tolerance:G17}"
);
}
Expand Down Expand Up @@ -388,8 +389,8 @@ public static void Equal(

if (!(object.Equals(expected, actual) || Math.Abs(expected - actual) <= tolerance))
throw EqualException.ForMismatchedValues(
expected.ToString("G9"),
actual.ToString("G9"),
expected.ToString("G9", CultureInfo.CurrentCulture),
actual.ToString("G9", CultureInfo.CurrentCulture),
$"Values are not within tolerance {tolerance:G9}"
);
}
Expand Down Expand Up @@ -733,8 +734,8 @@ public static void NotEqual(

if (object.Equals(expected, actual) || Math.Abs(expected - actual) <= tolerance)
throw NotEqualException.ForEqualValues(
expected.ToString("G17"),
actual.ToString("G17"),
expected.ToString("G17", CultureInfo.CurrentCulture),
actual.ToString("G17", CultureInfo.CurrentCulture),
$"Values are within tolerance {tolerance:G17}"
);
}
Expand Down Expand Up @@ -805,8 +806,8 @@ public static void NotEqual(

if (object.Equals(expected, actual) || Math.Abs(expected - actual) <= tolerance)
throw NotEqualException.ForEqualValues(
expected.ToString("G9"),
actual.ToString("G9"),
expected.ToString("G9", CultureInfo.CurrentCulture),
actual.ToString("G9", CultureInfo.CurrentCulture),
$"Values are within tolerance {tolerance:G9}"
);
}
Expand Down
10 changes: 6 additions & 4 deletions Guards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ namespace Xunit
partial class Assert
{
/// <summary/>
internal static void GuardArgumentNotNull(
internal static T GuardArgumentNotNull<T>(
string argName,
#if XUNIT_NULLABLE
[NotNull] object? argValue)
[NotNull] T? argValue)
#else
object argValue)
T argValue)
#endif
{
if (argValue == null)
throw new ArgumentNullException(argName);
throw new ArgumentNullException(argName.TrimStart('@'));

return argValue;
}
}
}
Loading

0 comments on commit 2b57770

Please sign in to comment.