public void Constructor()
        {
            var sut = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, 0, 2 });

            Assert.AreEqual(2, sut.CoverableLines, "Not equal");
            Assert.AreEqual(1, sut.CoveredLines, "Not equal");
        }
Exemplo n.º 2
0
        public void AnalyzeFile_ExistingFile_AnalysisIsReturned()
        {
            var sut = new CodeFile("C:\\temp\\Program.cs", new int[] { -2, -1, 0, 1 });

            Assert.IsNull(sut.TotalLines);

            var fileAnalysis = sut.AnalyzeFile();

            Assert.IsNotNull(fileAnalysis);
            Assert.IsNull(fileAnalysis.Error);
            Assert.AreEqual(fileAnalysis.Path, fileAnalysis.Path);
            Assert.AreEqual(84, sut.TotalLines);
            Assert.AreEqual(84, fileAnalysis.Lines.Count());

            Assert.AreEqual(1, fileAnalysis.Lines.ElementAt(0).LineNumber);
            Assert.AreEqual(-1, fileAnalysis.Lines.ElementAt(0).LineVisits);
            Assert.AreEqual(LineVisitStatus.NotCoverable, fileAnalysis.Lines.ElementAt(0).LineVisitStatus);

            Assert.AreEqual(2, fileAnalysis.Lines.ElementAt(1).LineNumber);
            Assert.AreEqual(0, fileAnalysis.Lines.ElementAt(1).LineVisits);
            Assert.AreEqual(LineVisitStatus.NotCovered, fileAnalysis.Lines.ElementAt(1).LineVisitStatus);

            Assert.AreEqual(3, fileAnalysis.Lines.ElementAt(2).LineNumber);
            Assert.AreEqual(1, fileAnalysis.Lines.ElementAt(2).LineVisits);
            Assert.AreEqual(LineVisitStatus.Covered, fileAnalysis.Lines.ElementAt(2).LineVisitStatus);
        }
        public void Merge_MergeCodeFileWithLongerCoverageArray_CoverageInformationIsUpdated()
        {
            var sut = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, -1, -1, 0, 0, 0, 1, 1, 1 });
            var codeFileToMerge = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 });

            sut.Merge(codeFileToMerge);

            Assert.AreEqual(10, sut.CoverableLines, "Not equal");
            Assert.AreEqual(6, sut.CoveredLines, "Not equal");
        }
Exemplo n.º 4
0
        public void AddFile_AddSingleFile_FileIsStored()
        {
            var assembly = new Assembly("C:\\test\\TestAssembly.dll");
            var sut = new Class("Test", assembly);
            var file = new CodeFile("C:\\temp\\Program.cs", new int[0]);
            sut.AddFile(file);

            Assert.AreEqual(file, sut.Files.First(), "Not equal");
            Assert.AreEqual(1, sut.Files.Count(), "Wrong number of classes");
        }
Exemplo n.º 5
0
        public void AnalyzeFile_ExistingFileWithTrackedMethods_AnalysisIsReturned()
        {
            var sut = new CodeFile("C:\\temp\\Program.cs", new int[] { -2, -1, 0, 1 });
            var testMethod = new TestMethod("TestFull", "Test");
            sut.AddCoverageByTestMethod(testMethod, new int[] { -2, 1, -1, 0 });

            var fileAnalysis = sut.AnalyzeFile();

            Assert.AreEqual(1, fileAnalysis.Lines.First().LineCoverageByTestMethod[testMethod].LineVisits);
            Assert.AreEqual(LineVisitStatus.Covered, fileAnalysis.Lines.First().LineCoverageByTestMethod[testMethod].LineVisitStatus);
        }
Exemplo n.º 6
0
        public void Constructor_WithBranches()
        {
            var branches = new Dictionary<int, List<Branch>>()
            {
                { 1, new List<Branch>() { new Branch(1, "1"), new Branch(0, "2") } },
                { 2, new List<Branch>() { new Branch(0, "3"), new Branch(2, "4") } }
            };

            var sut = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, branches);

            Assert.AreEqual(2, sut.CoveredBranches, "Not equal");
            Assert.AreEqual(4, sut.TotalBranches, "Not equal");
        }
