public MutantOrchestratorTests() { _target = new MutantOrchestrator(new Collection <IMutator> { new AddMutator() }); _currentDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); }
protected override BasePropertyDeclarationSyntax OrchestrateChildrenMutation(PropertyDeclarationSyntax node, MutationContext context) { if (!node.IsStatic()) { return(base.OrchestrateChildrenMutation(node, context)); } return(node.ReplaceNodes(node.ChildNodes(), (original, _) => MutantOrchestrator.Mutate(original, original == node.Initializer ? context.EnterStatic() : context))); }
public CsharpMutationProcess(MutationTestInput mutationTestInput, IFileSystem fileSystem = null, IStrykerOptions options = null, IMutantFilter mutantFilter = null, MutantOrchestrator <SyntaxNode> orchestrator = null) { _input = mutationTestInput; _projectInfo = (ProjectComponent <SyntaxTree>)mutationTestInput.ProjectInfo.ProjectContents; _options = options; _orchestrator = orchestrator ?? new CsharpMutantOrchestrator(options: _options); _compilingProcess = new CompilingProcess(mutationTestInput, new RollbackProcess()); _fileSystem = fileSystem ?? new FileSystem(); _logger = ApplicationLogging.LoggerFactory.CreateLogger <MutationTestProcess>(); _mutantFilter = mutantFilter ?? MutantFilterFactory.Create(options); }
public void Mutator_TestResourcesInputShouldBecomeOutputForFullScope(string inputFile, string outputFile, int nbMutants, int mutant1Id, int mutant1Location, int mutant2Id, int mutant2Location) { string source = File.ReadAllText(CurrentDirectory + "/Mutants/TestResources/" + inputFile); string expected = File.ReadAllText(CurrentDirectory + "/Mutants/TestResources/" + outputFile).Replace("StrykerNamespace", CodeInjection.HelperNamespace); var target = new MutantOrchestrator(); var actualNode = target.Mutate(CSharpSyntaxTree.ParseText(source).GetRoot()); var expectedNode = CSharpSyntaxTree.ParseText(expected).GetRoot(); actualNode.ShouldBeSemantically(expectedNode); actualNode.ShouldNotContainErrors(); var mutants = target.GetLatestMutantBatch().ToList(); mutants.Count.ShouldBe(nbMutants); mutants[mutant1Id].Mutation.OriginalNode.GetLocation().GetLineSpan().StartLinePosition.Line.ShouldBe(mutant1Location); mutants[mutant2Id].Mutation.OriginalNode.GetLocation().GetLineSpan().StartLinePosition.Line.ShouldBe(mutant2Location); }
public void ShouldMutateStaticConstructor(string source) { source = @"static string Value { get; } static TestClass() " + source; var expected = @"static string Value { get; } static TestClass() {using(new StrykerNamespace.MutantContext()){Value = (StrykerNamespace.MutantControl.IsActive(0)?"""":""Hello, World!"");}}"; expected = expected.Replace("StrykerNamespace", CodeInjection.HelperNamespace); var orchestrator = new MutantOrchestrator(options: new StrykerOptions()); var actualNode = orchestrator.Mutate(CSharpSyntaxTree.ParseText(source).GetRoot()); var expectedNode = CSharpSyntaxTree.ParseText(expected).GetRoot(); actualNode.ShouldBeSemantically(expectedNode); actualNode.ShouldNotContainErrors(); }
private IList <Mutant> GetMethodMutants(string method) { var methodSyntax = _class .DescendantNodes <MethodDeclarationSyntax>() .FirstOrDefault(x => x.MethodName() == method); if (methodSyntax != null) { var mutantOrchestrator = new MutantOrchestrator(); var syntaxNodeAnalysisFactory = new SyntaxNodeAnalysisFactory(); var classDeclaration = new ClassDeclaration(_class); var syntaxNodeAnalysis = syntaxNodeAnalysisFactory.Create(methodSyntax, classDeclaration); mutantOrchestrator.Mutate(syntaxNodeAnalysis); return(mutantOrchestrator.GetLatestMutantBatch().ToList()); } return(new List <Mutant>()); }
public MutationTestProcess(MutationTestInput mutationTestInput, IReporter reporter, IMutationTestExecutor mutationTestExecutor, MutantOrchestrator <SyntaxNode> orchestrator = null, IFileSystem fileSystem = null, IMutantFilter mutantFilter = null, ICoverageAnalyser coverageAnalyser = null, IStrykerOptions options = null) { Input = mutationTestInput; _projectContents = mutationTestInput.ProjectInfo.ProjectContents; _reporter = reporter; _options = options; _mutationTestExecutor = mutationTestExecutor; _logger = ApplicationLogging.LoggerFactory.CreateLogger <MutationTestProcess>(); _coverageAnalyser = coverageAnalyser ?? new CoverageAnalyser(_options, _mutationTestExecutor, Input); _mutationProcess = new CsharpMutationProcess(Input, fileSystem ?? new FileSystem(), _options, mutantFilter, _reporter, orchestrator); }
public void ShouldMutateStaticProperties() { var source = @"class Test { static string Value => """"; static TestClass(){}}"; var expected = @"class Test { static string Value => (StrykerNamespace.MutantControl.IsActive(0)?""Stryker was here!"":""""); static TestClass(){using(new StrykerNamespace.MutantContext()){}}}"; var orchestrator = new MutantOrchestrator(options: new StrykerOptions()); var actualNode = orchestrator.Mutate(CSharpSyntaxTree.ParseText(source).GetRoot()); expected = expected.Replace("StrykerNamespace", CodeInjection.HelperNamespace); var expectedNode = CSharpSyntaxTree.ParseText(expected).GetRoot(); actualNode.ShouldBeSemantically(expectedNode); actualNode.ShouldNotContainErrors(); }
private async Task Initialization(SourceClassDetail source) { var defaultMutants = MutantOrchestrator.GetDefaultMutants(source.Claz.Syntax, source.Claz); await InitItemSources(source); if ((defaultMutants.Any() || string.IsNullOrWhiteSpace(ProcessWholeProject)) && !UseExternalCodeCoverage) { if (TestExecutionTime > -1) { await FindTestExecutionTime(source); } await ExecuteTests(source); } await InitializeMutants(source); await AnalyzeMutant(source); }
/// <inheritdoc/> /// `<remarks>The sole benefit of this orchestrator is to provide code ordered mutations for now.</remarks> protected override StatementSyntax OrchestrateChildrenMutation(ForStatementSyntax forStatement, MutationContext context) { // for needs special treatments for its incrementer(s) var originalFor = forStatement; forStatement = originalFor.ReplaceNodes(originalFor.Initializers.Union(originalFor.Incrementors), (syntax, expressionSyntax) => MutantOrchestrator.Mutate(syntax, context)); if (forStatement.Declaration != null) { forStatement = forStatement.ReplaceNode(forStatement.Declaration, MutantOrchestrator.Mutate(originalFor.Declaration, context)); } // mutate condition, if any if (originalFor.Condition != null) { forStatement = forStatement.ReplaceNode(forStatement.Condition !, MutantOrchestrator.Mutate(originalFor.Condition, context)); } // mutate the statement/block forStatement = forStatement.ReplaceNode(forStatement.Statement, MutantOrchestrator.Mutate(originalFor.Statement, context)); return(forStatement); }
private static IList <ClassCoverage> FindExternalCoveredClasses(SourceClassDetail source, CoverageDSPriv codeCoverage) { var data = new List <ClassCoverage>(); var thirdPartyLibs = source.TestClaz.ClassProject .GetProjectThirdPartyLibraries() .Select(x => x .Split('\\') .Last()).ToList(); thirdPartyLibs.Add("nunit"); thirdPartyLibs.Add("microsoft."); if (codeCoverage != null) { var parentClassNameList = $"{source.Claz.Syntax.NameSpace()}.{string.Join(".", source.Claz.Syntax.Ancestors<ClassDeclarationSyntax>().Select(x => x.ClassNameWithoutGeneric()))}".TrimEnd('.'); var nestedClassNameList = $"{parentClassNameList}.{source.Claz.Syntax.ClassNameWithoutGeneric()}.{string.Join(".", source.Claz.Syntax.DescendantNodes<ClassDeclarationSyntax>().Select(x => x.ClassNameWithoutGeneric()))}".TrimEnd('.'); if (parentClassNameList == source.Claz.Syntax.NameSpace()) { parentClassNameList = $"{parentClassNameList}.{source.Claz.Syntax.ClassNameWithoutGeneric()}"; } foreach (CoverageDSPriv.ClassRow claz in codeCoverage.Class) { if (claz.LinesCovered > 0 && thirdPartyLibs.All(x => !claz.NamespaceTableRow.ModuleRow.ModuleName.StartsWith(x, StringComparison.InvariantCultureIgnoreCase))) { var className = claz.ClassName; var genericIndexLocation = claz.ClassName.IndexOf(GenericMethodStart, StringComparison.Ordinal); if (genericIndexLocation != -1) { className = className.Substring(0, genericIndexLocation).TrimEnd('.'); } var fullName = $"{claz.NamespaceTableRow.NamespaceName}.{className}"; if (data.All(x => x.ClassName != fullName) && !fullName.Contains(parentClassNameList) && !fullName.Contains(nestedClassNameList)) { var coverages = codeCoverage .Class .Where(x => x.ClassName == className || x.ClassName.StartsWith($"{className}.{GenericMethodStart}") || x.ClassName.StartsWith($"{className}{GenericMethodStart}")).ToList(); coverages = coverages.Where(x => x.NamespaceTableRow.NamespaceKeyName == claz.NamespaceKeyName).ToList(); if (coverages.Any()) { var methods = codeCoverage.Method.Where(x => x.ClassKeyName == claz.ClassKeyName).ToList(); var method = methods.FirstOrDefault(); var numberOfMutants = 0; var excluded = false; var mutantsLines = new List <int>(); uint autogeneratedLineCovered = 0; uint autogeneratedLineNonCovered = 0; uint autogeneratedBlockCovered = 0; uint autogeneratedBlockNonCovered = 0; var file = string.Empty; if (method != null) { file = codeCoverage.SourceFileNames.FirstOrDefault(x => x.SourceFileID == method.GetLinesRows().FirstOrDefault()?.SourceFileID)?.SourceFileName; if (!string.IsNullOrWhiteSpace(file) && File.Exists(file)) { var root = file.GetCodeFileContent().RootNode().ClassNode(className.Split('.').Last()); var classDeclaration = new ClassDeclaration(root); var classDetail = new SourceClassDetail { Claz = classDeclaration, TestClaz = new TestClassDetail() }; if (root != null) { new MethodsInitializer().FindMethods(classDetail).Wait(); var mutants = classDetail.MethodDetails .Where(x => !x.IsProperty && !x.IsConstructor && !x.IsOverrideMethod) .SelectMany(x => MutantOrchestrator.GetDefaultMutants(x.Method, classDetail.Claz)); var coveredLines = claz.GetMethodRows().SelectMany(x => x.GetLinesRows()).Where(x => x.Coverage == 0).ToList(); mutants = mutants.Where(x => coveredLines.Any(y => y.LnStart == x.Mutation.Location)).ToList(); mutantsLines = mutants.Select(x => x.Mutation.Location ?? 0).ToList(); numberOfMutants = mutants.Count(); excluded = root.ExcludeFromExternalCoverage(); var autogeneratedMethods = root.GetGeneratedCodeMethods(); foreach (var methodSyntax in autogeneratedMethods) { var autoGeneratedCoverage = methods.FirstOrDefault(x => x.MethodFullName.Equals($"{methodSyntax.MethodName()}()") || x.MethodName.Equals($"{methodSyntax.MethodName()}()")); if (autoGeneratedCoverage != null) { autogeneratedLineCovered += autoGeneratedCoverage.LinesCovered; autogeneratedLineNonCovered += autoGeneratedCoverage.LinesNotCovered; autogeneratedBlockCovered += autoGeneratedCoverage.BlocksCovered; autogeneratedBlockNonCovered += autoGeneratedCoverage.BlocksNotCovered; } } if (methods.Any(x => x.MethodFullName.Equals($"{InitializecomponentMethod}()")) && !autogeneratedMethods.Any() && !classDetail.MethodDetails.Any(x => x.Method.MethodName().Equals(InitializecomponentMethod))) { var autoGeneratedCoverage = methods.First(x => x.MethodFullName.Equals($"{InitializecomponentMethod}()")); autogeneratedLineCovered += autoGeneratedCoverage.LinesCovered; autogeneratedLineNonCovered += autoGeneratedCoverage.LinesNotCovered; autogeneratedBlockCovered += autoGeneratedCoverage.BlocksCovered; autogeneratedBlockNonCovered += autoGeneratedCoverage.BlocksNotCovered; } } else { excluded = true; } } } var classCoverage = new ClassCoverage { ClassName = fullName, ClassPath = file, Coverage = new Coverage { LinesCovered = (uint)coverages.Sum(x => x.LinesCovered) - autogeneratedLineCovered, LinesNotCovered = (uint)coverages.Sum(x => x.LinesNotCovered) - autogeneratedLineNonCovered, BlocksCovered = (uint)coverages.Sum(x => x.BlocksCovered) - autogeneratedBlockCovered, BlocksNotCovered = (uint)coverages.Sum(x => x.BlocksNotCovered) - autogeneratedBlockNonCovered }, NumberOfMutants = numberOfMutants, Excluded = excluded }; classCoverage.MutantsLines.AddRange(mutantsLines); data.Add(classCoverage); } } } } } return(data); }
public ExpressionSpecificOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public StaticFieldDeclarationOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public StatementSpecificOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public async Task InitializeMutants(IList <IMutator> selectedMutators) { if (selectedMutators == null || !selectedMutators.Any()) { return; } var mutatorFinder = new MutantOrchestrator(selectedMutators); var id = 1; foreach (var method in _source.MethodDetails) { method.TestMethods.Clear(); method.Mutants.Clear(); var syntaxNodeAnalysis = SyntaxNodeAnalysisFactory.Create(method.Method, _source.Claz); mutatorFinder.Mutate(syntaxNodeAnalysis); var latestMutantBatch = mutatorFinder.GetLatestMutantBatch().ToList(); latestMutantBatch = _selector.SelectMutants(MutantsPerLine, latestMutantBatch).ToList(); foreach (var mutant in latestMutantBatch) { mutant.Method = method; } method.Mutants.AddRange(latestMutantBatch); foreach (var mutant in method.Mutants) { mutant.Id = id++; } FilterMutants(method); await Task.Run(() => { foreach (var testMethod in _source.TestClaz.MethodDetails) { if (method.Coverage?.LinesCovered == 0) { break; } var sourceMethodName = method.Method.MethodName(); var className = method.Method.Class().ClassName(); if (!ExecuteAllTests && !method.IsProperty) { if (testMethod.Method.ValidTestMethod(className, sourceMethodName, _source.TestClaz.Claz.Syntax)) { method.TestMethods.Add(testMethod); continue; } var methods = method.Method.Class().Methods(); if (methods != null) { foreach (var classMethod in methods) { var methodName = classMethod.MethodName(); if (classMethod.ChildMethodNames().Any(x => x.Contains(sourceMethodName)) && testMethod.Method.ValidTestMethod(className, methodName, _source.TestClaz.Claz.Syntax)) { method.TestMethods.Add(testMethod); if (!method.ParentMethodNames.Contains(methodName)) { method.ParentMethodNames.Add(methodName); } } } } } else { method.TestMethods.Add(testMethod); } } if (!method.TestMethods.Any() && method.Coverage?.LinesCovered > 0) { method.TestMethods.AddRange(_source.TestClaz.MethodDetails); } }); foreach (var sourceMethod in _source.MethodDetails) { var parentMethods = _source .MethodDetails .Where(x => sourceMethod.ParentMethodNames.Contains(x.Method.MethodName())).ToList(); foreach (MethodDetail parentMethod in parentMethods) { foreach (var testMethod in parentMethod.TestMethods) { if (sourceMethod.TestMethods.All(x => x.Method.MethodName() != testMethod.Method.MethodName())) { sourceMethod.TestMethods.Add(testMethod); } } } } var uncoveredLines = new List <CoverageDSPriv.LinesRow>(); var containTests = method.TestMethods.Any(); if (containTests) { uncoveredLines = method .Lines .Where(x => x.Coverage > 0).ToList(); } foreach (var mutant in method.Mutants) { if (containTests && uncoveredLines.Any()) { if (uncoveredLines.Any(x => x.LnStart == mutant.Mutation.Location)) { mutant.ResultStatus = MutantStatus.NotCovered; } } if (!containTests) { mutant.ResultStatus = MutantStatus.NotCovered; } } } }
public ForStatementOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public SyntaxNodeOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public StaticConstructorOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public PostfixUnaryExpressionOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public ArrayInitializerOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public MutantOrchestratorTests() { _target = new MutantOrchestrator(options: new StrykerOptions(mutationLevel: MutationLevel.Complete.ToString())); }
public BaseMethodDeclarationOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public BlockOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
protected NodeSpecificOrchestrator(MutantOrchestrator mutantOrchestrator) { MutantOrchestrator = mutantOrchestrator; }
public ConstLocalDeclarationOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public AssignmentStatementOrchestrator(MutantOrchestrator mutantOrchestrator) : base(mutantOrchestrator) { }
public MutantOrchestratorTests() { _target = new MutantOrchestrator(); }
public async Task FindMethods(SourceClassDetail source) { if (source.Claz == null) { return; } source.MethodDetails.Clear(); var index = 1; IList <MethodDeclarationSyntax> sourceMethods; IList <ConstructorDeclarationSyntax> constructorMethods; IList <PropertyDeclarationSyntax> sourceProperties; if (IncludeNestedClasses) { sourceMethods = source .Claz .Syntax .Root() .GetMethods() .OrderBy(x => x.MethodName()).ToList(); constructorMethods = source .Claz .Syntax .Root() .DescendantNodes <ConstructorDeclarationSyntax>() .OrderBy(x => x.MethodName()) .ToList(); sourceProperties = source .Claz .Syntax .Root() .DescendantNodes <PropertyDeclarationSyntax>() .Where(x => MutantOrchestrator.GetDefaultMutants(x, source.Claz).Any()) .OrderBy(x => x.Identifier.ValueText) .ToList(); } else { var parentClassNodesCount = source.Claz?.Syntax.AncestorsAndSelf().OfType <ClassDeclarationSyntax>().Count() ?? 0; sourceMethods = source .Claz .Syntax .GetMethods() .Where(x => x.Ancestors <ClassDeclarationSyntax>().Count == parentClassNodesCount) .OrderBy(x => x.MethodName()) .ToList(); constructorMethods = source .Claz .Syntax .DescendantNodes <ConstructorDeclarationSyntax>() .Where(x => x.Ancestors <ClassDeclarationSyntax>().Count == parentClassNodesCount) .OrderBy(x => x.MethodName()) .ToList(); sourceProperties = source .Claz .Syntax .DescendantNodes <PropertyDeclarationSyntax>() .Where(x => x.Ancestors <ClassDeclarationSyntax>().Count == parentClassNodesCount) .Where(x => MutantOrchestrator.GetDefaultMutants(x, source.Claz).Any()) .OrderBy(x => x.Identifier.ValueText) .ToList(); } source.MethodDetails.AddRange(sourceMethods .Select(x => new MethodDetail { MethodName = $"{index++}. {x.MethodName()}{x.DescendantNodes<ParameterListSyntax>().FirstOrDefault()}", Method = x, IsOverrideMethod = x.IsOverride() })); source.MethodDetails.AddRange(constructorMethods .Select(x => new MethodDetail { MethodName = $"{index++}. {x.MethodName()}{x.DescendantNodes<ParameterListSyntax>().FirstOrDefault()}", Method = x, IsConstructor = true })); source.MethodDetails.AddRange(sourceProperties .Select(x => new MethodDetail { MethodName = $"{index++}. Property - {x.Identifier.ValueText}", Method = x, IsProperty = true })); source.TestClaz.MethodDetails.Clear(); if (source.TestClaz.BaseClass != null) { source.TestClaz.MethodDetails.AddRange(GetTestMethods(source.TestClaz.BaseClass)); } foreach (var partialClass in source.TestClaz.PartialClasses) { source.TestClaz.MethodDetails.AddRange(GetTestMethods(partialClass.Claz.Syntax)); } }