Skip to content

Commit

Permalink
Cleanup the conditional directives in analyzer/fixer tests in shared …
Browse files Browse the repository at this point in the history
…layer. Should be much cleaner now.
  • Loading branch information
mavasani committed Mar 10, 2020
1 parent cbead33 commit e4c7e8e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand All @@ -14,14 +16,6 @@
Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression.ConvertSwitchStatementToExpressionDiagnosticAnalyzer,
Microsoft.CodeAnalysis.CSharp.ConvertSwitchStatementToExpression.ConvertSwitchStatementToExpressionCodeFixProvider>;

#if CODE_STYLE
using Microsoft.CodeAnalysis.CSharp.Internal.CodeStyle;
using Microsoft.CodeAnalysis.Internal.Options;
#else
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
#endif

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ConvertSwitchStatementToExpression
{
public class ConvertSwitchStatementToExpressionTests
Expand Down Expand Up @@ -729,7 +723,7 @@ int M(int i)
},
Options =
{
{ CSharpCodeStyleOptions.PreferSwitchExpression, true, NotificationOption.Warning },
{ CSharpCodeStyleOptions.PreferSwitchExpression, true, NotificationOption2.Warning },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1070,7 +1064,7 @@ public static void Test(string name)
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1138,7 +1132,7 @@ public static void Test(string name)
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1206,7 +1200,7 @@ public static void Test(string name)
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1282,7 +1276,7 @@ public static void Test(string name)
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1350,7 +1344,7 @@ public static void Test(string name)
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1418,7 +1412,7 @@ public static void Test(string name)
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1486,7 +1480,7 @@ public static void Test(string name)
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarElsewhere, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1533,7 +1527,7 @@ public static void Test()
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1578,7 +1572,7 @@ public static void Test()
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1624,7 +1618,7 @@ public static void Test()
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1670,7 +1664,7 @@ public static void Test()
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarForBuiltInTypes, false, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, false, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down Expand Up @@ -1716,7 +1710,7 @@ public static void Test()
FixedCode = expected,
Options =
{
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true, NotificationOption.Silent },
{ CSharpCodeStyleOptions.VarForBuiltInTypes, true, NotificationOption2.Silent },
},
}.RunAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,60 @@
// 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.Generic;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle;
using Microsoft.CodeAnalysis.CSharp.TypeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

#if CODE_STYLE
using Microsoft.CodeAnalysis.Internal.Options;
using Microsoft.CodeAnalysis.CSharp.Internal.CodeStyle;
#else
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Options;
#endif

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.UseExplicitType
{
public partial class UseExplicitTypeTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
{
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new CSharpUseExplicitTypeDiagnosticAnalyzer(), new UseExplicitTypeCodeFixProvider());

private readonly CodeStyleOption<bool> onWithSilent = new CodeStyleOption<bool>(true, NotificationOption.Silent);
private readonly CodeStyleOption<bool> offWithSilent = new CodeStyleOption<bool>(false, NotificationOption.Silent);
private readonly CodeStyleOption<bool> onWithInfo = new CodeStyleOption<bool>(true, NotificationOption.Suggestion);
private readonly CodeStyleOption<bool> offWithInfo = new CodeStyleOption<bool>(false, NotificationOption.Suggestion);
private readonly CodeStyleOption<bool> onWithWarning = new CodeStyleOption<bool>(true, NotificationOption.Warning);
private readonly CodeStyleOption<bool> offWithWarning = new CodeStyleOption<bool>(false, NotificationOption.Warning);
private readonly CodeStyleOption<bool> onWithError = new CodeStyleOption<bool>(true, NotificationOption.Error);
private readonly CodeStyleOption<bool> offWithError = new CodeStyleOption<bool>(false, NotificationOption.Error);
private readonly CodeStyleOption2<bool> onWithSilent = new CodeStyleOption2<bool>(true, NotificationOption2.Silent);
private readonly CodeStyleOption2<bool> offWithSilent = new CodeStyleOption2<bool>(false, NotificationOption2.Silent);
private readonly CodeStyleOption2<bool> onWithInfo = new CodeStyleOption2<bool>(true, NotificationOption2.Suggestion);
private readonly CodeStyleOption2<bool> offWithInfo = new CodeStyleOption2<bool>(false, NotificationOption2.Suggestion);
private readonly CodeStyleOption2<bool> onWithWarning = new CodeStyleOption2<bool>(true, NotificationOption2.Warning);
private readonly CodeStyleOption2<bool> offWithWarning = new CodeStyleOption2<bool>(false, NotificationOption2.Warning);
private readonly CodeStyleOption2<bool> onWithError = new CodeStyleOption2<bool>(true, NotificationOption2.Error);
private readonly CodeStyleOption2<bool> offWithError = new CodeStyleOption2<bool>(false, NotificationOption2.Error);

// specify all options explicitly to override defaults.
private IDictionary<OptionKey, object> ExplicitTypeEverywhere() => OptionsSet(
private IOptionsCollection ExplicitTypeEverywhere() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithInfo),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithInfo),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithInfo));

