public void ExcludeFromCodeCoverage_CompilerGeneratedMethodsAndTypes_NestedMembers()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <MethodsWithExcludeFromCodeCoverageAttr_NestedStateMachines>(instance =>
                    {
                        instance.Test();
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0]);

                    return(0);
                }, new string[] { path });

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.ExcludeFromCoverage.NestedStateMachines.cs")
                .AssertLinesCovered(BuildConfiguration.Debug, (14, 1), (15, 1), (16, 1))
                .AssertNonInstrumentedLines(BuildConfiguration.Debug, 9, 11);
            }
            finally
            {
                File.Delete(path);
            }
        }
        public void Switch_DoesNotReturnAttribute_InstrumentsCorrect()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance =>
                    {
                        try { instance.Switch(); }
                        catch (Exception) { }
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0]);

                    return(0);
                }, new string[] { path });

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.DoesNotReturn.cs")
                .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 33, 34, 36, 39, 40, 44, 45, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 68, 69)
                .AssertNonInstrumentedLines(BuildConfiguration.Debug, 41, 42);
            }
            finally
            {
                File.Delete(path);
            }
        }
        public void FiltersAndFinallies_DoesNotReturnAttribute_InstrumentsCorrect()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance =>
                    {
                        try { instance.FiltersAndFinallies(); }
                        catch (Exception) { }
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0], doesNotReturnAttributes: _ => new string[] { "DoesNotReturnAttribute" });

                    return(0);
                }, new string[] { path });

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.DoesNotReturn.cs")
                .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 171, 173, 174, 175, 179, 180, 181, 182, 185, 186, 187, 188, 192, 193, 194)
                .AssertNonInstrumentedLines(BuildConfiguration.Debug, 176, 177, 183, 184, 189, 190, 195, 196, 197);
            }
            finally
            {
                File.Delete(path);
            }
        }
Exemple #4
0
        public void TestReport()
        {
            CoverageResult result = new CoverageResult();

            result.Identifier = Guid.NewGuid().ToString();

            Lines lines = new Lines();

            lines.Add(1, 1);
            lines.Add(2, 0);

            Methods methods      = new Methods();
            var     methodString = "System.Void Coverlet.Core.Reporters.Tests.JsonReporterTests.TestReport()";

            methods.Add(methodString, new Method());
            methods[methodString].Lines = lines;

            Classes classes = new Classes();

            classes.Add("Coverlet.Core.Reporters.Tests.JsonReporterTests", methods);

            Documents documents = new Documents();

            documents.Add("doc.cs", classes);

            result.Modules = new Modules();
            result.Modules.Add("module", documents);

            JsonReporter reporter = new JsonReporter();

            Assert.NotEqual("{\n}", reporter.Report(result));
            Assert.NotEqual(string.Empty, reporter.Report(result));
        }
        public void WithLeave_DoesNotReturnAttribute_InstrumentsCorrect()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance =>
                    {
                        try { instance.WithLeave(); }
                        catch (Exception) { }
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0], doesNotReturnAttributes: _ => new string[] { "DoesNotReturnAttribute" });

                    return(0);
                }, new string[] { path });

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.DoesNotReturn.cs")
                .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 147, 149, 150, 151, 152, 153, 154, 155, 156, 159, 161, 166, 167, 168)
                .AssertNonInstrumentedLines(BuildConfiguration.Debug, 163, 164);
            }
            finally
            {
                File.Delete(path);
            }
        }
