public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax syntaxNode)
            {
                bool? isAssertTF = IsAssertTrueOrFalse(syntaxNode);
                if (isAssertTF != null)
                {
                    string firstArg = null, secondArg = null;

                    var expr = syntaxNode.Expression as InvocationExpressionSyntax;
                    var firstArgNode = expr.ArgumentList.Arguments.First().Expression;

                    if (firstArgNode.IsKind(SyntaxKind.LogicalNotExpression))
                    {
                        // revert True and False
                        string fmt = isAssertTF.Value ? AssertFalseNoMsg : AssertTrueNoMsg;
                        // the first char should be !
                        // the comments associated with this arg will be lost
                        firstArg = firstArgNode.ToString().Trim().Substring(1);
                        if (expr.ArgumentList.Arguments.Count == 2)
                        {
                            secondArg = expr.ArgumentList.Arguments.Last().ToString().Trim();
                            fmt = isAssertTF.Value ? AssertFalse : AssertTrue;
                        }

                        return SyntaxFactory.ParseStatement(syntaxNode.GetLeadingTrivia().ToFullString() +
                            string.Format(fmt, firstArg, secondArg) +
                            syntaxNode.GetTrailingTrivia().ToFullString());
                    }
                    else if (firstArgNode.IsKind(SyntaxKind.EqualsExpression) || firstArgNode.IsKind(SyntaxKind.NotEqualsExpression))
                    {
                        BinaryExpressionSyntax expr2 = firstArgNode as BinaryExpressionSyntax;
                        firstArg = expr2.Left.ToString().Trim();
                        secondArg = expr2.Right.ToString().Trim();

                        bool isEqual = firstArgNode.IsKind(SyntaxKind.EqualsExpression);
                        // Assert.True(a==b) || Assert.False(a!=b)
                        bool positive = isAssertTF.Value && isEqual || !(isAssertTF.Value || isEqual);
                        var fmt = positive ? AssertEqual : AssertNotEqual;

                        // special case
                        if (IsSpecialValue(ref firstArg, ref secondArg, "null"))
                        {
                            // Assert.True(cond ==|!= null) || Assert.False(cond ==|!= null)
                            fmt = positive ? AssertNull : AssertNotNull;
                        }
                        else if (IsSpecialValue(ref firstArg, ref secondArg, "true"))
                        {
                            // Assert.True(cond ==|!= true) || Assert.False(cond ==|!= true)
                            fmt = positive ? AssertTrueNoMsg : AssertFalseNoMsg;
                        }
                        else if (IsSpecialValue(ref firstArg, ref secondArg, "false"))
                        {
                            // Assert.True(cond ==|!= false) || Assert.False(cond ==|!= false)
                            fmt = positive ? AssertFalseNoMsg : AssertTrueNoMsg;
                        }
                        else
                        {
                            int v = 0;
                            // if second is a const (int only for now)
                            if (int.TryParse(secondArg, out v))
                            {
                                // swap
                                string tmp = firstArg;
                                firstArg = secondArg;
                                secondArg = tmp;
                            }
                        }

                        return SyntaxFactory.ParseStatement(
                                syntaxNode.GetLeadingTrivia().ToFullString() +
                                string.Format(fmt, firstArg, secondArg) +
                                syntaxNode.GetTrailingTrivia().ToFullString());
                    }
                }

                return base.VisitExpressionStatement(syntaxNode);
            }
