diff --git a/src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.CSharp.cs b/src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.CSharp.cs index afe742136c6ad14337350be26e5351527144239b..bd98dc20424ebfc70cacdafca461bbe0c487d6fc 100644 --- a/src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.CSharp.cs +++ b/src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.CSharp.cs @@ -8,14 +8,17 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeGeneration; using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.CodeStyle; using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces; +using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Text; using Roslyn.Test.Utilities; using Roslyn.Utilities; using Xunit; +using Microsoft.CodeAnalysis.CodeStyle; using CS = Microsoft.CodeAnalysis.CSharp; using VB = Microsoft.CodeAnalysis.VisualBasic; @@ -639,10 +642,10 @@ public async Task AddUnsafeAutoProperty() } [Fact, Trait(Traits.Feature, Traits.Features.CodeGeneration)] - public async Task AddIndexer() + public async Task AddIndexer1() { var input = "class [|C|] { }"; - var expected = "class C { public string this[int i] { get { $$ } } }"; + var expected = "class C { public string this[int i] => String.Empty; }"; await TestAddPropertyAsync(input, expected, type: typeof(string), parameters: Parameters(Parameter(typeof(int), "i")), @@ -650,6 +653,22 @@ public async Task AddIndexer() isIndexer: true); } + [Fact, Trait(Traits.Feature, Traits.Features.CodeGeneration)] + public async Task AddIndexer2() + { + var input = "class [|C|] { }"; + var expected = "class C { public string this[int i] { get { $$ } } }"; + await TestAddPropertyAsync(input, expected, + type: typeof(string), + parameters: Parameters(Parameter(typeof(int), "i")), + getStatements: "return String.Empty;", + isIndexer: true, + options: new Dictionary { + { CSharpCodeStyleOptions.PreferExpressionBodiedAccessors, CodeStyleOptions.falseWithNoneEnforcement }, + { CSharpCodeStyleOptions.PreferExpressionBodiedIndexers, CodeStyleOptions.falseWithNoneEnforcement }, + }); + } + [Fact, Trait(Traits.Feature, Traits.Features.CodeGeneration)] public async Task AddParameterfulProperty() { diff --git a/src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs b/src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs index 6de66b2e4fc0753b7b82814faab3b7a4b9be92bb..c68418c6ab9b6f704de8718126c12d6f656c7f24 100644 --- a/src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs +++ b/src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs @@ -13,6 +13,7 @@ using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces; using Microsoft.CodeAnalysis.Formatting; +using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Simplification; using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.VisualBasic; @@ -373,7 +374,8 @@ public partial class CodeGenerationTests IList> parameters = null, bool isIndexer = false, CodeGenerationOptions codeGenerationOptions = default(CodeGenerationOptions), - bool compareTokens = true) + bool compareTokens = true, + IDictionary options = null) { // This assumes that tests will not use place holders for get/set statements at the same time if (getStatements != null) @@ -388,6 +390,14 @@ public partial class CodeGenerationTests using (var context = await TestContext.CreateAsync(initial, expected, compareTokens)) { + if (options != null) + { + foreach (var kvp in options) + { + context.Workspace.Options = context.Workspace.Options.WithChangedOption(kvp.Key, kvp.Value); + } + } + var typeSymbol = GetTypeSymbol(type)(context.SemanticModel); var getParameterSymbols = GetParameterSymbols(parameters, context); var setParameterSymbols = getParameterSymbols == null ? null : new List(getParameterSymbols) { Parameter(type, "value")(context.SemanticModel) }; @@ -788,7 +798,7 @@ internal class TestContext : IDisposable public Document Result; - private readonly TestWorkspace _workspace; + public readonly TestWorkspace Workspace; private readonly string _language; private readonly bool _compareTokens; private readonly bool _ignoreResult; @@ -807,8 +817,8 @@ internal class TestContext : IDisposable this.IsVisualBasic = _language == LanguageNames.VisualBasic; _compareTokens = compareTokens; _ignoreResult = ignoreResult; - _workspace = workspace; - this.Document = _workspace.CurrentSolution.Projects.Single().Documents.Single(); + Workspace = workspace; + this.Document = Workspace.CurrentSolution.Projects.Single().Documents.Single(); this.SemanticModel = semanticModel; this.SyntaxTree = SemanticModel.SyntaxTree; this.Service = Document.Project.LanguageServices.GetService(); @@ -824,23 +834,23 @@ public static async Task CreateAsync(string initial, string expecte return new TestContext(initial, expected, compareTokens, ignoreResult, language, workspace, semanticModel); } - public Solution Solution { get { return _workspace.CurrentSolution; } } + public Solution Solution { get { return Workspace.CurrentSolution; } } public SyntaxNode GetDestinationNode() { - var destSpan = _workspace.Documents.Single().SelectedSpans.Single(); + var destSpan = Workspace.Documents.Single().SelectedSpans.Single(); return SemanticModel.SyntaxTree.GetRoot().FindNode(destSpan, getInnermostNodeForTie: true); } public INamespaceOrTypeSymbol GetDestination() { - var destSpan = _workspace.Documents.Single().SelectedSpans.Single(); + var destSpan = Workspace.Documents.Single().SelectedSpans.Single(); return GetSelectedSymbol(destSpan, this.SemanticModel); } public IEnumerable GetAnnotatedDeclaredSymbols(string key, SemanticModel semanticModel) { - var annotatedSpans = _workspace.Documents.Single().AnnotatedSpans[key]; + var annotatedSpans = Workspace.Documents.Single().AnnotatedSpans[key]; foreach (var span in annotatedSpans) { yield return GetSelectedSymbol(span, semanticModel); @@ -861,7 +871,7 @@ public T GetSelectedSymbol(TextSpan selection, SemanticModel semanticModel) public T GetSelectedSyntax(bool fullSpanCoverage = false) where T : SyntaxNode { - var destSpan = _workspace.Documents.Single().SelectedSpans.Single(); + var destSpan = Workspace.Documents.Single().SelectedSpans.Single(); var token = SemanticModel.SyntaxTree.GetRoot().FindToken(destSpan.Start); return token.Parent.AncestorsAndSelf().OfType().FirstOrDefault(t => !fullSpanCoverage || t.Span.End >= destSpan.End); } @@ -913,7 +923,7 @@ public void Dispose() } finally { - _workspace.Dispose(); + Workspace.Dispose(); } }