Example #1
0
        /// <summary>
        /// Gets mutations containing all conditionals found in the given assemlby.
        /// </summary>
        /// <param name="assemblyName">The assembly from which to retrieve the conditionals.</param>
        /// <returns>An enumeration of mutations containing all conditionals found in the given assembly.</returns>
        private IEnumerable <MutationDto> GetMutations(string assemblyName)
        {
            BranchingOpCodes    codes        = new BranchingOpCodes();
            IList <MutationDto> conditionals = new List <MutationDto>();

            AssemblyDefinition assembly = AssemblyFactory.GetAssembly(assemblyName);

            foreach (ModuleDefinition module in assembly.Modules)
            {
                foreach (TypeDefinition type in module.Types)
                {
                    foreach (MethodDefinition method in type.Methods)
                    {
                        if (method.HasBody)
                        {
                            for (int i = 0; i < method.Body.Instructions.Count; i++)
                            {
                                if (codes.ContainsKey(method.Body.Instructions[i].OpCode))
                                {
                                    conditionals.Add(new MutationDto(new ConditionalDefinitionDto(method, i), new List <TestResultDto>()));
                                }
                            }
                        }
                    }
                }
            }

            return(conditionals);
        }
        /// <summary>
        /// Reflects through the currently selected assembly and reflects the type tree
        /// in tvAssemblyGraph.
        /// </summary>
        public void LoadAssemblies(string[] fileNames)
        {
            BranchingOpCodes opCodes = new BranchingOpCodes();

            treeView.Nodes.Clear();

            for (int theAssembly = 0; theAssembly < fileNames.Length; theAssembly++)
            {
                // Load our input assembly and create its node in the tree
                AssemblyDefinition inputAssembly =
                    AssemblyFactory.GetAssembly(fileNames[theAssembly]);

                TreeNode assemblyTreeNode =
                    CreateTreeNode(fileNames[theAssembly], inputAssembly, Assembly);
                treeView.Nodes.Add(assemblyTreeNode);

                // Retrieve the modules from the assembly.  Most assemblies only have one
                // module, but it is possible for assemblies to possess multiple modules
                for (int theModule = 0; theModule < inputAssembly.Modules.Count; theModule++)
                {
                    // Add a node to the tree to represent the module
                    TreeNode moduleTreeNode =
                        CreateTreeNode(inputAssembly.Modules[theModule].Name,
                                       inputAssembly.Modules[theModule], Module);
                    treeView.Nodes[theAssembly].Nodes.Add(moduleTreeNode);

                    // Add the classes in each type
                    for (int theType = 0;
                         theType < inputAssembly.Modules[theModule].Types.Count;
                         theType++)
                    {
                        // Add a node to the tree to represent the class
                        treeView.Nodes[theAssembly].Nodes[theModule].Nodes.Add(
                            CreateTreeNode(inputAssembly.Modules[theModule].Types[theType].FullName,
                                           inputAssembly.Modules[theModule].Types[theType], Class));

                        // Create a test method for each method in this type
                        for (int theMethod = 0;
                             theMethod <
                             inputAssembly.Modules[theModule].Types[theType].Methods.Count;
                             theMethod++)
                        {
                            MethodDefinition methodDefinition =
                                inputAssembly.Modules[theModule].Types[theType].Methods[theMethod];
                            treeView.Nodes[theAssembly].Nodes[theModule].Nodes[theType].Nodes.Add(
                                CreateTreeNode(methodDefinition.Name, methodDefinition, Method));

                            // Store the method's MethodInfo object in this node's tag
                            // so that we may retrieve it later
                            treeView.Nodes[theAssembly].Nodes[theModule].Nodes[theType].Nodes[
                                theMethod].Tag =
                                methodDefinition;

                            // Interfaces or abstract classes don't have a body so we should skip
                            // them
                            if (methodDefinition.HasBody)
                            {
                                MethodBody body = methodDefinition.Body;
                                for (int theInstruction = 0;
                                     theInstruction < body.Instructions.Count;
                                     theInstruction++)
                                {
                                    if (opCodes.ContainsKey(
                                            body.Instructions[theInstruction].OpCode))
                                    {
                                        treeView.
                                        Nodes[theAssembly].
                                        Nodes[theModule].
                                        Nodes[theType].
                                        Nodes[theMethod].
                                        Nodes.Add(
                                            CreateTreeNode(
                                                opCodes[body.Instructions[theInstruction].OpCode].ToString(),
                                                new ConditionalDefinitionDto(methodDefinition, theInstruction), Branch));
                                    }
                                }
                            }
                        }
                    }
                    moduleTreeNode.Expand();
                }
                assemblyTreeNode.Expand();
            }
        }
        /// <summary>
        /// Gets mutations containing all conditionals found in the given assemlby.
        /// </summary>
        /// <param name="assemblyName">The assembly from which to retrieve the conditionals.</param>
        /// <returns>An enumeration of mutations containing all conditionals found in the given assembly.</returns>
        private IEnumerable<MutationDto> GetMutations(string assemblyName)
        {
            BranchingOpCodes codes = new BranchingOpCodes();
            IList<MutationDto> conditionals = new List<MutationDto>();

            AssemblyDefinition assembly = AssemblyFactory.GetAssembly(assemblyName);
            foreach (ModuleDefinition module in assembly.Modules)
                foreach (TypeDefinition type in module.Types)
                    foreach (MethodDefinition method in type.Methods)
                        if (method.HasBody)
                        {
                            for (int i = 0; i < method.Body.Instructions.Count; i++)
                                if (codes.ContainsKey(method.Body.Instructions[i].OpCode))
                                    conditionals.Add(new MutationDto(new ConditionalDefinitionDto(method, i), new List<TestResultDto>()));
                        }

            return conditionals;
        }