protected override SyntaxNode InjectMutations(AccessorDeclarationSyntax sourceNode, SyntaxNode targetNode, MutationContext context)
        {
            var result = base.InjectMutations(sourceNode, targetNode, context) as AccessorDeclarationSyntax;

            if (result?.Body == null && result?.ExpressionBody == null)
            {
                return(result);
            }

            if (!context.HasStatementLevelMutant)
            {
                return(result);
            }

            // we need to inject static marker
            if (result.Body == null)
            {
                result = MutantPlacer.ConvertExpressionToBody(result);
            }

            var converter = sourceNode.NeedsReturn()
                ? (Func <Mutation, StatementSyntax>)((toConvert) =>
                                                     SyntaxFactory.ReturnStatement(sourceNode.ExpressionBody !.Expression.InjectMutation(toConvert)))
                : (toConvert) =>
                            SyntaxFactory.ExpressionStatement(sourceNode.ExpressionBody !.Expression.InjectMutation(toConvert));

            var newBody =
                MutantPlacer.PlaceStatementControlledMutations(result.Body,
                                                               context.StatementLevelControlledMutations.Union(context.BlockLevelControlledMutations).
                                                               Select(m => (m.Id, converter(m.Mutation))));

            context.BlockLevelControlledMutations.Clear();
            context.StatementLevelControlledMutations.Clear();
            return(result.WithBody(SyntaxFactory.Block(newBody)));
        }
        /// <inheritdoc/>
        /// Inject mutations and convert expression body to block body if required.
        protected override BaseMethodDeclarationSyntax InjectMutations(T sourceNode, BaseMethodDeclarationSyntax targetNode,
                                                                       MutationContext context)
        {
            targetNode = base.InjectMutations(sourceNode, targetNode, context);
            if (targetNode.Body != null)
            {
                // add a return in case we changed the control flow
                return(MutantPlacer.AddEndingReturn(targetNode) as T);
            }

            if (!context.HasStatementLevelMutant)
            {
                return(targetNode);
            }

            // we need to move to a body version of the method
            targetNode = MutantPlacer.ConvertExpressionToBody(targetNode);

            StatementSyntax mutatedBlock = targetNode.Body;

            var converter = targetNode.NeedsReturn()
                ? (Func <Mutation, StatementSyntax>)((toConvert) =>
                                                     SyntaxFactory.ReturnStatement(sourceNode.ExpressionBody !.Expression.InjectMutation(toConvert)))
                : (toConvert) =>
                            SyntaxFactory.ExpressionStatement(sourceNode.ExpressionBody !.Expression.InjectMutation(toConvert));

            mutatedBlock =
                MutantPlacer.PlaceStatementControlledMutations(mutatedBlock,
                                                               context.StatementLevelControlledMutations.Union(context.BlockLevelControlledMutations).
                                                               Select(m => (m.Id, converter(m.Mutation))));
            context.BlockLevelControlledMutations.Clear();
            context.StatementLevelControlledMutations.Clear();
            return(targetNode.ReplaceNode(targetNode.Body !,
                                          SyntaxFactory.Block(mutatedBlock)));
        }
        protected override BasePropertyDeclarationSyntax InjectMutations(PropertyDeclarationSyntax sourceNode,
                                                                         BasePropertyDeclarationSyntax targetNode, MutationContext context)
        {
            var result = base.InjectMutations(sourceNode, targetNode, context);

            if (!context.HasStatementLevelMutant)
            {
                return(result);
            }
            var mutated = result as PropertyDeclarationSyntax;

            if (mutated?.ExpressionBody == null)
            {
                return(result);
            }
            mutated = MutantPlacer.ConvertPropertyExpressionToBodyAccessor(mutated);
            var getter = mutated.GetAccessor();

            result = mutated.ReplaceNode(getter.Body !, SyntaxFactory.Block(
                                             MutantPlacer.PlaceStatementControlledMutations(
                                                 getter.Body,
                                                 context.StatementLevelControlledMutations.Union(context.BlockLevelControlledMutations).Select(
                                                     m => (m.Id,
                                                           SyntaxFactory.ReturnStatement(
                                                               sourceNode.ExpressionBody !.Expression
                                                               .InjectMutation(m.Mutation)) as StatementSyntax)))))
                     .WithSemicolonToken(SyntaxFactory.MissingToken(SyntaxKind.SemicolonToken));
            return(result);
        }
        public void MutantPlacer_ShouldPlaceWithIfStatement(int id)
        {
            // 1 + 8;
            var originalNode = SyntaxFactory.ExpressionStatement(SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression,
                                                                                                SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1)),
                                                                                                SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(8))));

            // 1 - 8;
            var mutatedNode = SyntaxFactory.ExpressionStatement(SyntaxFactory.BinaryExpression(SyntaxKind.SubtractExpression,
                                                                                               SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1)),
                                                                                               SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(8))));

            var mutants = new List <(Mutant, StatementSyntax)> {
                (new Mutant {
                    Id = id, Mutation = new Mutation {
                        ReplacementNode = mutatedNode
                    }
                }, mutatedNode)
            };

            var result = MutantPlacer.PlaceStatementControlledMutations(originalNode, mutants);

            result.ToFullString().Replace(CodeInjection.HelperNamespace, "StrykerNamespace").ShouldBeSemantically("if (StrykerNamespace.MutantControl.IsActive(" + id + @"))
            {
                1 - 8;
            } else {
                1 + 8;
            }");

            var removedResult = MutantPlacer.RemoveMutant(result);

            removedResult.ToString().ShouldBeSemantically(originalNode.ToString());
        }
        /// <inheritdoc/>
        /// <remarks>Inject pending mutations that are controlled with 'if' statements.</remarks>
        protected override StatementSyntax InjectMutations(T sourceNode, StatementSyntax targetNode, MutationContext context)
        {
            var mutated = MutantPlacer.PlaceStatementControlledMutations(targetNode,
                                                                         context.StatementLevelControlledMutations.Select(m => (m.Id, (sourceNode as StatementSyntax).InjectMutation(m.Mutation))));

            context.StatementLevelControlledMutations.Clear();
            return(mutated);
        }
