Skip to content

Commit

Permalink
Update for new IAsyncEnumerable<> assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jan 16, 2024
1 parent 43c6329 commit 43e9232
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit.assert.source" Version="2.6.5" />
<PackageReference Include="xunit.core" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6" />
<PackageReference Include="xunit.assert.source" Version="2.6.7-pre.4" />
<PackageReference Include="xunit.core" Version="2.6.7-pre.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7-pre.5" />
</ItemGroup>

</When>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class AssertEmptyCollectionCheckShouldNotBeUsedTests
"new System.Collections.Generic.HashSet<int>()",
"new System.Collections.ObjectModel.Collection<int>()",
"System.Linq.Enumerable.Empty<int>()",
#if NETCOREAPP3_0_OR_GREATER
"default(System.Collections.Generic.IAsyncEnumerable<int>)",
#endif
};

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,50 @@

public class AssertSingleShouldBeUsedForSingleParameterTests
{
[Fact]
public async void FindsInfo_ForSingleItemCollectionCheck()
[Theory]
[InlineData("default(IEnumerable<int>)")]
#if NETCOREAPP3_0_OR_GREATER
[InlineData("default(IAsyncEnumerable<int>)")]
#endif
public async void FindsInfo_ForSingleItemCollectionCheck(string collection)
{
var code = @"
var code = @$"
using Xunit;
using System.Collections.Generic;
public class TestClass {
public class TestClass {{
[Fact]
public void TestMethod() {
IEnumerable<object> collection = new List<object>() { new object() };
Assert.Collection(collection, item => Assert.NotNull(item));
}
}";
public void TestMethod() {{
Assert.Collection({collection}, item => Assert.NotNull(item));
}}
}}";

var expected =
Verify
.Diagnostic()
.WithSpan(10, 9, 10, 68)
.WithSpan(8, 9, 8, 58 + collection.Length)
.WithArguments("Collection");

await Verify.VerifyAnalyzer(code, expected);
}

[Fact]
public async void DoesNotFindInfo_ForMultipleItemCollectionCheck()
[Theory]
[InlineData("default(IEnumerable<int>)")]
#if NETCOREAPP3_0_OR_GREATER
[InlineData("default(IAsyncEnumerable<int>)")]
#endif
public async void DoesNotFindInfo_ForMultipleItemCollectionCheck(string collection)
{
var code = @"
var code = @$"
using Xunit;
using System.Collections.Generic;
public class TestClass {
public class TestClass {{
[Fact]
public void TestMethod() {
IEnumerable<object> collection = new List<object>() { new object(), new object() };
Assert.Collection(collection, item1 => Assert.NotNull(item1), item2 => Assert.NotNull(item2));
}
}";
public void TestMethod() {{
Assert.Collection({collection}, item1 => Assert.NotNull(item1), item2 => Assert.NotNull(item2));
}}
}}";

await Verify.VerifyAnalyzer(code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class TestClass {

string codeTemplate = @"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -48,6 +49,14 @@ public static class MyTaskExtensions {{

public static TheoryData<string, string> AsyncAssertions = new()
{
{ "AllAsync", "Assert.AllAsync(default(IEnumerable<int>), i => Task.FromResult(true))" },
#if NETCOREAPP3_0_OR_GREATER
{ "AllAsync", "Assert.AllAsync(default(IAsyncEnumerable<int>), i => Task.FromResult(true))" },
#endif
{ "CollectionAsync", "Assert.CollectionAsync(default(IEnumerable<int>))" },
#if NETCOREAPP3_0_OR_GREATER
{ "CollectionAsync", "Assert.CollectionAsync(default(IAsyncEnumerable<int>))" },
#endif
{ "PropertyChangedAsync", "Assert.PropertyChangedAsync(this, nameof(Property), async () => throw new DivideByZeroException())" },
{ "RaisesAnyAsync", "Assert.RaisesAnyAsync(eh => SimpleEvent += eh, eh => SimpleEvent -= eh, async () => throw new DivideByZeroException())" },
{ "RaisesAnyAsync", "Assert.RaisesAnyAsync<int>(eh => SimpleIntEvent += eh, eh => SimpleIntEvent -= eh, async () => throw new DivideByZeroException())" },
Expand Down Expand Up @@ -112,7 +121,7 @@ public static class MyTaskExtensions {{
var expected =
Verify
.Diagnostic()
.WithSpan(16, 9, 16, 9 + assertion.Length)
.WithSpan(17, 9, 17, 9 + assertion.Length)
.WithSeverity(DiagnosticSeverity.Error)
.WithArguments(assertionName);

Expand All @@ -129,7 +138,7 @@ public static class MyTaskExtensions {{
var expected =
Verify
.Diagnostic()
.WithSpan(16, 9, 16, 9 + assertion.Length)
.WithSpan(17, 9, 17, 9 + assertion.Length)
.WithSeverity(DiagnosticSeverity.Error)
.WithArguments(assertionName);

Expand Down
16 changes: 8 additions & 8 deletions src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ static CodeAnalyzerHelper()
new PackageIdentity("System.Collections.Immutable", "1.6.0"),
new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"),
new PackageIdentity("xunit.abstractions", "2.0.3"),
new PackageIdentity("xunit.assert", "2.6.5"),
new PackageIdentity("xunit.core", "2.6.5")
new PackageIdentity("xunit.assert", "2.6.7-pre.4"),
new PackageIdentity("xunit.core", "2.6.7-pre.4")
)
);

Expand All @@ -46,7 +46,7 @@ static CodeAnalyzerHelper()
new PackageIdentity("System.Collections.Immutable", "1.6.0"),
new PackageIdentity("System.Threading.Tasks.Extensions", "4.5.4"),
new PackageIdentity("xunit.abstractions", "2.0.3"),
new PackageIdentity("xunit.runner.utility", "2.6.5")
new PackageIdentity("xunit.runner.utility", "2.6.7-pre.4")
)
);

Expand All @@ -56,9 +56,9 @@ static CodeAnalyzerHelper()
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"),
new PackageIdentity("xunit.v3.common", "0.1.1-pre.342"),
new PackageIdentity("xunit.v3.extensibility.core", "0.1.1-pre.342")
new PackageIdentity("xunit.v3.assert", "0.1.1-pre.350"),
new PackageIdentity("xunit.v3.common", "0.1.1-pre.350"),
new PackageIdentity("xunit.v3.extensibility.core", "0.1.1-pre.350")
)
);

Expand All @@ -68,8 +68,8 @@ static CodeAnalyzerHelper()
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"),
new PackageIdentity("xunit.v3.runner.utility", "0.1.1-pre.342")
new PackageIdentity("xunit.v3.common", "0.1.1-pre.350"),
new PackageIdentity("xunit.v3.runner.utility", "0.1.1-pre.350")
)
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/xunit.analyzers.tests/xunit.analyzers.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<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]" />
<PackageDownload Include="xunit.assert" Version="[2.6.5]" />
<PackageDownload Include="xunit.runner.utility" Version="[2.6.5]" />
<PackageDownload Include="xunit.v3.assert" Version="[0.1.1-pre.342]" />
<PackageDownload Include="xunit.v3.common" Version="[0.1.1-pre.342]" />
<PackageDownload Include="xunit.v3.extensibility.core" Version="[0.1.1-pre.342]" />
<PackageDownload Include="xunit.v3.runner.utility" Version="[0.1.1-pre.342]" />
<PackageDownload Include="xunit.assert" Version="[2.6.7-pre.4]" />
<PackageDownload Include="xunit.runner.utility" Version="[2.6.7-pre.4]" />
<PackageDownload Include="xunit.v3.assert" Version="[0.1.1-pre.350]" />
<PackageDownload Include="xunit.v3.common" Version="[0.1.1-pre.350]" />
<PackageDownload Include="xunit.v3.extensibility.core" Version="[0.1.1-pre.350]" />
<PackageDownload Include="xunit.v3.runner.utility" Version="[0.1.1-pre.350]" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/xunit.analyzers/Utility/TypeSymbolFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public static class TypeSymbolFactory
public static INamedTypeSymbol? IAssemblyInfo_V2(Compilation compilation) =>
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName("Xunit.Abstractions.IAssemblyInfo");

public static INamedTypeSymbol? IAsyncEnumerableOfT(Compilation compilation) =>
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName("System.Collections.Generic.IAsyncEnumerable`1");

public static INamedTypeSymbol? IAsyncLifetime(Compilation compilation) =>
Guard.ArgumentNotNull(compilation).GetTypeByMetadataName("Xunit.IAsyncLifetime");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public AssertEmptyCollectionCheckShouldNotBeUsed()
if (arguments.Count != 1)
return;

if (!method.Parameters[0].Type.OriginalDefinition.SpecialType.Equals(SpecialType.System_Collections_Generic_IEnumerable_T))
var matchedType = false;
var asyncEnumerable = TypeSymbolFactory.IAsyncEnumerableOfT(context.Compilation);
if (asyncEnumerable != null)
matchedType = SymbolEqualityComparer.Default.Equals(method.Parameters[0].Type.OriginalDefinition, asyncEnumerable);

matchedType = matchedType || method.Parameters[0].Type.OriginalDefinition.SpecialType.Equals(SpecialType.System_Collections_Generic_IEnumerable_T);

if (!matchedType)
return;

context.ReportDiagnostic(
Expand Down
2 changes: 2 additions & 0 deletions src/xunit.analyzers/X2000/AsyncAssertsShouldBeAwaited.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class AsyncAssertsShouldBeAwaited : AssertUsageAnalyzerBase
{
static readonly string[] targetMethods =
{
Constants.Asserts.AllAsync,
Constants.Asserts.CollectionAsync,
Constants.Asserts.PropertyChangedAsync,
Constants.Asserts.RaisesAnyAsync,
Constants.Asserts.RaisesAsync,
Expand Down

0 comments on commit 43e9232

Please sign in to comment.