Exemplo n.º 7
0
        public void AnalyzeFile_NonExistingFile_AnalysisIsReturned()
        {
            var sut = new CodeFile("C:\\temp\\Other.cs", new int[] { -2, -1, 0, 1 });

            Assert.IsNull(sut.TotalLines);

            var fileAnalysis = sut.AnalyzeFile();

            Assert.IsNotNull(fileAnalysis);
            Assert.IsNotNull(fileAnalysis.Error);
            Assert.AreEqual(fileAnalysis.Path, fileAnalysis.Path);
            Assert.IsNull(sut.TotalLines);
            Assert.AreEqual(0, fileAnalysis.Lines.Count());
        }
Exemplo n.º 8
0
        public void Merge_MergeClassWithOneFileAndOneMethodMetric_FileIsStored()
        {
            var assembly = new Assembly("C:\\test\\TestAssembly.dll");
            var sut = new Class("Test", assembly);
            var classToMerge = new Class("Test", assembly);
            var file = new CodeFile("C:\\temp\\Program.cs", new int[0]);
            var methodMetric = new MethodMetric("Test");
            classToMerge.AddFile(file);
            classToMerge.AddMethodMetric(methodMetric);
            sut.Merge(classToMerge);

            Assert.AreEqual(file, sut.Files.First(), "Not equal");
            Assert.AreEqual(1, sut.Files.Count(), "Wrong number of classes");
            Assert.AreEqual(methodMetric, sut.MethodMetrics.First(), "Not equal");
            Assert.AreEqual(1, sut.MethodMetrics.Count(), "Wrong number of method metrics");
        }
Exemplo n.º 9
0
        public void Merge_MergeCodeFileWithLongerCoverageArray_CoverageInformationIsUpdated()
        {
            var branches = new Dictionary<int, List<Branch>>()
            {
                { 1, new List<Branch>() { new Branch(1, "1"), new Branch(0, "2") } },
                { 2, new List<Branch>() { new Branch(0, "3"), new Branch(2, "4") } }
            };
            var sut = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, branches);

            var branches2 = new Dictionary<int, List<Branch>>()
            {
                { 1, new List<Branch>() { new Branch(4, "1"), new Branch(3, "5") } },
                { 3, new List<Branch>() { new Branch(0, "3"), new Branch(2, "4") } }
            };
            var codeFileToMerge = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 }, branches2);

            sut.Merge(codeFileToMerge);

            Assert.AreEqual(10, sut.CoverableLines, "Not equal");
            Assert.AreEqual(6, sut.CoveredLines, "Not equal");

            Assert.AreEqual(4, sut.CoveredBranches, "Not equal");
            Assert.AreEqual(7, sut.TotalBranches, "Not equal");
        }
Exemplo n.º 10
0
        public void Merge_MergeCodeFileWithEqualLengthCoverageArray_CoverageInformationIsUpdated()
        {
            var sut = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, -1, -1, 0, 0, 0, 1, 1, 1 });
            var codeFileToMerge = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, 0, 1, -1, 0, 1, -1, 0, 1 });
            var testMethod = new TestMethod("TestFull", "Test");
            codeFileToMerge.AddCoverageByTestMethod(testMethod, new int[] { -1, -1, -1, 0, 0, 0, 1, 1, 1 });

            sut.Merge(codeFileToMerge);

            Assert.AreEqual(8, sut.CoverableLines, "Not equal");
            Assert.AreEqual(5, sut.CoveredLines, "Not equal");

            Assert.IsNull(sut.CoveredBranches, "Not null");
            Assert.IsNull(sut.TotalBranches, "Not null");

            Assert.IsTrue(sut.TestMethods.Contains(testMethod));
        }
