diff --git a/eng/Versions.props b/eng/Versions.props index 12cffd628b7f51aefd132b614c43d927e88ef7b2..106d12fb72c13cc6a636b42d7f2d441287e9d02e 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -28,7 +28,7 @@ 3.0.0-beta2.20059.3+77df2220 3.3.1 - 1.0.1-beta1.20067.1 + 1.0.1-beta1.20114.4 3.5.0-beta3-20078-04 16.4.248 5.0.0-alpha1.19409.1 @@ -97,7 +97,7 @@ 15.8.27812-alpha 1.1.20180503.2 16.0.198-g52de9c2988 - 15.5.23 + 15.6.36 $(VisualStudioEditorPackagesVersion) 16.0.29431.108 16.4.1091102-preview diff --git a/src/EditorFeatures/TestUtilities/CodeActions/CSharpCodeFixVerifier`2+Test.cs b/src/EditorFeatures/TestUtilities/CodeActions/CSharpCodeFixVerifier`2+Test.cs new file mode 100644 index 0000000000000000000000000000000000000000..986765afc1974a7599dd29b8613275ca01ec85f0 --- /dev/null +++ b/src/EditorFeatures/TestUtilities/CodeActions/CSharpCodeFixVerifier`2+Test.cs @@ -0,0 +1,73 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Immutable; +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Testing; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing.Verifiers; +using Roslyn.Utilities; + +namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions +{ + public static partial class CSharpCodeFixVerifier + where TAnalyzer : DiagnosticAnalyzer, new() + where TCodeFix : CodeFixProvider, new() + { + public class Test : CSharpCodeFixTest + { + private static readonly ImmutableDictionary NullableWarnings = GetNullableWarningsFromCompiler(); + + public Test() + { + MarkupOptions = Testing.MarkupOptions.UseFirstDescriptor; + + SolutionTransforms.Add((solution, projectId) => + { + var parseOptions = (CSharpParseOptions)solution.GetProject(projectId).ParseOptions; + solution = solution.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(LanguageVersion)); + + var compilationOptions = solution.GetProject(projectId).CompilationOptions; + compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(compilationOptions.SpecificDiagnosticOptions.SetItems(NullableWarnings)); + solution = solution.WithProjectCompilationOptions(projectId, compilationOptions); + + var options = solution.Options; + foreach (var (key, value) in Options) + { + options = options.WithChangedOption(key, value); + } + + solution = solution.WithOptions(options); + + return solution; + }); + } + + private static ImmutableDictionary 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; + } + + public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.CSharp8; + + public OptionsCollection Options { get; } = new OptionsCollection(LanguageNames.CSharp); + + protected override AnalyzerOptions GetAnalyzerOptions(Project project) + { + return new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project.Solution); + } + } + } +} diff --git a/src/EditorFeatures/TestUtilities/CodeActions/CSharpCodeFixVerifier`2.cs b/src/EditorFeatures/TestUtilities/CodeActions/CSharpCodeFixVerifier`2.cs new file mode 100644 index 0000000000000000000000000000000000000000..8956322ddc4132405e90627b7d465f79199bc8ee --- /dev/null +++ b/src/EditorFeatures/TestUtilities/CodeActions/CSharpCodeFixVerifier`2.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.CSharp.Testing; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.CodeAnalysis.Testing.Verifiers; + +namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions +{ + public static partial class CSharpCodeFixVerifier + where TAnalyzer : DiagnosticAnalyzer, new() + where TCodeFix : CodeFixProvider, new() + { + public static DiagnosticResult Diagnostic() + => CSharpCodeFixVerifier.Diagnostic(); + + public static DiagnosticResult Diagnostic(string diagnosticId) + => CSharpCodeFixVerifier.Diagnostic(diagnosticId); + + public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) + => CSharpCodeFixVerifier.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(); + } + + 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(); + } + } +} diff --git a/src/EditorFeatures/TestUtilities/CodeActions/OptionsCollection.cs b/src/EditorFeatures/TestUtilities/CodeActions/OptionsCollection.cs new file mode 100644 index 0000000000000000000000000000000000000000..99ac6de7263a299c9008071d056e1a95a7433669 --- /dev/null +++ b/src/EditorFeatures/TestUtilities/CodeActions/OptionsCollection.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections; +using System.Collections.Generic; +using Microsoft.CodeAnalysis.CodeStyle; +using Microsoft.CodeAnalysis.Options; + +namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions +{ + public sealed class OptionsCollection : IEnumerable> + { + private readonly Dictionary _options = new Dictionary(); + private readonly string _languageName; + + public OptionsCollection(string languageName) + { + _languageName = languageName; + } + + public void Add(Option option, T value) + => _options.Add(new OptionKey(option), value); + + public void Add(Option> option, T value, NotificationOption notification) + => _options.Add(new OptionKey(option), new CodeStyleOption(value, notification)); + + public void Add(PerLanguageOption option, T value) + => _options.Add(new OptionKey(option, _languageName), value); + + public void Add(PerLanguageOption> option, T value) + => Add(option, value, option.DefaultValue.Notification); + + public void Add(PerLanguageOption> option, T value, NotificationOption notification) + => _options.Add(new OptionKey(option, _languageName), new CodeStyleOption(value, notification)); + + public IEnumerator> GetEnumerator() + => _options.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() + => GetEnumerator(); + } +} diff --git a/src/EditorFeatures/TestUtilities/CodeActions/VisualBasicCodeFixVerifier`2+Test.cs b/src/EditorFeatures/TestUtilities/CodeActions/VisualBasicCodeFixVerifier`2+Test.cs new file mode 100644 index 0000000000000000000000000000000000000000..017f1780800a73e1f0fdd7a283a7b3f7d452ef42 --- /dev/null +++ b/src/EditorFeatures/TestUtilities/CodeActions/VisualBasicCodeFixVerifier`2+Test.cs @@ -0,0 +1,51 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing.Verifiers; +using Microsoft.CodeAnalysis.VisualBasic; +using Microsoft.CodeAnalysis.VisualBasic.Testing; +using Roslyn.Utilities; + +namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions +{ + public static partial class VisualBasicCodeFixVerifier + where TAnalyzer : DiagnosticAnalyzer, new() + where TCodeFix : CodeFixProvider, new() + { + public class Test : VisualBasicCodeFixTest + { + public Test() + { + MarkupOptions = Testing.MarkupOptions.UseFirstDescriptor; + + SolutionTransforms.Add((solution, projectId) => + { + var parseOptions = (VisualBasicParseOptions)solution.GetProject(projectId).ParseOptions; + solution = solution.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(LanguageVersion)); + + var options = solution.Options; + foreach (var (key, value) in Options) + { + options = options.WithChangedOption(key, value); + } + + solution = solution.WithOptions(options); + + return solution; + }); + } + + public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.VisualBasic16; + + public OptionsCollection Options { get; } = new OptionsCollection(LanguageNames.CSharp); + + protected override AnalyzerOptions GetAnalyzerOptions(Project project) + { + return new WorkspaceAnalyzerOptions(base.GetAnalyzerOptions(project), project.Solution); + } + } + } +} diff --git a/src/EditorFeatures/TestUtilities/CodeActions/VisualBasicCodeFixVerifier`2.cs b/src/EditorFeatures/TestUtilities/CodeActions/VisualBasicCodeFixVerifier`2.cs new file mode 100644 index 0000000000000000000000000000000000000000..d1941b9ec9a5c0e765ebedc16c133df9b92e2392 --- /dev/null +++ b/src/EditorFeatures/TestUtilities/CodeActions/VisualBasicCodeFixVerifier`2.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CodeFixes; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.CodeAnalysis.Testing.Verifiers; +using Microsoft.CodeAnalysis.VisualBasic.Testing; + +namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions +{ + public static partial class VisualBasicCodeFixVerifier + where TAnalyzer : DiagnosticAnalyzer, new() + where TCodeFix : CodeFixProvider, new() + { + public static DiagnosticResult Diagnostic() + => VisualBasicCodeFixVerifier.Diagnostic(); + + public static DiagnosticResult Diagnostic(string diagnosticId) + => VisualBasicCodeFixVerifier.Diagnostic(diagnosticId); + + public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) + => VisualBasicCodeFixVerifier.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(); + } + + 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(); + } + } +} diff --git a/src/EditorFeatures/TestUtilities/Roslyn.Services.Test.Utilities.csproj b/src/EditorFeatures/TestUtilities/Roslyn.Services.Test.Utilities.csproj index 9b15252cf1be4669e182f40c58f3e7711cb7766f..1e0ea1173fd2a140cb87d71671691a9d2956edef 100644 --- a/src/EditorFeatures/TestUtilities/Roslyn.Services.Test.Utilities.csproj +++ b/src/EditorFeatures/TestUtilities/Roslyn.Services.Test.Utilities.csproj @@ -57,6 +57,8 @@ + +