Exemple #6
0
        public void TestReport()
        {
            CoverageResult result = new CoverageResult();

            result.Identifier = Guid.NewGuid().ToString();
            Lines lines = new Lines();

            lines.Add(1, new LineInfo {
                Hits = 1
            });
            lines.Add(2, new LineInfo {
                Hits = 0
            });
            Methods methods = new Methods();

            methods.Add("System.Void Coverlet.Core.Reporters.Tests.LcovReporterTests.TestReport()", lines);
            Classes classes = new Classes();

            classes.Add("Coverlet.Core.Reporters.Tests.LcovReporterTests", methods);
            Documents documents = new Documents();

            documents.Add("doc.cs", classes);
            result.Modules = new Modules();
            result.Modules.Add("module", documents);

            LcovReporter reporter = new LcovReporter();
            string       report   = reporter.Report(result);

            Assert.NotEmpty(report);
            Assert.Equal("SF:doc.cs", report.Split(Environment.NewLine)[0]);
            Assert.Equal("DA:1,1", report.Split(Environment.NewLine)[1]);
            Assert.Equal("DA:2,0", report.Split(Environment.NewLine)[2]);
            Assert.Equal("end_of_record", report.Split(Environment.NewLine)[3]);
        }
        public void Subtle_DoesNotReturnAttribute_InstrumentsCorrect()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance =>
                    {
                        try { instance.Subtle(); }
                        catch (Exception) { }
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0]);

                    return(0);
                }, new string[] { path });

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.DoesNotReturn.cs")
                .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 72, 73, 75, 78, 82, 83, 86, 87, 91, 92, 95, 101, 102, 103)
                .AssertNonInstrumentedLines(BuildConfiguration.Debug, 79, 80, 88, 96, 98, 99);
            }
            finally
            {
                File.Delete(path);
            }
        }
        public void ExcludeFromCodeCoverageCompilerGeneratedMethodsAndTypes_Issue670()
        {
            string path = Path.GetTempFileName();

            try
            {
                RemoteExecutor.Invoke(async pathSerialize =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <MethodsWithExcludeFromCodeCoverageAttr_Issue670>(instance =>
                    {
                        instance.Test("test");
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize);

                    return(0);
                }, path).Dispose();

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.ExcludeFromCoverage.Issue670.cs")
                .AssertLinesCovered(BuildConfiguration.Debug, (8, 1), (9, 1), (10, 1), (11, 1))
                .AssertNonInstrumentedLines(BuildConfiguration.Debug, 15, 53);
            }
            finally
            {
                File.Delete(path);
            }
        }
        public void TestCalculateSummary()
        {
            CoverageResult result = new CoverageResult();

            result.Identifier = Guid.NewGuid().ToString();
            Lines lines = new Lines();

            lines.Add(1, new LineInfo {
                Hits = 1
            });
            lines.Add(2, new LineInfo {
                Hits = 0
            });
            Methods methods = new Methods();

            methods.Add("System.Void Coverlet.Core.Tests.CoverageSummaryTests.TestCalculateSummary()", lines);
            Classes classes = new Classes();

            classes.Add("Coverlet.Core.Tests.CoverageSummaryTests", methods);
            Documents documents = new Documents();

            documents.Add("doc.cs", classes);
            result.Modules = new Modules();
            result.Modules.Add("module", documents);

            CoverageSummary       summary       = new CoverageSummary(result);
            CoverageSummaryResult summaryResult = summary.CalculateSummary();

            Assert.NotEmpty(summaryResult);
            Assert.True(summaryResult.ContainsKey("module"));
            Assert.Equal(50, summaryResult["module"]);
        }
Exemple #10
0
        public string Report(CoverageResult result)
        {
            List <string> lcov = new List <string>();

            foreach (var module in result.Modules)
            {
                foreach (var doc in module.Value)
                {
                    lcov.Add("SF:" + doc.Key);
                    foreach (var @class in doc.Value)
                    {
                        foreach (var method in @class.Value)
                        {
                            foreach (var line in method.Value)
                            {
                                lcov.Add($"DA:{line.Key},{line.Value}");
                            }
                        }
                    }

                    lcov.Add("end_of_record");
                }
            }

            return(string.Join(Environment.NewLine, lcov));
        }
        public void TestReport()
        {
            CoverageResult result = new CoverageResult();

            result.Identifier = Guid.NewGuid().ToString();
            Lines lines = new Lines();

            lines.Add(1, new LineInfo {
                Hits = 1
            });
            lines.Add(2, new LineInfo {
                Hits = 0
            });
            Methods methods = new Methods();

            methods.Add("System.Void Coverlet.Core.Reporters.Tests.CoberturaReporterTests::TestReport()", lines);
            Classes classes = new Classes();

            classes.Add("Coverlet.Core.Reporters.Tests.CoberturaReporterTests", methods);
            Documents documents = new Documents();

            documents.Add("doc.cs", classes);
            result.Modules = new Modules();
            result.Modules.Add("module", documents);

            CoberturaReporter reporter = new CoberturaReporter();

            Assert.NotEqual(string.Empty, reporter.Report(result));
        }