Exemplo n.º 11
0
        public void Equals()
        {
            var target1 = new CodeFile("C:\\temp\\Program.cs", new int[0]);
            var target2 = new CodeFile("C:\\temp\\Program.cs", new int[0]);
            var target3 = new CodeFile("C:\\temp\\Other.cs", new int[0]);

            Assert.IsTrue(target1.Equals(target2), "Objects are not equal");
            Assert.IsFalse(target1.Equals(target3), "Objects are equal");
            Assert.IsFalse(target1.Equals(null), "Objects are equal");
            Assert.IsFalse(target1.Equals(new object()), "Objects are equal");
        }
Exemplo n.º 12
0
        /// <summary>
        /// Processes the file.
        /// </summary>
        /// <param name="fileIds">The file ids of the class.</param>
        /// <param name="class">The class.</param>
        /// <param name="filePath">The file path.</param>
        /// <returns>The <see cref="CodeFile"/>.</returns>
        private CodeFile ProcessFile(HashSet<string> fileIds, Class @class, string filePath)
        {
            var methods = this.modules
                .Where(m => m.Element("ModuleName").Value.Equals(@class.Assembly.Name))
                .Elements("Classes")
                .Elements("Class")
                .Where(c => c.Element("FullName").Value.Equals(@class.Name)
                            || c.Element("FullName").Value.StartsWith(@class.Name + "/", StringComparison.Ordinal))
                .Elements("Methods")
                .Elements("Method")
                .ToArray();

            var methodsOfFile = methods
                .Where(m => m.Element("FileRef") != null && fileIds.Contains(m.Element("FileRef").Attribute("uid").Value))
                .ToArray();

            SetMethodMetrics(methodsOfFile, @class);

            var seqpntsOfFile = methods
                .Elements("SequencePoints")
                .Elements("SequencePoint")
                .Where(seqpnt => (seqpnt.Attribute("fileid") != null
                                    && fileIds.Contains(seqpnt.Attribute("fileid").Value))
                    || (seqpnt.Attribute("fileid") == null && seqpnt.Parent.Parent.Element("FileRef") != null
                        && fileIds.Contains(seqpnt.Parent.Parent.Element("FileRef").Attribute("uid").Value)))
                .Select(seqpnt => new
                {
                    LineNumberStart = int.Parse(seqpnt.Attribute("sl").Value, CultureInfo.InvariantCulture),
                    LineNumberEnd = seqpnt.Attribute("el") != null ? int.Parse(seqpnt.Attribute("el").Value, CultureInfo.InvariantCulture) : int.Parse(seqpnt.Attribute("sl").Value, CultureInfo.InvariantCulture),
                    Visits = int.Parse(seqpnt.Attribute("vc").Value, CultureInfo.InvariantCulture),
                    TrackedMethodRefs = seqpnt.Elements("TrackedMethodRefs")
                        .Elements("TrackedMethodRef")
                        .Select(t => new
                        {
                            Visits = int.Parse(t.Attribute("vc").Value, CultureInfo.InvariantCulture),
                            TrackedMethodId = t.Attribute("uid").Value
                        })
                })
                .OrderBy(seqpnt => seqpnt.LineNumberEnd)
                .ToArray();

            int[] coverage = new int[] { };
            var branches = GetBranches(methods, fileIds);

            var trackedMethodsCoverage = seqpntsOfFile
                .SelectMany(s => s.TrackedMethodRefs)
                .Select(t => t.TrackedMethodId)
                .Distinct()
                .ToDictionary(id => id, id => new int[] { });

            if (seqpntsOfFile.Length > 0)
            {
                coverage = new int[seqpntsOfFile[seqpntsOfFile.LongLength - 1].LineNumberEnd + 1];

                for (int i = 0; i < coverage.Length; i++)
                {
                    coverage[i] = -1;
                }

                foreach (var name in trackedMethodsCoverage.Keys.ToArray())
                {
                    trackedMethodsCoverage[name] = (int[])coverage.Clone();
                }

                foreach (var seqpnt in seqpntsOfFile)
                {
                    for (int lineNumber = seqpnt.LineNumberStart; lineNumber <= seqpnt.LineNumberEnd; lineNumber++)
                    {
                        int visits = coverage[lineNumber] == -1 ? seqpnt.Visits : coverage[lineNumber] + seqpnt.Visits;
                        coverage[lineNumber] = visits;

                        if (visits > -1)
                        {
                            foreach (var trackedMethodCoverage in trackedMethodsCoverage)
                            {
                                if (trackedMethodCoverage.Value[lineNumber] == -1)
                                {
                                    trackedMethodCoverage.Value[lineNumber] = 0;
                                }
                            }
                        }

                        foreach (var trackedMethod in seqpnt.TrackedMethodRefs)
                        {
                            var trackedMethodCoverage = trackedMethodsCoverage[trackedMethod.TrackedMethodId];
                            trackedMethodCoverage[lineNumber] = trackedMethodCoverage[lineNumber] == -1 ? trackedMethod.Visits : trackedMethodCoverage[lineNumber] + trackedMethod.Visits;
                        }
                    }
                }
            }

            var codeFile = new CodeFile(filePath, coverage, branches);

            foreach (var trackedMethodCoverage in trackedMethodsCoverage)
            {
                string name = null;

                // Sometimes no corresponding MethodRef element exists
                if (this.trackedMethods.TryGetValue(trackedMethodCoverage.Key, out name))
                {
                    string shortName = name.Substring(name.Substring(0, name.IndexOf(':') + 1).LastIndexOf('.') + 1);
                    TestMethod testMethod = new TestMethod(name, shortName);
                    codeFile.AddCoverageByTestMethod(testMethod, trackedMethodCoverage.Value);
                }
            }

            return codeFile;
        }
