Skip to content

Commit

Permalink
xunit/xunit#2859: Update xUnit2013 to stop warning about .Count check…
Browse files Browse the repository at this point in the history
… with StringValues
  • Loading branch information
bradwilson committed Jan 6, 2024
1 parent 4bf17e3 commit db847bb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

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()
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static 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"),
Expand All @@ -41,6 +42,7 @@ static 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"),
Expand All @@ -51,6 +53,7 @@ static 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"),
Expand All @@ -62,6 +65,7 @@ static 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"),
Expand Down
1 change: 1 addition & 0 deletions src/xunit.analyzers.tests/xunit.analyzers.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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]" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -73,7 +76,7 @@ protected override void AnalyzeInvocation(
if (symbol is null)
return;

if (IsCollectionsWithExceptionThrowingGetEnumeratorMethod(symbol) ||
if (IsAllowedCollection(symbol) ||
!IsWellKnownSizeMethod(symbol) &&
!IsICollectionCountProperty(context, symbol) &&
!IsICollectionOfTCountProperty(context, symbol) &&
Expand Down Expand Up @@ -114,8 +117,8 @@ static string GetReplacementMethodName(
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());
Expand Down

0 comments on commit db847bb

Please sign in to comment.