提交 c33cc40e 编写于 作者: C CyrusNajmabadi

Fix up codegen tests.

上级 612dd308
......@@ -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<OptionKey, object> {
{ CSharpCodeStyleOptions.PreferExpressionBodiedAccessors, CodeStyleOptions.falseWithNoneEnforcement },
{ CSharpCodeStyleOptions.PreferExpressionBodiedIndexers, CodeStyleOptions.falseWithNoneEnforcement },
});
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeGeneration)]
public async Task AddParameterfulProperty()
{
......
......@@ -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<Func<SemanticModel, IParameterSymbol>> parameters = null,
bool isIndexer = false,
CodeGenerationOptions codeGenerationOptions = default(CodeGenerationOptions),
bool compareTokens = true)
bool compareTokens = true,
IDictionary<OptionKey, object> 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<IParameterSymbol>(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<ICodeGenerationService>();
......@@ -824,23 +834,23 @@ public static async Task<TestContext> 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<INamespaceOrTypeSymbol>(destSpan, this.SemanticModel);
}
public IEnumerable<ISymbol> 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<ISymbol>(span, semanticModel);
......@@ -861,7 +871,7 @@ public T GetSelectedSymbol<T>(TextSpan selection, SemanticModel semanticModel)
public T GetSelectedSyntax<T>(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<T>().FirstOrDefault(t => !fullSpanCoverage || t.Span.End >= destSpan.End);
}
......@@ -913,7 +923,7 @@ public void Dispose()
}
finally
{
_workspace.Dispose();
Workspace.Dispose();
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册