Skip to content
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

Properly respect 'var' settings when generating equals/GetHashCode #39958

Merged
merged 8 commits into from
Nov 22, 2019
Merged
84 changes: 84 additions & 0 deletions src/EditorFeatures/CSharpTest/CodeActions/UseVarTestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeActions
{
internal static class UseVarTestExtensions
{
private static readonly CodeStyleOption<bool> onWithNone = new CodeStyleOption<bool>(true, NotificationOption.None);
private static readonly CodeStyleOption<bool> offWithNone = new CodeStyleOption<bool>(false, NotificationOption.None);
private static readonly CodeStyleOption<bool> onWithSilent = new CodeStyleOption<bool>(true, NotificationOption.Silent);
private static readonly CodeStyleOption<bool> offWithSilent = new CodeStyleOption<bool>(false, NotificationOption.Silent);
private static readonly CodeStyleOption<bool> onWithInfo = new CodeStyleOption<bool>(true, NotificationOption.Suggestion);
private static readonly CodeStyleOption<bool> offWithInfo = new CodeStyleOption<bool>(false, NotificationOption.Suggestion);
private static readonly CodeStyleOption<bool> onWithWarning = new CodeStyleOption<bool>(true, NotificationOption.Warning);
private static readonly CodeStyleOption<bool> offWithWarning = new CodeStyleOption<bool>(false, NotificationOption.Warning);
private static readonly CodeStyleOption<bool> offWithError = new CodeStyleOption<bool>(false, NotificationOption.Error);
private static readonly CodeStyleOption<bool> onWithError = new CodeStyleOption<bool>(true, NotificationOption.Error);

public static IDictionary<OptionKey, object> PreferExplicitTypeWithError(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithError),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithError),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithError));

public static IDictionary<OptionKey, object> PreferImplicitTypeWithError(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithError),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithError),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithError));

public static IDictionary<OptionKey, object> PreferExplicitTypeWithWarning(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithWarning),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithWarning),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithWarning));

public static IDictionary<OptionKey, object> PreferImplicitTypeWithWarning(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithWarning),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithWarning),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithWarning));

public static IDictionary<OptionKey, object> PreferExplicitTypeWithInfo(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithInfo),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithInfo),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithInfo));

public static IDictionary<OptionKey, object> PreferImplicitTypeWithInfo(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithInfo),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithInfo),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithInfo));

public static IDictionary<OptionKey, object> PreferExplicitTypeWithSilent(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithSilent),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithSilent),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithSilent));

public static IDictionary<OptionKey, object> PreferImplicitTypeWithSilent(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithSilent),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithSilent),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithSilent));

public static IDictionary<OptionKey, object> PreferExplicitTypeWithNone(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithNone),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithNone),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithNone));

public static IDictionary<OptionKey, object> PreferImplicitTypeWithNone(this AbstractCodeActionOrUserDiagnosticTest test)
=> AbstractCodeActionOrUserDiagnosticTest.OptionsSet(
test.SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithNone),
test.SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithNone),
test.SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithNone));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp.CodeRefactorings.UseExplicitType;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings.UseExplicitOrImplicitType
{
[Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
public class UseExplicitTypeRefactoringTests : AbstractUseTypeRefactoringTests
public class UseExplicitTypeRefactoringTests : AbstractCSharpCodeActionTest
{
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace, TestParameters parameters)
=> new UseExplicitTypeCodeRefactoringProvider();
Expand Down Expand Up @@ -424,36 +425,36 @@ static void Main()
private async Task TestInRegularAndScriptWhenDiagnosticNotAppliedAsync(string initialMarkup, string expectedMarkup)
{
// Enabled because the diagnostic is disabled
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: PreferExplicitTypeWithNone());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: this.PreferExplicitTypeWithNone());

// Enabled because the diagnostic is checking for the other direction
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: PreferImplicitTypeWithNone());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: PreferImplicitTypeWithSilent());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: PreferImplicitTypeWithInfo());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: this.PreferImplicitTypeWithNone());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: this.PreferImplicitTypeWithSilent());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: this.PreferImplicitTypeWithInfo());

// Disabled because the diagnostic will report it instead
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithSilent()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithInfo()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithWarning()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithError()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithSilent()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithInfo()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithWarning()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithError()));

// Currently this refactoring is still enabled in cases where it would cause a warning or error
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: PreferImplicitTypeWithWarning());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: PreferImplicitTypeWithError());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: this.PreferImplicitTypeWithWarning());
await TestInRegularAndScriptAsync(initialMarkup, expectedMarkup, options: this.PreferImplicitTypeWithError());
}

private async Task TestMissingInRegularAndScriptAsync(string initialMarkup)
{
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferImplicitTypeWithNone()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithNone()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferImplicitTypeWithSilent()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithSilent()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferImplicitTypeWithInfo()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithInfo()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferImplicitTypeWithWarning()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithWarning()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferImplicitTypeWithError()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: PreferExplicitTypeWithError()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferImplicitTypeWithNone()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithNone()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferImplicitTypeWithSilent()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithSilent()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferImplicitTypeWithInfo()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithInfo()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferImplicitTypeWithWarning()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithWarning()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferImplicitTypeWithError()));
await TestMissingInRegularAndScriptAsync(initialMarkup, parameters: new TestParameters(options: this.PreferExplicitTypeWithError()));
}
}
}
Loading