private async Task <SurvivingMutant> CreateSurvivingMutantFromExpression(
            string originalExpression,
            string mutatedExpression)
        {
            var originalDocument = SourceToDocument(
                $@"namespace DummyNamespace
{{
    public static class DummyClass
    {{
        public static bool IsPositive(int a)
        {{
            return {originalExpression};
        }}
    }}
}}");
            var originalSyntaxRoot = await originalDocument.GetSyntaxRootAsync();

            var originalNode = originalSyntaxRoot.DescendantNodes().OfType <BinaryExpressionSyntax>().First();

            var mutatedNode = SyntaxFactory.ParseExpression(mutatedExpression);

            var mutatedRoot = originalSyntaxRoot.ReplaceNode(originalNode, mutatedNode);

            return(await SurvivingMutant.Create(originalDocument, originalNode, mutatedRoot));
        }
Exemplo n.º 2
0
        public async Task <SurvivingMutant> Run(ITestRunner testRunner, string tempDirectory, IEventListener eventListener)
        {
            var mutatedNode = mutator.Mutate(OriginalNode);

            MutatedSyntaxRoot = originalSyntaxRoot.ReplaceNode(OriginalNode, mutatedNode);

            var compilationResult = await CompileContainingProject(tempDirectory);

            if (!compilationResult.Success)
            {
                // Not all mutations are valid in all circumstances, and therefore may not compile.
                // E.g. "a + b" => "a - b" works when a and b are integers but not when they're strings.
                return(null);
            }

            CopyMutatedAssemblyIntoTempTestAssemblyDirectories(compilationResult.OutputFilePath, tempDirectory, config);
            var copiedTempTestAssemblyFilePaths = TempTestAssemblyFilePaths(config, tempDirectory).ToArray();

            var ranAnyTests = false;

            for (var testAssemblyIndex = 0; testAssemblyIndex < config.TestAssemblyFilePaths.Length; ++testAssemblyIndex)
            {
                var originalTestAssemblyFilePath = config.TestAssemblyFilePaths[testAssemblyIndex];
                var tempTestAssemblyFilePath     = copiedTempTestAssemblyFilePaths[testAssemblyIndex];

                string[] testsToRun = null;
                if (coverageAnalysisResult != null)
                {
                    testsToRun = coverageAnalysisResult.TestsThatCoverMember(memberName, originalTestAssemblyFilePath);
                    if (!testsToRun.Any())
                    {
                        continue;
                    }
                }

                ranAnyTests = true;

                var result = testsToRun != null?
                             testRunner.RunTests(new[] { tempTestAssemblyFilePath }, testsToRun) :
                                 testRunner.RunAllTests(new[] { tempTestAssemblyFilePath });

                if (result.Status == TestRunStatus.SomeTestsFailed)
                {
                    return(null);
                }
            }

            return(ranAnyTests ?
                   await SurvivingMutant.Create(OriginalClass, OriginalNode, MutatedSyntaxRoot)
                : null);
        }