Exemplo n.º 13
0
 /// <summary>
 /// Adds the given file.
 /// </summary>
 /// <param name="codeFile">The code file.</param>
 internal void AddFile(CodeFile codeFile)
 {
     this.files.Add(codeFile);
 }
Exemplo n.º 14
0
        /// <summary>
        /// Merges the given file with the current instance.
        /// </summary>
        /// <param name="file">The file to merge.</param>
        internal void Merge(CodeFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            // Resize coverage array if neccessary
            if (file.lineCoverage.LongLength > this.lineCoverage.LongLength)
            {
                int[] newLineCoverage = new int[file.lineCoverage.LongLength];

                Array.Copy(this.lineCoverage, newLineCoverage, this.lineCoverage.LongLength);

                for (long i = this.lineCoverage.LongLength; i < file.lineCoverage.LongLength; i++)
                {
                    newLineCoverage[i] = -1;
                }

                this.lineCoverage = newLineCoverage;
            }

            for (long i = 0; i < file.lineCoverage.LongLength; i++)
            {
                int coverage = this.lineCoverage[i];

                if (coverage < 0)
                {
                    coverage = file.lineCoverage[i];
                }
                else if (file.lineCoverage[i] > 0)
                {
                    coverage += file.lineCoverage[i];
                }

                this.lineCoverage[i] = coverage;
            }

            foreach (var lineCoverageByTestMethod in file.lineCoveragesByTestMethod)
            {
                int[] existingLineCoverageByTestMethod = null;

                this.lineCoveragesByTestMethod.TryGetValue(lineCoverageByTestMethod.Key, out existingLineCoverageByTestMethod);

                if (existingLineCoverageByTestMethod == null)
                {
                    this.lineCoveragesByTestMethod.Add(lineCoverageByTestMethod);
                }
                else
                {
                    // Resize coverage array if neccessary
                    if (existingLineCoverageByTestMethod.LongLength > lineCoverageByTestMethod.Value.LongLength)
                    {
                        int[] newLineCoverage = new int[lineCoverageByTestMethod.Value.LongLength];

                        Array.Copy(lineCoverageByTestMethod.Value, newLineCoverage, lineCoverageByTestMethod.Value.LongLength);

                        for (long i = existingLineCoverageByTestMethod.LongLength; i < lineCoverageByTestMethod.Value.LongLength; i++)
                        {
                            newLineCoverage[i] = -1;
                        }

                        existingLineCoverageByTestMethod = newLineCoverage;
                    }

                    for (long i = 0; i < lineCoverageByTestMethod.Value.LongLength; i++)
                    {
                        int coverage = existingLineCoverageByTestMethod[i];

                        if (coverage < 0)
                        {
                            coverage = lineCoverageByTestMethod.Value[i];
                        }
                        else if (lineCoverageByTestMethod.Value[i] > 0)
                        {
                            coverage += lineCoverageByTestMethod.Value[i];
                        }

                        existingLineCoverageByTestMethod[i] = coverage;
                    }

                    this.lineCoveragesByTestMethod[lineCoverageByTestMethod.Key] = existingLineCoverageByTestMethod;
                }
            }

            if (file.branches != null)
            {
                if (this.branches == null)
                {
                    this.branches = new Dictionary<int, List<Branch>>();
                }

                foreach (var branchByLine in file.branches)
                {
                    List<Branch> branches = null;

                    if (this.branches.TryGetValue(branchByLine.Key, out branches))
                    {
                        foreach (var branch in branchByLine.Value)
                        {
                            Branch existingBranch = branches.FirstOrDefault(b => b.Equals(branch));
                            if (existingBranch != null)
                            {
                                existingBranch.BranchVisits += branch.BranchVisits;
                            }
                            else
                            {
                                branches.Add(branch);
                            }
                        }
                    }
                    else
                    {
                        this.branches.Add(branchByLine);
                    }
                }
            }
        }
        private IEnumerable<CodeFile> ProcessFiles(Class rootClass, IEnumerable<CoverageLine> lines)
        {
            var files = lines.Where (x => !string.IsNullOrEmpty (x.SourceFile)).GroupBy(x=>x.SourceFile);

            foreach (var sourceFile in files) {
                var maxLine = sourceFile.Max ((arg) => arg.LineNumStart);

                if (maxLine == -1)
                    continue;
                
                int [] coverage = new int [maxLine+1];
                for (int i = 0; i < coverage.Length; i++)
                    coverage [i] = -1;

                foreach (var sourceLine in sourceFile)
                {
                    if (sourceLine.LineNumStart == -1)
                        continue;

                    for (int i = sourceLine.LineNumStart; i <= sourceLine.LineNumEnd; i++)
                        coverage [i] = sourceLine.Missed ? 0 : 1;
                   
                }

                CodeFile codeFile = new CodeFile (sourceFile.Key, coverage);
                ProcessTestMethods (codeFile, sourceFile.ToList());
                yield return codeFile;

            }
        }
        private void ProcessTestMethods(CodeFile codeFile, IEnumerable<CoverageLine> lines)
        {
            var methods = lines.GroupBy (x => x.MethodName);
          
                
            foreach (var method in methods)
            {
               
                
                string shortName = method.Key.Substring(method.Key.Substring(0, method.Key.IndexOf('/') + 1).LastIndexOf('.') + 1);
                TestMethod testMethod = new TestMethod (method.Key, shortName);

                var maxLine = method.Max (x => x.LineNumStart);


                if (maxLine == -1)
                    continue;

                int [] coverage = new int [maxLine+1];
                for (int i = 0; i < coverage.Length; i++)
                    coverage [i] = -1;
                
                foreach (var sourceLine in method)
                {
                    if (sourceLine.LineNumStart != -1)
                        for (int i = sourceLine.LineNumStart; i <= sourceLine.LineNumEnd; i++)
                            coverage [i] = sourceLine.Missed ? 0 : 1;
                }
                codeFile.AddCoverageByTestMethod (testMethod, coverage);
            }
        }