Exemple #12
0
        /// <summary>
        /// Gets coverlet coverage reports
        /// </summary>
        /// <returns>Coverage reports</returns>
        public IEnumerable <(string report, string fileName)> GetCoverageReports()
        {
            // Get coverage result
            CoverageResult coverageResult = this.GetCoverageResult();

            return(this.GetCoverageReports(coverageResult));
        }
Exemple #13
0
        public void SelectionStatements_Switch()
        {
            string path = Path.GetTempFileName();

            try
            {
                RemoteExecutor.Invoke(async pathSerialize =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <SelectionStatements>(instance =>
                    {
                        instance.Switch(1);
                        return(Task.CompletedTask);
                    }, pathSerialize);
                    return(0);
                }, path).Dispose();

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.SelectionStatements.cs")
                .AssertLinesCovered(BuildConfiguration.Release, (24, 1), (26, 0), (28, 0))
                .AssertBranchesCovered(BuildConfiguration.Release, (24, 1, 1))
                .AssertLinesCovered(BuildConfiguration.Debug, (20, 1), (21, 1), (24, 1), (30, 1))
                .AssertBranchesCovered(BuildConfiguration.Debug, (21, 0, 0), (21, 1, 1), (21, 2, 0), (21, 3, 0));
            }
            finally
            {
                File.Delete(path);
            }
        }
Exemple #14
0
        /// <summary>
        /// caller sample:  TestInstrumentationHelper.GenerateHtmlReport(result, sourceFileFilter: @"+**\Samples\Instrumentation.cs");
        ///                 TestInstrumentationHelper.GenerateHtmlReport(result);
        /// </summary>
        public static void GenerateHtmlReport(CoverageResult coverageResult, IReporter reporter = null, string sourceFileFilter = "", [CallerMemberName] string directory = "")
        {
            JsonReporter defaultReporter = new JsonReporter();

            reporter ??= new CoberturaReporter();
            DirectoryInfo dir = Directory.CreateDirectory(directory);

            dir.Delete(true);
            dir.Create();
            string reportFile = Path.Combine(dir.FullName, Path.ChangeExtension("report", defaultReporter.Extension));

            File.WriteAllText(reportFile, defaultReporter.Report(coverageResult));
            reportFile = Path.Combine(dir.FullName, Path.ChangeExtension("report", reporter.Extension));
            File.WriteAllText(reportFile, reporter.Report(coverageResult));
            // i.e. reportgenerator -reports:"C:\git\coverlet\test\coverlet.core.tests\bin\Debug\netcoreapp2.0\Condition_If\report.cobertura.xml" -targetdir:"C:\git\coverlet\test\coverlet.core.tests\bin\Debug\netcoreapp2.0\Condition_If" -filefilters:+**\Samples\Instrumentation.cs
            Assert.True(new Generator().GenerateReport(new ReportConfiguration(
                                                           new[] { reportFile },
                                                           dir.FullName,
                                                           new string[0],
                                                           null,
                                                           new string[0],
                                                           new string[0],
                                                           new string[0],
                                                           new string[0],
                                                           string.IsNullOrEmpty(sourceFileFilter) ? new string[0] : new[] { sourceFileFilter },
                                                           null,
                                                           null)));
        }
        public TestCreateReporterTests()
        {
            _reporter          = new TeamCityReporter();
            _result            = new CoverageResult();
            _result.Identifier = Guid.NewGuid().ToString();

            var lines = new Lines {
                { 1, 1 }, { 2, 0 }
            };

            var branches = new Branches
            {
                new BranchInfo
                {
                    Line      = 1,
                    Hits      = 1,
                    Offset    = 23,
                    EndOffset = 24,
                    Path      = 0,
                    Ordinal   = 1
                },
                new BranchInfo
                {
                    Line      = 1,
                    Hits      = 0,
                    Offset    = 23,
                    EndOffset = 27,
                    Path      = 1,
                    Ordinal   = 2
                },
                new BranchInfo
                {
                    Line      = 1,
                    Hits      = 0,
                    Offset    = 23,
                    EndOffset = 27,
                    Path      = 1,
                    Ordinal   = 2
                }
            };

            var methods      = new Methods();
            var methodString = "System.Void Coverlet.Core.Reporters.Tests.CoberturaReporterTests::TestReport()";

            methods.Add(methodString, new Method());
            methods[methodString].Lines    = lines;
            methods[methodString].Branches = branches;

            var classes = new Classes {
                { "Coverlet.Core.Reporters.Tests.CoberturaReporterTests", methods }
            };

            var documents = new Documents {
                { "doc.cs", classes }
            };

            _result.Modules = new Modules {
                { "module", documents }
            };
        }
