Esempio n. 1
0
        public void GetMutantSource_Gets_Partial_Line()
        {
            // Arrange
            var path = $"TestResources{Path.DirectorySeparatorChar}ExampleSourceFile.cs";

            var file = new FileInfo(path);

            var source = File.ReadAllText(file.FullName);

            var jsonMutant = new JsonMutant
            {
                Location = new JsonMutantLocation(new JsonMutantPosition
                {
                    Column = 30,
                    Line   = 34
                },
                                                  new JsonMutantPosition
                {
                    Column = 34,
                    Line   = 34
                }),
            };

            var target = new BaselineMutantHelper();

            // Act
            var result = target.GetMutantSourceCode(source, jsonMutant);

            // Assert
            result.ShouldBe("\"\\n\"");
        }
Esempio n. 2
0
        public void GetMutantSourceShouldReturnMutantSource_When_Multiple_Lines()
        {
            // Arrange
            var path = $"TestResources{Path.DirectorySeparatorChar}ExampleSourceFile.cs";

            var file = new FileInfo(path);

            var source = File.ReadAllText(file.FullName);

            var jsonMutant = new JsonMutant
            {
                Location = new JsonMutantLocation(new JsonMutantPosition
                {
                    Column = 13,
                    Line   = 24
                },
                                                  new JsonMutantPosition
                {
                    Column = 38,
                    Line   = 26
                }),
            };

            var target = new BaselineMutantHelper();

            // Act
            var result = target.GetMutantSourceCode(source, jsonMutant);

            // Assert
            result.ShouldBe(@"return @""Lorem Ipsum
                    Dolor Sit Amet
                    Lorem Dolor Sit"";");
        }
Esempio n. 3
0
        public void GetMutantSourceShouldReturnMutantSource()
        {
            // Arrange
            var path = $"TestResources{Path.DirectorySeparatorChar}ExampleSourceFile.cs";

            var file = new FileInfo(path);

            var source = File.ReadAllText(file.FullName);

            var jsonMutant = new JsonMutant
            {
                Location = new JsonMutantLocation(new JsonMutantPosition
                {
                    Column = 17,
                    Line   = 17
                },
                                                  new JsonMutantPosition
                {
                    Column = 62,
                    Line   = 17
                }),
            };

            var target = new BaselineMutantHelper();

            // Act
            var result = target.GetMutantSourceCode(source, jsonMutant);

            // Assert
            result.ShouldBe("return Fibonacci(b, a + b, counter + 1, len);");
        }
        public string GetMutantSourceCode(string source, JsonMutant baselineMutant)
        {
            SyntaxTree tree = CSharpSyntaxTree.ParseText(source);

            var beginLinePosition = new LinePosition(baselineMutant.Location.Start.Line - 1, baselineMutant.Location.Start.Column - 1);
            var endLinePosition   = new LinePosition(baselineMutant.Location.End.Line - 1, baselineMutant.Location.End.Column - 1);

            var span = new LinePositionSpan(beginLinePosition, endLinePosition);

            var textSpan     = tree.GetText().Lines.GetTextSpan(span);
            var originalNode = tree.GetRoot().DescendantNodes(textSpan).FirstOrDefault(n => textSpan.Equals(n.Span));

            return(originalNode?.ToString());
        }
Esempio n. 5
0
        public void GetMutantSourceShouldReturnMutantSource_When_Multiple_Lines()
        {
            // Arrange
            var path = $"TestResources{Path.DirectorySeparatorChar}ExampleSourceFile.cs";

            var file = new FileInfo(path);

            var source = File.ReadAllText(file.FullName);

            var diffProviderMock = new Mock <IDiffProvider>(MockBehavior.Loose);

            var gitInfoProvider = new Mock <IGitInfoProvider>(MockBehavior.Loose);

            var baselineProvider = new Mock <IBaselineProvider>(MockBehavior.Loose);

            var jsonMutant = new JsonMutant
            {
                Location = new JsonMutantLocation(new JsonMutantPosition
                {
                    Column = 13,
                    Line   = 24
                },
                                                  new JsonMutantPosition
                {
                    Column = 38,
                    Line   = 26
                }),
            };

            var target = new DiffMutantFilter(new StrykerOptions(diff: false), diffProviderMock.Object, baselineProvider.Object, gitInfoProvider.Object);

            // Act
            var result = target.GetMutantSourceCode(source, jsonMutant);

            // Assert
            result.ShouldBe(@"return @""Lorem Ipsum
                    Dolor Sit Amet
                    Lorem Dolor Sit"";");
        }
Esempio n. 6
0
        public void GetMutantSourceShouldReturnMutantSource()
        {
            // Arrange
            var path = $"TestResources{Path.DirectorySeparatorChar}ExampleSourceFile.cs";

            var file = new FileInfo(path);

            var source = File.ReadAllText(file.FullName);

            var diffProviderMock = new Mock <IDiffProvider>(MockBehavior.Loose);

            var gitInfoProvider = new Mock <IGitInfoProvider>(MockBehavior.Loose);

            var baselineProvider = new Mock <IBaselineProvider>(MockBehavior.Loose);

            var jsonMutant = new JsonMutant
            {
                Location = new JsonMutantLocation(new JsonMutantPosition
                {
                    Column = 17,
                    Line   = 17
                },
                                                  new JsonMutantPosition
                {
                    Column = 62,
                    Line   = 17
                }),
            };

            var target = new DiffMutantFilter(new StrykerOptions(diff: false), diffProviderMock.Object, baselineProvider.Object, gitInfoProvider.Object);

            // Act
            var result = target.GetMutantSourceCode(source, jsonMutant);

            // Assert
            result.ShouldBe("return Fibonacci(b, a + b, counter + 1, len);");
        }
 public IEnumerable <Mutant> GetMutantMatchingSourceCode(IEnumerable <Mutant> mutants, JsonMutant baselineMutant, string baselineMutantSourceCode)
 {
     return(mutants.Where(x =>
                          x.Mutation.OriginalNode.ToString() == baselineMutantSourceCode &&
                          x.Mutation.DisplayName == baselineMutant.MutatorName));
 }
Esempio n. 8
0
 private void UpdateMutantStatusWithBaseline(JsonMutant baselineMutant, Mutant matchingMutants)
 {
     matchingMutants.ResultStatus       = (MutantStatus)Enum.Parse(typeof(MutantStatus), baselineMutant.Status);
     matchingMutants.ResultStatusReason = "Result based on previous run.";
 }
Esempio n. 9
0
            public static JsonReportComponent FromProjectComponent(IReadOnlyInputComponent component, ThresholdOptions thresholdOptions)
            {
                var report = new JsonReportComponent
                {
                    Name            = component.Name,
                    DetectedMutants = component.DetectedMutants.Count(),
                    TotalMutants    = component.TotalMutants.Count(),
                    KilledMutants   = component.ReadOnlyMutants.Where(m => m.ResultStatus == MutantStatus.Killed).Count(),
                    SurvivedMutants = component.ReadOnlyMutants.Where(m => m.ResultStatus == MutantStatus.Survived).Count(),
                    SkippedMutants  = component.ReadOnlyMutants.Where(m => m.ResultStatus == MutantStatus.Skipped).Count(),
                    TimeoutMutants  = component.ReadOnlyMutants.Where(m => m.ResultStatus == MutantStatus.Timeout).Count(),
                    CompileErrors   = component.ReadOnlyMutants.Where(m => m.ResultStatus == MutantStatus.BuildError).Count(),
                    MutationScore   = component.GetMutationScore(),
                    ThresholdHigh   = thresholdOptions.ThresholdHigh,
                    ThresholdLow    = thresholdOptions.ThresholdLow,
                    ThresholdBreak  = thresholdOptions.ThresholdBreak,
                };

                report.PossibleMutants = report.TotalMutants + report.SkippedMutants;

                if (report.MutationScore >= report.ThresholdHigh)
                {
                    report.Health = "ok";
                }
                else if (report.MutationScore <= report.ThresholdBreak)
                {
                    report.Health = "danger";
                }
                else
                {
                    report.Health = "warning";
                }

                if (component is FolderComposite folderComponent)
                {
                    foreach (var child in folderComponent.Children)
                    {
                        report.ChildResults.Add(FromProjectComponent(child, thresholdOptions));
                    }
                }
                else if (component is FileLeaf fileComponent)
                {
                    report.Source = fileComponent.SourceCode;
                    foreach (var mutant in fileComponent.Mutants)
                    {
                        var jsonMutant = new JsonMutant
                        {
                            Id          = mutant.Id,
                            MutatorName = mutant.Mutation.DisplayName,
                            Replacement = mutant.Mutation.ReplacementNode.ToFullString(),
                            Span        = new[] { mutant.Mutation.OriginalNode.SpanStart, mutant.Mutation.OriginalNode.Span.End },
                            Status      = StrykerStatusToMutationStatus(mutant.ResultStatus)
                        };

                        report.Mutants.Add(jsonMutant);
                    }
                }
                else
                {
                    throw new System.Exception("Unknown IReadOnlyInputComponent implementation");
                }

                return(report);
            }