private IDictionary<OptionKey, object> ExplicitTypeExceptWhereApparent() => OptionsSet(
private IOptionsCollection ExplicitTypeExceptWhereApparent() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithInfo),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithInfo),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithInfo));

private IDictionary<OptionKey, object> ExplicitTypeForBuiltInTypesOnly() => OptionsSet(
private IOptionsCollection ExplicitTypeForBuiltInTypesOnly() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithInfo),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithInfo),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithInfo));

private IDictionary<OptionKey, object> ExplicitTypeEnforcements() => OptionsSet(
private IOptionsCollection ExplicitTypeEnforcements() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithWarning),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithError),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithInfo));

private IDictionary<OptionKey, object> ExplicitTypeSilentEnforcement() => OptionsSet(
private IOptionsCollection ExplicitTypeSilentEnforcement() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithSilent),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, offWithSilent),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithSilent));

private IDictionary<OptionKey, object> Options(OptionKey option, object value)
{
var options = new Dictionary<OptionKey, object>();
options.Add(option, value);
return options;
}

#region Error Cases

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.TypeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

#if CODE_STYLE
using Microsoft.CodeAnalysis.CSharp.Internal.CodeStyle;
using Microsoft.CodeAnalysis.Internal.Options;
#else
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Options;
#endif
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.UseImplicitType
{
Expand All @@ -29,49 +24,46 @@ public partial class UseImplicitTypeTests : AbstractCSharpDiagnosticProviderBase
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new CSharpUseImplicitTypeDiagnosticAnalyzer(), new UseImplicitTypeCodeFixProvider());

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> onWithError = new CodeStyleOption<bool>(true, NotificationOption.Error);
private static readonly CodeStyleOption<bool> offWithError = new CodeStyleOption<bool>(false, NotificationOption.Error);
private static readonly CodeStyleOption2<bool> onWithSilent = new CodeStyleOption2<bool>(true, NotificationOption2.Silent);
private static readonly CodeStyleOption2<bool> offWithSilent = new CodeStyleOption2<bool>(false, NotificationOption2.Silent);
private static readonly CodeStyleOption2<bool> onWithInfo = new CodeStyleOption2<bool>(true, NotificationOption2.Suggestion);
private static readonly CodeStyleOption2<bool> offWithInfo = new CodeStyleOption2<bool>(false, NotificationOption2.Suggestion);
private static readonly CodeStyleOption2<bool> onWithWarning = new CodeStyleOption2<bool>(true, NotificationOption2.Warning);
private static readonly CodeStyleOption2<bool> offWithWarning = new CodeStyleOption2<bool>(false, NotificationOption2.Warning);
private static readonly CodeStyleOption2<bool> onWithError = new CodeStyleOption2<bool>(true, NotificationOption2.Error);
private static readonly CodeStyleOption2<bool> offWithError = new CodeStyleOption2<bool>(false, NotificationOption2.Error);

// specify all options explicitly to override defaults.
public IDictionary<OptionKey, object> ImplicitTypeEverywhere() => OptionsSet(
internal IOptionsCollection ImplicitTypeEverywhere() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithInfo),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithInfo),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithInfo));

private IDictionary<OptionKey, object> ImplicitTypeWhereApparent() => OptionsSet(
private IOptionsCollection ImplicitTypeWhereApparent() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithInfo),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithInfo),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithInfo));

private IDictionary<OptionKey, object> ImplicitTypeWhereApparentAndForIntrinsics() => OptionsSet(
private IOptionsCollection ImplicitTypeWhereApparentAndForIntrinsics() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, offWithInfo),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithInfo),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithInfo));

public IDictionary<OptionKey, object> ImplicitTypeButKeepIntrinsics() => OptionsSet(
internal IOptionsCollection ImplicitTypeButKeepIntrinsics() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithInfo),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, offWithInfo),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithInfo));

private IDictionary<OptionKey, object> ImplicitTypeEnforcements() => OptionsSet(
private IOptionsCollection ImplicitTypeEnforcements() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithWarning),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithError),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithInfo));

private IDictionary<OptionKey, object> ImplicitTypeSilentEnforcement() => OptionsSet(
private IOptionsCollection ImplicitTypeSilentEnforcement() => OptionsSet(
SingleOption(CSharpCodeStyleOptions.VarElsewhere, onWithSilent),
SingleOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithSilent),
SingleOption(CSharpCodeStyleOptions.VarForBuiltInTypes, onWithSilent));

private static IDictionary<OptionKey, object> Options(OptionKey option, object value)
=> new Dictionary<OptionKey, object> { { option, value } };

[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseImplicitType)]
public async Task NotOnFieldDeclaration()
{
Expand Down

0 comments on commit e4c7e8e

Please sign in to comment.