private void VerifyRange(string code, IEnumerable<ICodeCleanupProvider> codeCleanups, IEnumerable<TextSpan> spans, ref IEnumerable<TextSpan> expectedResult, string language) { var result = default(IEnumerable<TextSpan>); var spanCodeCleanup = new SimpleCodeCleanupProvider("TestCodeCleanup", (d, s, c) => { result = s; return Task.FromResult(d); }); var document = CreateDocument(code, language); CodeCleaner.CleanupAsync(document, spans, codeCleanups.Concat(spanCodeCleanup)).Wait(); var sortedSpans = result.ToList(); var expectedSpans = expectedResult.ToList(); sortedSpans.Sort(); expectedSpans.Sort(); AssertEx.Equal(expectedSpans, sortedSpans); }
public void RangeWithTransformation_RemoveMember() { var expectedResult = default(IEnumerable<TextSpan>); var transformer = new SimpleCodeCleanupProvider("TransformerCleanup", async (doc, spans, cancellationToken) => { var root = await doc.GetSyntaxRootAsync().ConfigureAwait(false); var @class = root.GetMember(0).GetMember(0); var classWithMember = @class.RemoveCSharpMember(0); root = root.ReplaceNode(@class, classWithMember); expectedResult = SpecializedCollections.SingletonEnumerable(root.GetMember(0).GetMember(0).GetCodeCleanupSpan()); return doc.WithSyntaxRoot(root); }); VerifyRange("namespace N { {|b:class C { void Method() { } }|} }", transformer, ref expectedResult); }
public void RangeWithTransformation_OutsideOfRange() { var expectedResult = default(IEnumerable<TextSpan>); var transformer = new SimpleCodeCleanupProvider("TransformerCleanup", async (doc, spans, cancellationToken) => { var root = await doc.GetSyntaxRootAsync().ConfigureAwait(false); var member = root.GetMember(0).GetMember(0).GetMember(0); var previousToken = member.GetFirstToken().GetPreviousToken().GetPreviousToken(); var nextToken = member.GetLastToken().GetNextToken().GetNextToken(); root = root.ReplaceToken(previousToken, CSharp.SyntaxFactory.Identifier(previousToken.LeadingTrivia, previousToken.ValueText, previousToken.TrailingTrivia)); root = root.ReplaceToken(nextToken, CSharp.SyntaxFactory.Token(nextToken.LeadingTrivia, nextToken.CSharpKind(), nextToken.TrailingTrivia)); expectedResult = SpecializedCollections.EmptyEnumerable<TextSpan>(); return doc.WithSyntaxRoot(root); }); VerifyRange("namespace N { class C { {|b:void Method() { }|} } }", transformer, ref expectedResult); }
public void EntireRangeWithTransformation_RemoveClass() { var expectedResult = default(IEnumerable<TextSpan>); var transformer = new SimpleCodeCleanupProvider("TransformerCleanup", async (doc, spans, cancellationToken) => { var root = await doc.GetSyntaxRootAsync().ConfigureAwait(false); root = root.RemoveCSharpMember(0); expectedResult = SpecializedCollections.SingletonEnumerable(root.FullSpan); return doc.WithSyntaxRoot(root); }); VerifyRange("{|b:class C {}|}", transformer, ref expectedResult); }