예제 #1
0
        public async Task <MutationResult> ExecuteMutation(Mutant mutant, CciModuleSource moduleSource)
        {
            var type   = new TypeIdentifier((INamedTypeDefinition)mutant.MutationTarget.ProcessingContext.Type.Object);
            var method = new MethodIdentifier((IMethodDefinition)mutant.MutationTarget.ProcessingContext.Method.Object);
            var filter = new MutationFilter(type.InList(), method.InList());

            _log.Debug("ExecuteMutation of: " + type + " - " + method);
            IMutationOperator mutationOperator = mutant.MutationTarget.OperatorId == null ? new IdentityOperator() :
                                                 _mutOperators.Single(m => mutant.MutationTarget.OperatorId == m.Info.Id);
            var cci = moduleSource;

            try
            {
                _log.Info("Execute mutation of " + mutant.MutationTarget + " contained in " + mutant.MutationTarget.MethodRaw + " modules. ");
                var mutatedModules = new List <IModuleInfo>();
                var module         = moduleSource.Modules.Single();

                var visitorBack = new VisualCodeVisitorBack(mutant.MutationTarget.InList(),
                                                            _sharedTargets.GetValues(mutationOperator, returnEmptySet: true),
                                                            module.Module, mutationOperator.Info.Id);
                var traverser2 = new VisualCodeTraverser(_filter, visitorBack, moduleSource);
                traverser2.Traverse(module.Module);
                visitorBack.PostProcess();
                var operatorCodeRewriter = mutationOperator.CreateRewriter();

                var rewriter = new VisualCodeRewriter(cci.Host, visitorBack.TargetAstObjects,
                                                      visitorBack.SharedAstObjects, _filter, operatorCodeRewriter, traverser2);

                operatorCodeRewriter.MutationTarget =
                    new UserMutationTarget(mutant.MutationTarget.Variant.Signature, mutant.MutationTarget.Variant.AstObjects);


                operatorCodeRewriter.NameTable     = cci.Host.NameTable;
                operatorCodeRewriter.Host          = cci.Host;
                operatorCodeRewriter.Module        = module.Module;
                operatorCodeRewriter.OperatorUtils = _operatorUtils;
                operatorCodeRewriter.Initialize();

                var rewrittenModule = rewriter.Rewrite(module.Module);

                rewriter.CheckForUnfoundObjects();

                mutant.MutationTarget.Variant.AstObjects = null; //TODO: avoiding leaking memory. refactor
                mutatedModules.Add(new ModuleInfo(rewrittenModule, ""));

                var result = new MutationResult(mutant, cci, null,
                                                mutant.MutationTarget.MethodMutated);
                mutant.MutationTarget.MethodMutated = null; //TODO: avoiding leaking memory. refactor
                return(result);
            }
            catch (Exception e)
            {
                throw new MutationException("CreateMutants failed on operator: {0}.".Formatted(mutationOperator.Info.Name), e);
            }
        }