Exemple #16
0
        public void TestReportWithSourcelinkPaths()
        {
            CoverageResult result = new CoverageResult {
                UseSourceLink = true, Identifier = Guid.NewGuid().ToString()
            };

            var absolutePath =
                @"https://raw.githubusercontent.com/johndoe/Coverlet/02c09baa8bfdee3b6cdf4be89bd98c8157b0bc08/Demo.cs";

            var classes = new Classes {
                { "Class", new Methods() }
            };
            var documents = new Documents {
                { absolutePath, classes }
            };

            result.Modules = new Modules {
                { "Module", documents }
            };

            CoberturaReporter reporter = new CoberturaReporter();
            string            report   = reporter.Report(result);

            var doc      = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));
            var fileName = doc.Element("coverage").Element("packages").Element("package").Element("classes").Elements()
                           .Select(e => e.Attribute("filename").Value).Single();

            Assert.Equal(absolutePath, fileName);
        }
        public void If_DoesNotReturnAttribute_InstrumentsCorrect()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance =>
                    {
                        try { instance.If(); }
                        catch (Exception) { }
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0]);

                    return(0);
                }, new string[] { path });

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.DoesNotReturn.cs")
                .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 19, 20, 22, 23, 24, 25, 29, 30)
                .AssertNonInstrumentedLines(BuildConfiguration.Debug, 26, 27);
            }
            finally
            {
                File.Delete(path);
            }
        }
        public void TestLineBranchCoverage()
        {
            var result = new CoverageResult
            {
                Identifier = Guid.NewGuid().ToString(),
                Modules    = new Modules {
                    { "Coverlet.Core.Reporters.Tests", CreateBranchCoverageDocuments() }
                },
                Parameters = new CoverageParameters()
            };

            var xml = new OpenCoverReporter().Report(result, new Mock <ISourceRootTranslator>().Object);

            // Line 1: Two branches, no coverage (bec = 2, bev = 0)
            Assert.Contains(@"<SequencePoint vc=""1"" uspid=""1"" ordinal=""0"" sl=""1"" sc=""1"" el=""1"" ec=""2"" bec=""2"" bev=""0"" fileid=""1"" />", xml);

            // Line 2: Two branches, one covered (bec = 2, bev = 1)
            Assert.Contains(@"<SequencePoint vc=""1"" uspid=""2"" ordinal=""1"" sl=""2"" sc=""1"" el=""2"" ec=""2"" bec=""2"" bev=""1"" fileid=""1"" />", xml);

            // Line 3: Two branches, all covered (bec = 2, bev = 2)
            Assert.Contains(@"<SequencePoint vc=""1"" uspid=""3"" ordinal=""2"" sl=""3"" sc=""1"" el=""3"" ec=""2"" bec=""2"" bev=""2"" fileid=""1"" />", xml);

            // Line 4: Three branches, two covered (bec = 3, bev = 2)
            Assert.Contains(@"<SequencePoint vc=""1"" uspid=""4"" ordinal=""3"" sl=""4"" sc=""1"" el=""4"" ec=""2"" bec=""3"" bev=""2"" fileid=""1"" />", xml);
        }
        public void CallsGenericMethodDoesNotReturn_DoesNotReturnAttribute_InstrumentsCorrect()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance =>
                    {
                        try { instance.CallsGenericMethodDoesNotReturn(); }
                        catch (Exception) { }
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0], doesNotReturnAttributes: _ => new string[] { "DoesNotReturnAttribute" });

                    return(0);
                }, new string[] { path });

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.DoesNotReturn.cs")
                .AssertInstrumentLines(BuildConfiguration.Debug, 118, 119, 124, 125, 126)
                .AssertNonInstrumentedLines(BuildConfiguration.Debug, 127, 128);
            }
            finally
            {
                File.Delete(path);
            }
        }
