예제 #1
0
        public void ShouldMergeTests()
        {
            var contexts = new[]
            {
                new HitContext(
                    "Sample.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
                    "Sample.UnitTests.UnitTest1",
                    "XUnitTest2",
                    new Dictionary <int, int>
                {
                    { 8, 1 },
                }),
                new HitContext(
                    "Sample.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
                    "Sample.UnitTests.UnitTest1",
                    "XUnitTest2",
                    new Dictionary <int, int>
                {
                    { 8, 1 },
                })
            };

            var hits = new HitsInfo(contexts);

            hits.GetInstructionHitCount(8).Should().Be(2);
            hits.GetInstructionHitContexts(8).Should().HaveCount(1);
            hits.GetInstructionHitContexts(8).First().GetHitCount(8).Should().Be(2);
        }
예제 #2
0
        public void ShouldMergeTestsCorrectly()
        {
            var contexts = new[]
            {
                new HitContext(
                    "Sample.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
                    "Sample.UnitTests.UnitTest1",
                    "XUnitTest2",
                    new Dictionary <int, int>
                {
                    { 17, 2500000 },
                    { 19, 2500000 },
                    { 20, 50 },
                    { 21, 2500000 },
                    { 22, 2500000 },
                    { 23, 2500050 },
                    { 24, 50 },
                    { 33, 1 },
                    { 34, 1 },
                    { 35, 1 },
                    { 37, 1 },
                    { 38, 50 },
                    { 39, 50 },
                    { 40, 51 }
                }),
                new HitContext(
                    "Sample.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
                    "Sample.UnitTests.UnitTest1", "NUnitTest2",
                    new Dictionary <int, int>
                {
                    { 9, 1 },
                    { 10, 1 },
                    { 11, 1 },
                    { 13, 1 },
                    { 14, 50 },
                    { 15, 50 },
                    { 16, 51 },
                    { 17, 2500000 },
                    { 19, 2500000 },
                    { 20, 50 },
                    { 21, 2500000 },
                    { 22, 2500000 },
                    { 23, 2500050 },
                    { 24, 50 }
                })
            };

            var hits = new HitsInfo(contexts);

            hits.GetInstructionHitCount(17).Should().Be(5000000);
            hits.GetInstructionHitContexts(17).Count().Should().Be(2);
            hits.GetInstructionHitContexts(17).First().GetHitCount(17).Should().Be(2500000);
        }
예제 #3
0
        protected override void WriteDetailedReport(InstrumentationResult result, IDictionary <string, SourceFile> files, HitsInfo hitsInfo)
        {
            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("<style>");
                    htmlWriter.WriteLine("details summary::-webkit-details-marker {");
                    htmlWriter.WriteLine("display: none;");
                    htmlWriter.WriteLine("}");
                    htmlWriter.WriteLine("</style>");
                    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 (hitsInfo.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;
                            style += CursorPointer;
                        }
                        else if (uncoveredLineNumbers.Contains(l))
                        {
                            style += BgColorRed;
                        }
                        else
                        {
                            style += BgColorNeutral;
                        }

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

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

                        var codeContent = !string.IsNullOrEmpty(line)
                            ? WebUtility.HtmlEncode(line)
                            : "&nbsp;";

                        var contexts = instructions
                                       .SelectMany(i => hitsInfo.GetInstructionHitContexts(i.Id))
                                       .Distinct()
                                       .ToArray();

                        var hitCountHtml = coveredLineNumbers.Contains(l) || uncoveredLineNumbers.Contains(l)
                            ? $"<span style=\"display: inline-block; width: 30px; font-size: 10px;\">{counter}x</span>"
                            : "<span style=\"display: inline-block; width: 30px; font-size: 10px;\"></span>";

                        if (coveredLineNumbers.Contains(l))
                        {
                            htmlWriter.WriteLine($"<details>");
                            htmlWriter.WriteLine($"<summary style=\"{style}\">{hitCountHtml}{codeContent}</summary>");

                            htmlWriter.WriteLine("<ul>");
                            foreach (var context in contexts)
                            {
                                var count       = instructions.Sum(i => context.GetHitCount(i.Id));
                                var description = $"{context.ClassName}.{context.MethodName}";
                                htmlWriter.WriteLine($"<li>{WebUtility.HtmlEncode(description)}: {count}x</li>");
                            }
                            htmlWriter.WriteLine("</ul>");

                            htmlWriter.WriteLine($"</details>");
                        }
                        else
                        {
                            htmlWriter.WriteLine($"<div style=\"{style}\">{hitCountHtml}{codeContent}</div>");
                        }
                    }

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