예제 #2
0
        public void IntegrationTestingMiscUtilLight()
        {
            string filePath      = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath), @"..\..\..\..\Tests\SampleTestProjectsForTestDiscoveryAndExecution\SampleLogicNunit3Tests\bin\SampleLogic.dll"));
            string filePathTests = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath), @"..\..\..\..\Tests\SampleTestProjectsForTestDiscoveryAndExecution\SampleLogicNunit3Tests\bin\SampleLogicNunit3Tests.dll"));

            var cci     = new CciModuleSource(filePath);
            var choices = new MutationSessionChoices();
            var tt      = cci.Module.Module.GetAllTypes();
            //  var type = (NamedTypeDefinition)cci.Module.Module.GetAllTypes().Single(t => t.Name.Value == "Range");
            // var method = new MethodIdentifier(type.Methods.First(m => m.Name.Value == "Contains"));
            MethodIdentifier method = null;

            choices.SelectedOperators.Add(new IdentityOperator2());
            choices.Filter = method == null?MutationFilter.AllowAll() :
                                 new MutationFilter(
                                     new List <TypeIdentifier>(),
                                     method.InList());

            var options = new OptionsModel();
            var muexe   = new MutationExecutor(options, choices, null);
            var mucon   = new MutantsContainer(muexe, new OriginalCodebase(cci.InList()), new OptionsModel());
            var nodes   = mucon.InitMutantsForOperators(ProgressCounter.Inactive());

            Mutant mutant = nodes.Cast <CheckedNode>()
                            .SelectManyRecursive(n => n.Children ?? new NotifyingCollection <CheckedNode>())
                            .OfType <Mutant>().First();//.Take(4);

            var cciWhite = new CciModuleSource(filePath);
            var resu     = muexe.ExecuteMutation(mutant, cciWhite).Result;

            using (var file = File.OpenWrite(filePath))
            {
                resu.MutatedModules.WriteToStream(resu.MutatedModules.Modules.Single(), file, filePath);
            }

            var runContext = new NUnitTestsRunContext(
                options,
                _kernel.Get <IProcesses>(),
                _kernel.Get <CommonServices>(),
                new NUnitResultsParser(),
                TestProjects.NUnitConsolePath,
                filePathTests,
                new TestsSelector());

            var testResult = runContext.RunTests().Result;

            var count = testResult.ResultMethods
                        .GroupBy(t => t.State)
                        .ToDictionary(t => t.Key);
            var countStrings = count.Select(pair => pair.Key.ToString() + ": " + pair.Value.Count());

            _log.Info(string.Format("All test results: " + string.Join(" ", countStrings)));
            count[TestNodeState.Failure].Count().ShouldEqual(0);
        }
예제 #3
0
        public async Task<MutationResult> ExecuteMutation(Mutant mutant, CciModuleSource moduleSource)
        {
            var type = new TypeIdentifier((INamedTypeDefinition) mutant.MutationTarget.ProcessingContext.Type.Object);
            var method = new MethodIdentifier((IMethodDefinition) mutant.MutationTarget.ProcessingContext.Method.Object);
            var filter = new MutationFilter(type.InList(), method.InList());

            _log.Debug("ExecuteMutation of: " + type+" - " +method );
            IMutationOperator mutationOperator = mutant.MutationTarget.OperatorId == null ? new IdentityOperator() :
                _mutOperators.Single(m => mutant.MutationTarget.OperatorId == m.Info.Id);
            var cci = moduleSource;
            try
            {
                _log.Info("Execute mutation of " + mutant.MutationTarget + " contained in " + mutant.MutationTarget.MethodRaw + " modules. ");
                var mutatedModules = new List<IModuleInfo>();
                var module = moduleSource.Modules.Single();



                var visitorBack = new VisualCodeVisitorBack(mutant.MutationTarget.InList(),
                    _sharedTargets.GetValues(mutationOperator, returnEmptySet: true),
                    module.Module, mutationOperator.Info.Id);

                var traverser2 = new VisualCodeTraverser(_filter, visitorBack, moduleSource);

                traverser2.Traverse(module.Module);

                visitorBack.PostProcess();

                var operatorCodeRewriter = mutationOperator.CreateRewriter();

                var rewriter = new VisualCodeRewriter(cci.Host, visitorBack.TargetAstObjects,
                    visitorBack.SharedAstObjects, _filter, operatorCodeRewriter, traverser2);

                operatorCodeRewriter.MutationTarget =
                    new UserMutationTarget(mutant.MutationTarget.Variant.Signature, mutant.MutationTarget.Variant.AstObjects);

                operatorCodeRewriter.NameTable = cci.Host.NameTable;
                operatorCodeRewriter.Host = cci.Host;
                operatorCodeRewriter.Module = module.Module;
                operatorCodeRewriter.OperatorUtils = _operatorUtils;
                operatorCodeRewriter.Initialize();

                var rewrittenModule = rewriter.Rewrite(module.Module);

                rewriter.CheckForUnfoundObjects();

                mutant.MutationTarget.Variant.AstObjects = null; //TODO: avoiding leaking memory. refactor
                mutatedModules.Add(new ModuleInfo(rewrittenModule, ""));

                List<ICciModuleSource> cci1 = new List<ICciModuleSource>();
                cci1.Add(cci);

                List<IMethodDefinition> methodMutated = new List<IMethodDefinition>();
                methodMutated.Add(mutant.MutationTarget.MethodMutated);

                var result = new MutationResult(mutant, cci/*1*/, null, mutant.MutationTarget.MethodMutated);
                mutant.MutationTarget.MethodMutated = null; //TODO: avoiding leaking memory. refactor

                return result;
            }
            catch (Exception e)
            {
                throw new MutationException("CreateMutants failed on operator: {0}.".Formatted(mutationOperator.Info.Name), e);
            }
        }