Exemple #20
0
        public string Report(CoverageResult result, ISourceRootTranslator sourceRootTranslator)
        {
            if (result.Parameters.DeterministicReport)
            {
                throw new NotSupportedException("Deterministic report not supported by lcov reporter");
            }

            CoverageSummary summary = new CoverageSummary();
            List <string>   lcov    = new List <string>();

            foreach (var module in result.Modules)
            {
                foreach (var doc in module.Value)
                {
                    var docLineCoverage   = summary.CalculateLineCoverage(doc.Value);
                    var docBranchCoverage = summary.CalculateBranchCoverage(doc.Value);
                    var docMethodCoverage = summary.CalculateMethodCoverage(doc.Value);

                    lcov.Add("SF:" + doc.Key);
                    foreach (var @class in doc.Value)
                    {
                        foreach (var method in @class.Value)
                        {
                            // Skip all methods with no lines
                            if (method.Value.Lines.Count == 0)
                            {
                                continue;
                            }

                            lcov.Add($"FN:{method.Value.Lines.First().Key - 1},{method.Key}");
                            lcov.Add($"FNDA:{method.Value.Lines.First().Value},{method.Key}");

                            foreach (var line in method.Value.Lines)
                            {
                                lcov.Add($"DA:{line.Key},{line.Value}");
                            }

                            foreach (var branch in method.Value.Branches)
                            {
                                lcov.Add($"BRDA:{branch.Line},{branch.Offset},{branch.Path},{branch.Hits}");
                            }
                        }
                    }

                    lcov.Add($"LF:{docLineCoverage.Total}");
                    lcov.Add($"LH:{docLineCoverage.Covered}");

                    lcov.Add($"BRF:{docBranchCoverage.Total}");
                    lcov.Add($"BRH:{docBranchCoverage.Covered}");

                    lcov.Add($"FNF:{docMethodCoverage.Total}");
                    lcov.Add($"FNH:{docMethodCoverage.Covered}");

                    lcov.Add("end_of_record");
                }
            }

            return(string.Join(Environment.NewLine, lcov));
        }
Exemple #21
0
 public string Format(CoverageResult result)
 {
     using (var writer = new StringWriter())
     {
         JSON.Serialize(result.Modules, writer, Options.PrettyPrint);
         return(writer.ToString());
     }
 }
        public void TestEnsureParseMethodStringCorrectly(
            string methodString,
            string expectedMethodName,
            string expectedSignature)
        {
            var result = new CoverageResult();

            result.Parameters = new CoverageParameters();
            result.Identifier = Guid.NewGuid().ToString();

            var lines = new Lines();

            lines.Add(1, 1);

            var branches = new Branches();

            branches.Add(new BranchInfo {
                Line = 1, Hits = 1, Offset = 23, EndOffset = 24, Path = 0, Ordinal = 1
            });

            var methods = new Methods();

            methods.Add(methodString, new Method());
            methods[methodString].Lines    = lines;
            methods[methodString].Branches = branches;

            var classes = new Classes();

            classes.Add("Google.Protobuf.Reflection.MessageDescriptor", methods);

            var documents = new Documents();

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                documents.Add(@"C:\doc.cs", classes);
            }
            else
            {
                documents.Add(@"/doc.cs", classes);
            }

            result.Modules = new Modules();
            result.Modules.Add("module", documents);

            var    reporter = new CoberturaReporter();
            string report   = reporter.Report(result, new Mock <ISourceRootTranslator>().Object);

            Assert.NotEmpty(report);

            var doc         = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));
            var methodAttrs = doc.Descendants()
                              .Where(o => o.Name.LocalName == "method")
                              .Attributes()
                              .ToDictionary(o => o.Name.LocalName, o => o.Value);

            Assert.Equal(expectedMethodName, methodAttrs["name"]);
            Assert.Equal(expectedSignature, methodAttrs["signature"]);
        }
