コード例 #1
0
        public void MutationsShouldHaveLinespan()
        {
            string source = @"void TestMethod()
{
    var test3 = 2 + 5;
}";

            _target.Mutate(CSharpSyntaxTree.ParseText(source).GetRoot());

            var mutants = _target.GetLatestMutantBatch().ToList();

            mutants.ShouldHaveSingleItem().Mutation.OriginalNode.GetLocation().GetLineSpan().StartLinePosition.Line.ShouldBe(2);
        }
コード例 #2
0
        public void Mutator_TestResourcesInputShouldBecomeOutputForFullScope(string inputFile, string outputFile,
            int nbMutants, int mutant1Id, int mutant1Location, int mutant2Id, int mutant2Location)
        {
            string source = File.ReadAllText(CurrentDirectory + "/Mutants/TestResources/" + inputFile);
            string expected = File.ReadAllText(CurrentDirectory + "/Mutants/TestResources/" + outputFile).Replace("StrykerNamespace", CodeInjection.HelperNamespace);
            var target = new MutantOrchestrator();
            var actualNode = target.Mutate(CSharpSyntaxTree.ParseText(source).GetRoot());
            var expectedNode = CSharpSyntaxTree.ParseText(expected).GetRoot();
            actualNode.ShouldBeSemantically(expectedNode);
            actualNode.ShouldNotContainErrors();

            var mutants = target.GetLatestMutantBatch().ToList();
            mutants.Count.ShouldBe(nbMutants);
            mutants[mutant1Id].Mutation.OriginalNode.GetLocation().GetLineSpan().StartLinePosition.Line.ShouldBe(mutant1Location);
            mutants[mutant2Id].Mutation.OriginalNode.GetLocation().GetLineSpan().StartLinePosition.Line.ShouldBe(mutant2Location);
        }
コード例 #3
0
        private IList <Mutant> GetMethodMutants(string method)
        {
            var methodSyntax = _class
                               .DescendantNodes <MethodDeclarationSyntax>()
                               .FirstOrDefault(x => x.MethodName() == method);

            if (methodSyntax != null)
            {
                var mutantOrchestrator        = new MutantOrchestrator();
                var syntaxNodeAnalysisFactory = new SyntaxNodeAnalysisFactory();
                var classDeclaration          = new ClassDeclaration(_class);
                var syntaxNodeAnalysis        = syntaxNodeAnalysisFactory.Create(methodSyntax, classDeclaration);
                mutantOrchestrator.Mutate(syntaxNodeAnalysis);
                return(mutantOrchestrator.GetLatestMutantBatch().ToList());
            }

            return(new List <Mutant>());
        }
コード例 #4
0
        public void MutationsShouldHaveLinespan()
        {
            string source   = @"void TestMethod()
{
    var test3 = 2 + 5;
}";
            string expected = @"void TestMethod()
{
    var test3 = (StrykerNamespace.MutantControl.IsActive(0)?2 - 5:2 + 5);
}";

            expected = expected.Replace("StrykerNamespace", CodeInjection.HelperNamespace);
            var actualNode   = _target.Mutate(CSharpSyntaxTree.ParseText(source).GetRoot());
            var expectedNode = CSharpSyntaxTree.ParseText(expected).GetRoot();

            actualNode.ShouldBeSemantically(expectedNode);
            actualNode.ShouldNotContainErrors();

            var mutants = _target.GetLatestMutantBatch().ToList();

            mutants.ShouldHaveSingleItem().Mutation.OriginalNode.GetLocation().GetLineSpan().StartLinePosition.Line.ShouldBe(2);
        }
コード例 #5
0
        public void Mutate()
        {
            // Mutate source files
            foreach (var file in _projectInfo.GetAllFiles().Cast <CsharpFileLeaf>())
            {
                _logger.LogDebug($"Mutating {file.FullPath}");
                // Mutate the syntax tree
                var mutatedSyntaxTree = _orchestrator.Mutate(file.SyntaxTree.GetRoot());
                // Add the mutated syntax tree for compilation
                file.MutatedSyntaxTree = mutatedSyntaxTree.SyntaxTree;
                if (_options.DevMode)
                {
                    _logger.LogTrace($"Mutated {file.FullPath}:{Environment.NewLine}{mutatedSyntaxTree.ToFullString()}");
                }
                // Filter the mutants
                var allMutants = _orchestrator.GetLatestMutantBatch();
                file.Mutants = allMutants;
            }

            _logger.LogDebug("{0} mutants created", _projectInfo.Mutants.Count());

            CompileMutations();
        }
