Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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());
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
0
        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);
        }
Esempio n. 13
0
        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);
        }
Esempio n. 14
0
        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);
        }
Esempio n. 15
0
        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));
        }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
        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);
        }
Esempio n. 18
0
        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));
        }
Esempio n. 19
0
        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));
        }
Esempio n. 20
0
        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);
        }
Esempio n. 21
0
        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);
        }
Esempio n. 22
0
        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);
        }
Esempio n. 23
0
        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);
        }
Esempio n. 24
0
        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);
        }
Esempio n. 25
0
        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);
        }
Esempio n. 26
0
        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);
        }
Esempio n. 27
0
        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);
        }
Esempio n. 28
0
        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);
        }
Esempio n. 29
0
        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);
        }
Esempio n. 30
0
        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);
        }
Esempio n. 31
0
        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));
        }
Esempio n. 32
0
        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());
        }