public override IEnumerable <Mutation> ApplyMutations(BinaryExpressionSyntax node)
        {
            if (KindsToMutate.ContainsKey(node.Kind()))
            {
                foreach (var mutationKind in KindsToMutate[node.Kind()])
                {
                    var nodeLeft = node.Left;
                    if (node.Kind() is SyntaxKind.AddExpression)
                    {
                        yield return(new Mutation
                        {
                            OriginalNode = node,
                            ReplacementNode = nodeLeft,
                            DisplayName = $"Arithmetic operator mutation - {node} replace with {nodeLeft}",
                            Type = MutatorType.Arithmetic
                        });
                    }
                    else
                    {
                        var replacementNode = SyntaxFactory.BinaryExpression(mutationKind, nodeLeft, node.Right);
                        replacementNode = replacementNode.WithOperatorToken(replacementNode.OperatorToken.WithTriviaFrom(node.OperatorToken));

                        yield return(new Mutation
                        {
                            OriginalNode = node,
                            ReplacementNode = replacementNode,
                            DisplayName = $"Arithmetic operator mutation - {node} replace with {replacementNode}",
                            Type = MutatorType.Arithmetic
                        });
                    }
                }
            }
        }
예제 #2
0
        public override IEnumerable <Mutation> ApplyMutations(BinaryExpressionSyntax node)
        {
            if (KindsToMutate.ContainsKey(node.Kind()))
            {
                foreach (var mutationKind in KindsToMutate[node.Kind()])
                {
                    var nodeLeft        = node.Left;
                    var replacementNode = SyntaxFactory.BinaryExpression(mutationKind, nodeLeft, node.Right);
                    replacementNode = replacementNode.WithOperatorToken(replacementNode.OperatorToken.WithTriviaFrom(node.OperatorToken));

                    yield return(new Mutation
                    {
                        OriginalNode = node,
                        ReplacementNode = replacementNode,
                        DisplayName = $"Bitwise operator mutation - {node} replace with {replacementNode}",
                        Type = MutatorType.Bitwise
                    });
                }
            }
            else if (node.Kind() == SyntaxKind.ExclusiveOrExpression)
            {
                yield return(GetLogicalMutation(node));

                yield return(GetIntegralMutation(node));
            }
        }
예제 #3
0
        /// <summary> Apply mutations to an <see cref="InvocationExpressionSyntax"/> </summary>
        public override IEnumerable <Mutation> ApplyMutations(InvocationExpressionSyntax parent)
        {
            if (parent.Expression is MemberAccessExpressionSyntax node)
            {
                if (Enum.TryParse(node.Name.Identifier.ValueText, out LinqExpression expression) &&
                    KindsToMutate.TryGetValue(expression, out var replacementExpression))
                {
                    var replacement = SyntaxFactory.IdentifierName(replacementExpression.ToString());
                    var displayName = $"Linq method mutation ({node.Name.Identifier.ValueText}() to {replacement}())";

                    if (RequireArguments.Contains(replacementExpression) && parent.ArgumentList.Arguments.Count == 0)
                    {
                        yield break;
                    }

                    yield return(new Mutation
                    {
                        DisplayName = displayName,
                        OriginalNode = parent,
                        ReplacementNode = parent.ReplaceNode(node.Name, replacement),
                        Type = Mutator.Linq
                    });
                }
            }
        }
예제 #4
0
        /// <summary> Apply mutations to an <see cref="InvocationExpressionSyntax"/> </summary>
        public override IEnumerable <Mutation> ApplyMutations(ExpressionSyntax expr)
        {
            var original = expr;

            if (expr.Parent is ConditionalAccessExpressionSyntax)
            {
                yield break;
            }
            while (expr is ConditionalAccessExpressionSyntax conditional)
            {
                expr = conditional.WhenNotNull;
            }

            if (!(expr is InvocationExpressionSyntax invocationExpression))
            {
                yield break;
            }

            string     memberName;
            SyntaxNode toReplace;

            switch (invocationExpression.Expression)
            {
            case MemberAccessExpressionSyntax node:
                toReplace  = node.Name;
                memberName = node.Name.Identifier.ValueText;
                break;

            case MemberBindingExpressionSyntax binding:
                toReplace  = binding.Name;
                memberName = binding.Name.Identifier.ValueText;
                break;

            default:
                yield break;
            }

            if (!Enum.TryParse(memberName, out LinqExpression expression) ||
                !KindsToMutate.TryGetValue(expression, out var replacementExpression))
            {
                yield break;
            }

            var replacement = SyntaxFactory.IdentifierName(replacementExpression.ToString());
            var displayName = $"Linq method mutation ({memberName}() to {replacement}())";

            if (RequireArguments.Contains(replacementExpression) && invocationExpression.ArgumentList.Arguments.Count == 0)
            {
                yield break;
            }

            yield return(new Mutation
            {
                DisplayName = displayName,
                OriginalNode = original,
                ReplacementNode = original.ReplaceNode(toReplace, replacement),
                Type = Mutator.Linq
            });
        }
예제 #5
0
 public override IEnumerable <Mutation> ApplyMutations(LiteralExpressionSyntax node)
 {
     if (KindsToMutate.ContainsKey(node.Kind()))
     {
         var replacementNode = SyntaxFactory.LiteralExpression(KindsToMutate[node.Kind()]);
         yield return(new Mutation()
         {
             OriginalNode = node,
             ReplacementNode = replacementNode,
             DisplayName = $"Boolean mutation mutation - {node} replace with {replacementNode}",
             Type = MutatorType.Boolean
         });
     }
 }
예제 #6
0
        private static IEnumerable <Mutation> FindMutableMethodCalls(ExpressionSyntax node, ExpressionSyntax original)
        {
            while (node is ConditionalAccessExpressionSyntax conditional)
            {
                foreach (var subMutants in FindMutableMethodCalls(conditional.Expression, original))
                {
                    yield return(subMutants);
                }
                node = conditional.WhenNotNull;
            }

            for (; ;)
            {
                ExpressionSyntax next = null;
                if (!(node is InvocationExpressionSyntax invocationExpression))
                {
                    yield break;
                }

                string     memberName;
                SyntaxNode toReplace;
                switch (invocationExpression.Expression)
                {
                case MemberAccessExpressionSyntax memberAccessExpression:
                    toReplace  = memberAccessExpression.Name;
                    memberName = memberAccessExpression.Name.Identifier.ValueText;
                    next       = memberAccessExpression.Expression;
                    break;

                case MemberBindingExpressionSyntax memberBindingExpression:
                    toReplace  = memberBindingExpression.Name;
                    memberName = memberBindingExpression.Name.Identifier.ValueText;
                    break;

                default:
                    yield break;
                }

                if (Enum.TryParse(memberName, out LinqExpression expression) &&
                    KindsToMutate.TryGetValue(expression, out var replacementExpression))
                {
                    if (RequireArguments.Contains(replacementExpression) &&
                        invocationExpression.ArgumentList.Arguments.Count == 0)
                    {
                        yield break;
                    }

                    yield return(new Mutation
                    {
                        DisplayName =
                            $"Linq method mutation ({memberName}() to {SyntaxFactory.IdentifierName(replacementExpression.ToString())}())",
                        OriginalNode = original,
                        ReplacementNode = original.ReplaceNode(toReplace,
                                                               SyntaxFactory.IdentifierName(replacementExpression.ToString())),
                        Type = Mutator.Linq
                    });
                }

                node = next;
            }
        }