public void Test() { var gen = new XmlResultsGenerator(null,null,null, null, null); var muSession = new MutationTestingSession(); var mutar = new MutationTarget(new MutationVariant()); var ass = new AssemblyNode("Assembly"); muSession.MutantsGrouped.Add(ass); var nodeNamespace = new TypeNamespaceNode(ass, "Namespace"); ass.Children.Add(nodeNamespace); var nodeType = new TypeNode(nodeNamespace, "Type"); nodeNamespace.Children.Add(nodeType); var nodeMethod = new MethodNode(nodeType, "Method", null, true); nodeType.Children.Add(nodeMethod); var nodeGroup = new MutantGroup("Gr1", nodeMethod); nodeMethod.Children.Add(nodeGroup); var nodeMutant = new Mutant("m1", nodeGroup, mutar); nodeGroup.Children.Add(nodeMutant); XDocument generateResults = gen.GenerateResults(muSession, false, false, ProgressCounter.Inactive(), CancellationToken.None).Result; Console.WriteLine(generateResults.ToString()); //gen. }
public IEnumerable<TestNodeNamespace> CreateMutantTestTree(Mutant mutant) { List<TmpTestNodeMethod> nodeMethods = mutant.TestRunContexts .SelectMany(c => c.TestResults.ResultMethods).ToList(); return _testResultTreeCreator.CreateMutantTestTree(nodeMethods); }
public MutationResult(Mutant mutant, ICciModuleSource mutatedModules, List<CciModuleSource> old, IMethodDefinition methodMutated) { _mutant = mutant; MutatedModules = mutatedModules; Old = old; MethodMutated = methodMutated; }
public async Task<CodeWithDifference> CreateDifferenceListing(CodeLanguage language, Mutant mutant, MutationResult mutationResult) { _log.Debug("CreateDifferenceListing in object: " + ToString() + GetHashCode()); try { var whiteCode = await VisualizeOriginalCode(language, mutant); var mutatedCode = await VisualizeMutatedCode(language, mutationResult); CodePair pair = new CodePair { OriginalCode = whiteCode, MutatedCode = mutatedCode }; return _differenceCreator.GetDiff(language, pair.OriginalCode, pair.MutatedCode); } catch (Exception e) { _log.Error(e); return new CodeWithDifference { Code = "Exception occurred while decompiling: " + e, LineChanges = Enumerable.Empty<LineChange>().ToList() }; } }
public MutationResult(Mutant mutant, ICciModuleSource mutatedModules, List<CciModuleSource> old, IMethodDefinition methodMutated, List<IMethodDefinition> additionalMethodsMutated=null) { _mutant = mutant; MutatedModules = mutatedModules; Old = old; MethodMutated = methodMutated; AdditionalMethodsMutated = additionalMethodsMutated; }
public override void Visit(Mutant mutant) { Console.WriteLine("Dynamic MutantRoar!"); foreach (var animal in mutant.Animals) { Visit((dynamic)animal); } }
public void LoadDetails(Mutant mutant) { _log.Debug("LoadDetails in object: " + ToString() + GetHashCode()); _currentMutant = mutant; LoadData(_viewModel.SelectedTabHeader); }
public async Task<string> VisualizeOriginalCode(CodeLanguage language, Mutant mutant) { var whiteCode = Visualize(language, mutant.MutationTarget.MethodRaw, _originalCodebase.Modules.Single(m => m.Module.Name == mutant.MutationTarget.ProcessingContext.ModuleName)); if (mutant._mutationTargets.Count != 0 && mutant.MutationTarget.MethodRaw != mutant._mutationTargets[0].MethodRaw) { whiteCode += Visualize(language, mutant._mutationTargets[0].MethodRaw, _originalCodebase.Modules.Single(m => m.Module.Name == mutant._mutationTargets[0].ProcessingContext.ModuleName)); } return whiteCode; }
public TestingMutant( MutantMaterializer mutantMaterializer, OptionsModel options, MutationSessionChoices choices, NUnitXmlTestService testService, TestServiceManager testServiceManager, //-------- IObserver<SessionEventArgs> sessionEventsSubject, Mutant mutant) { _mutantMaterializer = mutantMaterializer; _options = options; _choices = choices; _sessionEventsSubject = sessionEventsSubject; _testServiceManager = testServiceManager; _mutant = mutant; }
public Mutant CreateEquivalentMutant(out AssemblyNode assemblyNode) { assemblyNode = new AssemblyNode("All modules"); var nsNode = new TypeNamespaceNode(assemblyNode, ""); assemblyNode.Children.Add(nsNode); var typeNode = new TypeNode(nsNode, ""); nsNode.Children.Add(typeNode); var methodNode = new MethodNode(typeNode, "", null, true); typeNode.Children.Add(methodNode); var group = new MutantGroup("Testing original program", methodNode); var target = new MutationTarget(new MutationVariant()) { Name = "Original program", }; var mutant = new Mutant("0", group, target); group.Children.Add(mutant); methodNode.Children.Add(group); group.UpdateDisplayedText(); return mutant; }
static void Main(string[] args) { Cat c = new Cat(); Dog d = new Dog(); Shiba s = new Shiba(); Mutant m = new Mutant(); var visitor = new MakeSoundVisitor(); // Classic visitor pattern allows us to add behavior to existing objects without modifying them Console.WriteLine("Call Accept() using classic visitor"); c.Accept(visitor); d.Accept(visitor); s.Accept(visitor); m.Accept(visitor); // Think of the Accept() method as a way to delegate the desired action to whichever Visitor implementation is passed in // Virtual functions are dispatched dynamically while function overloading is done statically. // To get around this, double dispatch is used Console.WriteLine("Testing wrapper"); var wrappedCat = new AnimalWrapper(c); wrappedCat.Accept(visitor); var dynamicVisitor = new MakeSoundDynamicVisitor(); Console.WriteLine("Call Accept() using dynamic visitor"); c.Accept(dynamicVisitor); d.Accept(dynamicVisitor); s.Accept(dynamicVisitor); m.Accept(dynamicVisitor); Console.WriteLine("Direct call to dynamic visitor"); dynamicVisitor.Visit(c); dynamicVisitor.Visit(d); dynamicVisitor.Visit(s); dynamicVisitor.Visit(m); Console.ReadKey(); }
public void Process(IModuleSource original, Mutant mutant) { var tree = SyntaxTree.ParseText(execTemplate); CompilationUnitSyntax root = tree.GetRoot(); var firstParameters = from methodDeclaration in root.DescendantNodes().OfType<MethodDeclarationSyntax>() where methodDeclaration.Identifier.ValueText == "Main" select methodDeclaration.ParameterList.Parameters.First(); // Syntax.MethodDeclaration(Syntax.) var comp = Compilation.Create("MyCompilation", new CompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddSyntaxTrees(tree) .AddReferences(new MetadataFileReference(typeof(object).Assembly.Location)); var outputFileName = Path.Combine(Path.GetTempPath(), "MyCompilation.lib"); var ilStream = new FileStream(outputFileName, FileMode.OpenOrCreate); var result = comp.Emit(ilStream); ilStream.Close(); if (!result.Success) { var aggregate = result.Diagnostics.Select(a => a.Info.GetMessage()).Aggregate((a, b) => a + "\n" + b); throw new InvalidProgramException(aggregate); } /* Compilation compilation = Compilation.Create("") .AddReferences(new MetadataFileReference(typeof(object).Assembly.Location)) .AddSyntaxTrees() var method = ActeeFinder valueSupplier.supplyValues(method);*/ }
public void ShouldSetTimedoutState(bool coversEveryTest, bool coveringTestsContainTimedoutTests) { var failedTestsMock = new Mock <ITestListDescription>(); var resultTestsMock = new Mock <ITestListDescription>(); var timedoutTestsMock = new Mock <ITestListDescription>(); var coveringTestsMock = new Mock <ITestListDescription>(); failedTestsMock.Setup(x => x.IsEmpty).Returns(true); timedoutTestsMock.Setup(x => x.IsEmpty).Returns(false); coveringTestsMock.Setup(x => x.GetList()).Returns(new List <TestDescription>() { new TestDescription(Guid.NewGuid().ToString(), "test") } as IReadOnlyList <TestDescription>); coveringTestsMock.Setup(x => x.IsEveryTest).Returns(coversEveryTest); coveringTestsMock.Setup(x => x.ContainsAny(It.IsAny <IReadOnlyList <TestDescription> >())).Returns(coveringTestsContainTimedoutTests); var mutant = new Mutant(); mutant.CoveringTests = coveringTestsMock.Object; mutant.AnalyzeTestRun(failedTestsMock.Object, resultTestsMock.Object, timedoutTestsMock.Object); mutant.ResultStatus.ShouldBe(MutantStatus.Timeout); }
public void Test(Mutant mutant, int timeoutMs) { try { Logger.LogDebug($"Testing {mutant.DisplayName}."); var result = TestRunner.RunAll(timeoutMs, mutant); Logger.LogTrace($"Testrun for {mutant.DisplayName} with output {result.ResultMessage}"); mutant.ResultStatus = result.Success ? MutantStatus.Survived : MutantStatus.Killed; if (result.FailingTests != null) { foreach (var resultFailingTest in result.FailingTests) { mutant.CoveringTest[resultFailingTest.Guid] = true; } } } catch (OperationCanceledException) { Logger.LogTrace("Testrun aborted due to timeout"); mutant.ResultStatus = MutantStatus.Timeout; } }
public Mutant CreateEquivalentMutant(out AssemblyNode assemblyNode) { assemblyNode = new AssemblyNode("All modules"); var nsNode = new TypeNamespaceNode(assemblyNode, ""); assemblyNode.Children.Add(nsNode); var typeNode = new TypeNode(nsNode, ""); nsNode.Children.Add(typeNode); var methodNode = new MethodNode(typeNode, "", null, true); typeNode.Children.Add(methodNode); var group = new MutantGroup("Testing original program", methodNode); var target = new MutationTarget(new MutationVariant()) { Name = "Original program", }; var mutant = new Mutant("0", group, target); group.Children.Add(mutant); methodNode.Children.Add(group); group.UpdateDisplayedText(); return(mutant); }
public void ShouldMutateAllFilesWhenTurnedOff() { var options = new StrykerOptions(diff: false); var diffProvider = new Mock <IDiffProvider>(MockBehavior.Strict); string myFile = Path.Combine("C:/test/", "myfile.cs");; diffProvider.Setup(x => x.ScanDiff()).Returns(new DiffResult() { ChangedFiles = new Collection <string>() }); var target = new DiffMutantFilter(options, diffProvider.Object); var file = new FileLeaf { FullPath = myFile }; var mutant = new Mutant(); var filterResult = target.FilterMutants(new List <Mutant>() { mutant }, file, options); filterResult.ShouldContain(mutant); }
public void MutationTestProcess_ShouldCallExecutorForEveryMutant() { var mutant = new Mutant { Id = 1 }; var otherMutant = new Mutant { Id = 2 }; string basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test"); var input = new MutationTestInput() { ProjectInfo = new ProjectInfo() { ProjectUnderTestAnalyzerResult = new ProjectAnalyzerResult(null, null) { AssemblyPath = "/bin/Debug/netcoreapp2.1/TestName.dll", Properties = new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" } } }, ProjectContents = new FolderComposite() { Name = "ProjectRoot", Children = new Collection <ProjectComponent>() { new FileLeaf() { Name = "SomeFile.cs", SourceCode = SourceFile, Mutants = new List <Mutant>() { mutant, otherMutant } } } } }, AssemblyReferences = _assemblies }; var reporterMock = new Mock <IReporter>(MockBehavior.Strict); reporterMock.Setup(x => x.OnMutantTested(It.IsAny <Mutant>())); reporterMock.Setup(x => x.OnAllMutantsTested(It.IsAny <ProjectComponent>())); reporterMock.Setup(x => x.OnStartMutantTestRun(It.IsAny <IList <Mutant> >())); var executorMock = new Mock <IMutationTestExecutor>(MockBehavior.Strict); executorMock.SetupGet(x => x.TestRunner).Returns(Mock.Of <ITestRunner>()); executorMock.Setup(x => x.Test(It.IsAny <Mutant>(), It.IsAny <int>())); var options = new StrykerOptions(fileSystem: new MockFileSystem(), basePath: basePath); var target = new MutationTestProcess(input, reporterMock.Object, executorMock.Object); target.Test(options); executorMock.Verify(x => x.Test(mutant, It.IsAny <int>()), Times.Once); reporterMock.Verify(x => x.OnStartMutantTestRun(It.Is <IList <Mutant> >(y => y.Count == 2)), Times.Once); reporterMock.Verify(x => x.OnMutantTested(mutant), Times.Once); reporterMock.Verify(x => x.OnMutantTested(otherMutant), Times.Once); reporterMock.Verify(x => x.OnAllMutantsTested(It.IsAny <ProjectComponent>()), Times.Once); }
public void TestWithHighPriority(Mutant mutant) { _testingProcess.TestWithHighPriority(mutant); }
public async Task TestOneMutant(Mutant mutant) { try { _log.Info("Testing process for mutant: "+ mutant.Id); IObjectRoot<TestingMutant> testingMutant = _mutantTestingFactory.CreateWithParams(_sessionEventsSubject, mutant); await testingMutant.Get.RunAsync(); } catch (Exception e) { _log.Error(e); mutant.MutantTestSession.ErrorMessage = e.ToString(); mutant.MutantTestSession.ErrorDescription = e.Message; mutant.State = MutantResultState.Error; } lock (this) { _testedNonEquivalentMutantsCount++; _testedMutantsCount++; _mutantsKilledCount = _mutantsKilledCount.IncrementedIf(mutant.State == MutantResultState.Killed); //AKB if (mutant.Id.IndexOf("First Order Mutant")!=-1) { _numberOfFirstOrderMutants++; } RaiseTestingProgress(); } }
/// <summary> /// Mutation Predicate. /// Modifies a DNA string by changing one of its bases to be a random new value /// </summary> /// <param name="mut_index"> /// The point in which mutation is applied. /// </param> /// <param name="randomGene"> /// The function used to supply random Genes /// </param> public static Mutant mutate(int mut_index, DNA original, getRandomGeneFunct randomGene) { Mutant m; DNA g_dna; DNAPartial g_partial; DNAPartial rest; Gene g; g=randomGene(); while (original.Contains(g)) g = randomGene(); g_dna = new DNA(); g_dna.Add(g); var sp_mut = split(mut_index-1, original); g_partial = split(0, g_dna).back; // remove the base at the location that mutation is to occur DNA back = join(new DNAPartial(), sp_mut.back); back.RemoveAt(0); // add mutated item back = join(g_partial, split(0,back).back); DNA front = join(new DNAPartial(), sp_mut.front); // join front, mutation, and rest, lists together rest = split(0, join(split(0,front).back, split(0, back).back)).back; m = new Mutant(join(new DNAPartial(), rest)); return m; }
public void TestWithHighPriority(Mutant mutant) { _mutantsWorkers.LockingMoveToFront(mutant); }
public void MutantSurvived(Mutant survivingMutant) { }
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); } }
public async Task<StoredMutantInfo> StoreMutant(Mutant mutant) { mutant.State = MutantResultState.Creating; var mutationResult = await _mutantsCache.GetMutatedModulesAsync(mutant); mutant.State = MutantResultState.Writing; var clone = await _clonesManager.CreateCloneAsync("InitTestEnvironment"); var info = new StoredMutantInfo(clone); if(mutationResult.MutatedModules != null) { var singleMutated = mutationResult.MutatedModules.Modules.SingleOrDefault(); if (singleMutated != null) { //TODO: remove: assemblyDefinition.Name.Name + ".dll", use factual original file name string file = Path.Combine(info.Directory, singleMutated.Name + ".dll"); // // var memory = mutationResult.MutatedModules.WriteToStream(singleMutated); // // _mutantsCache.Release(mutationResult); // // using (FileStream peStream = File.Create(file)) // { // await memory.CopyToAsync(peStream); // } using (FileStream peStream = File.Create(file)) { mutationResult.MutatedModules.WriteToStream(singleMutated, peStream, file); // await memory.CopyToAsync(peStream); } info.AssembliesPaths.Add(file); } var otherModules = _originalCodebase.Modules .Where(_ => singleMutated == null || _.Module.Name != singleMutated.Name); foreach (var otherModule in otherModules) { string file = Path.Combine(info.Directory, otherModule.Module.Name + ".dll"); info.AssembliesPaths.Add(file); } } else { foreach (var cciModuleSource in mutationResult.Old) { var module = cciModuleSource.Modules.Single(); //TODO: remove: assemblyDefinition.Name.Name + ".dll", use factual original file name string file = Path.Combine(info.Directory, module.Name + ".dll"); // _mutantsCache.Release(mutationResult); using (FileStream peStream = File.Create(file)) { cciModuleSource.WriteToStream(module, peStream, file); // await memory.CopyToAsync(peStream); } info.AssembliesPaths.Add(file); } } return info; }
public void Clean() { _currentMutant = null; _viewModel.IsCodeLoading = false; _viewModel.TestNamespaces.Clear(); _viewModel.SelectedLanguage = CodeLanguage.CSharp; _viewModel.ClearCode(); _langObs.Dispose(); _tabObs.Dispose(); }
public MutantVerifiedEvent(Mutant mutant, bool verificationResult) : base(OperationsState.PreCheck) { Mutant = mutant; VerificationResult = verificationResult; }
private async Task<MutationResult> CreateNew(Mutant mutant) { MutationResult result; if (mutant.MutationTarget == null || mutant.MutationTarget.ProcessingContext == null) { result = new MutationResult(mutant, new CciModuleSource(), null, null); } else { if(_options.ParsedParams.LegacyCreation) { _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting white cache."); var cci = await _whiteCache.GetWhiteModulesAsyncOld(); _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting mutation start."); result = await _mutationExecutor.ExecuteMutation(mutant, cci); _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting mutation finished."); } else { _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting white cache."); var cci = await _whiteCache.GetWhiteSourceAsync(mutant.MutationTarget.ProcessingContext.ModuleName); _log.Debug("Cache#Mutant " + mutant.Id + ": taken source: "+cci.Guid); _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting mutation start."); result = await _mutationExecutor.ExecuteMutation(mutant, cci); _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting mutation finished."); } if (!_disableCache) { _cache.Add(new CacheItem(mutant.Id, result), new CacheItemPolicy()); } } return result; }
//TODO:test error behaviour public async Task<MutationResult> GetMutatedModulesAsync(Mutant mutant) { try { _log.Debug("GetMutatedModules in object: " + ToString() + GetHashCode()); _log.Info("Request to cache for mutant: " + mutant.Id); bool creating = false; MutationResult result; if (_disableCache || !_cache.Contains(mutant.Id)) { _log.Debug("Cache#Mutant " + mutant.Id + " not yet in cache."); Task<MutationResult> resultTask; ConcurrentBag<TaskCompletionSource<MutationResult>> awaitQueue; if (_map.TryGetValue(mutant.Id, out awaitQueue)) { _log.Debug("Cache#Mutant " + mutant.Id + " is being awaited for. Subscribing."); var tcs = new TaskCompletionSource<MutationResult>(); awaitQueue.Add(tcs); resultTask = tcs.Task; } else { _log.Debug("Cache#Mutant " + mutant.Id + " is not waited for. Creating subscriber."); _map.TryAdd(mutant.Id, new ConcurrentBag<TaskCompletionSource<MutationResult>>()); resultTask = CreateNew(mutant); creating = true; } _log.Debug("Cache#Mutant " + mutant.Id + ": Task"); result = await resultTask; if (creating) { _log.Debug("Cache#Mutant " + mutant.Id + ": Creation complete. Trying to remove subscribers queue"); ConcurrentBag<TaskCompletionSource<MutationResult>> awaiters; if(_map.TryRemove(mutant.Id, out awaiters)) { _log.Debug("Cache#Mutant " + mutant.Id + string.Format(": Subscribers queue removed. Setting result on {0} subscribers", awaiters.Count)); foreach (var tcs in awaiters) { tcs.SetResult(result); } } else { _log.Debug("Cache#Mutant " + mutant.Id + ": Could not remove subscribers queue"); } } return result; } else { _log.Debug("Cache contains value for mutant " + mutant.Id); result = (MutationResult) _cache.Get(mutant.Id); } return result; } catch (Exception e) { _log.Error(e); throw; } }
public async Task<MutationResult> ExecuteMutation(Mutant mutant, CciModuleSource moduleSource, CciModuleSource moduleSource2) { /*var type = new TypeIdentifier((INamedTypeDefinition) mutant.MutationTarget.ProcessingContext.Type.Object); var type2 = new TypeIdentifier((INamedTypeDefinition)mutant.MutationTarget2.ProcessingContext.Type.Object);// var method = new MethodIdentifier((IMethodDefinition) mutant.MutationTarget.ProcessingContext.Method.Object); var method2 = new MethodIdentifier((IMethodDefinition)mutant._mutationTarget2.ProcessingContext.Method.Object); // var filter = new MutationFilter(type.InList(), method.InList()); var filter2 = new MutationFilter(type2.InList(), method2.InList());// */ //_log.Debug("ExecuteMutation of: " + type+" - " +method ); //_log.Debug("ExecuteMutation of: " + type + " - " + method + " + " + method2); IMutationOperator mutationOperator = mutant.MutationTarget.OperatorId == null ? new IdentityOperator() : _mutOperators.Single(m => mutant.MutationTarget.OperatorId == m.Info.Id); IMutationOperator mutationOperator2 = mutant._mutationTargets[0].OperatorId == null ? new IdentityOperator() : _mutOperators.Single(m => mutant._mutationTargets[0].OperatorId == m.Info.Id); var cci = moduleSource; try { //_log.Info("Execute mutation of " + mutant.MutationTarget + " contained in " + mutant.MutationTarget.MethodRaw + " modules. "); _log.Info("Execute mutation of " + mutant.MutationTarget + " contained in " + mutant.MutationTarget.MethodRaw + " modules and " + mutant._mutationTargets[0] + " contained in " + mutant._mutationTargets[0].MethodRaw + " modules."); var mutatedModules = new List<IModuleInfo>(); var module = moduleSource.Modules.Single(); List<MutationTarget> mutationTargets = new List<MutationTarget>(); mutationTargets.Add(mutant.MutationTarget); mutationTargets.Add(mutant._mutationTargets[0]); List<MutationTarget> sharedtargets = new List<MutationTarget>(); foreach (var element in _sharedTargets.GetValues(mutationOperator, returnEmptySet: true)) { sharedtargets.Add(element); } foreach (var element in _sharedTargets.GetValues(mutationOperator2, returnEmptySet: true)) { sharedtargets.Add(element); } var visitorBack = new VisualCodeVisitorBack(mutationTargets, sharedtargets, module.Module, mutationOperator.Info.Id+mutationOperator2.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 operatorCodeRewriter2 = mutationOperator2.CreateRewriter();// var rewriter2 = new VisualCodeRewriter(cci.Host, visitorBack.TargetAstObjects, visitorBack.SharedAstObjects, _filter, operatorCodeRewriter2, traverser2);// operatorCodeRewriter2.MutationTarget = new UserMutationTarget(mutant._mutationTarget2.Variant.Signature, mutant.MutationTarget2.Variant.AstObjects);// operatorCodeRewriter2.NameTable = cci.Host.NameTable; operatorCodeRewriter2.Host = cci.Host; operatorCodeRewriter2.Module = module.Module; operatorCodeRewriter2.OperatorUtils = _operatorUtils; operatorCodeRewriter2.Initialize(); */ var rewrittenModule = rewriter.Rewrite(module.Module); //var rewrittenModule2 = rewriter2.Rewrite(module.Module);// rewriter.CheckForUnfoundObjects(); //rewriter2.CheckForUnfoundObjects();// //2nd mutation /*IMutationOperator mutationOperator2 = mutant._mutationTarget2.OperatorId == null ? new IdentityOperator() : _mutOperators.Single(m => mutant._mutationTarget2.OperatorId == m.Info.Id); // var cci2 = moduleSource2; var module2 = moduleSource2.Modules.Single();// var visitorBack2 = new VisualCodeVisitorBack(mutant.MutationTarget2.InList(), _sharedTargets.GetValues(mutationOperator2, returnEmptySet: true), module2.Module, mutationOperator2.Info.Id); // var traverser22 = new VisualCodeTraverser(_filter, visitorBack2, moduleSource2);// traverser22.Traverse(module2.Module);// visitorBack2.PostProcess();// var operatorCodeRewriter2 = mutationOperator2.CreateRewriter();// var rewriter2 = new VisualCodeRewriter(cci2.Host, visitorBack2.TargetAstObjects, visitorBack2.SharedAstObjects, _filter, operatorCodeRewriter2, traverser22);// operatorCodeRewriter2.MutationTarget = new UserMutationTarget(mutant._mutationTarget2.Variant.Signature, mutant.MutationTarget2.Variant.AstObjects);// operatorCodeRewriter2.NameTable = cci2.Host.NameTable; operatorCodeRewriter2.Host = cci2.Host; operatorCodeRewriter2.Module = module2.Module; operatorCodeRewriter2.OperatorUtils = _operatorUtils; operatorCodeRewriter2.Initialize(); var rewrittenModule2 = rewriter2.Rewrite(module2.Module);// rewriter2.CheckForUnfoundObjects();// */ mutant.MutationTarget.Variant.AstObjects = null; //TODO: avoiding leaking memory. refactor mutant._mutationTargets[0].Variant.AstObjects = null; //TODO: avoiding leaking memory. refactor mutatedModules.Add(new ModuleInfo(rewrittenModule, "")); //mutatedModules.Add(new ModuleInfo(rewrittenModule2, ""));// //List<ICciModuleSource> cci = new List<ICciModuleSource>(); //cci.Add(cci1); //cci.Add(cci2); List<IMethodDefinition> methodsMutated = new List<IMethodDefinition>(); methodsMutated.Add(mutant._mutationTargets[0].MethodMutated); var result = new MutationResult(mutant, cci, null, mutant.MutationTarget.MethodMutated, methodsMutated); mutant.MutationTarget.MethodMutated = null; //TODO: avoiding leaking memory. refactor mutant._mutationTargets[0].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); } }
public void MutantSkipped(Mutant skippedMutant, string reason) { }
public async Task <StoredMutantInfo> StoreMutant(Mutant mutant) { mutant.State = MutantResultState.Creating; var mutationResult = await _mutantsCache.GetMutatedModulesAsync(mutant); mutant.State = MutantResultState.Writing; var clone = await _clonesManager.CreateCloneAsync("InitTestEnvironment"); var info = new StoredMutantInfo(clone); if (mutationResult.MutatedModules != null) { //AKB /*foreach (ICciModuleSource mutatedModule in mutationResult.MutatedModules) * {*/ var singleMutated = mutationResult.MutatedModules.Modules.SingleOrDefault(); if (singleMutated != null) { //TODO: remove: assemblyDefinition.Name.Name + ".dll", use factual original file name string file = Path.Combine(info.Directory, singleMutated.Name + ".dll"); // // var memory = mutationResult.MutatedModules.WriteToStream(singleMutated); // // _mutantsCache.Release(mutationResult); // // using (FileStream peStream = File.Create(file)) // { // await memory.CopyToAsync(peStream); // } using (FileStream peStream = File.Create(file)) { mutationResult.MutatedModules.WriteToStream(singleMutated, peStream, file); // await memory.CopyToAsync(peStream); } info.AssembliesPaths.Add(file); } var otherModules = _originalCodebase.Modules .Where(_ => singleMutated == null || _.Module.Name != singleMutated.Name); foreach (var otherModule in otherModules) { string file = Path.Combine(info.Directory, otherModule.Module.Name + ".dll"); info.AssembliesPaths.Add(file); } //} } else { foreach (var cciModuleSource in mutationResult.Old) { var module = cciModuleSource.Modules.Single(); //TODO: remove: assemblyDefinition.Name.Name + ".dll", use factual original file name string file = Path.Combine(info.Directory, module.Name + ".dll"); // _mutantsCache.Release(mutationResult); using (FileStream peStream = File.Create(file)) { cciModuleSource.WriteToStream(module, peStream, file); // await memory.CopyToAsync(peStream); } info.AssembliesPaths.Add(file); } } return(info); }
public void OnTestingStarting(string directory, Mutant mutant) { }
private Mutant findPartner(List<AssemblyNode> assNodes, Mutant mutant) { bool existsAlreadyUsed = false; int lowestNrOfRep = 0; foreach (var assembly in assNodes) { foreach (var assProjectNode in assembly.Children) { foreach (var assFileNode in assProjectNode.Children) { foreach (var assMutantsNode in assFileNode.Children) { for (int i = assMutantsNode.Children.Count - 1; i >= 0; i--) { if (assMutantsNode.Children[i].Children != null) { for (int j = assMutantsNode.Children[i].Children.Count - 1; j >= 0; j--) { if (((Mutant)assMutantsNode.Children[i].Children[j]).Description == mutant.Description && ((Mutant)assMutantsNode.Children[i].Children[j])._mutationTargets.Count == 0 && ((Mutant)assMutantsNode.Children[i].Children[j]).Id != mutant.Id && getIdWithoutNr(((Mutant)assMutantsNode.Children[i].Children[j]).Id) == getIdWithoutNr(mutant.Id)) { if (((Mutant)assMutantsNode.Children[i].Children[j])._nrTimesWasAdded == 0) { ((Mutant)assMutantsNode.Children[i].Children[j])._nrTimesWasAdded++; return ((Mutant)assMutantsNode.Children[i].Children[j]); } else { existsAlreadyUsed = true; if (lowestNrOfRep == 0) { lowestNrOfRep = ((Mutant)assMutantsNode.Children[i].Children[j])._nrTimesWasAdded; } else { lowestNrOfRep = Math.Min(lowestNrOfRep, ((Mutant)assMutantsNode.Children[i].Children[j])._nrTimesWasAdded); } } } } } else { if (((Mutant)assMutantsNode.Children[i]).Description == mutant.Description && ((Mutant)assMutantsNode.Children[i])._mutationTargets.Count == 0 && ((Mutant)assMutantsNode.Children[i]).Id != mutant.Id && getIdWithoutNr(((Mutant)assMutantsNode.Children[i]).Id) == getIdWithoutNr(mutant.Id)) { if (((Mutant)assMutantsNode.Children[i])._nrTimesWasAdded == 0) { ((Mutant)assMutantsNode.Children[i])._nrTimesWasAdded++; return ((Mutant)assMutantsNode.Children[i]); } else { existsAlreadyUsed = true; if (lowestNrOfRep == 0) { lowestNrOfRep = ((Mutant)assMutantsNode.Children[i])._nrTimesWasAdded; } else { lowestNrOfRep = Math.Min(lowestNrOfRep, ((Mutant)assMutantsNode.Children[i])._nrTimesWasAdded); } } } } } } } } } if (existsAlreadyUsed) { foreach (var assembly in assNodes) { foreach (var assProjectNode in assembly.Children) { foreach (var assFileNode in assProjectNode.Children) { foreach (var assMutantsNode in assFileNode.Children) { for (int i = assMutantsNode.Children.Count - 1; i >= 0; i--) { if (assMutantsNode.Children[i].Children != null) { for (int j = assMutantsNode.Children[i].Children.Count - 1; j >= 0; j--) { if (((Mutant)assMutantsNode.Children[i].Children[j]).Description == mutant.Description && ((Mutant)assMutantsNode.Children[i].Children[j])._mutationTargets.Count == 0 && ((Mutant)assMutantsNode.Children[i].Children[j]).Id != mutant.Id && getIdWithoutNr(((Mutant)assMutantsNode.Children[i].Children[j]).Id) == getIdWithoutNr(mutant.Id) && ((Mutant)assMutantsNode.Children[i].Children[j])._nrTimesWasAdded == lowestNrOfRep) { ((Mutant)assMutantsNode.Children[i].Children[j])._nrTimesWasAdded++; return ((Mutant)assMutantsNode.Children[i].Children[j]); } } } else { if (((Mutant)assMutantsNode.Children[i]).Description == mutant.Description && ((Mutant)assMutantsNode.Children[i])._mutationTargets.Count == 0 && ((Mutant)assMutantsNode.Children[i]).Id != mutant.Id && getIdWithoutNr(((Mutant)assMutantsNode.Children[i]).Id) == getIdWithoutNr(mutant.Id) && ((Mutant)assMutantsNode.Children[i])._nrTimesWasAdded == lowestNrOfRep) { ((Mutant)assMutantsNode.Children[i])._nrTimesWasAdded++; return ((Mutant)assMutantsNode.Children[i]); } } } } } } } } else { Mutant mutantCopy = new Mutant("First Order Mutant", mutant.MutationTarget); mutantCopy.Id = "First Order Mutant"; return mutantCopy; } return mutant; }
/// <summary> /// </summary> /// <param name = "changelessMutant"></param> /// <returns>true if session can continue</returns> private bool CheckForTestingErrors(Mutant changelessMutant) { if (changelessMutant.State == MutantResultState.Error && !(changelessMutant.MutantTestSession.Exception is AssemblyVerificationException)) { _svc.Logging.ShowError(UserMessages.ErrorPretest_UnknownError( changelessMutant.MutantTestSession.Exception.ToString())); return false; } else if (changelessMutant.State == MutantResultState.Killed) { if (changelessMutant.KilledSubstate == MutantKilledSubstate.Cancelled) { return _svc.Logging.ShowYesNoQuestion(UserMessages.ErrorPretest_Cancelled()); } var testMethods = changelessMutant.TestRunContexts .SelectMany(c => c.TestResults.ResultMethods).ToList(); var test = testMethods.FirstOrDefault(t => t.State == TestNodeState.Failure); var allFailedTests = testMethods .Where(t => t.State == TestNodeState.Failure || t.State == TestNodeState.Inconclusive) .Select(_ => _.Name) .ToList(); string allFailedString = allFailedTests.Aggregate((a, b) => a + "\n" + b); string testName = null; string testMessage = null; if (test != null) { testName = test.Name; testMessage = test.Message; } else { var testInconcl = testMethods .First(t =>t.State == TestNodeState.Inconclusive); testName = testInconcl.Name; testMessage = "Test was inconclusive."; } bool disableAndContinue = _svc.Logging.ShowYesNoQuestion( UserMessages.ErrorPretest_TestsFailed(allFailedTests.Count.ToString(), allFailedString, testName, testMessage)); return disableAndContinue; //return _svc.Logging.ShowYesNoQuestion(UserMessages.ErrorPretest_TestsFailed(testName, testMessage)); } return true; }
public async Task <MutationResult> ExecuteMutation(Mutant mutant, CciModuleSource moduleSource, CciModuleSource moduleSource2) { /*var type = new TypeIdentifier((INamedTypeDefinition) mutant.MutationTarget.ProcessingContext.Type.Object); * var type2 = new TypeIdentifier((INamedTypeDefinition)mutant.MutationTarget2.ProcessingContext.Type.Object);// * var method = new MethodIdentifier((IMethodDefinition) mutant.MutationTarget.ProcessingContext.Method.Object); * var method2 = new MethodIdentifier((IMethodDefinition)mutant._mutationTarget2.ProcessingContext.Method.Object); // * var filter = new MutationFilter(type.InList(), method.InList()); * var filter2 = new MutationFilter(type2.InList(), method2.InList());// */ //_log.Debug("ExecuteMutation of: " + type+" - " +method ); //_log.Debug("ExecuteMutation of: " + type + " - " + method + " + " + method2); IMutationOperator mutationOperator = mutant.MutationTarget.OperatorId == null ? new IdentityOperator() : _mutOperators.Single(m => mutant.MutationTarget.OperatorId == m.Info.Id); IMutationOperator mutationOperator2 = mutant._mutationTargets[0].OperatorId == null ? new IdentityOperator() : _mutOperators.Single(m => mutant._mutationTargets[0].OperatorId == m.Info.Id); var cci = moduleSource; try { //_log.Info("Execute mutation of " + mutant.MutationTarget + " contained in " + mutant.MutationTarget.MethodRaw + " modules. "); _log.Info("Execute mutation of " + mutant.MutationTarget + " contained in " + mutant.MutationTarget.MethodRaw + " modules and " + mutant._mutationTargets[0] + " contained in " + mutant._mutationTargets[0].MethodRaw + " modules."); var mutatedModules = new List <IModuleInfo>(); var module = moduleSource.Modules.Single(); List <MutationTarget> mutationTargets = new List <MutationTarget>(); mutationTargets.Add(mutant.MutationTarget); mutationTargets.Add(mutant._mutationTargets[0]); List <MutationTarget> sharedtargets = new List <MutationTarget>(); foreach (var element in _sharedTargets.GetValues(mutationOperator, returnEmptySet: true)) { sharedtargets.Add(element); } foreach (var element in _sharedTargets.GetValues(mutationOperator2, returnEmptySet: true)) { sharedtargets.Add(element); } var visitorBack = new VisualCodeVisitorBack(mutationTargets, sharedtargets, module.Module, mutationOperator.Info.Id + mutationOperator2.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 operatorCodeRewriter2 = mutationOperator2.CreateRewriter();// * var rewriter2 = new VisualCodeRewriter(cci.Host, visitorBack.TargetAstObjects, * visitorBack.SharedAstObjects, _filter, operatorCodeRewriter2, traverser2);// * * operatorCodeRewriter2.MutationTarget = * new UserMutationTarget(mutant._mutationTarget2.Variant.Signature, mutant.MutationTarget2.Variant.AstObjects);// * * operatorCodeRewriter2.NameTable = cci.Host.NameTable; * operatorCodeRewriter2.Host = cci.Host; * operatorCodeRewriter2.Module = module.Module; * operatorCodeRewriter2.OperatorUtils = _operatorUtils; * operatorCodeRewriter2.Initialize(); */ var rewrittenModule = rewriter.Rewrite(module.Module); //var rewrittenModule2 = rewriter2.Rewrite(module.Module);// rewriter.CheckForUnfoundObjects(); //rewriter2.CheckForUnfoundObjects();// //2nd mutation /*IMutationOperator mutationOperator2 = mutant._mutationTarget2.OperatorId == null ? new IdentityOperator() : * _mutOperators.Single(m => mutant._mutationTarget2.OperatorId == m.Info.Id); // * var cci2 = moduleSource2; * var module2 = moduleSource2.Modules.Single();// * var visitorBack2 = new VisualCodeVisitorBack(mutant.MutationTarget2.InList(), * _sharedTargets.GetValues(mutationOperator2, returnEmptySet: true), * module2.Module, mutationOperator2.Info.Id); // * * var traverser22 = new VisualCodeTraverser(_filter, visitorBack2, moduleSource2);// * traverser22.Traverse(module2.Module);// * visitorBack2.PostProcess();// * var operatorCodeRewriter2 = mutationOperator2.CreateRewriter();// * var rewriter2 = new VisualCodeRewriter(cci2.Host, visitorBack2.TargetAstObjects, * visitorBack2.SharedAstObjects, _filter, operatorCodeRewriter2, traverser22);// * * operatorCodeRewriter2.MutationTarget = * new UserMutationTarget(mutant._mutationTarget2.Variant.Signature, mutant.MutationTarget2.Variant.AstObjects);// * * operatorCodeRewriter2.NameTable = cci2.Host.NameTable; * operatorCodeRewriter2.Host = cci2.Host; * operatorCodeRewriter2.Module = module2.Module; * operatorCodeRewriter2.OperatorUtils = _operatorUtils; * operatorCodeRewriter2.Initialize(); * * var rewrittenModule2 = rewriter2.Rewrite(module2.Module);// * rewriter2.CheckForUnfoundObjects();// */ mutant.MutationTarget.Variant.AstObjects = null; //TODO: avoiding leaking memory. refactor mutant._mutationTargets[0].Variant.AstObjects = null; //TODO: avoiding leaking memory. refactor mutatedModules.Add(new ModuleInfo(rewrittenModule, "")); //mutatedModules.Add(new ModuleInfo(rewrittenModule2, ""));// //List<ICciModuleSource> cci = new List<ICciModuleSource>(); //cci.Add(cci1); //cci.Add(cci2); List <IMethodDefinition> methodsMutated = new List <IMethodDefinition>(); methodsMutated.Add(mutant._mutationTargets[0].MethodMutated); var result = new MutationResult(mutant, cci, null, mutant.MutationTarget.MethodMutated, methodsMutated); mutant.MutationTarget.MethodMutated = null; //TODO: avoiding leaking memory. refactor mutant._mutationTargets[0].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); } }
public TestRunResult RunAll(int?timeoutMs, Mutant mutant, TestUpdateHandler update) { return(TestMultipleMutants(timeoutMs, mutant == null ? null : new List <Mutant> { mutant }, update)); }
public void MutantKilled(Mutant killedMutant, string testFailureDescription) { }
public MutMod(Mutant mutant, IModuleSource modulesProvider) { Mutant = mutant; ModulesProvider = modulesProvider; }
public void Add(Mutant mutant) => repository.Add(mutant);
public void ShouldNotTest_WhenThereAreNoTestableMutations() { var mutant = new Mutant() { Id = 1, ResultStatus = MutantStatus.Ignored }; var mutant2 = new Mutant() { Id = 2, ResultStatus = MutantStatus.CompileError }; string basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test"); var input = new MutationTestInput() { ProjectInfo = new ProjectInfo() { ProjectContents = new FolderComposite() { Name = "ProjectRoot", Children = new Collection <ProjectComponent>() { new FileLeaf() { Name = "SomeFile.cs", Mutants = new Collection <Mutant>() { mutant, mutant2 } } } }, }, AssemblyReferences = _assemblies }; var reporterMock = new Mock <IReporter>(MockBehavior.Strict); reporterMock.Setup(x => x.OnMutantTested(It.IsAny <Mutant>())); reporterMock.Setup(x => x.OnAllMutantsTested(It.IsAny <ProjectComponent>())); reporterMock.Setup(x => x.OnStartMutantTestRun(It.IsAny <IList <Mutant> >(), It.IsAny <IEnumerable <TestDescription> >())); var runnerMock = new Mock <ITestRunner>(); runnerMock.Setup(x => x.DiscoverNumberOfTests()).Returns(1); var executorMock = new Mock <IMutationTestExecutor>(MockBehavior.Strict); executorMock.SetupGet(x => x.TestRunner).Returns(runnerMock.Object); executorMock.Setup(x => x.Test(It.IsAny <IList <Mutant> >(), It.IsAny <int>(), It.IsAny <TestUpdateHandler>())); var mutantFilterMock = new Mock <IMutantFilter>(MockBehavior.Loose); var options = new StrykerOptions(fileSystem: new MockFileSystem(), basePath: basePath); var target = new MutationTestProcess(input, reporterMock.Object, executorMock.Object, mutantFilter: mutantFilterMock.Object, options: options); var testResult = target.Test(options); executorMock.Verify(x => x.Test(It.IsAny <IList <Mutant> >(), It.IsAny <int>(), It.IsAny <TestUpdateHandler>()), Times.Never); reporterMock.Verify(x => x.OnStartMutantTestRun(It.IsAny <IList <Mutant> >(), It.IsAny <IEnumerable <TestDescription> >()), Times.Never); reporterMock.Verify(x => x.OnMutantTested(It.IsAny <Mutant>()), Times.Never); reporterMock.Verify(x => x.OnAllMutantsTested(It.IsAny <ProjectComponent>()), Times.Once); testResult.MutationScore.ShouldBe(double.NaN); }
//TODO:test error behaviour public async Task <MutationResult> GetMutatedModulesAsync(Mutant mutant) { try { _log.Debug("GetMutatedModules in object: " + ToString() + GetHashCode()); _log.Info("Request to cache for mutant: " + mutant.Id); bool creating = false; MutationResult result; if (_disableCache || !_cache.Contains(mutant.Id)) { _log.Debug("Cache#Mutant " + mutant.Id + " not yet in cache."); Task <MutationResult> resultTask; ConcurrentBag <TaskCompletionSource <MutationResult> > awaitQueue; if (_map.TryGetValue(mutant.Id, out awaitQueue)) { _log.Debug("Cache#Mutant " + mutant.Id + " is being awaited for. Subscribing."); var tcs = new TaskCompletionSource <MutationResult>(); awaitQueue.Add(tcs); resultTask = tcs.Task; } else { _log.Debug("Cache#Mutant " + mutant.Id + " is not waited for. Creating subscriber."); _map.TryAdd(mutant.Id, new ConcurrentBag <TaskCompletionSource <MutationResult> >()); resultTask = CreateNew(mutant); creating = true; } _log.Debug("Cache#Mutant " + mutant.Id + ": Task"); result = await resultTask; if (creating) { _log.Debug("Cache#Mutant " + mutant.Id + ": Creation complete. Trying to remove subscribers queue"); ConcurrentBag <TaskCompletionSource <MutationResult> > awaiters; if (_map.TryRemove(mutant.Id, out awaiters)) { _log.Debug("Cache#Mutant " + mutant.Id + string.Format(": Subscribers queue removed. Setting result on {0} subscribers", awaiters.Count)); foreach (var tcs in awaiters) { tcs.SetResult(result); } } else { _log.Debug("Cache#Mutant " + mutant.Id + ": Could not remove subscribers queue"); } } return(result); } else { _log.Debug("Cache contains value for mutant " + mutant.Id); result = (MutationResult)_cache.Get(mutant.Id); } return(result); } catch (Exception e) { _log.Error(e); throw; } }
public void LoadDetails(Mutant mutant) { _mutantDetailsController.LoadDetails(mutant); }
public void Add(Mutant mutant) { context.Mutants.Add(mutant); context.SaveChanges(); }
public abstract void Visit(Mutant mutant);
public MutantController(Mutant mutant) { _mutant = mutant; }
public async Task <CodeWithDifference> CreateDifferenceListing(CodeLanguage language, Mutant mutant, MutationResult mutationResult) { _log.Debug("CreateDifferenceListing in object: " + ToString() + GetHashCode()); try { var whiteCode = await VisualizeOriginalCode(language, mutant); var mutatedCode = await VisualizeMutatedCode(language, mutationResult); CodePair pair = new CodePair { OriginalCode = whiteCode, MutatedCode = mutatedCode }; return(_differenceCreator.GetDiff(language, pair.OriginalCode, pair.MutatedCode)); } catch (Exception e) { _log.Error(e); return(new CodeWithDifference { Code = "Exception occurred while decompiling: " + e, LineChanges = Enumerable.Empty <LineChange>().ToList() }); } }
public void FilterMutantsShouldCallMutantFilters() { var inputFile = new CsharpFileLeaf() { SourceCode = SourceFile, SyntaxTree = CSharpSyntaxTree.ParseText(SourceFile) }; var folder = new CsharpFolderComposite(); folder.Add(inputFile); var input = new MutationTestInput() { ProjectInfo = new ProjectInfo() { ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" }, { "AssemblyName", "AssemblyName" }, { "Language", "C#" } }).Object, TestProjectAnalyzerResults = new List <IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" }, { "AssemblyName", "AssemblyName" }, { "Language", "C#" } }).Object }, ProjectContents = folder }, AssemblyReferences = _assemblies }; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { Path.Combine(FilesystemRoot, "ExampleProject", "Recursive.cs"), new MockFileData(SourceFile) }, { Path.Combine(FilesystemRoot, "ExampleProject.Test", "bin", "Debug", "netcoreapp2.0", "ExampleProject.dll"), new MockFileData("Bytecode") }, { Path.Combine(FilesystemRoot, "ExampleProject.Test", "obj", "Release", "netcoreapp2.0", "ExampleProject.dll"), new MockFileData("Bytecode") } }); var mutantToBeSkipped = new Mutant() { Mutation = new Mutation() }; var compileErrorMutant = new Mutant() { Mutation = new Mutation(), ResultStatus = MutantStatus.CompileError }; var mockMutants = new Collection <Mutant>() { new Mutant() { Mutation = new Mutation() }, mutantToBeSkipped, compileErrorMutant }; // create mocks var orchestratorMock = new Mock <MutantOrchestrator <SyntaxNode> >(MockBehavior.Strict); var reporterMock = new Mock <IReporter>(MockBehavior.Strict); var mutationTestExecutorMock = new Mock <IMutationTestExecutor>(MockBehavior.Strict); var mutantFilterMock = new Mock <IMutantFilter>(MockBehavior.Strict); var coverageAnalyzerMock = new Mock <ICoverageAnalyser>(MockBehavior.Strict); // setup mocks reporterMock.Setup(x => x.OnMutantsCreated(It.IsAny <ReadOnlyProjectComponent>())); orchestratorMock.Setup(x => x.GetLatestMutantBatch()).Returns(mockMutants); orchestratorMock.Setup(x => x.Mutate(It.IsAny <SyntaxNode>())).Returns(CSharpSyntaxTree.ParseText(SourceFile).GetRoot()); orchestratorMock.SetupAllProperties(); mutantFilterMock.SetupGet(x => x.DisplayName).Returns("Mock filter"); IEnumerable <Mutant> mutantsPassedToFilter = null; mutantFilterMock.Setup(x => x.FilterMutants(It.IsAny <IEnumerable <Mutant> >(), It.IsAny <ReadOnlyFileLeaf>(), It.IsAny <IStrykerOptions>())) .Callback <IEnumerable <Mutant>, ReadOnlyFileLeaf, IStrykerOptions>((mutants, _, __) => mutantsPassedToFilter = mutants) .Returns((IEnumerable <Mutant> mutants, ReadOnlyFileLeaf file, IStrykerOptions o) => mutants.Take(1)); var options = new StrykerOptions(devMode: true, excludedMutations: new string[] { }, fileSystem: new MockFileSystem()); var target = new MutationTestProcess(input, reporterMock.Object, mutationTestExecutorMock.Object, orchestratorMock.Object, fileSystem, new BroadcastMutantFilter(new[] { mutantFilterMock.Object }), coverageAnalyzerMock.Object, options); // start mutation process target.Mutate(); target.FilterMutants(); // verify that compiler error mutants are not passed to filter mutantsPassedToFilter.ShouldNotContain(compileErrorMutant); // verify that filtered mutants are skipped inputFile.Mutants.ShouldContain(mutantToBeSkipped); mutantToBeSkipped.ResultStatus.ShouldBe(MutantStatus.Ignored); }
/// <summary> /// Mutation Predicate. /// Performs a mutation on chance. /// Mutation occurs if calculated chance is less than required probability, otherwise the DNA is not affected. /// </summary> /// <param name="probability"> /// Probability of mutation occuring. /// Value must be between 0.0 and 1.0 /// </param> /// <returns> /// Either a mutated DNA or unaffected DNA /// </returns> public static Mutant mutate_by_chance(DNA original, getRandomGeneFunct randomGene, double probability = MUTATION_RATE) { Mutant m; int mut_index; // a random point in the DNA at which mutation will occur Random rand = new Random(); if (rand.NextDouble() < probability) { mut_index = rand.Next(1, original.Count); return mutate(mut_index, original, randomGene); } else { m = new Mutant(original); } return m; }
public void ShouldCallExecutorForEveryMutant() { var mutant = new Mutant { Id = 1, MustRunAgainstAllTests = true }; var otherMutant = new Mutant { Id = 2, MustRunAgainstAllTests = true }; string basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test"); var folder = new CsharpFolderComposite(); folder.Add(new CsharpFileLeaf() { SourceCode = SourceFile, Mutants = new List <Mutant>() { mutant, otherMutant } }); var input = new MutationTestInput() { ProjectInfo = new ProjectInfo() { ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" }, { "Language", "C#" } }).Object , ProjectContents = folder }, AssemblyReferences = _assemblies }; var reporterMock = new Mock <IReporter>(MockBehavior.Strict); reporterMock.Setup(x => x.OnMutantTested(It.IsAny <Mutant>())); var runnerMock = new Mock <ITestRunner>(); runnerMock.Setup(x => x.DiscoverNumberOfTests()).Returns(1); var executorMock = new Mock <IMutationTestExecutor>(MockBehavior.Strict); executorMock.SetupGet(x => x.TestRunner).Returns(runnerMock.Object); executorMock.Setup(x => x.Test(It.IsAny <IList <Mutant> >(), It.IsAny <int>(), It.IsAny <TestUpdateHandler>())); var mutantFilterMock = new Mock <IMutantFilter>(MockBehavior.Loose); var options = new StrykerOptions(basePath: basePath, fileSystem: new MockFileSystem()); var target = new MutationTestProcess(input, reporterMock.Object, executorMock.Object, mutantFilter: mutantFilterMock.Object, options: options); target.Test(input.ProjectInfo.ProjectContents.Mutants); executorMock.Verify(x => x.Test(new List <Mutant> { mutant }, It.IsAny <int>(), It.IsAny <TestUpdateHandler>()), Times.Once); }
public void ShouldThrowExceptionWhenOtherStatusThanNotRunIsPassed(MutantStatus status) { var mutant = new Mutant { Id = 1, ResultStatus = status }; var basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test"); var folder = new CsharpFolderComposite(); folder.Add(new CsharpFileLeaf() { Mutants = new Collection <Mutant>() { mutant } }); var input = new MutationTestInput() { ProjectInfo = new ProjectInfo() { ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" } }).Object, TestProjectAnalyzerResults = new List <IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" }, { "Language", "C#" } }).Object }, ProjectContents = folder }, AssemblyReferences = new ReferenceProvider().GetReferencedAssemblies() }; var reporterMock = new Mock <IReporter>(MockBehavior.Strict); reporterMock.Setup(x => x.OnMutantTested(It.IsAny <Mutant>())); var runnerMock = new Mock <ITestRunner>(); runnerMock.Setup(x => x.DiscoverNumberOfTests()).Returns(1); var executorMock = new Mock <IMutationTestExecutor>(MockBehavior.Strict); executorMock.SetupGet(x => x.TestRunner).Returns(runnerMock.Object); executorMock.Setup(x => x.Test(It.IsAny <IList <Mutant> >(), It.IsAny <int>(), It.IsAny <TestUpdateHandler>())); var mutantFilterMock = new Mock <IMutantFilter>(MockBehavior.Loose); var options = new StrykerOptions(basePath: basePath, fileSystem: new MockFileSystem()); var target = new MutationTestProcess(input, reporterMock.Object, executorMock.Object, mutantFilter: mutantFilterMock.Object, options: options); Should.Throw <GeneralStrykerException>(() => target.Test(input.ProjectInfo.ProjectContents.Mutants)); }
public void ShouldCallMutantOrchestratorAndReporter() { var inputFile = new CsharpFileLeaf() { SourceCode = SourceFile, SyntaxTree = CSharpSyntaxTree.ParseText(SourceFile) }; var folder = new CsharpFolderComposite(); folder.Add(inputFile); var input = new MutationTestInput() { ProjectInfo = new ProjectInfo() { ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" }, { "AssemblyName", "AssemblyName" }, { "Language", "C#" } }).Object, TestProjectAnalyzerResults = new List <IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" }, { "AssemblyName", "AssemblyName" }, { "Language", "C#" } }).Object }, ProjectContents = folder }, AssemblyReferences = _assemblies }; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { Path.Combine(FilesystemRoot, "ExampleProject", "Recursive.cs"), new MockFileData(SourceFile) }, { Path.Combine(FilesystemRoot, "ExampleProject.Test", "bin", "Debug", "netcoreapp2.0", "ExampleProject.dll"), new MockFileData("Bytecode") }, { Path.Combine(FilesystemRoot, "ExampleProject.Test", "obj", "Release", "netcoreapp2.0", "ExampleProject.dll"), new MockFileData("Bytecode") } }); var mutantToBeSkipped = new Mutant() { Mutation = new Mutation() }; var mockMutants = new Collection <Mutant>() { new Mutant() { Mutation = new Mutation() }, mutantToBeSkipped }; // create mocks var orchestratorMock = new Mock <MutantOrchestrator <SyntaxNode> >(MockBehavior.Strict); var reporterMock = new Mock <IReporter>(MockBehavior.Strict); var mutationTestExecutorMock = new Mock <IMutationTestExecutor>(MockBehavior.Strict); var coverageAnalyzerMock = new Mock <ICoverageAnalyser>(MockBehavior.Strict); // setup mocks orchestratorMock.Setup(x => x.GetLatestMutantBatch()).Returns(mockMutants); orchestratorMock.Setup(x => x.Mutate(It.IsAny <SyntaxNode>())).Returns(CSharpSyntaxTree.ParseText(SourceFile).GetRoot()); orchestratorMock.SetupAllProperties(); var options = new StrykerOptions(devMode: true, excludedMutations: new string[] { }, fileSystem: new MockFileSystem()); var target = new MutationTestProcess(input, reporterMock.Object, mutationTestExecutorMock.Object, orchestratorMock.Object, fileSystem, new BroadcastMutantFilter(Enumerable.Empty <IMutantFilter>()), coverageAnalyzerMock.Object, options); // start mutation process target.Mutate(); target.FilterMutants(); // verify the right methods were called orchestratorMock.Verify(x => x.Mutate(It.IsAny <SyntaxNode>()), Times.Once); }
public void MutationTestProcess_ShouldNotCallExecutorForNotCoveredMutants() { var mutant = new Mutant { Id = 1, ResultStatus = MutantStatus.Survived }; var otherMutant = new Mutant { Id = 2, ResultStatus = MutantStatus.NotRun }; var basePath = Path.Combine(FilesystemRoot, "ExampleProject.Test"); var input = new MutationTestInput() { ProjectInfo = new ProjectInfo() { TestProjectAnalyzerResult = new ProjectAnalyzerResult(null, null) { AssemblyPath = "/bin/Debug/netcoreapp2.1/TestName.dll", Properties = new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" } } }, ProjectUnderTestAnalyzerResult = new ProjectAnalyzerResult(null, null) { AssemblyPath = "/bin/Debug/netcoreapp2.1/TestName.dll", Properties = new Dictionary <string, string>() { { "TargetDir", "/bin/Debug/netcoreapp2.1" }, { "TargetFileName", "TestName.dll" } } }, ProjectContents = new FolderComposite() { Name = "ProjectRoot", Children = new Collection <ProjectComponent>() { new FileLeaf() { Name = "SomeFile.cs", Mutants = new Collection <Mutant>() { mutant, otherMutant } } } }, }, AssemblyReferences = new ReferenceProvider().GetReferencedAssemblies() }; var reporterMock = new Mock <IReporter>(MockBehavior.Strict); reporterMock.Setup(x => x.OnMutantTested(It.IsAny <Mutant>())); reporterMock.Setup(x => x.OnAllMutantsTested(It.IsAny <ProjectComponent>())); reporterMock.Setup(x => x.OnStartMutantTestRun(It.IsAny <IList <Mutant> >())); var executorMock = new Mock <IMutationTestExecutor>(MockBehavior.Strict); executorMock.SetupGet(x => x.TestRunner).Returns(Mock.Of <ITestRunner>()); executorMock.Setup(x => x.Test(It.IsAny <Mutant>(), It.IsAny <int>())); var options = new StrykerOptions(fileSystem: new MockFileSystem(), basePath: basePath); var target = new MutationTestProcess(input, reporterMock.Object, executorMock.Object); var coverage = new TestCoverageInfos(); coverage.DeclareMappingForATest("toto", new [] { 2 }); target.Optimize(coverage); target.Test(options); reporterMock.Verify(x => x.OnStartMutantTestRun(It.Is <IList <Mutant> >(y => y.Count == 1)), Times.Once); executorMock.Verify(x => x.Test(otherMutant, It.IsAny <int>()), Times.Once); reporterMock.Verify(x => x.OnMutantTested(otherMutant), Times.Once); reporterMock.Verify(x => x.OnAllMutantsTested(It.IsAny <ProjectComponent>()), Times.Once); }
public void RunOnlyUsefulTest() { var options = new StrykerOptions(); using (var endProcess = new EventWaitHandle(false, EventResetMode.ManualReset)) { var mockVsTest = BuildVsTestRunner(options, endProcess, out var runner, OptimizationFlags.CoverageBasedTest); var coverageProperty = TestProperty.Register("Stryker.Coverage", "Coverage", typeof(string), typeof(TestResult)); mockVsTest.Setup(x => x.AbortTestRun()).Verifiable(); mockVsTest.Setup(x => x.RunTestsWithCustomTestHost( It.Is <IEnumerable <string> >(t => t.Any(source => source == _testAssemblyPath)), It.IsAny <string>(), It.IsAny <ITestRunEventsHandler>(), It.IsAny <ITestHostLauncher>())).Callback( (IEnumerable <string> sources, string settings, ITestRunEventsHandler testRunEvents, ITestHostLauncher host) => { Task.Run(() => { // generate coverage results settings.ShouldContain("DataCollector"); var results = new TestResult[_testCases.Length]; for (var i = 0; i < _testCases.Length; i++) { results[i] = new TestResult(_testCases[i]) { Outcome = TestOutcome.Passed, ComputerName = "." }; } // only first test covers one mutant results[0].SetPropertyValue(coverageProperty, "1;"); MoqTestRun(testRunEvents, results); endProcess.Set(); }); }); runner.CaptureCoverage(false, false); mockVsTest.Setup(x => x.RunTestsWithCustomTestHost( // we expect a run with only one test It.Is <IEnumerable <TestCase> >(t => t.Count() == 1), It.IsAny <string>(), It.IsAny <ITestRunEventsHandler>(), It.IsAny <ITestHostLauncher>())).Callback( (IEnumerable <TestCase> sources, string settings, ITestRunEventsHandler testRunEvents, ITestHostLauncher host) => { settings.ShouldNotContain("DataCollector"); // setup a normal test run var results = sources.Select(test => new TestResult(test) { Outcome = TestOutcome.Passed, ComputerName = "." }).ToList(); results.ShouldHaveSingleItem(); MoqTestRun(testRunEvents, results); endProcess.Set(); }); var mutant = new Mutant { Id = 1 }; var mutants = new List <Mutant> { mutant }; // process coverage information runner.CoverageMutants.UpdateMutants(mutants, _testCases.Length); var result = runner.RunAll(0, mutant); // verify Abort has been called result.Success.ShouldBe(true); } }
// initialize the test context and mock objects public VsTestRunnersShould() { var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var filesystemRoot = Path.GetPathRoot(currentDirectory); var sourceFile = File.ReadAllText(currentDirectory + "/TestResources/ExampleSourceFile.cs"); var testProjectPath = FilePathUtils.NormalizePathSeparators(Path.Combine(filesystemRoot, "TestProject", "TestProject.csproj")); var projectUnderTestPath = FilePathUtils.NormalizePathSeparators(Path.Combine(filesystemRoot, "ExampleProject", "ExampleProject.csproj")); const string defaultTestProjectFileContents = @"<Project Sdk=""Microsoft.NET.Sdk""> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <IsPackable>false</IsPackable> </PropertyGroup> <ItemGroup> <PackageReference Include=""Microsoft.NET.Test.Sdk"" Version = ""15.5.0"" /> <PackageReference Include=""xunit"" Version=""2.3.1"" /> <PackageReference Include=""xunit.runner.visualstudio"" Version=""2.3.1"" /> <DotNetCliToolReference Include=""dotnet-xunit"" Version=""2.3.1"" /> </ItemGroup> <ItemGroup> <ProjectReference Include=""..\ExampleProject\ExampleProject.csproj"" /> </ItemGroup> </Project>"; _testAssemblyPath = Path.Combine(filesystemRoot, "_firstTest", "bin", "Debug", "TestApp.dll"); _executorUri = new Uri("exec://nunit"); var firstTest = new TestCase("T0", _executorUri, _testAssemblyPath); var secondTest = new TestCase("T1", _executorUri, _testAssemblyPath); var content = new FolderComposite(); _coverageProperty = TestProperty.Register("Stryker.Coverage", "Coverage", typeof(string), typeof(TestResult)); _mutant = new Mutant { Id = 0 }; _otherMutant = new Mutant { Id = 1 }; _projectContents = content; _projectContents.Add(new FileLeaf { Mutants = new[] { _otherMutant, _mutant } }); _targetProject = new ProjectInfo() { TestProjectAnalyzerResults = new List <IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult( properties: new Dictionary <string, string>() { { "TargetDir", Path.GetDirectoryName(_testAssemblyPath) }, { "TargetFileName", Path.GetFileName(_testAssemblyPath) } }, targetFramework: "toto").Object }, ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult( properties: new Dictionary <string, string>() { { "TargetDir", Path.Combine(filesystemRoot, "app", "bin", "Debug") }, { "TargetFileName", "AppToTest.dll" } }).Object, ProjectContents = _projectContents }; //CodeInjection.HelperNamespace = "Stryker.Core.UnitTest.TestRunners"; _fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { projectUnderTestPath, new MockFileData(defaultTestProjectFileContents) }, { Path.Combine(filesystemRoot, "ExampleProject", "Recursive.cs"), new MockFileData(sourceFile) }, { Path.Combine(filesystemRoot, "ExampleProject", "OneFolderDeeper", "Recursive.cs"), new MockFileData(sourceFile) }, { testProjectPath, new MockFileData(defaultTestProjectFileContents) }, { _testAssemblyPath, new MockFileData("Bytecode") }, { Path.Combine(filesystemRoot, "app", "bin", "Debug", "AppToTest.dll"), new MockFileData("Bytecode") }, }); _testCases = new List <TestCase> { firstTest, secondTest }; }
public void RunAllTestsOnStatic() { var options = new StrykerOptions(); using (var endProcess = new EventWaitHandle(false, EventResetMode.ManualReset)) { var mockVsTest = BuildVsTestRunner(options, endProcess, out var runner, OptimizationFlags.CoverageBasedTest); var coverageProperty = TestProperty.Register("Stryker.Coverage", "Coverage", typeof(string), typeof(TestResult)); mockVsTest.Setup(x => x.AbortTestRun()).Verifiable(); mockVsTest.Setup(x => x.RunTestsWithCustomTestHost( It.Is <IEnumerable <string> >(t => t.Any(source => source == _testAssemblyPath)), It.Is <string>(settings => settings.Contains("DataCollector")), It.IsAny <ITestRunEventsHandler>(), It.IsAny <ITestHostLauncher>())).Callback( (IEnumerable <string> sources, string settings, ITestRunEventsHandler testRunEvents, ITestHostLauncher host) => { Task.Run(() => { // provides coverage result with static mutant var results = new TestResult[_testCases.Length]; for (var i = 0; i < _testCases.Length; i++) { results[i] = new TestResult(_testCases[i]) { Outcome = TestOutcome.Passed, ComputerName = "." }; } results[0].SetPropertyValue(coverageProperty, "1;1"); MoqTestRun(testRunEvents, results); endProcess.Set(); }); }); runner.CaptureCoverage(false, false); mockVsTest.Setup(x => x.RunTestsWithCustomTestHost( It.IsAny <IEnumerable <string> >(), It.Is <string>(settings => !settings.Contains("DataCollector")), It.IsAny <ITestRunEventsHandler>(), It.IsAny <ITestHostLauncher>())).Callback( (IEnumerable <string> sources, string settings, ITestRunEventsHandler testRunEvents, ITestHostLauncher host) => { var results = _testCases.Select(test => new TestResult(test) { Outcome = TestOutcome.Failed, ComputerName = "." }).ToList(); // we expect only one test results.Count.ShouldBe(2); MoqTestRun(testRunEvents, results); endProcess.Set(); }); var mutant = new Mutant { Id = 1 }; var otherMutant = new Mutant { Id = 0 }; var mutants = new List <Mutant> { mutant, otherMutant }; // process coverage info runner.CoverageMutants.UpdateMutants(mutants, _testCases.Length); // mutant 1 is covered mutant.MustRunAllTests.ShouldBeTrue(); var result = runner.RunAll(0, mutant); // mutant is killed result.Success.ShouldBe(false); // mutant 0 is not covered result = runner.RunAll(0, otherMutant); // tests are ok result.Success.ShouldBe(true); } }
public async Task<MutationResult> ExecuteMutation(Mutant mutant, List<CciModuleSource> moduleSource) { //Ta druga funkcja _log.Debug("ExecuteMutation in object: " + ToString() + GetHashCode()); IMutationOperator mutationOperator = mutant.MutationTarget.OperatorId == null ? new IdentityOperator() : _mutOperators.Single(m => mutant.MutationTarget.OperatorId == m.Info.Id); try { _log.Info("Execute mutation of " + mutant.MutationTarget + " contained in " + mutant.MutationTarget.MethodRaw + " modules. "); var mutatedModules = new List<IModuleInfo>(); foreach (var cci in moduleSource) { var module = cci.Module; var visitorBack = new VisualCodeVisitorBack(mutant.MutationTarget.InList(), _sharedTargets.GetValues(mutationOperator, returnEmptySet: true), module.Module, mutationOperator.Info.Id); var traverser2 = new VisualCodeTraverser(_filter, visitorBack, cci); 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<IMethodDefinition> methodMutated = new List<IMethodDefinition>(); methodMutated.Add(mutant.MutationTarget.MethodMutated); var result = new MutationResult(mutant, null, moduleSource, 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); } }
public void RunRelevantTestsOnStaticWhenPerTestCoverage() { var options = new StrykerOptions(); using (var endProcess = new EventWaitHandle(false, EventResetMode.ManualReset)) { var mockVsTest = BuildVsTestRunner(options, endProcess, out var runner, OptimizationFlags.CoverageBasedTest | OptimizationFlags.CaptureCoveragePerTest); var coverageProperty = TestProperty.Register("Stryker.Coverage", "Coverage", typeof(string), typeof(TestResult)); mockVsTest.Setup(x => x.AbortTestRun()).Verifiable(); mockVsTest.Setup(x => x.RunTestsWithCustomTestHost( It.Is <IEnumerable <string> >(t => t.Any(source => source == _testAssemblyPath)), It.IsAny <string>(), It.IsAny <ITestRunEventsHandler>(), It.IsAny <ITestHostLauncher>())).Callback( (IEnumerable <string> sources, string settings, ITestRunEventsHandler testRunEvents, ITestHostLauncher host) => { settings.ShouldContain("DataCollector"); var results = new TestResult[_testCases.Length]; for (var i = 0; i < _testCases.Length; i++) { results[i] = new TestResult(_testCases[i]) { Outcome = TestOutcome.Passed, ComputerName = "." }; } results[0].SetPropertyValue(coverageProperty, "0,1;1"); MoqTestRun(testRunEvents, results); endProcess.Set(); }); runner.CaptureCoverage(false, false); mockVsTest.Setup(x => x.RunTestsWithCustomTestHost( It.Is <IEnumerable <TestCase> >(t => t.Count() == 1), It.IsAny <string>(), It.IsAny <ITestRunEventsHandler>(), It.IsAny <ITestHostLauncher>())).Callback( (IEnumerable <TestCase> sources, string settings, ITestRunEventsHandler testRunEvents, ITestHostLauncher host) => { settings.ShouldNotContain("DataCollector"); var results = new List <TestResult>(); foreach (var test in sources) { results.Add(new TestResult(test) { Outcome = TestOutcome.Passed, ComputerName = "." }); } results.ShouldHaveSingleItem(); MoqTestRun(testRunEvents, results); endProcess.Set(); }); var mutant = new Mutant { Id = 1 }; runner.CoverageMutants.GetTests(mutant).ShouldHaveSingleItem(); var otherMutant = new Mutant { Id = 0 }; foreach (var testDescription in runner.CoverageMutants.GetTests(otherMutant)) { otherMutant.CoveringTest[testDescription.Guid] = false; } var result = runner.RunAll(0, otherMutant); // verify Abort has been called result.Success.ShouldBe(true); } }