public static void InsertAfterFirst() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary>M</summary> public int M() => 1; } }"); var expected = GetExpected(@" namespace N { public class C { /// <summary>M</summary> /// <returns>One</returns> public int M() => 1; } }"); var method = syntaxTree.FindMethodDeclaration("M"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var element = Parse.XmlElementSyntax("<returns>One</returns>", " "); var updated = comment.InsertAfter(comment.Content.OfType <XmlElementSyntax>().First(), element); RoslynAssert.Ast(expected, updated); }
public static void InsertAfterRemarksSingleLine() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <remarks></remarks> public void M() { } } }"); var expected = GetExpected(@" namespace N { public class C { /// <remarks></remarks> /// <returns>New text.</returns> public void M() { } } }"); var method = syntaxTree.FindMethodDeclaration("M"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithReturnsText("New text."); RoslynAssert.Ast(expected, updated); updated = comment.WithReturns(Parse.XmlElementSyntax("<returns>New text.</returns>", " ")); RoslynAssert.Ast(expected, updated); }
public static void AddWhenPragma() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { using System; public class C { #pragma warning disable WPF0013 // CLR accessor for attached property must match registered type. public int M() => 1; } }"); var expected = CSharpSyntaxTree.ParseText(@" namespace N { using System; public class C { [Obsolete] #pragma warning disable WPF0013 // CLR accessor for attached property must match registered type. public int M() => 1; } }").FindMethodDeclaration("M"); var method = syntaxTree.FindMethodDeclaration("M"); var attribute = "[Obsolete]"; var updated = method.WithAttributeListText(attribute); RoslynAssert.Ast(expected, updated); }
public static void WithSummaryTextParamAndReturns() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary>Sum two numbers.</summary> public int Add(int x, int y) => x + y; } }"); var expected = GetExpected(@" namespace N { public class C { /// <summary>Sum two numbers.</summary> /// <param name=""x"">The first term.</param> /// <param name=""y"">The other term.</param> /// <returns>The sum.</returns> public int Add(int x, int y) => x + y; } }"); var method = syntaxTree.FindMethodDeclaration("Add"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithParamText("x", "The first term.") .WithParamText("y", "The other term.") .WithReturnsText("The sum."); RoslynAssert.Ast(expected, updated); }
public static void InsertAfterLast() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary>The id function.</summary> /// <param name=""x"">The x.</param> public int Id(int x) => x; } }"); var expected = GetExpected(@" namespace N { public class C { /// <summary>The id function.</summary> /// <param name=""x"">The x.</param> /// <returns>The value passed in</returns> public int Id(int x) => x; } }"); var method = syntaxTree.FindMethodDeclaration("Id"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); Assert.AreEqual(true, comment.Content.TrySingleOfType(x => x.HasLocalName("param"), out XmlElementSyntax param)); var element = Parse.XmlElementSyntax("<returns>The value passed in</returns>", " "); var updated = comment.InsertAfter(param, element); RoslynAssert.Ast(expected, updated); }
public static void InsertBeforeSecond() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary>Sum two numbers.</summary> /// <param name=""y"">The y.</param> /// <returns>The sum.</returns> public int Add(int x, int y) => x + y; } }"); var expected = GetExpected(@" namespace N { public class C { /// <summary>Sum two numbers.</summary> /// <param name=""x"">The x.</param> /// <param name=""y"">The y.</param> /// <returns>The sum.</returns> public int Add(int x, int y) => x + y; } }"); var method = syntaxTree.FindMethodDeclaration("Add"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var element = Parse.XmlElementSyntax("<param name=\"x\">The x.</param>", " "); var updated = comment.InsertBefore(comment.Content.OfType <XmlElementSyntax>().Skip(1).First(), element); RoslynAssert.Ast(expected, updated); }
public static void ReplaceSingleLineWithSingleLine() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary>The identity function.</summary> /// <typeparam name=""T"">The type</typeparam> /// <param name=""i"">The value to return.</param> /// <returns><paramref name=""i""/></returns> public T Id<T>(T i) => i; } }"); var expected = GetExpected(@" namespace N { public class C { /// <summary>New text.</summary> /// <typeparam name=""T"">The type</typeparam> /// <param name=""i"">The value to return.</param> /// <returns><paramref name=""i""/></returns> public T Id<T>(T i) => i; } }"); var method = syntaxTree.FindMethodDeclaration("Id"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithSummaryText("New text."); Assert.AreEqual(true, updated.TryGetSummary(out var summary)); Assert.AreEqual("<summary>New text.</summary>", summary.ToFullString()); RoslynAssert.Ast(expected, updated); }
public static void WhenSummaryOnlyMultiLine() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary> /// The identity function. /// </summary> public int Id(int i) => i; } }"); var expected = GetExpected(@" namespace N { public class C { /// <summary> /// The identity function. /// </summary> /// <param name=""i"">The <see cref=""int""/> to return.</param> public int Id(int i) => i; } }"); var method = syntaxTree.FindMethodDeclaration("Id"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithParamText("i", "The <see cref=\"int\"/> to return."); RoslynAssert.Ast(expected, updated); updated = comment.WithParam(Parse.XmlElementSyntax("<param name=\"i\">The <see cref=\"int\"/> to return.</param>", " ")); RoslynAssert.Ast(expected, updated); }
public static void InsertAfterRemarksMultiLine() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <remarks></remarks> public void M() { } } }"); var expected = GetExpected(@" namespace N { public class C { /// <remarks></remarks> /// <returns> /// Line 1. /// Line 2. /// </returns> public void M() { } } }"); var method = syntaxTree.FindMethodDeclaration("M"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithReturnsText("Line 1.\r\nLine 2."); RoslynAssert.Ast(expected, updated); }
public static void Add() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public int M() => 1; } }"); var expected = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary>New summary.</summary> /// <returns>New returns.</returns> public int M() => 1; } }").FindMethodDeclaration("M"); var method = syntaxTree.FindMethodDeclaration("M"); var docs = " /// <summary>New summary.</summary>\r\n" + " /// <returns>New returns.</returns>"; var updated = method.WithDocumentationText(docs, adjustLeadingWhitespace: false); RoslynAssert.Ast(expected, updated); }
public static void WhenParamExistsInsertBefore() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary>Sum two numbers.</summary> /// <param name=""y"">The other <see cref=""int""/> term.</param> /// <returns>The sum.</returns> public int Add(int x, int y) => x + y; } }"); var expected = GetExpected(@" namespace N { public class C { /// <summary>Sum two numbers.</summary> /// <param name=""x"">The first <see cref=""int""/> term.</param> /// <param name=""y"">The other <see cref=""int""/> term.</param> /// <returns>The sum.</returns> public int Add(int x, int y) => x + y; } }"); var method = syntaxTree.FindMethodDeclaration("Add"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithParamText("x", "The first <see cref=\"int\"/> term."); RoslynAssert.Ast(expected, updated); updated = comment.WithParam(Parse.XmlElementSyntax("<param name=\"x\">The first <see cref=\"int\"/> term.</param>", " ")); RoslynAssert.Ast(expected, updated); }
public static void AddWhenPragmaExplicitWhitespace() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { #pragma warning disable WPF0013 // CLR accessor for attached property must match registered type. public int M() => 1; } }"); var expected = CSharpSyntaxTree.ParseText(@" namespace N { public class C { #pragma warning disable WPF0013 // CLR accessor for attached property must match registered type. /// <summary>New summary.</summary> /// <returns>New returns.</returns> public int M() => 1; } }").FindMethodDeclaration("M"); var method = syntaxTree.FindMethodDeclaration("M"); var docs = " /// <summary>New summary.</summary>\r\n" + " /// <returns>New returns.</returns>"; var updated = method.WithDocumentationText(docs, adjustLeadingWhitespace: false); RoslynAssert.Ast(expected, updated); }
public static void ReplaceExistingSecondMember() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public int M1() => 1; /// <summary>Old summary.</summary> /// <returns>Old returns.</returns> public int M2() => 2; } }"); var expected = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public int M1() => 1; /// <summary>New summary.</summary> /// <returns>New returns.</returns> public int M2() => 2; } }").FindMethodDeclaration("M2"); var method = syntaxTree.FindMethodDeclaration("M2"); var docs = "/// <summary>New summary.</summary>\r\n" + "/// <returns>New returns.</returns>"; var updated = method.WithDocumentationText(docs); RoslynAssert.Ast(expected, updated); }
public static void AddAdjustWhiteSpaceDefault() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public int M() => 1; } }"); var expected = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary>New summary.</summary> /// <returns>New returns.</returns> public int M() => 1; } }").FindMethodDeclaration("M"); var method = syntaxTree.FindMethodDeclaration("M"); var text = "/// <summary>New summary.</summary>\r\n" + "/// <returns>New returns.</returns>\r\n"; var updated = method.WithDocumentationText(text); RoslynAssert.Ast(expected, updated); }
public static void AddDoNotAdjustWhitespace() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { using System; public class C { public int M() => 1; } }"); var expected = CSharpSyntaxTree.ParseText(@" namespace N { using System; public class C { [Obsolete] public int M() => 1; } }").FindMethodDeclaration("M"); var method = syntaxTree.FindMethodDeclaration("M"); var attribute = " [Obsolete]"; var updated = method.WithAttributeListText(attribute, adjustLeadingWhitespace: false); RoslynAssert.Ast(expected, updated); }
public static void MethodDeclaration(string code) { var declaration = Parse.MethodDeclaration(code); Assert.AreEqual(code, declaration.ToFullString()); var expected = (MethodDeclarationSyntax)SyntaxFactory.ParseMemberDeclaration(code); RoslynAssert.Ast(expected, declaration); }
public static void PropertyDeclaration(string code) { var declaration = Parse.PropertyDeclaration(code); Assert.AreEqual(code, declaration.ToFullString()); var expected = (PropertyDeclarationSyntax)SyntaxFactory.ParseCompilationUnit(code).Members.Single(); RoslynAssert.Ast(expected, declaration); }
public static void XmlElementSyntaxSingleLine(string code) { var node = Parse.XmlElementSyntax(code, string.Empty); Assert.AreEqual(code, node.ToFullString()); var expected = SyntaxFactory.ParseLeadingTrivia("/// " + code) .Single(x => x.HasStructure) .GetStructure() .ChildNodes() .OfType <XmlElementSyntax>() .Single(); RoslynAssert.Ast(expected, node); }
public static void InsertBeforeRemarksSingleLineSummaryWhenPragma() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { #pragma warning disable WPF0013 // CLR accessor for attached property must match registered type. /// <remarks></remarks> public void M() { } } }"); var method = syntaxTree.FindMethodDeclaration("M"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithSummaryText("New text."); Assert.AreEqual(true, updated.TryGetSummary(out _)); var expected = GetExpected(@" namespace N { public class C { #pragma warning disable WPF0013 // CLR accessor for attached property must match registered type. /// <summary>New text.</summary> /// <remarks></remarks> public void M() { } } }"); RoslynAssert.Ast(expected, updated); updated = comment.WithSummary(Parse.XmlElementSyntax("<summary>New text.</summary>", " ")); Assert.AreEqual(true, updated.TryGetSummary(out _)); RoslynAssert.Ast(expected, updated); }
public static void WhenSummaryAndReturn() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { /// <summary> /// The identity function. /// </summary> /// <param name=""x"">The value.</param> /// <returns>The value passed in.</returns> public T Id<T>(T x) => x; } }"); var expected = GetExpected(@" namespace N { public class C { /// <summary> /// The identity function. /// </summary> /// <typeparam name=""T"">The type of the value.</typeparam> /// <param name=""x"">The value.</param> /// <returns>The value passed in.</returns> public T Id<T>(T x) => x; } }"); var method = syntaxTree.FindMethodDeclaration("Id"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithTypeParamText("T", "The type of the value."); RoslynAssert.Ast(expected, updated); updated = comment.WithTypeParam(Parse.XmlElementSyntax("<typeparam name=\"T\">The type of the value.</typeparam>", " ")); RoslynAssert.Ast(expected, updated); }
public static void ReplaceWhenPragma() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { #pragma warning disable WPF0013 // CLR accessor for attached property must match registered type. /// <summary>Old text.</summary> /// <typeparam name=""T"">The type</typeparam> /// <param name=""i"">The value to return.</param> /// <returns><paramref name=""i""/></returns> public T Id<T>(T i) => i; } }"); var expected = GetExpected(@" namespace N { public class C { #pragma warning disable WPF0013 // CLR accessor for attached property must match registered type. /// <summary>New text.</summary> /// <typeparam name=""T"">The type</typeparam> /// <param name=""i"">The value to return.</param> /// <returns><paramref name=""i""/></returns> public T Id<T>(T i) => i; } }"); var method = syntaxTree.FindMethodDeclaration("Id"); Assert.AreEqual(true, method.TryGetDocumentationComment(out var comment)); var updated = comment.WithSummaryText("New text."); Assert.AreEqual(true, updated.TryGetSummary(out var summary)); Assert.AreEqual("<summary>New text.</summary>", summary.ToFullString()); RoslynAssert.Ast(expected, updated); }