/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> /// <param name="metadataReferences">The metadata references to use when compiling.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public static Task ValidAsync(DiagnosticAnalyzer analyzer, ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> code, IReadOnlyList <MetadataReference> metadataReferences) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); return(ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), metadataReferences)); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code"> /// The code to create the solution from. /// Can be a .cs, .csproj or .sln file /// </param> public static void Valid(DiagnosticAnalyzer analyzer, ExpectedDiagnostic expectedDiagnostic, FileInfo code) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzer"/>. /// </summary> /// <param name="analyzer">The analyzer.</param> /// <param name="expectedDiagnostics">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void Valid(DiagnosticAnalyzer analyzer, IReadOnlyList <ExpectedDiagnostic> expectedDiagnostics, params string[] code) { AssertAnalyzerSupportsExpectedDiagnostics(analyzer, expectedDiagnostics, out var descriptors, out var suppressedDiagnostics); ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptors, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that <paramref name="code"/> produces the expected diagnostics. /// </summary> /// <param name="analyzer">The analyzer to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic</param> /// <param name="code">The code to analyze.</param> public static void Diagnostics(DiagnosticAnalyzer analyzer, ExpectedDiagnostic expectedDiagnostic, params string[] code) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); DiagnosticsWithMetadataAsync( analyzer, DiagnosticsAndSources.Create(expectedDiagnostic, code), CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences, null) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="analyzer">The type of the analyzer.</param> /// <param name="codeFix">The type of the code fix.</param> /// <param name="expectedDiagnostics">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void NoFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, IReadOnlyList <ExpectedDiagnostic> expectedDiagnostics, IReadOnlyList <string> code) { AssertAnalyzerSupportsExpectedDiagnostics(analyzer, expectedDiagnostics, out var descriptors, out var suppressedDiagnostics); NoFixAsync( analyzer, codeFix, new DiagnosticsAndSources(expectedDiagnostics, code), CodeFactory.DefaultCompilationOptions(descriptors, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <param name="analyzer">The type of the analyzer.</param> /// <param name="codeFix">The type of the code fix.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void NoFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, ExpectedDiagnostic expectedDiagnostic, string code) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); NoFixAsync( analyzer, codeFix, new DiagnosticsAndSources(new[] { expectedDiagnostic }, new[] { code }), CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="metadataReference">The meta data metadataReference to add to the compilation.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static Task FixAllAsync(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, IReadOnlyList <MetadataReference> metadataReference, string fixTitle, AllowCompilationErrors allowCompilationErrors) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); return(FixAllAsync( analyzer, codeFix, DiagnosticsAndSources.Create(expectedDiagnostic, codeWithErrorsIndicated), fixedCode, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), metadataReference, fixTitle, allowCompilationErrors)); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <paramref name="analyzerType"/>. /// </summary> /// <param name="analyzerType">The type of the analyzer.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void Valid(Type analyzerType, ExpectedDiagnostic expectedDiagnostic, params string[] code) { var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType); AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that <paramref name="code"/> produces the expected diagnostics. /// </summary> /// <param name="analyzerType">The type of the analyzer.</param> /// <param name="expectedDiagnostics">The expected diagnostics</param> /// <param name="code">The code to analyze.</param> public static void Diagnostics(Type analyzerType, IReadOnlyList <ExpectedDiagnostic> expectedDiagnostics, params string[] code) { var analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType); AssertAnalyzerSupportsExpectedDiagnostics(analyzer, expectedDiagnostics, out var descriptors, out var suppressedDiagnostics); DiagnosticsWithMetadataAsync( analyzer, new DiagnosticsAndSources(expectedDiagnostics, code), CodeFactory.DefaultCompilationOptions(descriptors, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences, null) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that <paramref name="code"/> produces no diagnostics when analyzed with <typeparamref name="TAnalyzer"/>. /// </summary> /// <typeparam name="TAnalyzer">The type of the analyzer.</typeparam> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void Valid <TAnalyzer>(ExpectedDiagnostic expectedDiagnostic, params string[] code) where TAnalyzer : DiagnosticAnalyzer, new() { var analyzer = new TAnalyzer(); AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); ValidAsync( analyzer, code, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="metadataReferences">The meta data references to use when compiling the code.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void FixAll(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> codeWithErrorsIndicated, IReadOnlyList <string> fixedCode, IReadOnlyList <MetadataReference> metadataReferences = null, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); FixAllAsync( analyzer, codeFix, DiagnosticsAndSources.Create(expectedDiagnostic, codeWithErrorsIndicated), fixedCode, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), metadataReferences ?? MetadataReferences, fixTitle, allowCompilationErrors) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix does not change the code. /// </summary> /// <typeparam name="TCodeFix">The type of the code fix.</typeparam> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> public static void NoFix <TCodeFix>(ExpectedDiagnostic expectedDiagnostic, params string[] code) where TCodeFix : CodeFixProvider, new() { var analyzer = new PlaceholderAnalyzer(expectedDiagnostic.Id); AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); NoFixAsync( analyzer, new TCodeFix(), new DiagnosticsAndSources(new[] { expectedDiagnostic }, code), CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <param name="analyzer">The analyzer to run on the code..</param> /// <param name="codeFix">The code fix to apply.</param> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void CodeFix(DiagnosticAnalyzer analyzer, CodeFixProvider codeFix, ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> code, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) { AssertCodeFixCanFixDiagnosticsFromAnalyzer(analyzer, codeFix); AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); CodeFixAsync( analyzer, codeFix, DiagnosticsAndSources.Create(expectedDiagnostic, code), fixedCode, fixTitle, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences, allowCompilationErrors) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="code"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <typeparam name="TCodeFix">The type of the code fix.</typeparam> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="code">The code to analyze.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void CodeFix <TCodeFix>(ExpectedDiagnostic expectedDiagnostic, IReadOnlyList <string> code, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) where TCodeFix : CodeFixProvider, new() { var analyzer = new PlaceholderAnalyzer(expectedDiagnostic.Id); AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); CodeFixAsync( analyzer, new TCodeFix(), DiagnosticsAndSources.Create(expectedDiagnostic, code), fixedCode, fixTitle, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences, allowCompilationErrors) .GetAwaiter() .GetResult(); }
/// <summary> /// Verifies that /// 1. <paramref name="codeWithErrorsIndicated"/> produces the expected diagnostics /// 2. The code fix fixes the code. /// </summary> /// <typeparam name="TAnalyzer">The type of the analyzer.</typeparam> /// <typeparam name="TCodeFix">The type of the code fix.</typeparam> /// <param name="expectedDiagnostic">The expected diagnostic.</param> /// <param name="codeWithErrorsIndicated">The code with error positions indicated.</param> /// <param name="fixedCode">The expected code produced by the code fix.</param> /// <param name="fixTitle">The title of the fix to apply if more than one.</param> /// <param name="allowCompilationErrors">If compilation errors are accepted in the fixed code.</param> public static void FixAll <TAnalyzer, TCodeFix>(ExpectedDiagnostic expectedDiagnostic, string codeWithErrorsIndicated, string fixedCode, string fixTitle = null, AllowCompilationErrors allowCompilationErrors = AllowCompilationErrors.No) where TAnalyzer : DiagnosticAnalyzer, new() where TCodeFix : CodeFixProvider, new() { var analyzer = new TAnalyzer(); AssertAnalyzerSupportsExpectedDiagnostic(analyzer, expectedDiagnostic, out var descriptor, out var suppressedDiagnostics); FixAllAsync( analyzer, new TCodeFix(), DiagnosticsAndSources.Create(expectedDiagnostic, new[] { codeWithErrorsIndicated }), new[] { fixedCode }, CodeFactory.DefaultCompilationOptions(descriptor, SuppressedDiagnostics.Concat(suppressedDiagnostics)), MetadataReferences, fixTitle, allowCompilationErrors) .GetAwaiter() .GetResult(); }