private void CreateReplacer(PostfixUnaryExpressionSyntax node, PostfixUnaryExpressionSyntax newNode)
 {
     Replacers.Add(new MutationDocumentDetails(
                       node,
                       newNode,
                       GetWhere(node),
                       CreateCategory(node.Kind().ToString())));
 }
Beispiel #2
0
        public IList <MutationDocument> GetMutatedDocument(SyntaxNode root, Document document)
        {
            Replacers.Clear();
            Visit(root);

            return(Replacers
                   .Where(r => !IsExcludedFromCodeCoverage(r.Location, r.Original))
                   .Select(r => new MutationDocument(document, r)).ToList());
        }
Beispiel #3
0
        public override SyntaxNode VisitReturnStatement(ReturnStatementSyntax node)
        {
            if (!node.DescendantNodes().Any())
            {
                return(base.VisitReturnStatement(node));
            }

            if (node.DescendantNodes().First() is LiteralExpressionSyntax literlaExpression)
            {
                SyntaxNode newNode = null;

                if (literlaExpression.IsKind(SyntaxKind.TrueLiteralExpression))
                {
                    newNode = node.ReplaceNode(literlaExpression, SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression, SyntaxFactory.Token(SyntaxKind.FalseKeyword)));
                }

                if (literlaExpression.IsKind(SyntaxKind.FalseLiteralExpression))
                {
                    newNode = node.ReplaceNode(literlaExpression, SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)));
                }

                if (literlaExpression.IsKind(SyntaxKind.NullLiteralExpression))
                {
                    newNode = node.Parent.ReplaceNode(node.Parent, SyntaxFactory.ThrowStatement(SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName($"System.{typeof(Exception).Name}")).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal("Mmmmutation"))))))));
                }

                if (literlaExpression.IsKind(SyntaxKind.StringLiteralExpression))
                {
                    newNode = node.ReplaceNode(literlaExpression, SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal("Mutation")));
                }

                if (literlaExpression.IsKind(SyntaxKind.NumericLiteralExpression))
                {
                    if (double.TryParse(literlaExpression.Token.Value.ToString(), out var value))
                    {
                        value   = value == 0 ? 1 : 0;
                        newNode = node.ReplaceNode(literlaExpression, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(value)));
                    }
                }

                if (newNode != null)
                {
                    newNode = newNode.NormalizeWhitespace();
                    Replacers.Add(new MutationDocumentDetails(node, newNode, GetWhere(node)));
                }
            }

            var objectCreationExpressions = node.DescendantNodes().OfType <ObjectCreationExpressionSyntax>().ToList();

            foreach (var objectCreationExpressionSyntax in objectCreationExpressions)
            {
                var newNode = node.ReplaceNode(objectCreationExpressionSyntax, SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));
                Replacers.Add(new MutationDocumentDetails(node, newNode, GetWhere(node)));
            }

            return(base.VisitReturnStatement(node));
        }
        public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
        {
            Replacers.Add(new MutationDocumentDetails(
                              node,
                              node.WithLeadingTrivia(TriviaList(Comment("/*"))).WithTrailingTrivia(TriviaList(Comment("*/"))),
                              GetWhere(node),
                              CreateCategory("InvocationExpression")));

            return(base.VisitExpressionStatement(node));
        }
        public override SyntaxNode VisitBinaryExpression(BinaryExpressionSyntax node)
        {
            if (node.IsKind(SyntaxKind.IsExpression))
            {
                var newNode = SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, SyntaxFactory.ParenthesizedExpression(node)).NormalizeWhitespace();
                Replacers.Add(new MutationDocumentDetails(node, newNode, GetWhere(node)));
            }

            return(base.VisitBinaryExpression(node));
        }
