Ejemplo n.º 1
0
 private static IEnumerable <XElement> CreateLinesElement(IEnumerable <InstrumentedInstruction> instructions, Hits hits)
 {
     return(instructions
            .SelectMany(t => t.GetLines(), (i, l) => new { instructionId = i.Id, line = l })
            .Select(instruction => new XElement(
                        XName.Get("line"),
                        new XAttribute(XName.Get("num"), instruction.line),
                        new XAttribute(XName.Get("count"), hits.GetInstructionHitCount(instruction.instructionId)),
                        new XAttribute(XName.Get("type"), "stmt")
                        )));
 }
Ejemplo n.º 2
0
        protected override void WriteDetailedReport(InstrumentationResult result, IDictionary <string, SourceFile> files, Hits hits)
        {
            foreach (var kvFile in files)
            {
                var lines = File.ReadAllLines(Path.Combine(result.SourcePath, kvFile.Key));

                var fileName = GetHtmlFileName(kvFile.Key);

                Directory.CreateDirectory(Path.GetDirectoryName(fileName));

                using (var htmlWriter = (TextWriter)File.CreateText(fileName))
                {
                    htmlWriter.WriteLine("<html>");
                    htmlWriter.WriteLine("<body style=\"font-family: monospace;\">");

                    var uncoveredLineNumbers = new HashSet <int>();
                    var coveredLineNumbers   = new HashSet <int>();
                    foreach (var i in kvFile.Value.Instructions)
                    {
                        if (hits.IsInstructionHit(i.Id))
                        {
                            coveredLineNumbers.UnionWith(i.GetLines());
                        }
                        else
                        {
                            uncoveredLineNumbers.UnionWith(i.GetLines());
                        }
                    }

                    var l = 0;
                    foreach (var line in lines)
                    {
                        l++;
                        var style = "white-space: pre;";
                        if (coveredLineNumbers.Contains(l))
                        {
                            style += BgColorGreen;
                        }
                        else if (uncoveredLineNumbers.Contains(l))
                        {
                            style += BgColorRed;
                        }
                        else
                        {
                            style += BgColorBlue;
                        }

                        var instructions = kvFile.Value.Instructions.Where(i => i.GetLines().Contains(l)).ToArray();

                        var counter = instructions.Sum(a => hits.GetInstructionHitCount(a.Id));

                        var testMethods = instructions
                                          .SelectMany(i => hits.GetInstructionTestMethods(i.Id))
                                          .Distinct()
                                          .ToArray();

                        var testNames = string.Join(", ", testMethods.Select(m => $"{m.ClassName}.{m.MethodName} ({m.Counter})"));

                        var testNamesIcon = testMethods.Length > 0
                            ? $"<span style=\"cursor: pointer; margin-right: 5px;\" title=\"Covered by tests: {testNames} for {counter}\">&#9432;</span>"
                            : $"<span style=\"margin-right: 5px;\">&nbsp;</span>";

                        if (!string.IsNullOrEmpty(line))
                        {
                            htmlWriter.WriteLine($"<div style=\"{style}\" title=\"{testNames}\">{testNamesIcon}{WebUtility.HtmlEncode(line)}</div>");
                        }
                        else
                        {
                            htmlWriter.WriteLine($"<div style=\"{style}\" title=\"{testNames}\">{testNamesIcon}&nbsp;</div>");
                        }
                    }

                    htmlWriter.WriteLine("</body>");
                    htmlWriter.WriteLine("</html>");
                }
            }
        }