public void TestRedConsecutiveSeparators() { //there are omitted array size expressions between the commas var input = "int[,,]a;"; //NB: no whitespace, since it would be deleted bool first = true; var rewriter = new RedRewriter( rewriteNode: node => { if (node != null && node.IsKind(SyntaxKind.OmittedArraySizeExpression) && first) { first = false; return(null); } return(node); } ); Exception caught = null; try { TestRed(input, "", rewriter, isExpr: false); } catch (Exception e) { caught = e; } Assert.NotNull(caught); Assert.True(caught is InvalidOperationException); }
public void TestRedSeparatedDeleteAll() { //the type argument list is a SeparateSyntaxList var input = "A<B,C,D>"; //NB: no whitespace, since it would be deleted //delete all type arguments, should clear the intervening commas var rewriter = new RedRewriter( rewriteNode: node => (node.IsKind(SyntaxKind.IdentifierName) && node.ToString() != "A") ? null : node ); Exception caught = null; try { TestRed(input, "", rewriter, isExpr: true); } catch (Exception e) { caught = e; } Assert.NotNull(caught); Assert.True(caught is InvalidOperationException); }
public void TestRedSeparatedDeleteNone() { //the type argument list is a SeparateSyntaxList var input = "A<B,C,D>"; //NB: no whitespace, since it would be deleted var output = input; var rewriter = new RedRewriter(); TestRed(input, output, rewriter, isExpr: true); }
public void TestRedTokenDeleteNone() { //commas in an implicit array creation constitute a SyntaxTokenList var input = "x=new[,,]{{{}}};"; //NB: no whitespace, since it would be deleted var output = input; var rewriter = new RedRewriter(); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedTriviaDeleteNone() { //whitespace and comments constitute a SyntaxTriviaList var input = " a(); //comment"; //NB: no whitespace, since it would be deleted var output = input; var rewriter = new RedRewriter(); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedDeleteNone() { //statements in block constitute a SyntaxList var input = "{A();B();C();}"; //NB: no whitespace, since it would be deleted var output = input; var rewriter = new RedRewriter(); TestRed(input, output, rewriter, isExpr: false); }
private static void TestRed(string input, string output, RedRewriter rewriter, bool isExpr) { var red = isExpr ? (CSharpSyntaxNode)SyntaxFactory.ParseExpression(input) : SyntaxFactory.ParseStatement(input); Assert.False(red.ContainsDiagnostics); var result = rewriter.Visit(red); Assert.Equal(input == output, ReferenceEquals(red, result)); Assert.Equal(output, result.ToFullString()); }
public void TestRedTriviaDeleteAll() { //whitespace and comments constitute a SyntaxTriviaList var input = " a(); //comment"; //NB: no whitespace, since it would be deleted var output = "a();"; //delete all trivia var rewriter = new RedRewriter(rewriteTrivia: trivia => default(SyntaxTrivia)); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedTokenDeleteAll() { //commas in an implicit array creation constitute a SyntaxTokenList var input = "x=new[,,]{{{}}};"; //NB: no whitespace, since it would be deleted var output = "x=new[]{{{}}};"; //delete all commas var rewriter = new RedRewriter(rewriteToken: token => (token.Kind() == SyntaxKind.CommaToken) ? default(SyntaxToken) : token); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedTriviaDeleteSome() { //whitespace and comments constitute a SyntaxTriviaList var input = " a(); //comment"; //NB: no whitespace, since it would be deleted var output = "a();//comment"; //delete all whitespace trivia (leave comments) var rewriter = new RedRewriter(rewriteTrivia: trivia => trivia.Kind() == SyntaxKind.WhitespaceTrivia ? default(SyntaxTrivia) : trivia); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedDeleteSome() { //statements in block constitute a SyntaxList var input = "{A();B();C();}"; //NB: no whitespace, since it would be deleted var output = "{A();C();}"; //delete the middle statement var rewriter = new RedRewriter(rewriteNode: node => (node.IsKind(SyntaxKind.ExpressionStatement) && node.ToString().Contains("B")) ? null : node); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedDeleteAll() { //statements in block constitute a SyntaxList var input = "{A();B();C();}"; //NB: no whitespace, since it would be deleted var output = "{}"; //delete all statements var rewriter = new RedRewriter(rewriteNode: node => (node.IsKind(SyntaxKind.ExpressionStatement)) ? null : node); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedSeparatedDeleteLast() { //the type argument list is a SeparateSyntaxList var input = "A<B,C,D>"; //NB: no whitespace, since it would be deleted var output = "A<B,C,>"; //delete the last type argument (should clear the *preceding* comma) var rewriter = new RedRewriter(rewriteNode: node => (node.IsKind(SyntaxKind.IdentifierName) && node.ToString() == "D") ? null : node); TestRed(input, output, rewriter, isExpr: true); }
public void RewriteMissingIdentifierInExpressionStatement_WithSyntaxTree() { var tree1 = SyntaxFactory.ParseSyntaxTree( "class C { static void Main() { if (true) } }" ); var ifStmt1 = tree1 .GetCompilationUnitRoot() .DescendantNodes() .OfType <IfStatementSyntax>() .Single(); var exprStmt1 = (ExpressionStatementSyntax)ifStmt1.Statement; var expr1 = (IdentifierNameSyntax)exprStmt1.Expression; var token1 = expr1.Identifier; Assert.Equal(tree1, expr1.SyntaxTree); Assert.True(expr1.IsMissing); Assert.True(expr1.ContainsDiagnostics); Assert.True(token1.IsMissing); Assert.False(token1.ContainsDiagnostics); var trivia = SyntaxFactory.ParseTrailingTrivia(" "); var rewriter = new RedRewriter( rewriteToken: tok => tok.Kind() == SyntaxKind.IdentifierToken ? tok.WithLeadingTrivia(trivia) : tok ); var ifStmt2 = (IfStatementSyntax)rewriter.Visit(ifStmt1); var exprStmt2 = (ExpressionStatementSyntax)ifStmt2.Statement; var expr2 = (IdentifierNameSyntax)exprStmt2.Expression; var token2 = expr2.Identifier; Assert.NotEqual(expr1, expr2); Assert.NotNull(expr2.SyntaxTree); Assert.False( expr2.SyntaxTree.HasCompilationUnitRoot, "how did we get a CompilationUnit root?" ); Assert.Same(ifStmt2, expr2.SyntaxTree.GetRoot()); Assert.True(expr2.IsMissing); Assert.False(expr2.ContainsDiagnostics); //gone after rewrite Assert.NotEqual(token1, token2); Assert.True(token2.IsMissing); Assert.False(token2.ContainsDiagnostics); Assert.True(IsStatementExpression(expr1)); Assert.True(IsStatementExpression(expr2)); }
public void TestRedTokenDeleteSome() { //commas in an implicit array creation constitute a SyntaxTokenList var input = "x=new[,,]{{{}}};"; //NB: no whitespace, since it would be deleted var output = "x=new[,]{{{}}};"; //delete one comma bool first = true; var rewriter = new RedRewriter(rewriteToken: token => { if (token.Kind() == SyntaxKind.CommaToken && first) { first = false; return(default(SyntaxToken)); } return(token); }); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedSeparatedDeleteSome() { //the type argument list is a SeparateSyntaxList var input = "A<B,C,D>"; //NB: no whitespace, since it would be deleted //delete the middle type argument (should clear the following comma) var rewriter = new RedRewriter(rewriteNode: node => (node.IsKind(SyntaxKind.IdentifierName) && node.ToString() == "C") ? null : node); Exception caught = null; try { TestRed(input, "", rewriter, isExpr: true); } catch (Exception e) { caught = e; } Assert.NotNull(caught); Assert.Equal(true, caught is InvalidOperationException); }
public void RewriteMissingIdentiferInExpressionStatement_ImplicitlyCreatedSyntaxTree() { var ifStmt1 = (IfStatementSyntax)SyntaxFactory.ParseStatement("if (true)"); var exprStmt1 = (ExpressionStatementSyntax)ifStmt1.Statement; var expr1 = (IdentifierNameSyntax)exprStmt1.Expression; var token1 = expr1.Identifier; Assert.NotNull(expr1.SyntaxTree); Assert.False(expr1.SyntaxTree.HasCompilationUnitRoot, "how did we get a CompilationUnit root?"); Assert.Same(ifStmt1, expr1.SyntaxTree.GetRoot()); Assert.True(expr1.IsMissing); Assert.True(expr1.ContainsDiagnostics); Assert.True(token1.IsMissing); Assert.False(token1.ContainsDiagnostics); var trivia = SyntaxFactory.ParseTrailingTrivia(" "); var rewriter = new RedRewriter(rewriteToken: tok => tok.CSharpKind() == SyntaxKind.IdentifierToken ? tok.WithLeadingTrivia(trivia) : tok); var ifStmt2 = (IfStatementSyntax)rewriter.Visit(ifStmt1); var exprStmt2 = (ExpressionStatementSyntax)ifStmt2.Statement; var expr2 = (IdentifierNameSyntax)exprStmt2.Expression; var token2 = expr2.Identifier; Assert.NotEqual(expr1, expr2); Assert.NotNull(expr2.SyntaxTree); Assert.False(expr2.SyntaxTree.HasCompilationUnitRoot, "how did we get a CompilationUnit root?"); Assert.Same(ifStmt2, expr2.SyntaxTree.GetRoot()); Assert.True(expr2.IsMissing); Assert.False(expr2.ContainsDiagnostics); //gone after rewrite Assert.NotEqual(token1, token2); Assert.True(token2.IsMissing); Assert.False(token2.ContainsDiagnostics); Assert.True(IsStatementExpression(expr1.Green)); Assert.True(IsStatementExpression(expr2.Green)); }
public void RewriteMissingIdentiferInExpressionStatement_ImplicitlyCreatedSyntaxTree() { var ifStmt1 = (IfStatementSyntax)SyntaxFactory.ParseStatement("if (true)"); var exprStmt1 = (ExpressionStatementSyntax)ifStmt1.Statement; var expr1 = (IdentifierNameSyntax)exprStmt1.Expression; var token1 = expr1.Identifier; Assert.NotNull(expr1.SyntaxTree); Assert.False(expr1.SyntaxTree.HasCompilationUnitRoot, "how did we get a CompilationUnit root?"); Assert.Same(ifStmt1, expr1.SyntaxTree.GetRoot()); Assert.True(expr1.IsMissing); Assert.True(expr1.ContainsDiagnostics); Assert.True(token1.IsMissing); Assert.False(token1.ContainsDiagnostics); var trivia = SyntaxFactory.ParseTrailingTrivia(" "); var rewriter = new RedRewriter(rewriteToken: tok => tok.Kind() == SyntaxKind.IdentifierToken ? tok.WithLeadingTrivia(trivia) : tok); var ifStmt2 = (IfStatementSyntax)rewriter.Visit(ifStmt1); var exprStmt2 = (ExpressionStatementSyntax)ifStmt2.Statement; var expr2 = (IdentifierNameSyntax)exprStmt2.Expression; var token2 = expr2.Identifier; Assert.NotEqual(expr1, expr2); Assert.NotNull(expr2.SyntaxTree); Assert.False(expr2.SyntaxTree.HasCompilationUnitRoot, "how did we get a CompilationUnit root?"); Assert.Same(ifStmt2, expr2.SyntaxTree.GetRoot()); Assert.True(expr2.IsMissing); Assert.False(expr2.ContainsDiagnostics); //gone after rewrite Assert.NotEqual(token1, token2); Assert.True(token2.IsMissing); Assert.False(token2.ContainsDiagnostics); Assert.True(IsStatementExpression(expr1)); Assert.True(IsStatementExpression(expr2)); }
public void TestRedTokenDeleteSome() { //commas in an implicit array creation constitute a SyntaxTokenList var input = "x=new[,,]{{{}}};"; //NB: no whitespace, since it would be deleted var output = "x=new[,]{{{}}};"; //delete one comma bool first = true; var rewriter = new RedRewriter(rewriteToken: token => { if (token.Kind() == SyntaxKind.CommaToken && first) { first = false; return default(SyntaxToken); } return token; }); TestRed(input, output, rewriter, isExpr: false); }
public void TestRedConsecutiveSeparators() { //there are omitted array size expressions between the commas var input = "int[,,]a;"; //NB: no whitespace, since it would be deleted bool first = true; var rewriter = new RedRewriter(rewriteNode: node => { if (node != null && node.IsKind(SyntaxKind.OmittedArraySizeExpression) && first) { first = false; return null; } return node; }); Exception caught = null; try { TestRed(input, "", rewriter, isExpr: false); } catch (Exception e) { caught = e; } Assert.NotNull(caught); Assert.Equal(true, caught is InvalidOperationException); }
public void RewriteMissingIdentifierInExpressionStatement_WithSyntaxTree() { var tree1 = SyntaxFactory.ParseSyntaxTree("class C { static void Main() { if (true) } }"); var ifStmt1 = tree1.GetCompilationUnitRoot().DescendantNodes().OfType<IfStatementSyntax>().Single(); var exprStmt1 = (ExpressionStatementSyntax)ifStmt1.Statement; var expr1 = (IdentifierNameSyntax)exprStmt1.Expression; var token1 = expr1.Identifier; Assert.Equal(tree1, expr1.SyntaxTree); Assert.True(expr1.IsMissing); Assert.True(expr1.ContainsDiagnostics); Assert.True(token1.IsMissing); Assert.False(token1.ContainsDiagnostics); var trivia = SyntaxFactory.ParseTrailingTrivia(" "); var rewriter = new RedRewriter(rewriteToken: tok => tok.Kind() == SyntaxKind.IdentifierToken ? tok.WithLeadingTrivia(trivia) : tok); var ifStmt2 = (IfStatementSyntax)rewriter.Visit(ifStmt1); var exprStmt2 = (ExpressionStatementSyntax)ifStmt2.Statement; var expr2 = (IdentifierNameSyntax)exprStmt2.Expression; var token2 = expr2.Identifier; Assert.NotEqual(expr1, expr2); Assert.NotNull(expr2.SyntaxTree); Assert.False(expr2.SyntaxTree.HasCompilationUnitRoot, "how did we get a CompilationUnit root?"); Assert.Same(ifStmt2, expr2.SyntaxTree.GetRoot()); Assert.True(expr2.IsMissing); Assert.False(expr2.ContainsDiagnostics); //gone after rewrite Assert.NotEqual(token1, token2); Assert.True(token2.IsMissing); Assert.False(token2.ContainsDiagnostics); Assert.True(IsStatementExpression(expr1)); Assert.True(IsStatementExpression(expr2)); }