Beispiel #6
0
        public override SyntaxNode VisitBinaryExpression(BinaryExpressionSyntax node)
        {
            var operatorKind     = node.OperatorToken.Kind();
            var replacementTable = GetReplacementTable();

            if (replacementTable.ContainsKey(operatorKind))
            {
                var newNode = node.ReplaceToken(node.OperatorToken, SyntaxFactory.Token(replacementTable[operatorKind])).NormalizeWhitespace();
                Replacers.Add(new MutationDocumentDetails(node, newNode, GetWhere(node)));
            }

            return(base.VisitBinaryExpression(node));
        }
        public override SyntaxNode VisitIfStatement(IfStatementSyntax node)
        {
            if (node.Condition is InvocationExpressionSyntax)
            {
                var newNode = SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, SyntaxFactory.ParenthesizedExpression(node.Condition)).NormalizeWhitespace();
                Replacers.Add(new MutationDocumentDetails(node.Condition, newNode, GetWhere(node.Condition)));
            }

            if (node.Condition is PrefixUnaryExpressionSyntax)
            {
                var condition = node.Condition as PrefixUnaryExpressionSyntax;
                Replacers.Add(new MutationDocumentDetails(node.Condition, condition.Operand, GetWhere(condition.Operand)));
            }

            return(base.VisitIfStatement(node));
        }
        private string Munge()
        {
            // if we process multiple words, add a space as padding
            // if we process a single word, no padding
            // and, yeah, this will only work on space-padded multi-words. c'est la vie.
            var words   = new TextTokenizer(Granularity.Word, Source).Tokens;
            var padding = (words.Any()) ? " " : string.Empty;

            var sb  = new StringBuilder();
            var rnd = new Random();

            // forget about mixed-caps -- too difficult to replicate with words of different lengths
            // although, if identical lengths, could make a go at it
            // but... not worth it?
            // TODO: look into using a regex for all of this. faster?
            // hunh. who knows. time it.

            // if word is in dictionary
            // replace it with replacement
            // if multiple replacements, select at random
            foreach (var word in words)
            {
                var replace = word;

                // so far, files are lowercase
                // if not, we will have to change code
                if (Replacers.ContainsKey(word.ToLower()))
                {
                    var index = rnd.Next(0, Replacers[word.ToLower()].Count); // random.next range := 0..(Count-1)
                    replace = Replacers[word.ToLower()][index];

                    if (AllCaps(word))
                    {
                        replace = replace.ToUpper();
                    }
                    else if (InitialCap(word))
                    {
                        var first = replace[0].ToString().ToUpper();
                        replace = first + replace.Substring(1);
                    }
                }
                sb.Append(replace + padding);
            }

            return(sb.ToString());
        }
Beispiel #9
0
        public override SyntaxNode VisitBinaryExpression(BinaryExpressionSyntax node)
        {
            var operatorKind = node.OperatorToken.Kind();

            if (operatorKind == SyntaxKind.PlusToken)
            {
                var left  = node.Left;
                var right = node.Right;

                if (!left.IsKind(SyntaxKind.StringLiteralExpression) && !right.IsKind(SyntaxKind.StringLiteralExpression))
                {
                    var newNode = node.ReplaceToken(node.OperatorToken, SyntaxFactory.Token(SyntaxKind.MinusToken)).NormalizeWhitespace();
                    Replacers.Add(new MutationDocumentDetails(node, newNode, GetWhere(node)));
                }
            }

            return(base.VisitBinaryExpression(node));
        }
 public void RegisterReplacer(Type replacerType, string replacerName)
 {
     Replacers.Add(replacerType, replacerName);
 }
Beispiel #11
0
 public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
 {
     Replacers.Add(new MutationDocumentDetails(node, SyntaxFactory.EmptyStatement(), GetWhere(node)));
     return(base.VisitExpressionStatement(node));
 }
Beispiel #12
0
 private void CreateReplacer(PostfixUnaryExpressionSyntax node, PostfixUnaryExpressionSyntax newNode)
 {
     Replacers.Add(new MutationDocumentDetails(node, newNode, GetWhere(node)));
 }