-
Notifications
You must be signed in to change notification settings - Fork 257
Add custom verifiers to the test project #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
26 changes: 26 additions & 0 deletions
26
...Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/CSAnalyzerVerifier`1+Test.cs
This file contains hidden or 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,26 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
public static partial class CSharpAnalyzerVerifier<TAnalyzer> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
public class Test : CSharpAnalyzerTest<TAnalyzer, MSTestVerifier> | ||
{ | ||
public Test() | ||
{ | ||
SolutionTransforms.Add((solution, projectId) => | ||
{ | ||
var compilationOptions = solution.GetProject(projectId).CompilationOptions; | ||
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions( | ||
compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); | ||
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions); | ||
|
||
return solution; | ||
}); | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
....SDK/Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/CSAnalyzerVerifier`1.cs
This file contains hidden or 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,38 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
public static partial class CSharpAnalyzerVerifier<TAnalyzer> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.Diagnostic()"/> | ||
public static DiagnosticResult Diagnostic() | ||
=> CSharpAnalyzerVerifier<TAnalyzer, MSTestVerifier>.Diagnostic(); | ||
|
||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.Diagnostic(string)"/> | ||
public static DiagnosticResult Diagnostic(string diagnosticId) | ||
=> CSharpAnalyzerVerifier<TAnalyzer, MSTestVerifier>.Diagnostic(diagnosticId); | ||
|
||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.Diagnostic(DiagnosticDescriptor)"/> | ||
public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) | ||
=> CSharpAnalyzerVerifier<TAnalyzer, MSTestVerifier>.Diagnostic(descriptor); | ||
|
||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.VerifyAnalyzerAsync(string, DiagnosticResult[])"/> | ||
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); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/CSCodeFixVerifier`2+Test.cs
This file contains hidden or 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,28 @@ | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
public static partial class CSharpCodeFixVerifier<TAnalyzer, TCodeFix> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
where TCodeFix : CodeFixProvider, new() | ||
{ | ||
public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, MSTestVerifier> | ||
{ | ||
public Test() | ||
{ | ||
SolutionTransforms.Add((solution, projectId) => | ||
{ | ||
var compilationOptions = solution.GetProject(projectId).CompilationOptions; | ||
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions( | ||
compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); | ||
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions); | ||
|
||
return solution; | ||
}); | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...n.SDK/Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/CSCodeFixVerifier`2.cs
This file contains hidden or 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,61 @@ | ||
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; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
public static partial class CSharpCodeFixVerifier<TAnalyzer, TCodeFix> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
where TCodeFix : CodeFixProvider, new() | ||
{ | ||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.Diagnostic()"/> | ||
public static DiagnosticResult Diagnostic() | ||
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, MSTestVerifier>.Diagnostic(); | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.Diagnostic(string)"/> | ||
public static DiagnosticResult Diagnostic(string diagnosticId) | ||
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, MSTestVerifier>.Diagnostic(diagnosticId); | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.Diagnostic(DiagnosticDescriptor)"/> | ||
public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) | ||
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, MSTestVerifier>.Diagnostic(descriptor); | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyAnalyzerAsync(string, DiagnosticResult[])"/> | ||
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); | ||
} | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, string)"/> | ||
public static async Task VerifyCodeFixAsync(string source, string fixedSource) | ||
=> await VerifyCodeFixAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, DiagnosticResult, string)"/> | ||
public static async Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource) | ||
=> await VerifyCodeFixAsync(source, new[] { expected }, fixedSource); | ||
|
||
/// <inheritdoc cref="CodeFixVerifier{TAnalyzer, TCodeFix, TTest, TVerifier}.VerifyCodeFixAsync(string, DiagnosticResult[], string)"/> | ||
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); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...lyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/CSRefactoringVerifier`1+Test.cs
This file contains hidden or 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,26 @@ | ||
using Microsoft.CodeAnalysis.CodeRefactorings; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
public static partial class CSharpCodeRefactoringVerifier<TCodeRefactoring> | ||
where TCodeRefactoring : CodeRefactoringProvider, new() | ||
{ | ||
public class Test : CSharpCodeRefactoringTest<TCodeRefactoring, MSTestVerifier> | ||
{ | ||
public Test() | ||
{ | ||
SolutionTransforms.Add((solution, projectId) => | ||
{ | ||
var compilationOptions = solution.GetProject(projectId).CompilationOptions; | ||
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions( | ||
compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); | ||
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions); | ||
|
||
return solution; | ||
}); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...K/Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/CSRefactoringVerifier`1.cs
This file contains hidden or 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,36 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CodeRefactorings; | ||
using Microsoft.CodeAnalysis.Testing; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
public static partial class CSharpCodeRefactoringVerifier<TCodeRefactoring> | ||
where TCodeRefactoring : CodeRefactoringProvider, new() | ||
{ | ||
/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, string)"/> | ||
public static async Task VerifyRefactoringAsync(string source, string fixedSource) | ||
{ | ||
await VerifyRefactoringAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); | ||
} | ||
|
||
/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, DiagnosticResult, string)"/> | ||
public static async Task VerifyRefactoringAsync(string source, DiagnosticResult expected, string fixedSource) | ||
{ | ||
await VerifyRefactoringAsync(source, new[] { expected }, fixedSource); | ||
} | ||
|
||
/// <inheritdoc cref="CodeRefactoringVerifier{TCodeRefactoring, TTest, TVerifier}.VerifyRefactoringAsync(string, DiagnosticResult[], string)"/> | ||
public static async Task VerifyRefactoringAsync(string source, DiagnosticResult[] expected, string fixedSource) | ||
{ | ||
var test = new Test | ||
{ | ||
TestCode = source, | ||
FixedCode = fixedSource, | ||
}; | ||
|
||
test.ExpectedDiagnostics.AddRange(expected); | ||
await test.RunAsync(CancellationToken.None); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...slyn.SDK/Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/CSVerifierHelper.cs
This file contains hidden or 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,33 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
internal static class CSharpVerifierHelper | ||
{ | ||
/// <summary> | ||
/// By default, the compiler reports diagnostics for nullable reference types at | ||
/// <see cref="DiagnosticSeverity.Warning"/>, and the analyzer test framework defaults to only validating | ||
/// diagnostics at <see cref="DiagnosticSeverity.Error"/>. This map contains all compiler diagnostic IDs | ||
/// related to nullability mapped to <see cref="ReportDiagnostic.Error"/>, which is then used to enable all | ||
/// of these warnings for default validation during analyzer and code fix tests. | ||
/// </summary> | ||
internal static ImmutableDictionary<string, ReportDiagnostic> NullableWarnings { get; } = GetNullableWarningsFromCompiler(); | ||
|
||
private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler() | ||
{ | ||
string[] args = { "/warnaserror:nullable" }; | ||
var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory); | ||
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions; | ||
|
||
// Workaround for https://github.com/dotnet/roslyn/issues/41610 | ||
nullableWarnings = nullableWarnings | ||
.SetItem("CS8632", ReportDiagnostic.Error) | ||
.SetItem("CS8669", ReportDiagnostic.Error); | ||
|
||
return nullableWarnings; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/VBAnalyzerVerifier`1+Test.cs
This file contains hidden or 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,17 @@ | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
using Microsoft.CodeAnalysis.VisualBasic.Testing; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
public static partial class VisualBasicAnalyzerVerifier<TAnalyzer> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
public class Test : VisualBasicAnalyzerTest<TAnalyzer, MSTestVerifier> | ||
{ | ||
public Test() | ||
{ | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
....SDK/Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers/VBAnalyzerVerifier`1.cs
This file contains hidden or 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,38 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
using Microsoft.CodeAnalysis.VisualBasic.Testing; | ||
|
||
namespace $safeprojectname$ | ||
{ | ||
public static partial class VisualBasicAnalyzerVerifier<TAnalyzer> | ||
where TAnalyzer : DiagnosticAnalyzer, new() | ||
{ | ||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.Diagnostic()"/> | ||
public static DiagnosticResult Diagnostic() | ||
=> VisualBasicAnalyzerVerifier<TAnalyzer, MSTestVerifier>.Diagnostic(); | ||
|
||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.Diagnostic(string)"/> | ||
public static DiagnosticResult Diagnostic(string diagnosticId) | ||
=> VisualBasicAnalyzerVerifier<TAnalyzer, MSTestVerifier>.Diagnostic(diagnosticId); | ||
|
||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.Diagnostic(DiagnosticDescriptor)"/> | ||
public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) | ||
=> VisualBasicAnalyzerVerifier<TAnalyzer, MSTestVerifier>.Diagnostic(descriptor); | ||
|
||
/// <inheritdoc cref="AnalyzerVerifier{TAnalyzer, TTest, TVerifier}.VerifyAnalyzerAsync(string, DiagnosticResult[])"/> | ||
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have these as source code in the templates instead of adding them the the library itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ones in the library cannot be edited as part of the project.