private ExpressionSyntax MutateSubExpressionWithConditional(ExpressionSyntax originalNode, ExpressionSyntax currentNode, MutationContext context)
        {
            var expressionAst = currentNode;

            foreach (var mutant in FindMutants(originalNode, context))
            {
                expressionAst = MutantPlacer.PlaceWithConditionalExpression(expressionAst, ApplyMutant(originalNode, mutant), mutant.Id);
            }
            return(expressionAst);
        }
        private SyntaxNode MutateWithConditionalExpressions(ExpressionSyntax currentNode)
        {
            var expressionAst = currentNode;

            foreach (var mutant in FindMutantsRecursive(currentNode))
            {
                _mutants.Add(mutant);
                var mutatedNode = ApplyMutant(currentNode, mutant);
                expressionAst = MutantPlacer.PlaceWithConditionalExpression(expressionAst, mutatedNode, mutant.Id);
            }
            return(expressionAst);
        }
 /// <summary>
 /// Recursively mutates a single SyntaxNode
 /// </summary>
 /// <param name="currentNode">The current root node</param>
 /// <returns>Mutated node</returns>
 public SyntaxNode Mutate(SyntaxNode currentNode)
 {
     if (GetExpressionSyntax(currentNode) is var expressionSyntax && expressionSyntax != null)
     {
         // The mutations should be placed using a ConditionalExpression
         ExpressionSyntax expressionAst = expressionSyntax;
         foreach (var mutant in FindMutants(expressionSyntax))
         {
             _mutants.Add(mutant);
             ExpressionSyntax mutatedNode = ApplyMutant(expressionSyntax, mutant);
             expressionAst = MutantPlacer.PlaceWithConditionalExpression(expressionAst, mutatedNode, mutant.Id);
         }
         return(currentNode.ReplaceNode(expressionSyntax, expressionAst));
     }
        private SyntaxNode MutateWithConditionalExpressions(ExpressionSyntax currentNode)
        {
            var expressionAst = currentNode.TrackNodes(currentNode.ChildNodes().Append(currentNode));

            foreach (var childNode in currentNode.ChildNodes())
            {
                var mutatedChild = Mutate(childNode);
                if (mutatedChild != childNode)
                {
                    expressionAst = expressionAst.ReplaceNode(expressionAst.GetCurrentNode(childNode), mutatedChild);
                }
            }
            foreach (var mutant in FindMutants(currentNode))
            {
                var mutatedNode = ApplyMutant(currentNode, mutant);
                expressionAst = MutantPlacer.PlaceWithConditionalExpression(expressionAst, mutatedNode, mutant.Id);
            }
            return(expressionAst);
        }
Esempio n. 5
0
 private ExpressionSyntax MutateSubExpressionWithConditional(ExpressionSyntax originalNode, ExpressionSyntax currentNode, MutationContext context)
 {
     return(FindMutants(originalNode, context).Aggregate(currentNode, (current, mutant) => MutantPlacer.PlaceWithConditionalExpression(current, ApplyMutant(originalNode, mutant), mutant.Id)));
 }
        private SyntaxNode MutateExpressions(SyntaxNode currentNode)
        {
            if (currentNode is ExpressionStatementSyntax expressionStatement)
            {
                // The subExpression of a ExpressionStatement cannot be mutated directly
                var mutant = Mutate(expressionStatement.Expression);
                return(currentNode.ReplaceNode(expressionStatement.Expression, mutant));
            }
            if (currentNode is InvocationExpressionSyntax invocationExpression && invocationExpression.ArgumentList.Arguments.Count == 0)
            {
                var mutant = FindMutants(invocationExpression).FirstOrDefault();
                if (mutant != null)
                {
                    _mutants.Add(mutant);
                    return(MutantPlacer.PlaceWithConditionalExpression(invocationExpression, mutant.Mutation.ReplacementNode as ExpressionSyntax, mutant.Id));
                }
            }
            var expressions = GetExpressionSyntax(currentNode).Where(x => x != null);

            if (expressions.Any())
            {
                var currentNodeCopy = currentNode.TrackNodes(expressions);
                foreach (var expressionSyntax in expressions)
                {
                    var currentExpressionSyntax = currentNodeCopy.GetCurrentNode(expressionSyntax);
                    if (expressionSyntax is InvocationExpressionSyntax)
                    {
                        // chained invocations, we will recurse
                        var mutant = Mutate(expressionSyntax);
                        currentNodeCopy = currentNodeCopy.ReplaceNode(currentExpressionSyntax, mutant);
                        continue;
                    }
                    SyntaxNode mutationCandidate = null;

                    if (expressionSyntax is ParenthesizedLambdaExpressionSyntax lambda)
                    {
                        mutationCandidate = lambda.Body;
                    }
                    else if (expressionSyntax is MemberAccessExpressionSyntax memberAccess)
                    {
                        mutationCandidate = memberAccess.Expression;
                    }
                    else if (expressionSyntax is AnonymousFunctionExpressionSyntax anonymousFunction)
                    {
                        mutationCandidate = anonymousFunction.Body;
                    }

                    if (mutationCandidate != null)
                    {
                        // we can  mutate a part of expression
                        var subNodeCopy = expressionSyntax.TrackNodes(mutationCandidate);
                        var sub         = subNodeCopy.ReplaceNode(subNodeCopy.GetCurrentNode(mutationCandidate), Mutate(mutationCandidate));
                        currentNodeCopy = currentNodeCopy.ReplaceNode(currentExpressionSyntax, sub);
                    }
                    else
                    {
                        // attempts to mutate the expression as a whole
                        currentNodeCopy = currentNodeCopy.ReplaceNode(currentExpressionSyntax,
                                                                      MutateWithConditionalExpressions(expressionSyntax));
                    }
                }

                return(currentNodeCopy);
            }

            return(null);
        }