Exemplo n.º 1
0
        private bool AddMutationAnalysisToUnitTestProject(List <GeneratedMutant> generatedUnitTestMutants,
                                                          EnvDTE.Project unitTestProject)
        {
            var selfUsing = SyntaxFactory.UsingDirective(
                SyntaxFactory.IdentifierName("SourceCodeProjectMutants"));

            selfUsing = selfUsing.WithUsingKeyword(
                selfUsing.UsingKeyword.WithTrailingTrivia(
                    SyntaxFactory.Whitespace(" ")));
            Usings.Add(selfUsing);

            var unitTestCompilationUnit = SyntaxFactory.CompilationUnit()
                                          .WithUsings(SyntaxFactory.List(Usings.ToArray()))
                                          .WithMembers(
                SyntaxFactory.SingletonList <MemberDeclarationSyntax>(
                    SyntaxFactory.NamespaceDeclaration(
                        SyntaxFactory.IdentifierName("UnitTestProjectMutants"))
                    .WithMembers(
                        SyntaxFactory.SingletonList <MemberDeclarationSyntax>(
                            SyntaxFactory.ClassDeclaration("FirstUnitTestMutant")
                            .WithModifiers(
                                SyntaxFactory.TokenList(
                                    SyntaxFactory.Token(SyntaxKind.PublicKeyword)))))))
                                          .NormalizeWhitespace();

            var unitTestCompilationUnitRoot = unitTestCompilationUnit.SyntaxTree.GetRoot() as CompilationUnitSyntax;

            var unitTestMutantsNameSpace =
                unitTestCompilationUnitRoot.DescendantNodes().OfType <NamespaceDeclarationSyntax>().First();

            var firstUnitTestMutant =
                unitTestMutantsNameSpace.DescendantNodes().OfType <ClassDeclarationSyntax>().First();

            var unitTestCompilationUnitSyntaxTree = unitTestCompilationUnitRoot
                                                    .InsertNodesAfter(firstUnitTestMutant,
                                                                      generatedUnitTestMutants.Select(p => p.MutatedCodeRoot));

            var unitTestTree = CSharpSyntaxTree.ParseText(unitTestCompilationUnitSyntaxTree.ToFullString());

            UnitTestsMutantsCodePath = Path.Combine(Path.GetDirectoryName(unitTestProject.FullName),
                                                    "UnitTestMutants.cs");
            File.WriteAllText(UnitTestsMutantsCodePath, unitTestTree.GetRoot().ToFullString());

            unitTestProject.ProjectItems.AddFromFile(UnitTestsMutantsCodePath);
            unitTestProject.Save();

            SolutionBuild2 solutionBuild2 = (SolutionBuild2)unitTestProject.DTE.Solution.SolutionBuild;

            solutionBuild2.BuildProject(solutionBuild2.ActiveConfiguration.Name,
                                        unitTestProject.UniqueName, true);
            bool unitTestCompiledOK = (solutionBuild2.LastBuildInfo == 0);

            return(unitTestCompiledOK);
        }
Exemplo n.º 2
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);
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var dte = GetActiveIDE();

            //dte.ExecuteCommand("CloseAll");

            if (unitTestProject == null && sourceCodeProject == null)
            {
                var projects = new List <ProjectPresentation>();
                foreach (var item in solutionProjectList.SelectedItems)
                {
                    var selectedProject = (ProjectPresentation)item;
                    selectedProject.IsSelected = true;
                    projects.Add(selectedProject);
                }
                sourceCodeProject = GetSourceCodeProject(projects);
                unitTestProject   = GetUnitTestProject(projects);
            }

            if (unitTestProject != null && sourceCodeProject != null)
            {
                //delete the mutated source code class
                foreach (ProjectItem item in sourceCodeProject.ProjectItems)
                {
                    var itemName = item.Name;
                    if (itemName == "SourceCodeMutants.cs")
                    {
                        item.Delete();
                        break;
                    }
                }
                sourceCodeProject.Save();

                //delete mutated unit test project
                foreach (ProjectItem item in unitTestProject.ProjectItems)
                {
                    var itemName = item.Name;
                    if (itemName == "UnitTestMutants.cs")
                    {
                        item.Delete();
                    }

                    //delete mutation analysis folder
                    if (itemName == "MutationAnalysis")
                    {
                        MutationAnalysisNumber = 0;
                        item.Delete();
                    }
                }
                unitTestProject.Save();

                //recompile projects
                SolutionBuild2 solutionBuild2 = (SolutionBuild2)unitTestProject.DTE.Solution.SolutionBuild;
                solutionBuild2.BuildProject(solutionBuild2.ActiveConfiguration.Name,
                                            unitTestProject.UniqueName, true);
                bool unitTestCompiledOK = (solutionBuild2.LastBuildInfo == 0);

                SolutionBuild2 solutionBuild = (SolutionBuild2)sourceCodeProject.DTE.Solution.SolutionBuild;
                solutionBuild.BuildProject(solutionBuild.ActiveConfiguration.Name,
                                           sourceCodeProject.UniqueName, true);
                bool sourceCodeCompiledOK = (solutionBuild.LastBuildInfo == 0);

                if (unitTestCompiledOK && sourceCodeCompiledOK)
                {
                    MessageBox.Show("MTOOS deletion done! No Errors!");
                }
                else
                {
                    MessageBox.Show("Error while MTOOS deletion!");
                }
            }
            else
            {
                MessageBox.Show("Select the source code and the unit test project!");
            }
        }