Skip to content

Commit

Permalink
Update xUnit2018 for the new IsType/IsNotType overloads with exact ma…
Browse files Browse the repository at this point in the history
…tch flag
  • Loading branch information
bradwilson committed Nov 3, 2024
1 parent 6455d4a commit 17d74da
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit.v3.assert.source" Version="0.5.0-pre.34" />
<PackageReference Include="xunit.v3.core" Version="0.5.0-pre.34" />
<PackageReference Include="xunit.v3.assert.source" Version="0.5.0-pre.35" />
<PackageReference Include="xunit.v3.core" Version="0.5.0-pre.35" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0-pre.48" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ void TestMethod() {{
await Verify.VerifyAnalyzer(source, expected);
}

[Theory]
[MemberData(nameof(Methods))]
public async Task Interface_WithExactMatchFlag_TriggersForTrue(
string method,
string replacement)
{
// We can only trigger when we know the literal true is being used; anything else,
// we let the runtime figure it out.
var source = string.Format(/* lang=c#-test */ """
using System;
using Xunit;
class TestClass {{
void TestMethod() {{
var flag = true;
{{|#0:Assert.{0}<IDisposable>(new object(), true)|}};
Assert.{0}<IDisposable>(new object(), flag);
}}
}}
""", method);
var expected = Verify.Diagnostic().WithLocation(0).WithArguments("interface", "System.IDisposable", replacement);

await Verify.VerifyAnalyzer(source, expected);
}

[Theory]
[MemberData(nameof(Methods))]
public async Task AbstractClass_Triggers(
Expand All @@ -52,6 +78,30 @@ void TestMethod() {{
await Verify.VerifyAnalyzer(source, expected);
}

[Theory]
[MemberData(nameof(Methods))]
public async Task AbstractClass_WithExactMatchFlag_TriggersForTrue(
string method,
string replacement)
{
var source = string.Format(/* lang=c#-test */ """
using System.IO;
using Xunit;
class TestClass {{
void TestMethod() {{
var flag = true;
{{|#0:Assert.{0}<Stream>(new object(), true)|}};
Assert.{0}<Stream>(new object(), flag);
}}
}}
""", method);
var expected = Verify.Diagnostic().WithLocation(0).WithArguments("abstract class", "System.IO.Stream", replacement);

await Verify.VerifyAnalyzer(source, expected);
}

[Theory]
[MemberData(nameof(Methods))]
public async Task UsingStatic_Triggers(
Expand Down
12 changes: 6 additions & 6 deletions src/xunit.analyzers.tests/Utility/CodeAnalyzerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ 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.5.0-pre.34"),
new PackageIdentity("xunit.v3.common", "0.5.0-pre.34"),
new PackageIdentity("xunit.v3.extensibility.core", "0.5.0-pre.34"),
new PackageIdentity("xunit.v3.runner.common", "0.5.0-pre.34")
new PackageIdentity("xunit.v3.assert", "0.5.0-pre.35"),
new PackageIdentity("xunit.v3.common", "0.5.0-pre.35"),
new PackageIdentity("xunit.v3.extensibility.core", "0.5.0-pre.35"),
new PackageIdentity("xunit.v3.runner.common", "0.5.0-pre.35")
)
);

Expand All @@ -74,8 +74,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.5.0-pre.34"),
new PackageIdentity("xunit.v3.runner.utility", "0.5.0-pre.34")
new PackageIdentity("xunit.v3.common", "0.5.0-pre.35"),
new PackageIdentity("xunit.v3.runner.utility", "0.5.0-pre.35")
)
);
}
Expand Down
14 changes: 7 additions & 7 deletions src/xunit.analyzers.tests/xunit.analyzers.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
<PackageDownload Include="xunit.extensibility.core" Version="[2.9.3-pre.4]" />
<PackageDownload Include="xunit.extensibility.execution" Version="[2.9.3-pre.4]" />
<PackageDownload Include="xunit.runner.utility" Version="[2.9.3-pre.4]" />
<PackageDownload Include="xunit.v3.assert" Version="[0.5.0-pre.34]" />
<PackageDownload Include="xunit.v3.common" Version="[0.5.0-pre.34]" />
<PackageDownload Include="xunit.v3.extensibility.core" Version="[0.5.0-pre.34]" />
<PackageDownload Include="xunit.v3.runner.common" Version="[0.5.0-pre.34]" />
<PackageDownload Include="xunit.v3.runner.utility" Version="[0.5.0-pre.34]" />

<PackageDownload Include="xunit.v3.assert" Version="[0.5.0-pre.35]" />
<PackageDownload Include="xunit.v3.common" Version="[0.5.0-pre.35]" />
<PackageDownload Include="xunit.v3.extensibility.core" Version="[0.5.0-pre.35]" />
<PackageDownload Include="xunit.v3.runner.common" Version="[0.5.0-pre.35]" />
<PackageDownload Include="xunit.v3.runner.utility" Version="[0.5.0-pre.35]" />

<!-- Download packages referenced by CodeAnalysisNetAnalyzers -->
<PackageDownload Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="[9.0.0-preview.24454.1]" />
<PackageDownload Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="[3.11.0]" />

<!-- Download packages referenced by VsThreadingAnalyzers -->
<PackageDownload Include="Microsoft.VisualStudio.Threading.Analyzers" Version="[17.11.20]" />
<PackageDownload Include="System.Collections.Immutable" Version="[6.0.0]" Condition=" '$(TargetFramework)' == 'net8.0' " />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ protected override void AnalyzeInvocation(
if (typeKind is null)
return;

if (invocationOperation.Arguments.Length > 1)
{
if (invocationOperation.Arguments[1].Value is not ILiteralOperation operation)
return;
if (operation.ConstantValue.Value is not bool value)
return;
if (value != true)
return;
}

var typeName = SymbolDisplay.ToDisplayString(type);

if (!ReplacementMethods.TryGetValue(invocationOperation.TargetMethod.Name, out var replacement))
Expand Down

0 comments on commit 17d74da

Please sign in to comment.