Exemple #6
0
        public void MutantPlacer_ShouldPlaceWithIfStatement(int id)
        {
            // 1 + 8;
            var originalNode = SyntaxFactory.ExpressionStatement(SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression,
                                                                                                SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1)),
                                                                                                SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(8))));

            // 1 - 8;
            var mutatedNode = SyntaxFactory.ExpressionStatement(SyntaxFactory.BinaryExpression(SyntaxKind.SubtractExpression,
                                                                                               SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1)),
                                                                                               SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(8))));

            var result = MutantPlacer.PlaceStatementControlledMutations(originalNode, new[] { (id, (StatementSyntax)mutatedNode) });
        /// <inheritdoc/>
        /// <remarks>Inject all pending mutations and control them with if statements.</remarks>
        protected override StatementSyntax InjectMutations(T sourceNode, StatementSyntax targetNode, MutationContext context)
        {
            // we inject all pending mutations
            var mutationsToInject = context.StatementLevelControlledMutations
                                    .Union(context.BlockLevelControlledMutations);
            // mutations are controlled by 'if's
            var blockLevelMutations = MutantPlacer.PlaceStatementControlledMutations(targetNode,
                                                                                     mutationsToInject.Select(m =>
                                                                                                              (m.Id, (sourceNode as StatementSyntax).InjectMutation(m.Mutation))));

            context.BlockLevelControlledMutations.Clear();
            context.StatementLevelControlledMutations.Clear();
            // ensure we have a block at the end
            return(blockLevelMutations);
        }
        /// <inheritdoc/>
        /// Inject mutations and convert expression body to block body if required.
        protected override BaseMethodDeclarationSyntax InjectMutations(T sourceNode, BaseMethodDeclarationSyntax targetNode,
                                                                       MutationContext context)
        {
            // find out parameters
            targetNode = base.InjectMutations(sourceNode, targetNode, context);
            if (targetNode.Body != null)
            {
                // inject initialization to default for all out parameters
                targetNode = sourceNode.ParameterList.Parameters.Where(p => p.Modifiers.Any(m => m.Kind() == SyntaxKind.OutKeyword))
                             .Aggregate(targetNode, (current, parameter) => MutantPlacer.AddDefaultInitialization(current, parameter.Identifier, parameter.Type));
                // add a return in case we changed the control flow
                return(MutantPlacer.AddEndingReturn(targetNode) as T);
            }

            if (!context.HasStatementLevelMutant)
            {
                return(targetNode);
            }

            // we need to move to a body version of the method
            targetNode = MutantPlacer.ConvertExpressionToBody(targetNode);

            StatementSyntax mutatedBlock = targetNode.Body;

            var converter = targetNode.NeedsReturn()
                ? (Func <Mutation, StatementSyntax>)((toConvert) =>
                                                     SyntaxFactory.ReturnStatement(sourceNode.ExpressionBody !.Expression.InjectMutation(toConvert)))
                : (toConvert) =>
                            SyntaxFactory.ExpressionStatement(sourceNode.ExpressionBody !.Expression.InjectMutation(toConvert));

            mutatedBlock =
                MutantPlacer.PlaceStatementControlledMutations(mutatedBlock,
                                                               context.StatementLevelControlledMutations.Union(context.BlockLevelControlledMutations).
                                                               Select(m => (m.Id, converter(m.Mutation))));
            context.BlockLevelControlledMutations.Clear();
            context.StatementLevelControlledMutations.Clear();
            return(targetNode.ReplaceNode(targetNode.Body !,
                                          SyntaxFactory.Block(mutatedBlock)));
        }