Exemplo n.º 1
0
        /// <summary>
        ///     Flush any changes made to the passed list of mutations to the file system.
        /// </summary>
        /// <param name="mutationVariants"></param>
        public void FlushMutations(IList <MutationVariant> mutationVariants)
        {
            var assemblies = new HashSet <AssemblyMutator>(mutationVariants.Select(x => x.Assembly));

            foreach (var assembly in assemblies)
            {
                var fileDuplication = TestProjectReferences.FirstOrDefault(x =>
                                                                           assembly.Module.Name == x.Name);

                using var writeStream = fileDuplication.OpenReadWriteStream();
                using var ms          = new MemoryStream();
                assembly.Module.Write(ms);
                writeStream.Write(ms.ToArray());

                ms.Position = 0;
                var decompiler = new CodeDecompiler(fileDuplication.FullFilePath(), ms);

                foreach (var mutationVariant in mutationVariants)
                {
                    if (assembly == mutationVariant.Assembly && string.IsNullOrEmpty(mutationVariant.MutatedSource))
                    {
                        mutationVariant.MutatedSource = decompiler.Decompile(mutationVariant.MemberHandle);
                    }
                }
                fileDuplication.Dispose();
            }

            TestProjectFile.Dispose();
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Returns a list of <see cref="MutationVariant" /> that can be executed on this given test project duplication.
        /// </summary>
        /// <param name="mutationIdentifiers"></param>
        /// <param name="mutationLevel"></param>
        /// <returns></returns>
        public IList <MutationVariant> GetMutationVariants(IList <MutationVariantIdentifier> mutationIdentifiers,
                                                           MutationLevel mutationLevel)
        {
            var foundMutations = new List <MutationVariant>();

            foreach (var reference in TestProjectReferences)
            {
                // Read the reference its contents
                using var stream    = reference.OpenReadStream();
                using var binReader = new BinaryReader(stream);
                var data = binReader.ReadBytes((int)stream.Length);

                var decompiler = new CodeDecompiler(reference.FullFilePath(), new MemoryStream(data));

                // Create assembly mutator and look up the mutations according to the passed identifiers.
                var assembly = new AssemblyMutator(new MemoryStream(data));

                foreach (var type in assembly.Types)
                {
                    var toMutateMethods = new HashSet <string>(
                        mutationIdentifiers.Select(x => x.MemberName)
                        );

                    foreach (var method in type.Methods)
                    {
                        if (!toMutateMethods.Contains(method.AssemblyQualifiedName))
                        {
                            continue;
                        }

                        var methodMutationId = 0;

                        foreach (var group in method.AllMutations(mutationLevel))
                        {
                            foreach (var mutation in group)
                            {
                                var mutationIdentifier = mutationIdentifiers.FirstOrDefault(x =>
                                                                                            x.MutationId == methodMutationId && method.AssemblyQualifiedName == x.MemberName);

                                if (mutationIdentifier.MemberName != null)
                                {
                                    foundMutations.Add(new MutationVariant(false, assembly, mutationIdentifier,
                                                                           new MutationAnalyzerInfo
                                    {
                                        AnalyzerDescription = group.AnalyzerDescription,
                                        AnalyzerName        = group.AnalyzerName
                                    }, method.Handle, mutation, "", decompiler.Decompile(method.Handle)));
                                }

                                methodMutationId++;
                            }
                        }
                    }
                }
            }

            return(foundMutations);
        }
Exemplo n.º 3
0
        public void Decompile_Test_Field(string fieldName, string expectedNotClean)
        {
            // Arrange
            var cd       = new CodeDecompiler("test.dll", _stream);
            var handle   = DecompileHandleHelper.DecompileField(_module, TypeName, fieldName);
            var expected = expectedNotClean.CleanUpCode();

            // Act
            var actual = cd.Decompile(handle).CleanUpCode();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 4
0
        public void Decompile_Test_method(string methodName, string expectedNotClean,
                                          params Type[] typeList)
        {
            // Arrange
            var cd       = new CodeDecompiler("test.dll", _stream);
            var handle   = DecompileHandleHelper.DecompileMethod(_module, TypeName, methodName, typeList);
            var expected = expectedNotClean.CleanUpCode();

            // Act
            var actual = cd.Decompile(handle).CleanUpCode();

            // Assert
            Assert.AreEqual(expected, actual);
        }