コード例 #6
0
        public async Task InitializeMutants(IList <IMutator> selectedMutators)
        {
            if (selectedMutators == null || !selectedMutators.Any())
            {
                return;
            }

            var mutatorFinder = new MutantOrchestrator(selectedMutators);
            var id            = 1;

            foreach (var method in _source.MethodDetails)
            {
                method.TestMethods.Clear();
                method.Mutants.Clear();
                var syntaxNodeAnalysis = SyntaxNodeAnalysisFactory.Create(method.Method, _source.Claz);
                mutatorFinder.Mutate(syntaxNodeAnalysis);
                var latestMutantBatch = mutatorFinder.GetLatestMutantBatch().ToList();
                latestMutantBatch = _selector.SelectMutants(MutantsPerLine, latestMutantBatch).ToList();
                foreach (var mutant in latestMutantBatch)
                {
                    mutant.Method = method;
                }

                method.Mutants.AddRange(latestMutantBatch);

                foreach (var mutant in method.Mutants)
                {
                    mutant.Id = id++;
                }

                FilterMutants(method);

                await Task.Run(() =>
                {
                    foreach (var testMethod in _source.TestClaz.MethodDetails)
                    {
                        if (method.Coverage?.LinesCovered == 0)
                        {
                            break;
                        }

                        var sourceMethodName = method.Method.MethodName();
                        var className        = method.Method.Class().ClassName();
                        if (!ExecuteAllTests && !method.IsProperty)
                        {
                            if (testMethod.Method.ValidTestMethod(className, sourceMethodName, _source.TestClaz.Claz.Syntax))
                            {
                                method.TestMethods.Add(testMethod);
                                continue;
                            }

                            var methods = method.Method.Class().Methods();
                            if (methods != null)
                            {
                                foreach (var classMethod in methods)
                                {
                                    var methodName = classMethod.MethodName();
                                    if (classMethod.ChildMethodNames().Any(x => x.Contains(sourceMethodName)) &&
                                        testMethod.Method.ValidTestMethod(className, methodName, _source.TestClaz.Claz.Syntax))
                                    {
                                        method.TestMethods.Add(testMethod);
                                        if (!method.ParentMethodNames.Contains(methodName))
                                        {
                                            method.ParentMethodNames.Add(methodName);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            method.TestMethods.Add(testMethod);
                        }
                    }

                    if (!method.TestMethods.Any() && method.Coverage?.LinesCovered > 0)
                    {
                        method.TestMethods.AddRange(_source.TestClaz.MethodDetails);
                    }
                });

                foreach (var sourceMethod in _source.MethodDetails)
                {
                    var parentMethods = _source
                                        .MethodDetails
                                        .Where(x => sourceMethod.ParentMethodNames.Contains(x.Method.MethodName())).ToList();

                    foreach (MethodDetail parentMethod in parentMethods)
                    {
                        foreach (var testMethod in parentMethod.TestMethods)
                        {
                            if (sourceMethod.TestMethods.All(x => x.Method.MethodName() != testMethod.Method.MethodName()))
                            {
                                sourceMethod.TestMethods.Add(testMethod);
                            }
                        }
                    }
                }

                var uncoveredLines = new List <CoverageDSPriv.LinesRow>();
                var containTests   = method.TestMethods.Any();
                if (containTests)
                {
                    uncoveredLines = method
                                     .Lines
                                     .Where(x => x.Coverage > 0).ToList();
                }

                foreach (var mutant in method.Mutants)
                {
                    if (containTests &&
                        uncoveredLines.Any())
                    {
                        if (uncoveredLines.Any(x => x.LnStart == mutant.Mutation.Location))
                        {
                            mutant.ResultStatus = MutantStatus.NotCovered;
                        }
                    }

                    if (!containTests)
                    {
                        mutant.ResultStatus = MutantStatus.NotCovered;
                    }
                }
            }
        }