Exemplo n.º 17
0
 /// <summary>
 /// Adds the given file.
 /// </summary>
 /// <param name="codeFile">The code file.</param>
 internal void AddFile(CodeFile codeFile)
 {
     this.files.Add(codeFile);
 }
Exemplo n.º 18
0
        /// <summary>
        /// Processes the file.
        /// </summary>
        /// <param name="fileId">The id of the file.</param>
        /// <param name="class">The class.</param>
        /// <returns>The <see cref="CodeFile"/>.</returns>
        private CodeFile ProcessFile(string fileId, Class @class)
        {
            var assemblyElement = this.modules
                .Where(m => m.Attribute("Name").Value.Equals(@class.Assembly.Name));

            var statements = assemblyElement
               .Elements("Namespace")
               .Elements("Type")
               .Concat(assemblyElement.Elements("Type"))
               .Where(c => (c.Parent.Attribute("Name").Value + "." + c.Attribute("Name").Value).Equals(@class.Name))
               .Descendants("Statement")
               .Where(c => c.Attribute("FileIndex").Value == fileId)
               .Select(c => new
               {
                   LineNumberStart = int.Parse(c.Attribute("Line").Value, CultureInfo.InvariantCulture),
                   LineNumberEnd = int.Parse(c.Attribute("EndLine").Value, CultureInfo.InvariantCulture),
                   Visited = c.Attribute("Covered").Value == "True"
               })
               .OrderBy(seqpnt => seqpnt.LineNumberEnd)
               .ToArray();

            int[] coverage = new int[] { };

            if (statements.Length > 0)
            {
                coverage = new int[statements[statements.LongLength - 1].LineNumberEnd + 1];

                for (int i = 0; i < coverage.Length; i++)
                {
                    coverage[i] = -1;
                }

                foreach (var statement in statements)
                {
                    for (int lineNumber = statement.LineNumberStart; lineNumber <= statement.LineNumberEnd; lineNumber++)
                    {
                        int visits = statement.Visited ? 1 : 0;
                        coverage[lineNumber] = coverage[lineNumber] == -1 ? visits : Math.Min(coverage[lineNumber] + visits, 1);
                    }
                }
            }

            string filePath = this.files.First(f => f.Attribute("Index").Value == fileId).Attribute("Name").Value;
            var codeFile = new CodeFile(filePath, coverage);

            return codeFile;
        }
