Exemplo n.º 1
0
        protected override LocalFunctionStatementSyntax InjectMutations(LocalFunctionStatementSyntax sourceNode, LocalFunctionStatementSyntax targetNode,
                                                                        MutationContext context)
        {
            // find out parameters
            targetNode = base.InjectMutations(sourceNode, targetNode, context);

            var fullTargetBody          = targetNode.Body;
            var sourceNodeParameterList = sourceNode.ParameterList;

            if (fullTargetBody != null)
            {
                // the function is in the body form
                // inject initialization to default for all out parameters
                targetNode = sourceNode.WithBody(MutantPlacer.AddDefaultInitializers(fullTargetBody,
                                                                                     sourceNodeParameterList.Parameters.Where(p =>
                                                                                                                              p.Modifiers.Any(m => m.Kind() == SyntaxKind.OutKeyword))));
                // add a return in case we changed the control flow
                return(MutantPlacer.AddEndingReturn(targetNode));
            }
            // nothing to do if there is now pending statement mutations
            if (!context.HasStatementLevelMutant)
            {
                return(targetNode);
            }

            // we need to move to a body version of the function to inject pending mutations
            targetNode = MutantPlacer.ConvertExpressionToBody(targetNode);
            return(targetNode.WithBody(SyntaxFactory.Block(context.InjectBlockLevelExpressionMutation(targetNode.Body, sourceNode.ExpressionBody.Expression, sourceNode.NeedsReturn()))));
        }
        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)
            {
                if (result.Body != null && sourceNode.NeedsReturn())
                {
                    result = MutantPlacer.AddEndingReturn(result, sourceNode.ReturnType());
                }
                return(result);
            }

            if (result.Body == null)
            {
                result = MutantPlacer.ConvertExpressionToBody(result);
            }

            var newBody = context.InjectBlockLevelExpressionMutation(result.Body, sourceNode.ExpressionBody !.Expression, sourceNode.NeedsReturn());

            result = result.WithBody(SyntaxFactory.Block(newBody));
            if (sourceNode.NeedsReturn())
            {
                result = MutantPlacer.AddEndingReturn(result, sourceNode.ReturnType());
            }
            return(result);
        }
        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)));
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        /// <remarks>Injects a static marker used for coverage information; this implies converting
        /// expression arrow bodied method to regular ones.</remarks>
        protected override BaseMethodDeclarationSyntax InjectMutations(ConstructorDeclarationSyntax sourceNode,
                                                                       BaseMethodDeclarationSyntax targetNode, MutationContext context)
        {
            var mutated = base.InjectMutations(sourceNode, targetNode, context);

            if (!context.MustInjectCoverageLogic)
            {
                return(mutated);
            }
            if (mutated.ExpressionBody != null)
            {
                // we need a body to place the marker
                mutated = MutantPlacer.ConvertExpressionToBody(mutated);
            }

            return(mutated.ReplaceNode(mutated.Body !, MutantPlacer.PlaceStaticContextMarker(mutated.Body)));
        }
Exemplo n.º 6
0
        protected override AnonymousFunctionExpressionSyntax InjectMutations(AnonymousFunctionExpressionSyntax sourceNode,
                                                                             AnonymousFunctionExpressionSyntax targetNode, MutationContext context)
        {
            targetNode = base.InjectMutations(sourceNode, targetNode, context);

            if (targetNode.Block == null)
            {
                if (targetNode.ExpressionBody == null)
                {
                    // only a definition (eg interface)
                    return(targetNode);
                }

                // this is an expression body method
                if (!context.HasStatementLevelMutant)
                {
                    // there is no statement or block level mutant, so the method control flow is not changed by mutations
                    // there is no need to change the method in any may
                    return(targetNode);
                }

                // we need to convert it to expression body form
                targetNode = MutantPlacer.ConvertExpressionToBody(targetNode);

                // we need to inject pending block (and statement) level mutations
                targetNode = targetNode.WithBody(
                    SyntaxFactory.Block(context.InjectBlockLevelExpressionMutation(targetNode.Block,
                                                                                   sourceNode.ExpressionBody, true)));
            }
            else
            {
                // we add an ending return, just in case
                targetNode = MutantPlacer.AddEndingReturn(targetNode);
            }

            if (targetNode is SimpleLambdaExpressionSyntax lambdaExpression && lambdaExpression.Parameter.Modifiers.Any(m => m.Kind() == SyntaxKind.OutKeyword))
            {
                targetNode = targetNode.WithBody(MutantPlacer.AddDefaultInitializers(targetNode.Block, new List <ParameterSyntax> {
                    lambdaExpression.Parameter
                }));
            }
Exemplo n.º 7
0
        /// <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)
            {
                if (targetNode.ExpressionBody == null)
                {
                    // only a definition (eg interface)
                    return(targetNode);
                }

                // this is an expression body method
                if (!context.HasStatementLevelMutant)
                {
                    // there is no statement or block level mutant, so the method control flow is not changed by mutations
                    // there is no need to change the method in any may
                    return(targetNode);
                }

                // we need to convert it to expression body form
                targetNode = MutantPlacer.ConvertExpressionToBody(targetNode);

                // we need to inject pending block (and statement) level mutations
                targetNode = targetNode.WithBody(
                    SyntaxFactory.Block(context.InjectBlockLevelExpressionMutation(targetNode.Body,
                                                                                   sourceNode.ExpressionBody?.Expression, sourceNode.NeedsReturn())));
            }
            else
            {
                // we add an ending return, just in case
                targetNode = MutantPlacer.AddEndingReturn(targetNode);
            }

            // inject initialization to default for all out parameters
            targetNode = targetNode.WithBody(MutantPlacer.AddDefaultInitializers(targetNode.Body, sourceNode.ParameterList.Parameters.Where(p =>
                                                                                                                                            p.Modifiers.Any(m => m.Kind() == SyntaxKind.OutKeyword))));
            return(targetNode);
        }
        /// <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)));
        }