public void ShouldMutate(Mutator expectedKind, SyntaxKind input, params SyntaxKind[] expectedOutput) { var target = new BinaryExpressionMutator(); var originalNode = SyntaxFactory.BinaryExpression(input, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1)), SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(8))); var result = target.ApplyMutations(originalNode).ToList(); if (expectedOutput.Count() == 1) { // there should be only one mutation result.ShouldHaveSingleItem(); } else { // there should be two mutations result.Count.ShouldBe(2, "Two mutations should have been made"); } int index = 0; foreach (var mutation in result) { mutation.ReplacementNode.IsKind(expectedOutput[index]).ShouldBeTrue(); mutation.Type.ShouldBe(expectedKind); mutation.DisplayName.ShouldBe($"{mutation.Type} mutation"); index++; } }
void ShouldMutate_ExclusiveOr() { var kind = SyntaxKind.ExclusiveOrExpression; var target = new BinaryExpressionMutator(); var originalNode = SyntaxFactory.BinaryExpression(kind, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(4)), SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(2))); var result = target.ApplyMutations(originalNode).ToList(); result.Count.ShouldBe(2, "There should be two mutations"); var logicalMutation = result.SingleOrDefault(x => x.Type == Mutator.Logical); logicalMutation.ShouldNotBeNull(); logicalMutation.ReplacementNode.ShouldNotBeNull(); logicalMutation.ReplacementNode.IsKind(SyntaxKind.EqualsExpression).ShouldBeTrue(); logicalMutation.DisplayName.ShouldBe("Logical mutation"); var integralMutation = result.SingleOrDefault(x => x.Type == Mutator.Bitwise); integralMutation.ShouldNotBeNull(); integralMutation.ReplacementNode.ShouldNotBeNull(); integralMutation.ReplacementNode.IsKind(SyntaxKind.BitwiseNotExpression).ShouldBeTrue(); integralMutation.DisplayName.ShouldBe("Bitwise mutation"); var parenthesizedExpression = integralMutation.ReplacementNode.ChildNodes().SingleOrDefault(); parenthesizedExpression.ShouldNotBeNull(); parenthesizedExpression.IsKind(SyntaxKind.ParenthesizedExpression).ShouldBeTrue(); var exclusiveOrExpression = parenthesizedExpression.ChildNodes().SingleOrDefault(); exclusiveOrExpression.ShouldNotBeNull(); exclusiveOrExpression.IsKind(SyntaxKind.ExclusiveOrExpression).ShouldBeTrue(); }
public void ApplyMutations_ValidBinaryExpression_ReturnsSingleMutatedBinaryExpression(string originalCode, string expectedCode) { var mutator = new BinaryExpressionMutator(); var node = GetBinaryExpression(originalCode); var mutatedNodes = mutator.ApplyMutations(node); var mutatedCode = mutatedNodes.Single().ToFullString(); Assert.Equal(expectedCode, mutatedCode); }
public void ShouldNotMutate_StringsRight() { var target = new BinaryExpressionMutator(); var originalNode = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1)), SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(8))); var result = target.ApplyMutations(originalNode).ToList(); result.ShouldBeEmpty(); }
public void ApplyMutations_ValidBinaryExpression_ReturnsMultipleMutatedBinaryExpressions(string originalCode, string[] expectedMutatedCode) { var mutator = new BinaryExpressionMutator(); var node = GetBinaryExpression(originalCode); var mutatedNodes = mutator.ApplyMutations(node).ToArray(); Assert.Equal(expectedMutatedCode.Length, mutatedNodes.Length); for (int i = 0; i < expectedMutatedCode.Length; i++) { var mutatedCode = mutatedNodes[i].ToFullString(); Assert.Equal(expectedMutatedCode[i], mutatedCode); } }
public void MathMutator_ShouldMutate(SyntaxKind input, SyntaxKind expectedOutput, SyntaxKind optionalExpectedOutput = SyntaxKind.None) { var target = new BinaryExpressionMutator(); var originalNode = SyntaxFactory.BinaryExpression(input, SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1)), SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(8))); var result = target.ApplyMutations(originalNode).ToList(); if (optionalExpectedOutput == SyntaxKind.None) { // there should be only one mutation result.ShouldHaveSingleItem(); result.ShouldContain(x => x.ReplacementNode.IsKind(expectedOutput)); } else { // there should be two mutations result.Count.ShouldBe(2, "Two mutations should have been made"); result.ShouldContain(x => x.ReplacementNode.IsKind(expectedOutput)); result.ShouldContain(x => x.ReplacementNode.IsKind(optionalExpectedOutput)); } }
public void ShouldBeMutationLevelBasic() { var target = new BinaryExpressionMutator(); target.MutationLevel.ShouldBe(MutationLevel.Basic); }