-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
153 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/EditorFeatures/DiagnosticsTestUtilities/CodeActions/SharedVerifierState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// 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 Microsoft.CodeAnalysis.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
|
||
#if !CODE_STYLE | ||
using Roslyn.Utilities; | ||
#endif | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions | ||
{ | ||
internal sealed class SharedVerifierState | ||
{ | ||
private readonly AnalyzerTest<XUnitVerifier> _test; | ||
private readonly string _defaultFileExt; | ||
|
||
/// <summary> | ||
/// The index in <see cref="Testing.ProjectState.AnalyzerConfigFiles"/> of the generated | ||
/// <strong>.editorconfig</strong> file for <see cref="Options"/>, or <see langword="null"/> if no such | ||
/// file has been generated yet. | ||
/// </summary> | ||
private int? _analyzerConfigIndex; | ||
|
||
/// <summary> | ||
/// The index in <see cref="AnalyzerTest{TVerifier}.SolutionTransforms"/> of the options transformation for | ||
/// remaining <see cref="Options"/>, or <see langword="null"/> if no such transfor has been generated yet. | ||
/// </summary> | ||
private Func<Solution, ProjectId, Solution>? _remainingOptionsSolutionTransform; | ||
|
||
public SharedVerifierState(AnalyzerTest<XUnitVerifier> test, string defaultFileExt) | ||
{ | ||
_test = test; | ||
_defaultFileExt = defaultFileExt; | ||
Options = new OptionsCollection(test.Language); | ||
} | ||
|
||
public string? EditorConfig { get; set; } | ||
|
||
/// <summary> | ||
/// Gets a collection of options to apply to <see cref="Solution.Options"/> for testing. Values may be added | ||
/// using a collection initializer. | ||
/// </summary> | ||
internal OptionsCollection Options { get; } | ||
|
||
internal void Apply() | ||
{ | ||
var (analyzerConfigSource, remainingOptions) = CodeFixVerifierHelper.ConvertOptionsToAnalyzerConfig(_defaultFileExt, EditorConfig, Options); | ||
if (analyzerConfigSource is not null) | ||
{ | ||
if (_analyzerConfigIndex is null) | ||
{ | ||
_analyzerConfigIndex = _test.TestState.AnalyzerConfigFiles.Count; | ||
_test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", analyzerConfigSource)); | ||
} | ||
else | ||
{ | ||
_test.TestState.AnalyzerConfigFiles[_analyzerConfigIndex.Value] = ("/.editorconfig", analyzerConfigSource); | ||
} | ||
} | ||
else if (_analyzerConfigIndex is { } index) | ||
{ | ||
_analyzerConfigIndex = null; | ||
_test.TestState.AnalyzerConfigFiles.RemoveAt(index); | ||
} | ||
|
||
var solutionTransformIndex = _remainingOptionsSolutionTransform is not null ? _test.SolutionTransforms.IndexOf(_remainingOptionsSolutionTransform) : -1; | ||
if (remainingOptions is not null) | ||
{ | ||
// Generate a new solution transform | ||
_remainingOptionsSolutionTransform = (solution, projectId) => | ||
{ | ||
#if !CODE_STYLE | ||
var options = solution.Options; | ||
foreach (var (key, value) in remainingOptions) | ||
{ | ||
options = options.WithChangedOption(key, value); | ||
} | ||
solution = solution.WithOptions(options); | ||
#endif | ||
return solution; | ||
}; | ||
|
||
if (solutionTransformIndex < 0) | ||
{ | ||
_test.SolutionTransforms.Add(_remainingOptionsSolutionTransform); | ||
} | ||
else | ||
{ | ||
_test.SolutionTransforms[solutionTransformIndex] = _remainingOptionsSolutionTransform; | ||
} | ||
} | ||
else if (_remainingOptionsSolutionTransform is not null) | ||
{ | ||
_test.SolutionTransforms.Remove(_remainingOptionsSolutionTransform); | ||
_remainingOptionsSolutionTransform = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.