mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Implement test code analyzer
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Testing;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Testing.Verifiers;
|
||||
|
||||
namespace MakeConst.Test
|
||||
{
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Testing;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using Microsoft.CodeAnalysis.Testing.Verifiers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MakeConst.Test
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.CodeAnalysis.CodeFixes;
|
||||
using Microsoft.CodeAnalysis.CSharp.Testing;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Testing.Verifiers;
|
||||
|
||||
namespace MakeConst.Test
|
||||
{
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
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;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MakeConst.Test
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Microsoft.CodeAnalysis.CodeRefactorings;
|
||||
using Microsoft.CodeAnalysis.CSharp.Testing;
|
||||
using Microsoft.CodeAnalysis.Testing.Verifiers;
|
||||
|
||||
namespace MakeConst.Test
|
||||
{
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Microsoft.CodeAnalysis.CodeRefactorings;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MakeConst.Test
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace MakeConst.Test
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user