Esempio n. 2
0
            public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
            {
                SyntaxNode updatedNode = base.VisitExpressionStatement(node);

                if (node.Expression.Kind() == SyntaxKind.AddAssignmentExpression ||
                    node.Expression.Kind() == SyntaxKind.SubtractAssignmentExpression ||
                    node.Expression.Kind() == SyntaxKind.MultiplyAssignmentExpression ||
                    node.Expression.Kind() == SyntaxKind.DivideAssignmentExpression)
                {
                    // Print value of the variable on the 'Left' side of
                    // compound assignment statements encountered.
                    var compoundAssignmentExpression = (AssignmentExpressionSyntax)node.Expression;
                    StatementSyntax consoleWriteLineStatement =
                        SyntaxFactory.ParseStatement(string.Format("System.Console.WriteLine({0});", compoundAssignmentExpression.Left.ToString()));

                    updatedNode =
                        SyntaxFactory.Block(SyntaxFactory.List<StatementSyntax>(
                                                new StatementSyntax[]
                                                {
                                                    node.WithLeadingTrivia().WithTrailingTrivia(), // Remove leading and trailing trivia.
                                                    consoleWriteLineStatement
                                                }))
                            .WithLeadingTrivia(node.GetLeadingTrivia())        // Attach leading trivia from original node.
                            .WithTrailingTrivia(node.GetTrailingTrivia());     // Attach trailing trivia from original node.
                }

                return updatedNode;
            }
        //private Method TraverseStatementSyntax(StatementSyntax ss)
        //{
        //    Method retMethod = new Method();
        //    //TODO
        //    //Get catches for decisions
        //    if (ss is DoStatementSyntax)
        //    {
        //        DoWhileLoop dwl = TraverseDoStatements(ss as DoStatementSyntax);
        //        dwl.IsNested = true;
        //        retMethod.Decisions.DoWhileLoops.Add(dwl);
        //    }
        //    else if (ss is ExpressionStatementSyntax)
        //    {
        //        Method tempMethod = TraverseExpressionStatementSyntax(ss as ExpressionStatementSyntax);
        //        retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
        //        retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
        //    }
        //    else if (ss is ForEachStatementSyntax)
        //    {
        //        ForEachStatement fes = TraverseForEachStatements(ss as ForEachStatementSyntax);
        //        fes.IsNested = true;
        //        retMethod.Decisions.ForEachStatements.Add(fes);
        //    }
        //    else if (ss is ForStatementSyntax)
        //    {
        //        ForStatement fs = TraverseForStatements(ss as ForStatementSyntax);
        //        fs.IsNested = true;
        //        retMethod.Decisions.ForStatements.Add(fs);
        //    }
        //    else if (ss is IfStatementSyntax)
        //    {
        //        IfStatement ifstm = TraverseIfStatements(ss as IfStatementSyntax);
        //        ifstm.IsNested = true;
        //        retMethod.Decisions.IfStatements.Add(ifstm);
        //    }
        //    else if (ss is LocalDeclarationStatementSyntax)
        //    {
        //        Model.Type tempType = new Model.Type();
        //        LocalDeclarationStatementSyntax ldss = ss as LocalDeclarationStatementSyntax;
        //        if (ldss.Declaration != null)
        //        {
        //            VariableDeclarationSyntax vds = ldss.Declaration;
        //            tempType.Name = vds.Type.ToString();
        //            tempType.IsKnownType = true;
        //            tempType.IsNotUserDefined = true;
        //        }
        //        Method tempMethod = TransverseAccessVars(ss as LocalDeclarationStatementSyntax);
        //        //NOT SURE if this will work but here goes
        //        tempMethod.AccessedVariables[0].Type = tempType;
        //        retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
        //        retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
        //    }
        //    else if (ss is SwitchStatementSyntax)
        //    {
        //        SwitchStatement switchStm = TraverseSwitchStatements(ss as SwitchStatementSyntax);
        //        switchStm.IsNested = true;
        //        retMethod.Decisions.SwitchStatements.Add(switchStm);
        //    }
        //    else if (ss is WhileStatementSyntax)
        //    {
        //        WhileLoop wl = TraverseWhileLoops(ss as WhileStatementSyntax);
        //        wl.IsNested = true;
        //        retMethod.Decisions.WhileLoops.Add(wl);
        //    }
        //    return retMethod;
        //}
        private Method TraverseExpressionStatementSyntax(ExpressionStatementSyntax ess)
        {
            Method retMethod = new Method();

            if (ess.HasLeadingTrivia)
            {
                SetOuterComments(retMethod, ess.GetLeadingTrivia().ToFullString());
            }

            if (ess.HasTrailingTrivia)
            {
                SetInnerComments(retMethod, ess.GetTrailingTrivia().ToFullString());
            }

            var vars = from aVar in ess.ChildNodes().OfType<BinaryExpressionSyntax>() select aVar;
            foreach (BinaryExpressionSyntax bes in vars)
            {
                Method tempMethod = TraverseBinaryExpression(bes);
                retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
                retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
            }
            var invokedMethods = from aInvokedMethod in ess.ChildNodes().OfType<InvocationExpressionSyntax>() select aInvokedMethod;
            foreach (InvocationExpressionSyntax ies in invokedMethods)
            {
                Method tempMethod = TraverseInvocationExpression(ies);
                retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
                retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
            }
            var postfixUnaryExpressions = from aPostfixUnaryExpression in ess.ChildNodes().OfType<PostfixUnaryExpressionSyntax>() select aPostfixUnaryExpression;
            foreach (PostfixUnaryExpressionSyntax pues in postfixUnaryExpressions)
            {
                Method tempMethod = TraversePostfixUnaryExpressions(pues);
                retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
                retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
            }
            var prefixUnaryExpressions = from aPrefixUnaryExpressions in ess.ChildNodes().OfType<PrefixUnaryExpressionSyntax>() select aPrefixUnaryExpressions;
            foreach (PrefixUnaryExpressionSyntax pues in prefixUnaryExpressions)
            {
                Method tempMethod = TraversePrefixUnaryExpressions(pues);
                retMethod.AccessedVariables.AddRange(tempMethod.AccessedVariables);
                retMethod.InvokedMethods.AddRange(tempMethod.InvokedMethods);
            }
            return retMethod;
        }