xunit/xunit#2859: Update xUnit2013 to stop warning about .Count check with StringValues

This commit is contained in:
Brad Wilson 2024-01-06 14:10:16 -08:00
Родитель 4bf17e3cde
Коммит db847bb090
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 0B7BD15AD1EC5FDE
4 изменённых файлов: 16 добавлений и 7 удалений

Просмотреть файл

@ -5,9 +5,10 @@ using Verify = CSharpVerifier<Xunit.Analyzers.AssertEqualShouldNotBeUsedForColle
public class AssertEqualShouldNotBeUsedForCollectionSizeCheckTests
{
public static TheoryData<string> CollectionsWithExceptionThrowingGetEnumeratorMethod = new()
public static TheoryData<string> AllowedCollections = new()
{
"new System.ArraySegment<int>().Count",
"Microsoft.Extensions.Primitives.StringValues.Empty.Count",
};
public static TheoryData<string> Collections = new()
@ -42,8 +43,8 @@ public class AssertEqualShouldNotBeUsedForCollectionSizeCheckTests
};
[Theory]
[MemberData(nameof(CollectionsWithExceptionThrowingGetEnumeratorMethod))]
public async void DoesNotFindWarningForCollectionsWithExceptionThrowingGetEnumeratorMethod(string collection)
[MemberData(nameof(AllowedCollections))]
public async void AllowedCollection_DoesNotTrigger(string collection)
{
var source = $@"
using System.Linq;

Просмотреть файл

@ -31,6 +31,7 @@ static class CodeAnalyzerHelper
CurrentXunitV2 = defaultAssemblies.AddPackages(
ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.Primitives", "8.0.0"),
new PackageIdentity("System.Collections.Immutable", "1.6.0"),
new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"),
new PackageIdentity("xunit.abstractions", "2.0.3"),
@ -41,6 +42,7 @@ static class CodeAnalyzerHelper
CurrentXunitV2RunnerUtility = defaultAssemblies.AddPackages(
ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.Primitives", "8.0.0"),
new PackageIdentity("System.Collections.Immutable", "1.6.0"),
new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"),
new PackageIdentity("xunit.abstractions", "2.0.3"),
@ -51,6 +53,7 @@ static class CodeAnalyzerHelper
CurrentXunitV3 = defaultAssemblies.AddPackages(
ImmutableArray.Create(
new PackageIdentity("Microsoft.Bcl.AsyncInterfaces", "8.0.0"),
new PackageIdentity("Microsoft.Extensions.Primitives", "8.0.0"),
new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"),
new PackageIdentity("System.Text.Json", "8.0.0"),
new PackageIdentity("xunit.v3.assert", "0.1.1-pre.342"),
@ -62,6 +65,7 @@ static class CodeAnalyzerHelper
CurrentXunitV3RunnerUtility = defaultAssemblies.AddPackages(
ImmutableArray.Create(
new PackageIdentity("Microsoft.Bcl.AsyncInterfaces", "8.0.0"),
new PackageIdentity("Microsoft.Extensions.Primitives", "8.0.0"),
new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"),
new PackageIdentity("System.Text.Json", "8.0.0"),
new PackageIdentity("xunit.v3.common", "0.1.1-pre.342"),

Просмотреть файл

@ -8,6 +8,7 @@
<ItemGroup>
<!-- Download packages referenced by CodeAnalyzerHelper -->
<PackageDownload Include="Microsoft.Bcl.AsyncInterfaces" Version="[8.0.0]" />
<PackageDownload Include="Microsoft.Extensions.Primitives" Version="[8.0.0]" />
<PackageDownload Include="System.Collections.Immutable" Version="[1.6.0]" Condition=" '$(TargetFramework)' == 'net472' " />
<PackageDownload Include="System.Text.Json" Version="[8.0.0]" />
<PackageDownload Include="System.Threading.Tasks.Extensions" Version="[4.5.4]" />

Просмотреть файл

@ -13,9 +13,12 @@ namespace Xunit.Analyzers;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AssertEqualShouldNotBeUsedForCollectionSizeCheck : AssertUsageAnalyzerBase
{
static readonly HashSet<string> collectionTypesWithExceptionThrowingGetEnumeratorMethod = new()
static readonly HashSet<string> allowedCollections = new()
{
// ArraySegment<T>.GetEnumerator() can throw
"System.ArraySegment<T>",
// StringValues has an implicit string conversion that's preferred by the compiler, https://github.com/xunit/xunit/issues/2859
"Microsoft.Extensions.Primitives.StringValues",
};
static readonly HashSet<string> sizeMethods = new()
{
@ -73,7 +76,7 @@ public class AssertEqualShouldNotBeUsedForCollectionSizeCheck : AssertUsageAnaly
if (symbol is null)
return;
if (IsCollectionsWithExceptionThrowingGetEnumeratorMethod(symbol) ||
if (IsAllowedCollection(symbol) ||
!IsWellKnownSizeMethod(symbol) &&
!IsICollectionCountProperty(context, symbol) &&
!IsICollectionOfTCountProperty(context, symbol) &&
@ -114,8 +117,8 @@ public class AssertEqualShouldNotBeUsedForCollectionSizeCheck : AssertUsageAnaly
return methodName == Constants.Asserts.Equal ? Constants.Asserts.Empty : Constants.Asserts.NotEmpty;
}
static bool IsCollectionsWithExceptionThrowingGetEnumeratorMethod(ISymbol symbol) =>
collectionTypesWithExceptionThrowingGetEnumeratorMethod.Contains(symbol.ContainingType.ConstructedFrom.ToDisplayString());
static bool IsAllowedCollection(ISymbol symbol) =>
allowedCollections.Contains(symbol.ContainingType.ConstructedFrom.ToDisplayString());
static bool IsWellKnownSizeMethod(ISymbol symbol) =>
sizeMethods.Contains(symbol.OriginalDefinition.ToDisplayString());