Exemple #23
0
        public void AsyncAwait()
        {
            string path = Path.GetTempFileName();

            try
            {
                RemoteExecutor.Invoke(async pathSerialize =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AsyncAwait>(instance =>
                    {
                        instance.SyncExecution();

                        int res = ((Task <int>)instance.AsyncExecution(true)).ConfigureAwait(false).GetAwaiter().GetResult();
                        res     = ((Task <int>)instance.AsyncExecution(1)).ConfigureAwait(false).GetAwaiter().GetResult();
                        res     = ((Task <int>)instance.AsyncExecution(2)).ConfigureAwait(false).GetAwaiter().GetResult();
                        res     = ((Task <int>)instance.AsyncExecution(3)).ConfigureAwait(false).GetAwaiter().GetResult();
                        res     = ((Task <int>)instance.ContinuationCalled()).ConfigureAwait(false).GetAwaiter().GetResult();
                        res     = ((Task <int>)instance.ConfigureAwait()).ConfigureAwait(false).GetAwaiter().GetResult();

                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize);
                    return(0);
                }, path).Dispose();

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.AsyncAwait.cs")
                .AssertLinesCovered(BuildConfiguration.Debug,
                                    // AsyncExecution(bool)
                                    (10, 1), (11, 1), (12, 1), (14, 1), (16, 1), (17, 0), (18, 0), (19, 0), (21, 1), (22, 1),
                                    // Async
                                    (25, 9), (26, 9), (27, 9), (28, 9),
                                    // SyncExecution
                                    (31, 1), (32, 1), (33, 1),
                                    // Sync
                                    (36, 1), (37, 1), (38, 1),
                                    // AsyncExecution(int)
                                    (41, 3), (42, 3), (43, 3), (46, 1), (47, 1), (48, 1), (51, 1),
                                    (52, 1), (53, 1), (56, 1), (57, 1), (58, 1), (59, 1),
                                    (62, 0), (63, 0), (64, 0), (65, 0), (68, 0), (70, 3), (71, 3),
                                    // ContinuationNotCalled
                                    (74, 0), (75, 0), (76, 0), (77, 0), (78, 0),
                                    // ContinuationCalled -> line 83 should be 1 hit some issue with Continuation state machine
                                    (81, 1), (82, 1), (83, 2), (84, 1), (85, 1),
                                    // ConfigureAwait
                                    (89, 1), (90, 1)
                                    )
                .AssertBranchesCovered(BuildConfiguration.Debug, (16, 0, 0), (16, 1, 1), (43, 0, 3), (43, 1, 1), (43, 2, 1), (43, 3, 1), (43, 4, 0))
                // Real branch should be 2, we should try to remove compiler generated branch in method ContinuationNotCalled/ContinuationCalled
                // for Continuation state machine
                .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 4);
            }
            finally
            {
                File.Delete(path);
            }
        }
Exemple #24
0
        public string Report(CoverageResult result)
        {
            CoverageSummary summary = new CoverageSummary();
            List <string>   lcov    = new List <string>();

            foreach (var module in result.Modules)
            {
                foreach (var doc in module.Value)
                {
                    var docLineCoverage   = summary.CalculateLineCoverage(doc.Value);
                    var docBranchCoverage = summary.CalculateBranchCoverage(doc.Value);
                    var docMethodCoverage = summary.CalculateMethodCoverage(doc.Value);

                    lcov.Add("SF:" + doc.Key);
                    foreach (var @class in doc.Value)
                    {
                        foreach (var method in @class.Value)
                        {
                            // Skip all methods with no lines
                            if (method.Value.Lines.Count == 0)
                            {
                                continue;
                            }

                            lcov.Add($"FN:{method.Value.Lines.First().Key - 1},{method.Key}");
                            lcov.Add($"FNDA:{method.Value.Lines.First().Value.Hits},{method.Key}");

                            foreach (var line in method.Value.Lines)
                            {
                                lcov.Add($"DA:{line.Key},{line.Value.Hits}");
                            }

                            foreach (var branchs in method.Value.Branches)
                            {
                                foreach (var branch in branchs.Value)
                                {
                                    lcov.Add($"BRDA:{branchs.Key},{branch.Offset},{branch.Path},{branch.Hits}");
                                }
                            }
                        }
                    }

                    lcov.Add($"LF:{docLineCoverage.Total}");
                    lcov.Add($"LH:{docLineCoverage.Covered}");

                    lcov.Add($"BRF:{docBranchCoverage.Total}");
                    lcov.Add($"BRH:{docBranchCoverage.Covered}");

                    lcov.Add($"FNF:{docMethodCoverage.Total}");
                    lcov.Add($"FNH:{docMethodCoverage.Covered}");

                    lcov.Add("end_of_record");
                }
            }

            return(string.Join(Environment.NewLine, lcov));
        }