예제 #4
0
        private IEnumerable <Mutant> SetupMutations(OriginalCodebase original,
                                                    List <FilePathAbsolute> paths, List <string> toMutate,
                                                    List <IMutationOperator> operators, MethodIdentifier method = null)
        {
            var options = new OptionsModel();

            options.OtherParams = "--debugfiles true";

            _kernel.Bind <IProjectClonesManager>().To <ProjectClonesManager>().InSingletonScope();
            _kernel.Bind <ProjectFilesClone>().ToSelf().AndFromFactory();
            _kernel.Bind <FilesManager>().ToSelf().InSingletonScope();
            _kernel.Bind <TestServiceManager>().ToSelf().InSingletonScope();
            _kernel.Bind <XUnitTestService>().ToSelf().InSingletonScope();
            _kernel.Bind <XUnitResultsParser>().ToSelf().InSingletonScope();
            _kernel.Bind <WhiteCache>().ToSelf().AndFromFactory();

            _kernel.Bind <INUnitWrapper>().To <NUnitWrapper>().InSingletonScope();
            _kernel.Bind <OriginalCodebase>().ToConstant(original);
            _kernel.Bind <ICodeDifferenceCreator>().To <CodeDifferenceCreator>().InSingletonScope();
            _kernel.Bind <ICodeVisualizer>().To <CodeVisualizer>().InSingletonScope();
            _kernel.Bind <IMutantsCache>().To <MutantsCache>().InSingletonScope();
            _kernel.Bind <NUnitTestsRunContext>().ToSelf().AndFromFactory();
            _kernel.Bind <XUnitTestsRunContext>().ToSelf().AndFromFactory();
            _kernel.Bind <OptionsModel>().ToConstant(options);
            _kernel.Bind <IMutationExecutor>().To <MutationExecutor>().InSingletonScope();
            _kernel.Bind <TestingMutant>().ToSelf().AndFromFactory();
            _kernel.Bind <TestLoader>().ToSelf().AndFromFactory();

            _kernel.BindMock <IHostEnviromentConnection>(mock =>
            {
                mock.Setup(_ => _.GetProjectAssemblyPaths()).Returns(paths);
                mock.Setup(_ => _.GetTempPath()).Returns(Path.GetTempPath());
            });

            _kernel.Get <ISettingsManager>()["NUnitConsoleDirPath"] = TestProjects.NUnitConsoleDirPath;
            _kernel.Get <ISettingsManager>()["XUnitConsoleDirPath"] = TestProjects.XUnitConsoleDirPath;

            _kernel.Bind <IWhiteSource>().ToConstant(new WhiteDummy(toMutate));

            var testsClone = _kernel.Get <IProjectClonesManager>().CreateClone("Tests");
            var testsTask  = _kernel.Get <TestsLoader>().LoadTests(testsClone.Assemblies.AsStrings().ToList());

            var strategy = new AllTestsSelectStrategy(testsTask);

            var choices = new MutationSessionChoices
            {
                Filter = method == null?MutationFilter.AllowAll() :
                             new MutationFilter(
                                 new List <TypeIdentifier>(),
                                 method.InList()),
                             SelectedOperators = operators,
                             TestAssemblies    = strategy.SelectTests(null).Result
            };

            _kernel.Bind <MutationSessionChoices>().ToConstant(choices);

            var exec      = _kernel.Get <MutationExecutor>();
            var container = new MutantsContainer(exec, original);
            IList <AssemblyNode> assemblies = container.InitMutantsForOperators(ProgressCounter.Inactive());

            var mutants = assemblies.Cast <CheckedNode>()
                          .SelectManyRecursive(n => n.Children ?? new NotifyingCollection <CheckedNode>())
                          .OfType <Mutant>();//.Take(4);

            return(mutants);
        }
