// recursive version
        public SyntaxNode Mutate(SyntaxNode currentNode, MutationContext context)
        {
            // don't mutate immutable nodes
            if (!SyntaxHelper.CanBeMutated(currentNode))
            {
                return(currentNode);
            }

            // search for node specific handler
            var nodeHandler = _specificOrchestrator.FindHandler(currentNode);

            return(nodeHandler.Mutate(currentNode, context));
        }
Exemplo n.º 2
0
        private SyntaxNode Mutate(SyntaxNode currentNode, MutationContext context)
        {
            // don't mutate immutable nodes
            if (!SyntaxHelper.CanBeMutated(currentNode))
            {
                return(currentNode);
            }

            // identify static related structure
            switch (currentNode)
            {
            // static fields
            case FieldDeclarationSyntax fieldDeclaration when fieldDeclaration.Modifiers.Any(x => x.Kind() == SyntaxKind.StaticKeyword):
                context = new MutationContext
                {
                    InStaticValue = true
                };
                break;

            // static constructors
            case ConstructorDeclarationSyntax constructorDeclaration when constructorDeclaration.Modifiers.Any(x => x.Kind() == SyntaxKind.StaticKeyword):
                context = new MutationContext
                {
                    InStaticValue = true
                };
                if (MustInjectCoverageLogic)
                {
                    return(MutateStaticConstructor(constructorDeclaration, context));
                }
                break;

            // static properties
            case PropertyDeclarationSyntax propertyDeclaration when propertyDeclaration.Modifiers.Any(x => x.Kind() == SyntaxKind.StaticKeyword) && propertyDeclaration.AccessorList != null:
                context = new MutationContext
                {
                    InStaticValue = true
                };
                if (MustInjectCoverageLogic)
                {
                    return(MutateStaticAccessor(propertyDeclaration, context));
                }
                break;
            }

            switch (currentNode)
            {
            // apply statement specific strategies (where applicable)
            case ExpressionStatementSyntax tentativeAssignment when tentativeAssignment.Expression is AssignmentExpressionSyntax assign:
                return(MutateAssignment(tentativeAssignment, assign, context));

            case ExpressionStatementSyntax tentativeAssignment when tentativeAssignment.Expression is PostfixUnaryExpressionSyntax || tentativeAssignment.Expression is PrefixUnaryExpressionSyntax:
                return(MutateUnaryStatement(tentativeAssignment, context));

            case ExpressionStatementSyntax expressionStatement:
                // we must skip the expression statement part
                return(currentNode.ReplaceNode(expressionStatement.Expression, Mutate(expressionStatement.Expression, context)));

            case IfStatementSyntax ifStatement:
                return(MutateIfStatement(ifStatement, context));

            case ForStatementSyntax forStatement:
                return(MutateForStatement(forStatement, context));
            }

            var mutatedNode = MutateExpression(currentNode, context);

            return(AddReturnDefault(mutatedNode));
        }