-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59414c9
commit f93c224
Showing
24 changed files
with
1,218 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Machine.Specifications.Analyzers.Tests/AnalyzerVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace Machine.Specifications.Analyzers.Tests; | ||
|
||
public static class AnalyzerVerifier<TAnalyzer> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
public static DiagnosticResult Diagnostic() | ||
{ | ||
return CSharpAnalyzerVerifier<TAnalyzer, DefaultVerifier>.Diagnostic(); | ||
} | ||
|
||
public static DiagnosticResult Diagnostic(string diagnosticId) | ||
{ | ||
return CSharpAnalyzerVerifier<TAnalyzer, DefaultVerifier>.Diagnostic(diagnosticId); | ||
} | ||
|
||
public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) | ||
{ | ||
return CSharpAnalyzerVerifier<TAnalyzer, DefaultVerifier>.Diagnostic(descriptor); | ||
} | ||
|
||
public static async Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) | ||
{ | ||
var test = new Test | ||
{ | ||
TestCode = source | ||
}; | ||
|
||
test.ExpectedDiagnostics.AddRange(expected); | ||
|
||
await test.RunAsync(CancellationToken.None); | ||
} | ||
|
||
private class Test : CSharpAnalyzerTest<TAnalyzer, DefaultVerifier> | ||
{ | ||
public Test() | ||
{ | ||
ReferenceAssemblies = VerifierHelper.MspecAssemblies; | ||
SolutionTransforms.Add(VerifierHelper.GetNullableTransform); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Machine.Specifications.Analyzers.Tests/CodeFixVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace Machine.Specifications.Analyzers.Tests | ||
{ | ||
public static class CodeFixVerifier<TAnalyzer, TCodeFix> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
where TCodeFix : CodeFixProvider, new() | ||
{ | ||
public static DiagnosticResult Diagnostic() | ||
{ | ||
return CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.Diagnostic(); | ||
} | ||
|
||
public static DiagnosticResult Diagnostic(string diagnosticId) | ||
{ | ||
return CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.Diagnostic(diagnosticId); | ||
} | ||
|
||
public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) | ||
{ | ||
return CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.Diagnostic(descriptor); | ||
} | ||
|
||
public static async Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) | ||
{ | ||
var test = new Test | ||
{ | ||
TestCode = source | ||
}; | ||
|
||
test.ExpectedDiagnostics.AddRange(expected); | ||
|
||
await test.RunAsync(CancellationToken.None); | ||
} | ||
|
||
public static async Task VerifyCodeFixAsync(string source, string fixedSource) | ||
{ | ||
await VerifyCodeFixAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); | ||
} | ||
|
||
public static async Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource) | ||
{ | ||
await VerifyCodeFixAsync(source, new[] {expected}, fixedSource); | ||
} | ||
|
||
public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] expected, string fixedSource) | ||
{ | ||
var test = new Test | ||
{ | ||
TestCode = source, | ||
FixedCode = fixedSource | ||
}; | ||
|
||
test.ExpectedDiagnostics.AddRange(expected); | ||
|
||
await test.RunAsync(CancellationToken.None); | ||
} | ||
|
||
private class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, DefaultVerifier> | ||
{ | ||
public Test() | ||
{ | ||
ReferenceAssemblies = VerifierHelper.MspecAssemblies; | ||
SolutionTransforms.Add(VerifierHelper.GetNullableTransform); | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Machine.Specifications.Analyzers.Tests/Machine.Specifications.Analyzers.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>latest</LangVersion> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.11.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="xunit" Version="2.9.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Machine.Specifications.Analyzers\Machine.Specifications.Analyzers.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.