Example #1
0
        public UnitTestMutationResult PerformMutationForUnitTestProject(
            EnvDTE.Project unitTestProject,
            SourceCodeMutationResult sourceCodeMutationResult,
            EnvDTE.Project sourceCodeProject)
        {
            var solution = _roslynSetupHelper.GetSolutionToAnalyze(
                _roslynSetupHelper.CreateWorkspace(), _currentSolution.FileName);
            var projectToAnalyze = _roslynSetupHelper.GetProjectToAnalyze(solution, unitTestProject.Name);
            var projectAssembly  = _roslynSetupHelper.GetProjectAssembly(projectToAnalyze);

            List <GeneratedMutant> generatedUnitTestMutants =
                GenerateMutantsForUnitTestProject(projectAssembly, sourceCodeMutationResult.GeneratedMutants);

            var isMutatioAnalysisCompleted = GetMutatedCompilation(generatedUnitTestMutants, unitTestProject,
                                                                   sourceCodeProject, sourceCodeMutationResult, projectToAnalyze.OutputFilePath);

            if (isMutatioAnalysisCompleted)
            {
                return(new UnitTestMutationResult()
                {
                    GeneratedUnitTestMutants = generatedUnitTestMutants,
                    OutputPath = projectToAnalyze.OutputFilePath
                });
            }
            else
            {
                throw new Exception("Error at mutation analysis step.");
            }
        }
Example #2
0
        private bool GetMutatedCompilation(
            List <GeneratedMutant> generatedUnitTestMutants,
            EnvDTE.Project unitTestProject,
            EnvDTE.Project sourceCodeproject,
            SourceCodeMutationResult sourceCodeMutationResult,
            string unitTestProjectoutputPath)
        {
            bool sourceCodeMutation   = false;
            bool unitTestCodeMutation = false;

            sourceCodeMutation =
                AddMutationAnalysisToSourceCodeProject(sourceCodeMutationResult, sourceCodeproject);

            if (sourceCodeMutation)
            {
                //MessageBox.Show("Source code project compiled after mutation.");

                unitTestCodeMutation =
                    AddMutationAnalysisToUnitTestProject(generatedUnitTestMutants, unitTestProject);

                //if (unitTestCodeMutation)
                //{
                //    MessageBox.Show("Unit test project compiled after mutation.");
                //}
            }

            if (sourceCodeMutation && unitTestCodeMutation)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private UnitTestMutationResult MutateUnitTestProject(Solution2 solution,
                                                             SourceCodeMutationResult sourceCodeMutationResult, Project unitTestProject,
                                                             Project sourceCodeProject)
        {
            var unitTestsMutator       = new UnitTestSuiteMutator(solution);
            var unitTestMutationResult =
                unitTestsMutator.PerformMutationForUnitTestProject(unitTestProject, sourceCodeMutationResult,
                                                                   sourceCodeProject);

            return(unitTestMutationResult);
        }
Example #4
0
        private bool AddMutationAnalysisToSourceCodeProject(SourceCodeMutationResult sourceCodeMutationResult,
                                                            EnvDTE.Project sourceCodeProject)
        {
            var sourceCodeUsings = sourceCodeMutationResult.Usings
                                   .GroupBy(u => u.ToFullString()).Select(iu => iu.First()).ToList();
            var unitTestUsings = Usings.GroupBy(u => u.ToFullString()).Select(iu => iu.First()).ToList();

            var sourceCodeCompilationUnit = SyntaxFactory.CompilationUnit()
                                            .WithUsings(SyntaxFactory.List(sourceCodeUsings.ToArray()))
                                            .WithMembers(
                SyntaxFactory.SingletonList <MemberDeclarationSyntax>(
                    SyntaxFactory.NamespaceDeclaration(
                        SyntaxFactory.IdentifierName("SourceCodeProjectMutants"))
                    .WithMembers(
                        SyntaxFactory.SingletonList <MemberDeclarationSyntax>(
                            SyntaxFactory.ClassDeclaration("FirstSourceCodeMutant")
                            .WithModifiers(
                                SyntaxFactory.TokenList(
                                    SyntaxFactory.Token(SyntaxKind.PublicKeyword)))))))
                                            .NormalizeWhitespace();

            var sourceCodeCompilationUnitRoot = sourceCodeCompilationUnit.SyntaxTree.GetRoot() as CompilationUnitSyntax;

            var sourceCodeMutantsNameSpace =
                sourceCodeCompilationUnitRoot.DescendantNodes().OfType <NamespaceDeclarationSyntax>().First();

            var firstSourceCodeMutant =
                sourceCodeMutantsNameSpace.DescendantNodes().OfType <ClassDeclarationSyntax>().First();

            var sourceCodeCompilationUnitSyntaxTree = sourceCodeCompilationUnitRoot
                                                      .InsertNodesAfter(firstSourceCodeMutant,
                                                                        sourceCodeMutationResult.GeneratedMutants.Select(p => p.MutatedCodeRoot));

            var sourceCodeTree = CSharpSyntaxTree.ParseText(sourceCodeCompilationUnitSyntaxTree.ToFullString());

            SourceCodeMutantsCodePath = Path.Combine(Path.GetDirectoryName(sourceCodeProject.FullName),
                                                     "SourceCodeMutants.cs");
            File.WriteAllText(SourceCodeMutantsCodePath, sourceCodeTree.GetRoot().ToFullString());

            //add source code mutation analysis to sc project
            sourceCodeProject.ProjectItems.AddFromFile(SourceCodeMutantsCodePath);
            sourceCodeProject.Save();

            //recompile the source code project
            SolutionBuild2 sourceCodeSolutionBuild2 = (SolutionBuild2)sourceCodeProject.DTE.Solution.SolutionBuild;

            sourceCodeSolutionBuild2.BuildProject(sourceCodeSolutionBuild2.ActiveConfiguration.Name,
                                                  sourceCodeProject.UniqueName, true);

            bool sourceCodeCompiledOK = (sourceCodeSolutionBuild2.LastBuildInfo == 0);

            return(sourceCodeCompiledOK);
        }