Esempio n. 1
0
        /// <summary>
        /// Mutates the only the conditionals specified in <see cref="RunEventArgs.SelectedMutations"/>.
        /// </summary>
        /// <param name="e">The <see cref="RunEventArgs"/> instance containing the event data.</param>
        /// <param name="opCodes">The list of valid OP codes.</param>
        private void MutateSelectedConditionals(RunEventArgs e, BranchingOpCodes opCodes)
        {
            foreach (MutationDto mutation in e.SelectedMutations)
            {
                // If the user has cancelled this since the last test, bail out
                if (_view.CancellationPending)
                {
                    break;
                }

                string outputFile = GetOutputAssemblyFileName(e.InputAssembly);
                for (int i = 0; i < mutation.Conditional.MethodDefinition.Body.Instructions.Count; i++)
                {
                    Instruction instruction = mutation.Conditional.MethodDefinition.Body.Instructions[i];
                    if (opCodes.Contains(instruction.OpCode))
                    {
                        if (i == mutation.Conditional.InstructionNumber)
                        {
                            instruction.OpCode = opCodes.Invert(instruction.OpCode);
                        }
                    }
                }

                // Replace the original target assembly with the mutated assembly
                File.Delete(e.InputAssembly);
                AssemblyFactory.SaveAssembly(mutation.Conditional.MethodDefinition.DeclaringType.Module.Assembly, outputFile);
                File.Copy(outputFile, e.InputAssembly);

                // Run the unit tests again, this time against the mutated assembly
                MbUnitTestRunner runner = new MbUnitTestRunner();
                runner.Invoke(e.TestAssembly);

                foreach (TestResult result in runner.TestResults)
                {
                    if (result is SurvivingMutantTestResult)
                    {
                        mutation.TestResults.Add(new SurvivingMutantTestResultDto((SurvivingMutantTestResult)result, mutation));
                    }

                    else
                    {
                        mutation.TestResults.Add(new KilledMutantTestResultDto((KilledMutantTestResult)result, mutation));
                    }
                }

                if (_testComplete != null)
                {
                    _testComplete(this, new TestCompleteEventArgs(runner.TestResults));
                }
            }
        }
        public void Can_invoke_unit_test_runner()
        {
            // MbUnit needs a file that ends with DLL so we have to extract this manually.
            // Utility.CopyTo Disk() returns us a randomly generated extension
            Assembly assembly = Assembly.GetExecutingAssembly();
            string path =
                Utility.CopyToDisk(
                    assembly.GetManifestResourceStream(
                        "JesterDotNet.Model.Tests.SampleData.SampleDll.Tests.dll"));
            string renamedPath = Path.ChangeExtension(path, ".dll");
            File.Move(path, renamedPath);

            ITestRunner runner = new MbUnitTestRunner();
            runner.Invoke(renamedPath);
        }
Esempio n. 3
0
        /// <summary>
        /// Mutates all conditionals found in a given assembly.
        /// </summary>
        /// <param name="e">The <see cref="RunEventArgs"/> instance containing the event data.</param>
        /// <param name="opCodes">The list of valid OP codes.</param>
        private void MutateAllConditionals(RunEventArgs e, BranchingOpCodes opCodes)
        {
            string outputFile = GetOutputAssemblyFileName(e.InputAssembly);

            AssemblyDefinition inputAssembly = AssemblyFactory.GetAssembly(e.InputAssembly);

            foreach (ModuleDefinition module in inputAssembly.Modules)
            {
                foreach (TypeDefinition type in module.Types)
                {
                    if (type.IsInterface)
                    {
                        continue;
                    }

                    foreach (MethodDefinition method in type.Methods)
                    {
                        for (int i = 0; i < method.Body.Instructions.Count; i++)
                        {
                            Instruction instruction = method.Body.Instructions[i];
                            if (opCodes.Contains(instruction.OpCode))
                            {
                                instruction.OpCode = opCodes.Invert(instruction.OpCode);
                            }
                        }

                        // Replace the original target assembly with the mutated assembly
                        File.Delete(e.InputAssembly);
                        AssemblyFactory.SaveAssembly(method.DeclaringType.Module.Assembly, outputFile);
                        File.Copy(outputFile, e.InputAssembly);

                        // Run the unit tests again, this time against the mutated assembly
                        var runner = new MbUnitTestRunner();
                        runner.Invoke(e.TestAssembly);

                        if (_testComplete != null)
                        {
                            _testComplete(this, new TestCompleteEventArgs(runner.TestResults));
                        }
                    }
                }
            }
        }