Exemplo n.º 19
0
        public void Merge_TargetCodeFileHasNoBranches_BranchCoverageInformationIsUpdated()
        {
            var branches = new Dictionary<int, List<Branch>>()
            {
                { 1, new List<Branch>() { new Branch(1, "1"), new Branch(0, "2") } },
                { 2, new List<Branch>() { new Branch(0, "3"), new Branch(2, "4") } }
            };
            var sut = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, -1, -1, 0, 0, 0, 1, 1, 1 });

            var codeFileToMerge = new CodeFile("C:\\temp\\Program.cs", new int[] { -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 }, branches);

            sut.Merge(codeFileToMerge);

            Assert.AreEqual(2, sut.CoveredBranches, "Not equal");
            Assert.AreEqual(4, sut.TotalBranches, "Not equal");
        }
Exemplo n.º 20
0
        /// <summary>
        /// Merges the given file with the current instance.
        /// </summary>
        /// <param name="file">The file to merge.</param>
        public void Merge(CodeFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            // Resize coverage array if neccessary
            if (file.lineCoverage.LongLength > this.lineCoverage.LongLength)
            {
                int[] newLineCoverage = new int[file.lineCoverage.LongLength];

                Array.Copy(this.lineCoverage, newLineCoverage, this.lineCoverage.LongLength);

                for (long i = this.lineCoverage.LongLength; i < file.lineCoverage.LongLength; i++)
                {
                    newLineCoverage[i] = -1;
                }

                this.lineCoverage = newLineCoverage;
            }

            for (long i = 0; i < file.lineCoverage.LongLength; i++)
            {
                int coverage = this.lineCoverage[i];

                if (coverage < 0)
                {
                    coverage = file.lineCoverage[i];
                }
                else if (file.lineCoverage[i] > 0)
                {
                    coverage += file.lineCoverage[i];
                }

                this.lineCoverage[i] = coverage;
            }

            foreach (var lineCoverageByTestMethod in file.lineCoveragesByTestMethod)
            {
                int[] existingLineCoverageByTestMethod = null;

                this.lineCoveragesByTestMethod.TryGetValue(lineCoverageByTestMethod.Key, out existingLineCoverageByTestMethod);

                if (existingLineCoverageByTestMethod == null)
                {
                    this.lineCoveragesByTestMethod.Add(lineCoverageByTestMethod);
                }
                else
                {
                    // Resize coverage array if neccessary
                    if (existingLineCoverageByTestMethod.LongLength > lineCoverageByTestMethod.Value.LongLength)
                    {
                        int[] newLineCoverage = new int[lineCoverageByTestMethod.Value.LongLength];

                        Array.Copy(lineCoverageByTestMethod.Value, newLineCoverage, lineCoverageByTestMethod.Value.LongLength);

                        for (long i = existingLineCoverageByTestMethod.LongLength; i < lineCoverageByTestMethod.Value.LongLength; i++)
                        {
                            newLineCoverage[i] = -1;
                        }

                        existingLineCoverageByTestMethod = newLineCoverage;
                    }

                    for (long i = 0; i < lineCoverageByTestMethod.Value.LongLength; i++)
                    {
                        int coverage = existingLineCoverageByTestMethod[i];

                        if (coverage < 0)
                        {
                            coverage = lineCoverageByTestMethod.Value[i];
                        }
                        else if (lineCoverageByTestMethod.Value[i] > 0)
                        {
                            coverage += lineCoverageByTestMethod.Value[i];
                        }

                        existingLineCoverageByTestMethod[i] = coverage;
                    }

                    this.lineCoveragesByTestMethod[lineCoverageByTestMethod.Key] = existingLineCoverageByTestMethod;
                }
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Merges the given file with the current instance.
        /// </summary>
        /// <param name="file">The file to merge.</param>
        internal void Merge(CodeFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            // Resize coverage array if necessary
            if (file.lineCoverage.LongLength > this.lineCoverage.LongLength)
            {
                int[] newLineCoverage = new int[file.lineCoverage.LongLength];

                Array.Copy(this.lineCoverage, newLineCoverage, this.lineCoverage.LongLength);

                for (long i = this.lineCoverage.LongLength; i < file.lineCoverage.LongLength; i++)
                {
                    newLineCoverage[i] = -1;
                }

                this.lineCoverage = newLineCoverage;
            }

            // Resize line visit status array if necessary
            if (file.lineVisitStatus.LongLength > this.lineVisitStatus.LongLength)
            {
                LineVisitStatus[] newLineVisitStatus = new LineVisitStatus[file.lineVisitStatus.LongLength];
                Array.Copy(this.lineVisitStatus, newLineVisitStatus, this.lineVisitStatus.LongLength);
                this.lineVisitStatus = newLineVisitStatus;
            }

            for (long i = 0; i < file.lineCoverage.LongLength; i++)
            {
                int coverage = this.lineCoverage[i];

                if (coverage < 0)
                {
                    coverage = file.lineCoverage[i];
                }
                else if (file.lineCoverage[i] > 0)
                {
                    coverage += file.lineCoverage[i];
                }

                this.lineCoverage[i] = coverage;
            }

            for (long i = 0; i < file.lineVisitStatus.LongLength; i++)
            {
                int lineVisitStatus = Math.Max((int)this.lineVisitStatus[i], (int)file.lineVisitStatus[i]);

                this.lineVisitStatus[i] = (LineVisitStatus)lineVisitStatus;
            }

            foreach (var lineCoverageByTestMethod in file.lineCoveragesByTestMethod)
            {
                CoverageByTrackedMethod existingTrackedMethodCoverage = null;

                this.lineCoveragesByTestMethod.TryGetValue(lineCoverageByTestMethod.Key, out existingTrackedMethodCoverage);

                if (existingTrackedMethodCoverage == null)
                {
                    this.lineCoveragesByTestMethod.Add(lineCoverageByTestMethod);
                }
                else
                {
                    this.lineCoveragesByTestMethod[lineCoverageByTestMethod.Key] = MergeCoverageByTrackedMetho(existingTrackedMethodCoverage, lineCoverageByTestMethod.Value);
                }
            }

            foreach (var codeElement in file.codeElements)
            {
                this.codeElements.Add(codeElement);
            }

            if (file.branches != null)
            {
                if (this.branches == null)
                {
                    this.branches = new Dictionary <int, ICollection <Branch> >();
                }

                foreach (var branchByLine in file.branches)
                {
                    ICollection <Branch> existingBranches = null;

                    if (this.branches.TryGetValue(branchByLine.Key, out existingBranches))
                    {
                        foreach (var branch in branchByLine.Value)
                        {
                            Branch existingBranch = existingBranches.FirstOrDefault(b => b.Equals(branch));
                            if (existingBranch != null)
                            {
                                existingBranch.BranchVisits += branch.BranchVisits;
                            }
                            else
                            {
                                existingBranches.Add(branch);
                            }
                        }
                    }
                    else
                    {
                        this.branches.Add(branchByLine);
                    }
                }
            }
        }
Exemplo n.º 22
0
 /// <summary>
 /// Adds the given file.
 /// </summary>
 /// <param name="codeFile">The code file.</param>
 public void AddFile(CodeFile codeFile)
 {
     this.files.Add(codeFile);
 }
Exemplo n.º 23
0
        /// <summary>
        /// Merges the given file with the current instance.
        /// </summary>
        /// <param name="file">The file to merge.</param>
        public void Merge(CodeFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            // Resize coverage array if neccessary
            if (file.lineCoverage.LongLength > this.lineCoverage.LongLength)
            {
                int[] newLineCoverage = new int[file.lineCoverage.LongLength];

                Array.Copy(this.lineCoverage, newLineCoverage, this.lineCoverage.LongLength);

                for (long i = this.lineCoverage.LongLength; i < file.lineCoverage.LongLength; i++)
                {
                    newLineCoverage[i] = -1;
                }

                this.lineCoverage = newLineCoverage;
            }

            for (long i = 0; i < file.lineCoverage.LongLength; i++)
            {
                int coverage = this.lineCoverage[i];

                if (coverage < 0)
                {
                    coverage = file.lineCoverage[i];
                }
                else if (file.lineCoverage[i] > 0)
                {
                    coverage += file.lineCoverage[i];
                }

                this.lineCoverage[i] = coverage;
            }

            foreach (var lineCoverageByTestMethod in file.lineCoveragesByTestMethod)
            {
                int[] existingLineCoverageByTestMethod = null;

                this.lineCoveragesByTestMethod.TryGetValue(lineCoverageByTestMethod.Key, out existingLineCoverageByTestMethod);

                if (existingLineCoverageByTestMethod == null)
                {
                    this.lineCoveragesByTestMethod.Add(lineCoverageByTestMethod);
                }
                else
                {
                    // Resize coverage array if neccessary
                    if (existingLineCoverageByTestMethod.LongLength > lineCoverageByTestMethod.Value.LongLength)
                    {
                        int[] newLineCoverage = new int[lineCoverageByTestMethod.Value.LongLength];

                        Array.Copy(lineCoverageByTestMethod.Value, newLineCoverage, lineCoverageByTestMethod.Value.LongLength);

                        for (long i = existingLineCoverageByTestMethod.LongLength; i < lineCoverageByTestMethod.Value.LongLength; i++)
                        {
                            newLineCoverage[i] = -1;
                        }

                        existingLineCoverageByTestMethod = newLineCoverage;
                    }

                    for (long i = 0; i < lineCoverageByTestMethod.Value.LongLength; i++)
                    {
                        int coverage = existingLineCoverageByTestMethod[i];

                        if (coverage < 0)
                        {
                            coverage = lineCoverageByTestMethod.Value[i];
                        }
                        else if (lineCoverageByTestMethod.Value[i] > 0)
                        {
                            coverage += lineCoverageByTestMethod.Value[i];
                        }

                        existingLineCoverageByTestMethod[i] = coverage;
                    }

                    this.lineCoveragesByTestMethod[lineCoverageByTestMethod.Key] = existingLineCoverageByTestMethod;
                }
            }
        }