예제 #5
0
        private IEnumerable<Mutant> SetupMutations(OriginalCodebase original, 
            List<FilePathAbsolute> paths, List<string> toMutate, 
            List<IMutationOperator> operators, MethodIdentifier method= null)
        {

            var options = new OptionsModel();
            options.OtherParams = "--debugfiles true";

            _kernel.Bind<IProjectClonesManager>().To<ProjectClonesManager>().InSingletonScope();
            _kernel.Bind<ProjectFilesClone>().ToSelf().AndFromFactory();
            _kernel.Bind<FilesManager>().ToSelf().InSingletonScope();
            _kernel.Bind<TestServiceManager>().ToSelf().InSingletonScope();
            _kernel.Bind<XUnitTestService>().ToSelf().InSingletonScope();
            _kernel.Bind<XUnitResultsParser>().ToSelf().InSingletonScope();
            _kernel.Bind<WhiteCache>().ToSelf().AndFromFactory();

            _kernel.Bind<INUnitWrapper>().To<NUnitWrapper>().InSingletonScope();
            _kernel.Bind<OriginalCodebase>().ToConstant(original);
            _kernel.Bind<ICodeDifferenceCreator>().To<CodeDifferenceCreator>().InSingletonScope();
            _kernel.Bind<ICodeVisualizer>().To<CodeVisualizer>().InSingletonScope();
            _kernel.Bind<IMutantsCache>().To<MutantsCache>().InSingletonScope();
            _kernel.Bind<NUnitTestsRunContext>().ToSelf().AndFromFactory();
            _kernel.Bind<XUnitTestsRunContext>().ToSelf().AndFromFactory();
            _kernel.Bind<OptionsModel>().ToConstant(options);
            _kernel.Bind<IMutationExecutor>().To<MutationExecutor>().InSingletonScope();
            _kernel.Bind<TestingMutant>().ToSelf().AndFromFactory();
            _kernel.Bind<TestLoader>().ToSelf().AndFromFactory();

            _kernel.BindMock<IHostEnviromentConnection>(mock =>
            {
                mock.Setup(_ => _.GetProjectAssemblyPaths()).Returns(paths);
                mock.Setup(_ => _.GetTempPath()).Returns(Path.GetTempPath());
            });

            _kernel.Get<ISettingsManager>()["NUnitConsoleDirPath"] = TestProjects.NUnitConsoleDirPath;
            _kernel.Get<ISettingsManager>()["XUnitConsoleDirPath"] = TestProjects.XUnitConsoleDirPath;

            _kernel.Bind<IWhiteSource>().ToConstant(new WhiteDummy(toMutate));

            var testsClone = _kernel.Get<IProjectClonesManager>().CreateClone("Tests");
            var testsTask = _kernel.Get<TestsLoader>().LoadTests(testsClone.Assemblies.AsStrings().ToList());

            var strategy = new AllTestsSelectStrategy(testsTask);
         
            var choices = new MutationSessionChoices
                          {
                              Filter = method == null ? MutationFilter.AllowAll() :
                                 new MutationFilter(
                                  new List<TypeIdentifier>(),
                                  method.InList()),
                              SelectedOperators = operators,
                              TestAssemblies = strategy.SelectTests(null).Result
                          };
            _kernel.Bind<MutationSessionChoices>().ToConstant(choices);

            var exec = _kernel.Get<MutationExecutor>();
            var container = new MutantsContainer(exec, original);
            IList<AssemblyNode> assemblies = container.InitMutantsForOperators(ProgressCounter.Inactive());

            var mutants = assemblies.Cast<CheckedNode>()
                .SelectManyRecursive(n => n.Children ?? new NotifyingCollection<CheckedNode>())
                .OfType<Mutant>();//.Take(4);
            return mutants;
        }