public void MutationsShouldHaveLinespan() { string source = @"void TestMethod() { var test3 = 2 + 5; }"; _target.Mutate(CSharpSyntaxTree.ParseText(source).GetRoot()); var mutants = _target.GetLatestMutantBatch().ToList(); mutants.ShouldHaveSingleItem().Mutation.OriginalNode.GetLocation().GetLineSpan().StartLinePosition.Line.ShouldBe(2); }
public void ShouldRollBackFailedConstructor() { var source = @"class Test { static TestClass()=> Value-='a';}"; var orchestrator = new CsharpMutantOrchestrator(options: new StrykerOptions { OptimizationMode = OptimizationModes.CoverageBasedTest, MutationLevel = MutationLevel.Complete }); var actualNode = orchestrator.Mutate(CSharpSyntaxTree.ParseText(source).GetRoot()); var node = actualNode.DescendantNodes().First(t => t is BlockSyntax); // Remove marker var restored = MutantPlacer.RemoveMutant(node); actualNode = actualNode.ReplaceNode(node, restored); // remove mutation node = actualNode.DescendantNodes().First(t => t.IsKind(SyntaxKind.IfStatement)); restored = MutantPlacer.RemoveMutant(node); actualNode = actualNode.ReplaceNode(node, restored); // remove expression to body conversion node = actualNode.DescendantNodes().First(t => t is ConstructorDeclarationSyntax); restored = MutantPlacer.RemoveMutant(node); actualNode = actualNode.ReplaceNode(node, restored); var expectedNode = CSharpSyntaxTree.ParseText(source.Replace("StrykerNamespace", CodeInjection.HelperNamespace)).GetRoot(); expectedNode.ShouldNotContainErrors(); actualNode.ShouldBeSemantically(expectedNode); actualNode.ShouldNotContainErrors(); }
protected void ShouldMutateSourceToExpected(string actual, string expected) { actual = @"using System; using System.Collections.Generic; using System.Text; namespace StrykerNet.UnitTest.Mutants.TestResources { class TestClass {" + actual + "}}"; expected = @"using System; using System.Collections.Generic; using System.Text; namespace StrykerNet.UnitTest.Mutants.TestResources { class TestClass {" + expected + "}}"; var actualNode = _target.Mutate(CSharpSyntaxTree.ParseText(actual).GetRoot()); actual = actualNode.ToFullString(); actual = actual.Replace(CodeInjection.HelperNamespace, "StrykerNamespace"); actualNode = CSharpSyntaxTree.ParseText(actual).GetRoot(); var expectedNode = CSharpSyntaxTree.ParseText(expected).GetRoot(); actualNode.ShouldBeSemantically(expectedNode); actualNode.ShouldNotContainErrors(); }
protected void ShouldMutateSourceToExpected(string actual, string expected) { var actualNode = _target.Mutate(CSharpSyntaxTree.ParseText(actual).GetRoot()); actual = actualNode.ToFullString(); actual = actual.Replace(CodeInjection.HelperNamespace, "StrykerNamespace"); actualNode = CSharpSyntaxTree.ParseText(actual).GetRoot(); var expectedNode = CSharpSyntaxTree.ParseText(expected).GetRoot(); actualNode.ShouldBeSemantically(expectedNode); actualNode.ShouldNotContainErrors(); }
/// <summary> /// Mutate children, grandchildren (recursively). /// </summary> /// <param name="node">Node which children will be mutating</param> /// <param name="context">Mutation status</param> /// <returns>A <see cref="TBase"/> instance with the mutated children.</returns> /// <remarks>Override this method if you want to control how the node's children are mutated. simply return <see cref="node"/> if you want to /// skip mutation the children node.</remarks> protected virtual TBase OrchestrateChildrenMutation(TNode node, MutationContext context) => node.ReplaceNodes(node.ChildNodes(), (original, _) => MutantOrchestrator.Mutate(original, context));
public void ShouldRollbackIssueInExpression() { var syntaxTree = CSharpSyntaxTree.ParseText(@" using System; using System.Collections.Generic; using System.Linq; namespace ExampleProject { public class Test { public void SomeLinq() { var list = new List<List<double>>(); int[] listProjected = list.Select(l => l.Count()).ToArray(); } } }"); var mutator = new CsharpMutantOrchestrator(options: new StrykerOptions(mutationLevel: MutationLevel.Complete.ToString(), devMode: true)); var helpers = new List <SyntaxTree>(); foreach (var(name, code) in CodeInjection.MutantHelpers) { helpers.Add(CSharpSyntaxTree.ParseText(code, path: name, encoding: Encoding.UTF32)); } var mutant = mutator.Mutate(syntaxTree.GetRoot()); helpers.Add(mutant.SyntaxTree); var references = new List <PortableExecutableReference>() { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(List <string>).Assembly.Location), MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), MetadataReference.CreateFromFile(typeof(PipeStream).Assembly.Location), }; Assembly.GetEntryAssembly().GetReferencedAssemblies().ToList().ForEach(a => references.Add(MetadataReference.CreateFromFile(Assembly.Load(a).Location))); var input = new MutationTestInput() { ProjectInfo = new ProjectInfo() { ProjectUnderTestAnalyzerResult = TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "TargetDir", "" }, { "AssemblyName", "AssemblyName" }, { "TargetFileName", "TargetFileName.dll" }, { "SignAssembly", "true" }, { "AssemblyOriginatorKeyFile", Path.GetFullPath(Path.Combine("TestResources", "StrongNameKeyFile.snk")) } }, projectFilePath: "TestResources").Object, TestProjectAnalyzerResults = new List <IAnalyzerResult> { TestHelper.SetupProjectAnalyzerResult(properties: new Dictionary <string, string>() { { "AssemblyName", "AssemblyName" }, }).Object } }, AssemblyReferences = references }; var rollbackProcess = new RollbackProcess(); var target = new CompilingProcess(input, rollbackProcess); using (var ms = new MemoryStream()) { var result = target.Compile(helpers, ms, null, true); result.RollbackResult.RollbackedIds.Count().ShouldBe(1); } }