Exemple #25
0
        public void TestFormat()
        {
            var mock = new Mock <IReporter>();

            CoverageResult coverageResult = new CoverageResult();

            coverageResult.Format(mock.Object);

            mock.Verify(m => m.Format(coverageResult));
        }
Exemple #26
0
        /// <summary>
        /// Gets coverlet coverage report
        /// </summary>
        /// <returns>Coverage report</returns>
        public string GetCoverageReport()
        {
            // Get coverage result
            CoverageResult coverageResult = this.GetCoverageResult();

            // Get coverage report in default format
            string coverageReport = this.GetCoverageReport(coverageResult);

            return(coverageReport);
        }
Exemple #27
0
        public string GenerateReport(CoverageResult coverageResult)
        {
            var settings = new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore,
                Formatting        = Formatting.None
            };

            return(JsonConvert.SerializeObject(coverageResult, settings));
        }
Exemple #28
0
        public void TestReportWithTwoDifferentDirectories()
        {
            CoverageResult result = new CoverageResult();

            result.Identifier = Guid.NewGuid().ToString();

            var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

            string absolutePath1;
            string absolutePath2;

            if (isWindows)
            {
                absolutePath1 = @"C:\projA\file.cs";
                absolutePath2 = @"E:\projB\file.cs";
            }
            else
            {
                absolutePath1 = @"/projA/file.cs";
                absolutePath2 = @"/projB/file.cs";
            }

            var classes = new Classes {
                { "Class", new Methods() }
            };
            var documents = new Documents {
                { absolutePath1, classes }, { absolutePath2, classes }
            };

            result.Modules = new Modules {
                { "Module", documents }
            };

            CoberturaReporter reporter = new CoberturaReporter();
            string            report   = reporter.Report(result);

            var doc = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(report)));

            List <string> rootPaths     = doc.Element("coverage").Element("sources").Elements().Select(e => e.Value).ToList();
            List <string> relativePaths = doc.Element("coverage").Element("packages").Element("package")
                                          .Element("classes").Elements().Select(e => e.Attribute("filename").Value).ToList();

            List <string> possiblePaths = new List <string>();

            foreach (string root in rootPaths)
            {
                foreach (string relativePath in relativePaths)
                {
                    possiblePaths.Add(Path.Combine(root, relativePath));
                }
            }

            Assert.Contains(absolutePath1, possiblePaths);
            Assert.Contains(absolutePath2, possiblePaths);
        }
Exemple #29
0
        public override bool Execute()
        {
            try
            {
                Console.WriteLine("\nCalculating coverage result...");
                var            coverage = InstrumentationTask.Coverage;
                CoverageResult result   = coverage.GetCoverageResult();

                var directory = Path.GetDirectoryName(_filename);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                Console.WriteLine($"  Generating report '{_filename}'");

                IReporter reporter = default(IReporter);
                switch (_format)
                {
                case "lcov":
                    reporter = new LcovReporter();
                    break;

                case "opencover":
                    reporter = new OpenCoverReporter();
                    break;

                default:
                    reporter = new JsonReporter();
                    break;
                }

                File.WriteAllText(_filename, result.Format(reporter));

                CoverageSummary coverageSummary = new CoverageSummary(result);
                var             summary         = coverageSummary.CalculateSummary();

                ConsoleTable table = new ConsoleTable("Module", "Coverage");
                foreach (var item in summary)
                {
                    table.AddRow(item.Key, $"{item.Value}%");
                }

                Console.WriteLine();
                table.Write(Format.Alternative);
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                return(false);
            }

            return(true);
        }
        public static CoverageResult GenerateReport(this CoverageResult coverageResult, [CallerMemberName] string directory = "")
        {
            if (coverageResult is null)
            {
                throw new ArgumentNullException(nameof(coverageResult));
            }

            TestInstrumentationHelper.GenerateHtmlReport(coverageResult, directory: directory);

            return(coverageResult);
        }