| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Net; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Text.Encodings.Web; |
| | | 9 | | using System.Text.RegularExpressions; |
| | | 10 | | using Microsoft.Extensions.ObjectPool; |
| | | 11 | | using DirectSight.CodeAnalysis; |
| | | 12 | | using DirectSight.Common; |
| | | 13 | | using DirectSight.Logging; |
| | | 14 | | using DirectSight.Parser.Analysis; |
| | | 15 | | |
| | | 16 | | namespace DirectSight.Reporting.Builders.Rendering; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// HTML report renderer. |
| | | 20 | | /// </summary> |
| | | 21 | | internal class HtmlRenderer : IHtmlRenderer, IDisposable |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// The link to the static CSS file. |
| | | 25 | | /// </summary> |
| | | 26 | | private const string CssLink = "<link rel=\"stylesheet\" type=\"text/css\" href=\"report.css\" />"; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Indicates whether which CSS and Javascript files have already been written. |
| | | 30 | | /// </summary> |
| | 1 | 31 | | private static readonly HashSet<string> WrittenCssAndJavascriptFiles = []; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Dictionary containing the filenames of the class reports by class. |
| | | 35 | | /// </summary> |
| | | 36 | | private readonly IDictionary<string, string> fileNameByClass; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Indicates that only a summary report is created (no class reports). |
| | | 40 | | /// </summary> |
| | | 41 | | private readonly bool onlySummary; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Contains report specific JavaScript content. |
| | | 45 | | /// </summary> |
| | | 46 | | private readonly StringBuilder javaScriptContent; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// The css file resource. |
| | | 50 | | /// </summary> |
| | | 51 | | private readonly string cssFileResource; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Optional additional CSS file resources. |
| | | 55 | | /// </summary> |
| | | 56 | | private readonly string[] additionalCssFileResources; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Indicates that JavaScript was generated. |
| | | 60 | | /// </summary> |
| | | 61 | | private bool javaScriptGenerated; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// The report builder. |
| | | 65 | | /// </summary> |
| | | 66 | | private TextWriter reportTextWriter; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Indicates that that a class report is created (not the summary page). |
| | | 70 | | /// </summary> |
| | | 71 | | private bool classReport; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Initializes a new instance of the <see cref="HtmlRenderer" /> class. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="fileNameByClass">Dictionary containing the filenames of the class reports by class.</param> |
| | | 77 | | /// <param name="onlySummary">if set to <c>true</c> only a summary report is created (no class reports).</param> |
| | | 78 | | /// <param name="cssFileResource">Optional CSS file resource.</param> |
| | | 79 | | /// <param name="additionalCssFileResource">Optional additional CSS file resource.</param> |
| | 0 | 80 | | internal HtmlRenderer( |
| | 0 | 81 | | IDictionary<string, string> fileNameByClass, |
| | 0 | 82 | | bool onlySummary, |
| | 0 | 83 | | string cssFileResource = "custom.css", |
| | 0 | 84 | | string additionalCssFileResource = "custom_adaptive.css") |
| | 0 | 85 | | { |
| | 0 | 86 | | this.fileNameByClass = fileNameByClass; |
| | 0 | 87 | | this.onlySummary = onlySummary; |
| | 0 | 88 | | this.javaScriptContent = StringBuilderCache.Get(); |
| | 0 | 89 | | this.cssFileResource = cssFileResource; |
| | 0 | 90 | | this.additionalCssFileResources = additionalCssFileResource == null ? [] : [additionalCssFileResource]; |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Initializes a new instance of the <see cref="HtmlRenderer" /> class. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="fileNameByClass">Dictionary containing the filenames of the class reports by class.</param> |
| | | 97 | | /// <param name="onlySummary">if set to <c>true</c> only a summary report is created (no class reports).</param> |
| | | 98 | | /// <param name="additionalCssFileResources">Optional additional CSS file resources.</param> |
| | | 99 | | /// <param name="cssFileResource">Optional CSS file resource.</param> |
| | 38 | 100 | | internal HtmlRenderer( |
| | 38 | 101 | | IDictionary<string, string> fileNameByClass, |
| | 38 | 102 | | bool onlySummary, |
| | 38 | 103 | | string[] additionalCssFileResources, |
| | 38 | 104 | | string cssFileResource = "custom.css") |
| | 38 | 105 | | { |
| | 38 | 106 | | this.fileNameByClass = fileNameByClass; |
| | 38 | 107 | | this.onlySummary = onlySummary; |
| | 38 | 108 | | this.javaScriptContent = StringBuilderCache.Get(); |
| | 38 | 109 | | this.additionalCssFileResources = additionalCssFileResources ?? []; |
| | 38 | 110 | | this.cssFileResource = cssFileResource; |
| | 38 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <inheritdoc /> |
| | | 114 | | public void BeginSummaryReport(string targetDirectory, string fileName, string title) |
| | 2 | 115 | | { |
| | 2 | 116 | | string targetPath = Path.Combine(targetDirectory, this.onlySummary ? "summary.html" : "index.html"); |
| | | 117 | | |
| | 2 | 118 | | if (fileName != null) |
| | 0 | 119 | | { |
| | 0 | 120 | | targetPath = Path.Combine(targetDirectory, fileName); |
| | 0 | 121 | | } |
| | | 122 | | |
| | 2 | 123 | | ConsoleLogger.Info("Writing report file '{0}'", targetPath); |
| | 2 | 124 | | this.CreateTextWriter(targetPath); |
| | | 125 | | |
| | 2 | 126 | | WriteHtmlStart(this.reportTextWriter, title, "Coverage Report"); |
| | 2 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <inheritdoc /> |
| | | 130 | | public void BeginClassReport(string targetDirectory, Assembly assembly, string className, string classDisplayName, s |
| | 36 | 131 | | { |
| | 36 | 132 | | this.classReport = true; |
| | | 133 | | |
| | 36 | 134 | | string targetPath = this.GetClassReportFilename(assembly, className); |
| | | 135 | | |
| | 36 | 136 | | ConsoleLogger.Debug("Writing report file '{0}'", targetPath); |
| | 36 | 137 | | this.CreateTextWriter(Path.Combine(targetDirectory, targetPath)); |
| | | 138 | | |
| | 36 | 139 | | WriteHtmlStart(this.reportTextWriter, classDisplayName, additionalTitle + "Coverage Report"); |
| | 36 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <inheritdoc /> |
| | | 143 | | public void Cards(IEnumerable<Card> cards) |
| | 74 | 144 | | { |
| | 74 | 145 | | this.reportTextWriter.WriteLine("<div class=\"card-group\">"); |
| | | 146 | | |
| | 526 | 147 | | foreach (var card in cards) |
| | 152 | 148 | | { |
| | 152 | 149 | | this.reportTextWriter.WriteLine("<div class=\"card\">"); |
| | | 150 | | |
| | 152 | 151 | | if (!string.IsNullOrWhiteSpace(card.Title)) |
| | 152 | 152 | | { |
| | 152 | 153 | | this.reportTextWriter.WriteLine("<div class=\"card-header\">{0}</div>", card.Title); |
| | 152 | 154 | | } |
| | | 155 | | |
| | 152 | 156 | | this.reportTextWriter.WriteLine("<div class=\"card-body\">"); |
| | 152 | 157 | | if (!string.IsNullOrWhiteSpace(card.SubTitle)) |
| | 76 | 158 | | { |
| | 76 | 159 | | string clazz = string.Empty; |
| | | 160 | | |
| | 76 | 161 | | if (card.SubTitlePercentage.HasValue) |
| | 44 | 162 | | { |
| | 44 | 163 | | int uncovered = 100 - (int)Math.Round(card.SubTitlePercentage.Value, 0); |
| | | 164 | | |
| | 44 | 165 | | clazz = $" cardpercentagebar cardpercentagebar{uncovered}"; |
| | 44 | 166 | | } |
| | | 167 | | |
| | 76 | 168 | | this.reportTextWriter.WriteLine("<div class=\"large{0}\">{1}</div>", clazz, WebUtility.HtmlEncode(card.S |
| | 76 | 169 | | } |
| | | 170 | | |
| | 152 | 171 | | this.reportTextWriter.WriteLine("<div class=\"table\">"); |
| | 152 | 172 | | this.reportTextWriter.WriteLine("<table>"); |
| | | 173 | | |
| | 1296 | 174 | | foreach (var row in card.Rows) |
| | 420 | 175 | | { |
| | 420 | 176 | | this.reportTextWriter.WriteLine("<tr>"); |
| | 420 | 177 | | this.reportTextWriter.WriteLine("<th>{0}</th>", WebUtility.HtmlEncode(row.Header)); |
| | 420 | 178 | | if (row.Links != null) |
| | 36 | 179 | | { |
| | 36 | 180 | | int fileNumber = 1; |
| | | 181 | | |
| | 36 | 182 | | bool usePrefix = row.Links.Count > 1; |
| | | 183 | | |
| | 36 | 184 | | string value = string.Join( |
| | 36 | 185 | | "<br />", |
| | 72 | 186 | | row.Links.Select(v => string.Format( |
| | 72 | 187 | | "<a href=\"#{0}\" class=\"navigatetohash\">{1}</a>", |
| | 72 | 188 | | WebUtility.HtmlEncode(StringHelper.ReplaceNonLetterChars(v)), |
| | 72 | 189 | | WebUtility.HtmlEncode((usePrefix ? $"File {fileNumber++}: " : string.Empty) + v)))); |
| | | 190 | | |
| | 36 | 191 | | this.reportTextWriter.WriteLine( |
| | 36 | 192 | | "<td class=\"overflow-wrap\">{0}</td>", |
| | 36 | 193 | | value); |
| | 36 | 194 | | } |
| | | 195 | | else |
| | 384 | 196 | | { |
| | 384 | 197 | | this.reportTextWriter.WriteLine( |
| | 384 | 198 | | "<td class=\"limit-width {0}\" title=\"{1}\">{2}</td>", |
| | 384 | 199 | | row.Alignment == CardLineItemAlignment.Right ? "right" : string.Empty, |
| | 384 | 200 | | WebUtility.HtmlEncode(row.Tooltip ?? row.Text), |
| | 384 | 201 | | WebUtility.HtmlEncode(row.Text)); |
| | 384 | 202 | | } |
| | | 203 | | |
| | 420 | 204 | | this.reportTextWriter.WriteLine("</tr>"); |
| | 420 | 205 | | } |
| | | 206 | | |
| | 152 | 207 | | this.reportTextWriter.WriteLine("</table>"); |
| | 152 | 208 | | this.reportTextWriter.WriteLine("</div>"); |
| | | 209 | | |
| | 152 | 210 | | this.reportTextWriter.WriteLine("</div>"); |
| | 152 | 211 | | this.reportTextWriter.WriteLine("</div>"); |
| | 152 | 212 | | } |
| | | 213 | | |
| | 74 | 214 | | this.reportTextWriter.WriteLine("</div>"); |
| | 74 | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <inheritdoc /> |
| | | 218 | | public void Header(string text) |
| | 68 | 219 | | { |
| | 68 | 220 | | this.reportTextWriter.WriteLine("<h1>{0}</h1>", WebUtility.HtmlEncode(text)); |
| | 68 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <inheritdoc /> |
| | | 224 | | public void HeaderWithGithubLinks(string text) |
| | 2 | 225 | | { |
| | 2 | 226 | | this.reportTextWriter.WriteLine( |
| | 2 | 227 | | "<h1>{0}<a class=\"button\" href=\"https://github.com/joharasmus/DirectSight\" title=\"{1}\"><i class=\"icon |
| | 2 | 228 | | WebUtility.HtmlEncode(text), |
| | 2 | 229 | | WebUtility.HtmlEncode("Star on GitHub"), |
| | 2 | 230 | | WebUtility.HtmlEncode("Star")); |
| | 2 | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <inheritdoc /> |
| | | 234 | | public void HeaderWithBackLink(string text) |
| | 36 | 235 | | { |
| | 36 | 236 | | this.reportTextWriter.WriteLine("<h1><a href=\"index.html\" class=\"back\"><</a> {0}</h1>", WebUtility.HtmlEn |
| | 36 | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <inheritdoc /> |
| | | 240 | | public void TestMethods( |
| | | 241 | | IEnumerable<TestMethod> testMethods, |
| | | 242 | | IEnumerable<FileAnalysis> fileAnalyses, |
| | | 243 | | IDictionary<int, IEnumerable<CodeElement>> codeElementsByFileIndex) |
| | 32 | 244 | | { |
| | 32 | 245 | | ArgumentNullException.ThrowIfNull(testMethods); |
| | | 246 | | |
| | 32 | 247 | | if (!testMethods.Any() && codeElementsByFileIndex.Count == 0) |
| | 0 | 248 | | { |
| | 0 | 249 | | return; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | // Close 'containerleft' and begin 'containerright' |
| | 32 | 253 | | this.reportTextWriter.WriteLine("</div>"); |
| | 32 | 254 | | this.reportTextWriter.WriteLine("<div class=\"containerright\">"); |
| | 32 | 255 | | this.reportTextWriter.WriteLine("<div class=\"containerrightfixed\">"); |
| | | 256 | | |
| | 32 | 257 | | if (testMethods.Any()) |
| | 26 | 258 | | { |
| | 26 | 259 | | this.reportTextWriter.WriteLine("<h1>{0}</h1>", WebUtility.HtmlEncode("Coverage by test methods")); |
| | | 260 | | |
| | 1160 | 261 | | int coverableLines = fileAnalyses.SafeSum(f => f.Lines.Count(l => l.LineVisitStatus != LineVisitStatus.NotCo |
| | 1160 | 262 | | int coveredLines = fileAnalyses.SafeSum(f => f.Lines.Count(l => l.LineVisitStatus > LineVisitStatus.NotCover |
| | 26 | 263 | | decimal? coverage = (coverableLines == 0) ? null : MathExtensions.CalculatePercentage(coveredLines, coverabl |
| | | 264 | | |
| | 26 | 265 | | int? coverageRounded = null; |
| | | 266 | | |
| | 26 | 267 | | if (coverage.HasValue) |
| | 26 | 268 | | { |
| | 26 | 269 | | coverageRounded = (int)coverage.Value; |
| | 26 | 270 | | coverageRounded -= coverageRounded % 10; |
| | 26 | 271 | | } |
| | | 272 | | |
| | 26 | 273 | | this.reportTextWriter.WriteLine( |
| | 26 | 274 | | "<label id=\"AllTestMethods\" class=\"testmethod percentagebar percentagebar{0}\" title=\"{1}{2}\">" + |
| | 26 | 275 | | "<input type=\"radio\" name=\"method\" value=\"AllTestMethods\" class=\"switchtestmethod\" checked=\"che |
| | 26 | 276 | | coverage.HasValue ? coverageRounded.ToString() : "undefined", |
| | 26 | 277 | | coverage.HasValue ? "Line coverage: " + coverage.Value.ToString() + "% - " : string.Empty, |
| | 26 | 278 | | WebUtility.HtmlEncode("All")); |
| | | 279 | | |
| | 182 | 280 | | foreach (var testMethod in testMethods) |
| | 52 | 281 | | { |
| | 52 | 282 | | coveredLines = fileAnalyses.SafeSum( |
| | 112 | 283 | | f => f.Lines.Count( |
| | 2320 | 284 | | l => l.LineCoverageByTestMethod.ContainsKey(testMethod) |
| | 2320 | 285 | | && l.LineCoverageByTestMethod[testMethod].LineVisitStatus > LineVisitStatus.NotCovered)); |
| | | 286 | | |
| | 52 | 287 | | coverage = (coverableLines == 0) ? null : MathExtensions.CalculatePercentage(coveredLines, coverableLine |
| | | 288 | | |
| | 52 | 289 | | coverageRounded = null; |
| | | 290 | | |
| | 52 | 291 | | if (coverage.HasValue) |
| | 52 | 292 | | { |
| | 52 | 293 | | coverageRounded = (int)coverage.Value; |
| | 52 | 294 | | coverageRounded -= coverageRounded % 10; |
| | 52 | 295 | | } |
| | | 296 | | |
| | 52 | 297 | | this.reportTextWriter.WriteLine( |
| | 52 | 298 | | "<br /><label id=\"M{3}\" class=\"testmethod percentagebar percentagebar{0}\" title=\"{1}{2}\">" |
| | 52 | 299 | | + "<input type=\"radio\" name=\"method\" value=\"M{3}\" class=\"switchtestmethod\" />{4}</label>", |
| | 52 | 300 | | coverage.HasValue ? coverageRounded.ToString() : "undefined", |
| | 52 | 301 | | coverage.HasValue ? "Line coverage: " + coverage.Value.ToString() + "% - " : string.Empty, |
| | 52 | 302 | | WebUtility.HtmlEncode(testMethod.Name), |
| | 52 | 303 | | testMethod.Id, |
| | 52 | 304 | | WebUtility.HtmlEncode(testMethod.ShortName)); |
| | 52 | 305 | | } |
| | 26 | 306 | | } |
| | | 307 | | |
| | 32 | 308 | | if (codeElementsByFileIndex.Count > 0) |
| | 32 | 309 | | { |
| | 32 | 310 | | this.reportTextWriter.WriteLine("<h1>{0}</h1>", WebUtility.HtmlEncode("Methods/Properties")); |
| | | 311 | | |
| | 168 | 312 | | foreach (var item in codeElementsByFileIndex) |
| | 36 | 313 | | { |
| | 328 | 314 | | foreach (var codeElement in item.Value) |
| | 110 | 315 | | { |
| | 110 | 316 | | int? coverageRounded = null; |
| | | 317 | | |
| | 110 | 318 | | if (codeElement.CoverageQuota.HasValue) |
| | 110 | 319 | | { |
| | 110 | 320 | | coverageRounded = (int)codeElement.CoverageQuota.Value; |
| | 110 | 321 | | coverageRounded -= coverageRounded % 10; |
| | 110 | 322 | | } |
| | | 323 | | |
| | 110 | 324 | | string prefix = fileAnalyses.Count() > 1 ? $"File {item.Key + 1}: " : string.Empty; |
| | 110 | 325 | | this.reportTextWriter.WriteLine( |
| | 110 | 326 | | "<a href=\"#file{0}_line{1}\" class=\"navigatetohash percentagebar percentagebar{2}\" title=\"{3 |
| | 110 | 327 | | + "<i class=\"icon-{5}\"></i>{4}</a><br />", |
| | 110 | 328 | | item.Key, |
| | 110 | 329 | | codeElement.FirstLine, |
| | 110 | 330 | | codeElement.CoverageQuota.HasValue ? coverageRounded.ToString() : "undefined", |
| | 110 | 331 | | prefix + (codeElement.CoverageQuota.HasValue |
| | 110 | 332 | | ? "Line coverage: " + codeElement.CoverageQuota.Value.ToString() + "% - " |
| | 110 | 333 | | : string.Empty), |
| | 110 | 334 | | WebUtility.HtmlEncode(codeElement.Name), |
| | 110 | 335 | | codeElement.CodeElementType == CodeElementType.Method ? "cube" : "wrench"); |
| | 110 | 336 | | } |
| | 36 | 337 | | } |
| | 32 | 338 | | } |
| | | 339 | | |
| | 32 | 340 | | this.reportTextWriter.WriteLine("<br/></div>"); |
| | 32 | 341 | | } |
| | | 342 | | |
| | | 343 | | /// <inheritdoc /> |
| | | 344 | | public void File(string path) |
| | 36 | 345 | | { |
| | 36 | 346 | | this.reportTextWriter.WriteLine( |
| | 36 | 347 | | "<h2 id=\"{0}\">{1}</h2>", |
| | 36 | 348 | | WebUtility.HtmlEncode(StringHelper.ReplaceNonLetterChars(path)), |
| | 36 | 349 | | WebUtility.HtmlEncode(path)); |
| | 36 | 350 | | } |
| | | 351 | | |
| | | 352 | | /// <inheritdoc /> |
| | | 353 | | public void Paragraph(string text) |
| | 6 | 354 | | { |
| | 6 | 355 | | this.reportTextWriter.WriteLine("<p>{0}</p>", WebUtility.HtmlEncode(text)); |
| | 6 | 356 | | } |
| | | 357 | | |
| | | 358 | | /// <inheritdoc /> |
| | | 359 | | public void BeginSummaryTable() |
| | 2 | 360 | | { |
| | 2 | 361 | | this.reportTextWriter.WriteLine("<coverage-info>"); |
| | 2 | 362 | | } |
| | | 363 | | |
| | | 364 | | /// <inheritdoc /> |
| | | 365 | | public void FinishSummaryTable() |
| | 2 | 366 | | { |
| | 2 | 367 | | this.reportTextWriter.WriteLine("</coverage-info>"); |
| | 2 | 368 | | } |
| | | 369 | | |
| | | 370 | | /// <inheritdoc /> |
| | | 371 | | public void BeginSummaryTable(bool branchCoverageAvailable) |
| | 2 | 372 | | { |
| | 2 | 373 | | this.reportTextWriter.WriteLine("<div class=\"table-responsive\">"); |
| | 2 | 374 | | this.reportTextWriter.WriteLine("<table class=\"overview table-fixed stripped\">"); |
| | 2 | 375 | | this.reportTextWriter.WriteLine("<colgroup>"); |
| | 2 | 376 | | this.reportTextWriter.WriteLine("<col class=\"column-min-200\" />"); |
| | 2 | 377 | | this.reportTextWriter.WriteLine("<col class=\"column90\" />"); |
| | 2 | 378 | | this.reportTextWriter.WriteLine("<col class=\"column105\" />"); |
| | 2 | 379 | | this.reportTextWriter.WriteLine("<col class=\"column100\" />"); |
| | 2 | 380 | | this.reportTextWriter.WriteLine("<col class=\"column70\" />"); |
| | 2 | 381 | | this.reportTextWriter.WriteLine("<col class=\"column98\" />"); |
| | 2 | 382 | | this.reportTextWriter.WriteLine("<col class=\"column112\" />"); |
| | 2 | 383 | | if (branchCoverageAvailable) |
| | 2 | 384 | | { |
| | 2 | 385 | | this.reportTextWriter.WriteLine("<col class=\"column90\" />"); |
| | 2 | 386 | | this.reportTextWriter.WriteLine("<col class=\"column70\" />"); |
| | 2 | 387 | | this.reportTextWriter.WriteLine("<col class=\"column98\" />"); |
| | 2 | 388 | | this.reportTextWriter.WriteLine("<col class=\"column112\" />"); |
| | 2 | 389 | | } |
| | | 390 | | |
| | 2 | 391 | | this.reportTextWriter.WriteLine("</colgroup>"); |
| | | 392 | | |
| | 2 | 393 | | this.reportTextWriter.WriteLine("<thead>"); |
| | 2 | 394 | | this.reportTextWriter.Write("<tr class=\"header\">"); |
| | 2 | 395 | | this.reportTextWriter.Write( |
| | 2 | 396 | | "<th></th><th colspan=\"6\" class=\"center\">{0}</th>", |
| | 2 | 397 | | WebUtility.HtmlEncode("Line coverage")); |
| | | 398 | | |
| | 2 | 399 | | if (branchCoverageAvailable) |
| | 2 | 400 | | { |
| | 2 | 401 | | this.reportTextWriter.Write( |
| | 2 | 402 | | "<th colspan=\"4\" class=\"center\">{0}</th>", |
| | 2 | 403 | | WebUtility.HtmlEncode("Branch coverage")); |
| | 2 | 404 | | } |
| | | 405 | | |
| | 2 | 406 | | this.reportTextWriter.WriteLine("</tr>"); |
| | | 407 | | |
| | 2 | 408 | | this.reportTextWriter.Write( |
| | 2 | 409 | | "<tr>" |
| | 2 | 410 | | + "<th>{0}</th>" |
| | 2 | 411 | | + "<th class=\"right\">{1}</th>" |
| | 2 | 412 | | + "<th class=\"right\">{2}</th>" |
| | 2 | 413 | | + "<th class=\"right\">{3}</th>" |
| | 2 | 414 | | + "<th class=\"right\">{4}</th>" |
| | 2 | 415 | | + "<th class=\"center\" colspan=\"2\">{5}</th>", |
| | 2 | 416 | | WebUtility.HtmlEncode("Name"), |
| | 2 | 417 | | WebUtility.HtmlEncode("Covered"), |
| | 2 | 418 | | WebUtility.HtmlEncode("Uncovered"), |
| | 2 | 419 | | WebUtility.HtmlEncode("Coverable"), |
| | 2 | 420 | | WebUtility.HtmlEncode("Total"), |
| | 2 | 421 | | WebUtility.HtmlEncode("Percentage")); |
| | | 422 | | |
| | 2 | 423 | | if (branchCoverageAvailable) |
| | 2 | 424 | | { |
| | 2 | 425 | | this.reportTextWriter.Write( |
| | 2 | 426 | | "<th class=\"right\">{0}</th><th class=\"right\">{1}</th><th class=\"center\" colspan=\"2\">{2}</th>", |
| | 2 | 427 | | WebUtility.HtmlEncode("Covered"), |
| | 2 | 428 | | WebUtility.HtmlEncode("Total"), |
| | 2 | 429 | | WebUtility.HtmlEncode("Percentage")); |
| | 2 | 430 | | } |
| | | 431 | | |
| | 2 | 432 | | this.reportTextWriter.WriteLine("</tr></thead>"); |
| | 2 | 433 | | this.reportTextWriter.WriteLine("<tbody>"); |
| | 2 | 434 | | } |
| | | 435 | | |
| | | 436 | | /// <inheritdoc /> |
| | | 437 | | public void CustomSummary( |
| | | 438 | | IEnumerable<Assembly> assemblies, |
| | | 439 | | IEnumerable<RiskHotspot> riskHotspots, |
| | | 440 | | bool branchCoverageAvailable) |
| | 2 | 441 | | { |
| | 2 | 442 | | ArgumentNullException.ThrowIfNull(assemblies); |
| | | 443 | | |
| | 2 | 444 | | ArgumentNullException.ThrowIfNull(riskHotspots); |
| | | 445 | | |
| | 2 | 446 | | if (this.classReport) |
| | 0 | 447 | | { |
| | 0 | 448 | | return; |
| | | 449 | | } |
| | | 450 | | |
| | 2 | 451 | | this.javaScriptContent.AppendLine("var assemblies = ["); |
| | | 452 | | |
| | 2 | 453 | | var tagsByBistoricCoverageExecutionTime = new Dictionary<DateTime, string>(); |
| | 2 | 454 | | var metricsByName = new Dictionary<string, Metric>(); |
| | | 455 | | |
| | 10 | 456 | | foreach (var assembly in assemblies) |
| | 2 | 457 | | { |
| | 2 | 458 | | this.javaScriptContent.AppendLine(" {"); |
| | 2 | 459 | | this.javaScriptContent.AppendFormat(" \"name\": \"{0}\",", JavaScriptEncoder.Default.Encode(assembly.Name |
| | 2 | 460 | | this.javaScriptContent.AppendLine(); |
| | 2 | 461 | | this.javaScriptContent.AppendLine(" \"classes\": ["); |
| | | 462 | | |
| | 78 | 463 | | foreach (var @class in assembly.Classes) |
| | 36 | 464 | | { |
| | | 465 | | void WriteMetricsCoverage() |
| | 36 | 466 | | { |
| | 36 | 467 | | this.javaScriptContent.Append('{'); |
| | | 468 | | |
| | 856 | 469 | | foreach (var metricGroup in @class.Files.SelectMany(f => f.MethodMetrics).SelectMany(m => m.Metrics) |
| | 140 | 470 | | { |
| | 140 | 471 | | var firstMetric = metricGroup.First(); |
| | 140 | 472 | | metricsByName[firstMetric.Name] = firstMetric; |
| | 140 | 473 | | } |
| | | 474 | | |
| | 36 | 475 | | this.javaScriptContent.Append(" }"); |
| | 36 | 476 | | } |
| | | 477 | | |
| | 36 | 478 | | this.javaScriptContent.Append(" { "); |
| | 36 | 479 | | this.javaScriptContent.AppendFormat("\"name\": \"{0}\",", JavaScriptEncoder.Default.Encode(@class.Displa |
| | 36 | 480 | | this.javaScriptContent.AppendFormat( |
| | 36 | 481 | | " \"rp\": \"{0}\",", |
| | 36 | 482 | | this.onlySummary ? string.Empty : JavaScriptEncoder.Default.Encode(this.GetClassReportFilename(@clas |
| | 36 | 483 | | this.javaScriptContent.AppendFormat(" \"cl\": {0},", @class.CoveredLines.ToString()); |
| | 36 | 484 | | this.javaScriptContent.AppendFormat(" \"ucl\": {0},", (@class.CoverableLines - @class.CoveredLines).ToSt |
| | 36 | 485 | | this.javaScriptContent.AppendFormat(" \"cal\": {0},", @class.CoverableLines.ToString()); |
| | 36 | 486 | | this.javaScriptContent.AppendFormat(" \"tl\": {0},", @class.TotalLines.GetValueOrDefault().ToString()); |
| | | 487 | | |
| | 36 | 488 | | this.javaScriptContent.AppendFormat(" \"cb\": {0},", @class.CoveredBranches.GetValueOrDefault().ToString |
| | 36 | 489 | | this.javaScriptContent.AppendFormat(" \"tb\": {0},", @class.TotalBranches.GetValueOrDefault().ToString() |
| | 36 | 490 | | this.javaScriptContent.AppendFormat(" \"cm\": {0},", "0"); |
| | 36 | 491 | | this.javaScriptContent.AppendFormat(" \"fcm\": {0},","0"); |
| | 36 | 492 | | this.javaScriptContent.AppendFormat(" \"tm\": {0},", "0"); |
| | | 493 | | |
| | 36 | 494 | | this.javaScriptContent.Append(" \"hc\": "); |
| | 36 | 495 | | this.javaScriptContent.Append(','); |
| | | 496 | | |
| | 36 | 497 | | this.javaScriptContent.Append(" \"metrics\": "); |
| | 36 | 498 | | WriteMetricsCoverage(); |
| | | 499 | | |
| | 36 | 500 | | this.javaScriptContent.AppendLine(" },"); |
| | 36 | 501 | | } |
| | | 502 | | |
| | 2 | 503 | | this.javaScriptContent.AppendLine(" ]},"); |
| | 2 | 504 | | } |
| | | 505 | | |
| | 2 | 506 | | this.javaScriptContent.AppendLine("];"); |
| | | 507 | | |
| | 2 | 508 | | this.javaScriptContent.AppendLine(); |
| | | 509 | | |
| | 2 | 510 | | this.javaScriptContent.Append("var metrics = ["); |
| | 2 | 511 | | int metricAbbreviationCounter = 0; |
| | | 512 | | |
| | 26 | 513 | | foreach (var item in metricsByName) |
| | 10 | 514 | | { |
| | 10 | 515 | | if (metricAbbreviationCounter++ > 0) |
| | 8 | 516 | | { |
| | 8 | 517 | | this.javaScriptContent.Append(", "); |
| | 8 | 518 | | } |
| | | 519 | | |
| | 10 | 520 | | this.javaScriptContent.AppendFormat( |
| | 10 | 521 | | "{{ \"name\": \"{0}\", \"abbreviation\": \"{1}\", \"explanationUrl\": \"{2}\" }}", |
| | 10 | 522 | | JavaScriptEncoder.Default.Encode(item.Key), |
| | 10 | 523 | | JavaScriptEncoder.Default.Encode(item.Value.Abbreviation), |
| | 10 | 524 | | JavaScriptEncoder.Default.Encode(item.Value.ExplanationUrl.ToString())); |
| | 10 | 525 | | } |
| | | 526 | | |
| | 2 | 527 | | this.javaScriptContent.AppendLine("];"); |
| | | 528 | | |
| | 2 | 529 | | this.javaScriptContent.AppendLine(); |
| | | 530 | | |
| | 2 | 531 | | this.javaScriptContent.AppendLine("];"); |
| | | 532 | | |
| | 2 | 533 | | this.javaScriptContent.AppendLine(); |
| | | 534 | | |
| | 2 | 535 | | this.javaScriptContent.AppendLine("var riskHotspotMetrics = ["); |
| | | 536 | | |
| | 2 | 537 | | if (riskHotspots.Any()) |
| | 0 | 538 | | { |
| | 0 | 539 | | foreach (var metric in riskHotspots.First().StatusMetrics) |
| | 0 | 540 | | { |
| | 0 | 541 | | this.javaScriptContent.Append(" { "); |
| | 0 | 542 | | this.javaScriptContent.AppendFormat("\"name\": \"{0}\",", JavaScriptEncoder.Default.Encode(metric.Metric |
| | 0 | 543 | | this.javaScriptContent.AppendFormat( |
| | 0 | 544 | | " \"explanationUrl\": \"{0}\"", |
| | 0 | 545 | | JavaScriptEncoder.Default.Encode(metric.Metric.ExplanationUrl.ToString())); |
| | 0 | 546 | | this.javaScriptContent.AppendLine(" },"); |
| | 0 | 547 | | } |
| | 0 | 548 | | } |
| | | 549 | | |
| | 2 | 550 | | this.javaScriptContent.AppendLine("];"); |
| | | 551 | | |
| | 2 | 552 | | this.javaScriptContent.AppendLine(); |
| | | 553 | | |
| | 2 | 554 | | this.javaScriptContent.AppendLine("var riskHotspots = ["); |
| | | 555 | | |
| | 6 | 556 | | foreach (var riskHotspot in riskHotspots) |
| | 0 | 557 | | { |
| | 0 | 558 | | this.javaScriptContent.AppendLine(" {"); |
| | 0 | 559 | | this.javaScriptContent.AppendFormat(" \"assembly\": \"{0}\",", JavaScriptEncoder.Default.Encode(riskHotsp |
| | 0 | 560 | | this.javaScriptContent.AppendFormat(" \"class\": \"{0}\",", JavaScriptEncoder.Default.Encode(riskHotspot.Cla |
| | 0 | 561 | | this.javaScriptContent.AppendFormat( |
| | 0 | 562 | | " \"reportPath\": \"{0}\",", |
| | 0 | 563 | | this.onlySummary |
| | 0 | 564 | | ? string.Empty |
| | 0 | 565 | | : JavaScriptEncoder.Default.Encode(this.GetClassReportFilename(riskHotspot.Assembly, riskHotspot.Class.N |
| | 0 | 566 | | this.javaScriptContent.AppendFormat(" \"methodName\": \"{0}\",", JavaScriptEncoder.Default.Encode(riskHotspo |
| | 0 | 567 | | this.javaScriptContent.AppendFormat(" \"methodShortName\": \"{0}\",", JavaScriptEncoder.Default.Encode(riskH |
| | 0 | 568 | | this.javaScriptContent.AppendFormat(" \"fileIndex\": {0},", riskHotspot.FileIndex); |
| | 0 | 569 | | this.javaScriptContent.AppendFormat( |
| | 0 | 570 | | " \"line\": {0},", |
| | 0 | 571 | | !this.onlySummary && riskHotspot.MethodMetric.Line.HasValue |
| | 0 | 572 | | ? riskHotspot.MethodMetric.Line.Value.ToString() |
| | 0 | 573 | | : "null"); |
| | 0 | 574 | | this.javaScriptContent.AppendLine(); |
| | 0 | 575 | | this.javaScriptContent.AppendLine(" \"metrics\": ["); |
| | | 576 | | |
| | 0 | 577 | | foreach (var metric in riskHotspot.StatusMetrics) |
| | 0 | 578 | | { |
| | 0 | 579 | | this.javaScriptContent.Append(" { "); |
| | 0 | 580 | | this.javaScriptContent.AppendFormat( |
| | 0 | 581 | | "\"value\": {0},", |
| | 0 | 582 | | metric.Metric.Value.HasValue ? metric.Metric.Value.Value.ToString("0.##") : "null"); |
| | 0 | 583 | | this.javaScriptContent.AppendFormat(" \"exceeded\": {0}", metric.Exceeded.ToString().ToLowerInvariant()) |
| | 0 | 584 | | this.javaScriptContent.AppendLine(" },"); |
| | 0 | 585 | | } |
| | | 586 | | |
| | 0 | 587 | | this.javaScriptContent.AppendLine(" ]},"); |
| | 0 | 588 | | } |
| | | 589 | | |
| | 2 | 590 | | this.javaScriptContent.AppendLine("];"); |
| | | 591 | | |
| | 2 | 592 | | this.javaScriptContent.AppendLine(); |
| | | 593 | | |
| | 2 | 594 | | this.javaScriptContent.AppendLine("var branchCoverageAvailable = " + branchCoverageAvailable.ToString().ToLowerI |
| | 2 | 595 | | this.javaScriptContent.AppendLine("var methodCoverageAvailable = false;"); |
| | 2 | 596 | | this.javaScriptContent.AppendLine("var applyQueryStringToAllLinks = false;"); |
| | 2 | 597 | | this.javaScriptContent.AppendLine("var applyMaximumGroupingLevel = false;"); |
| | 2 | 598 | | this.javaScriptContent.AppendLine("var maximumDecimalPlacesForCoverageQuotas = " + MathExtensions.MaximumDecimal |
| | 2 | 599 | | this.javaScriptContent.AppendLine(); |
| | 2 | 600 | | } |
| | | 601 | | |
| | | 602 | | /// <inheritdoc /> |
| | | 603 | | public void BeginLineAnalysisTable(IEnumerable<string> headers) |
| | 36 | 604 | | { |
| | 36 | 605 | | ArgumentNullException.ThrowIfNull(headers); |
| | | 606 | | |
| | 36 | 607 | | this.reportTextWriter.WriteLine("<div class=\"table-responsive\">"); |
| | 36 | 608 | | this.reportTextWriter.WriteLine("<table class=\"lineAnalysis\">"); |
| | 36 | 609 | | this.reportTextWriter.Write("<thead><tr>"); |
| | | 610 | | |
| | 468 | 611 | | foreach (var header in headers) |
| | 180 | 612 | | { |
| | 180 | 613 | | this.reportTextWriter.Write("<th>{0}</th>", WebUtility.HtmlEncode(header)); |
| | 180 | 614 | | } |
| | | 615 | | |
| | 36 | 616 | | this.reportTextWriter.WriteLine("</tr></thead>"); |
| | 36 | 617 | | this.reportTextWriter.WriteLine("<tbody>"); |
| | 36 | 618 | | } |
| | | 619 | | |
| | | 620 | | /// <inheritdoc /> |
| | | 621 | | public void MetricsTable(Class @class) |
| | 28 | 622 | | { |
| | 28 | 623 | | ArgumentNullException.ThrowIfNull(@class); |
| | | 624 | | |
| | 28 | 625 | | var metrics = @class.Files |
| | 30 | 626 | | .SelectMany(f => f.MethodMetrics) |
| | 72 | 627 | | .SelectMany(m => m.Metrics) |
| | 28 | 628 | | .Distinct() |
| | 140 | 629 | | .OrderBy(m => m.Name) |
| | 28 | 630 | | .ToArray(); |
| | | 631 | | |
| | 28 | 632 | | this.reportTextWriter.WriteLine("<div class=\"table-responsive\">"); |
| | 28 | 633 | | this.reportTextWriter.WriteLine("<table class=\"overview table-fixed\">"); |
| | | 634 | | |
| | 28 | 635 | | this.reportTextWriter.WriteLine("<colgroup>"); |
| | 28 | 636 | | this.reportTextWriter.WriteLine("<col class=\"column-min-200\" />"); |
| | | 637 | | |
| | 364 | 638 | | foreach (var met in metrics) |
| | 140 | 639 | | { |
| | 140 | 640 | | this.reportTextWriter.WriteLine("<col class=\"column105\" />"); |
| | 140 | 641 | | } |
| | | 642 | | |
| | 28 | 643 | | this.reportTextWriter.WriteLine("</colgroup>"); |
| | | 644 | | |
| | 28 | 645 | | this.reportTextWriter.Write("<thead><tr>"); |
| | | 646 | | |
| | 28 | 647 | | this.reportTextWriter.Write("<th>{0}</th>", WebUtility.HtmlEncode("Method")); |
| | | 648 | | |
| | 364 | 649 | | foreach (var met in metrics) |
| | 140 | 650 | | { |
| | 140 | 651 | | if (met.ExplanationUrl == null) |
| | 0 | 652 | | { |
| | 0 | 653 | | this.reportTextWriter.Write("<th>{0}</th>", WebUtility.HtmlEncode(met.Name)); |
| | 0 | 654 | | } |
| | | 655 | | else |
| | 140 | 656 | | { |
| | 140 | 657 | | this.reportTextWriter.Write( |
| | 140 | 658 | | "<th>{0} <a href=\"{1}\" target=\"_blank\"><i class=\"icon-info-circled\"></i></a></th>", |
| | 140 | 659 | | WebUtility.HtmlEncode(met.Name), |
| | 140 | 660 | | WebUtility.HtmlEncode(met.ExplanationUrl.OriginalString)); |
| | 140 | 661 | | } |
| | 140 | 662 | | } |
| | | 663 | | |
| | 28 | 664 | | this.reportTextWriter.WriteLine("</tr></thead>"); |
| | 28 | 665 | | this.reportTextWriter.WriteLine("<tbody>"); |
| | | 666 | | |
| | 28 | 667 | | int fileIndex = 0; |
| | 28 | 668 | | bool usePrefix = @class.Files.Count() > 1; |
| | | 669 | | |
| | 144 | 670 | | foreach (var file in @class.Files) |
| | 30 | 671 | | { |
| | 306 | 672 | | foreach (var methodMetric in file.MethodMetrics.OrderBy(c => c.Line)) |
| | 72 | 673 | | { |
| | 72 | 674 | | this.reportTextWriter.Write("<tr>"); |
| | | 675 | | |
| | 72 | 676 | | if (methodMetric.Line.HasValue) |
| | 72 | 677 | | { |
| | 72 | 678 | | this.reportTextWriter.Write( |
| | 72 | 679 | | "<td title=\"{0}\"><a href=\"#file{1}_line{2}\" class=\"navigatetohash\">{3}</a></td>", |
| | 72 | 680 | | WebUtility.HtmlEncode(methodMetric.FullName), |
| | 72 | 681 | | fileIndex, |
| | 72 | 682 | | methodMetric.Line, |
| | 72 | 683 | | WebUtility.HtmlEncode( |
| | 72 | 684 | | (usePrefix ? $"File {fileIndex + 1}: " : string.Empty) + methodMetric.ShortName)); |
| | 72 | 685 | | } |
| | | 686 | | else |
| | 0 | 687 | | { |
| | 0 | 688 | | this.reportTextWriter.Write( |
| | 0 | 689 | | "<td title=\"{0}\">{1}</td>", |
| | 0 | 690 | | WebUtility.HtmlEncode(methodMetric.FullName), |
| | 0 | 691 | | WebUtility.HtmlEncode(file.Path + " => " + methodMetric.ShortName)); |
| | 0 | 692 | | } |
| | | 693 | | |
| | 936 | 694 | | foreach (var metric in metrics) |
| | 360 | 695 | | { |
| | 1440 | 696 | | var metricValue = methodMetric.Metrics.FirstOrDefault(m => m.Equals(metric)); |
| | | 697 | | |
| | 360 | 698 | | if (metricValue != null) |
| | 360 | 699 | | { |
| | 360 | 700 | | this.reportTextWriter.Write( |
| | 360 | 701 | | "<td>{0}{1}</td>", |
| | 360 | 702 | | metricValue.Value.HasValue ? metricValue.Value.Value.ToString("0.##") : "-", |
| | 360 | 703 | | metricValue.Value.HasValue && metricValue.MetricType == MetricType.CoveragePercentual ? "%" : st |
| | 360 | 704 | | } |
| | | 705 | | else |
| | 0 | 706 | | { |
| | 0 | 707 | | this.reportTextWriter.Write("<td>-</td>"); |
| | 0 | 708 | | } |
| | 360 | 709 | | } |
| | | 710 | | |
| | 72 | 711 | | this.reportTextWriter.WriteLine("</tr>"); |
| | 72 | 712 | | } |
| | | 713 | | |
| | 30 | 714 | | fileIndex++; |
| | 30 | 715 | | } |
| | | 716 | | |
| | 28 | 717 | | this.reportTextWriter.WriteLine("</tbody>"); |
| | 28 | 718 | | this.reportTextWriter.WriteLine("</table>"); |
| | 28 | 719 | | this.reportTextWriter.WriteLine("</div>"); |
| | 28 | 720 | | } |
| | | 721 | | |
| | | 722 | | /// <inheritdoc /> |
| | | 723 | | public void LineAnalysis(int fileIndex, LineAnalysis analysis) |
| | 1330 | 724 | | { |
| | 1330 | 725 | | ArgumentNullException.ThrowIfNull(analysis); |
| | | 726 | | |
| | 1330 | 727 | | string formattedLine = analysis.LineContent |
| | 1330 | 728 | | .Replace(((char)11).ToString(), " ") // replace tab |
| | 1330 | 729 | | .Replace(((char)9).ToString(), " "); // replace tab |
| | | 730 | | |
| | 1330 | 731 | | if (formattedLine.Length > 120) |
| | 4 | 732 | | { |
| | 4 | 733 | | formattedLine = formattedLine[..120]; |
| | 4 | 734 | | } |
| | | 735 | | |
| | 1330 | 736 | | formattedLine = WebUtility.HtmlEncode(formattedLine); |
| | 1330 | 737 | | formattedLine = formattedLine.Replace(" ", " "); |
| | | 738 | | |
| | 1330 | 739 | | string lineVisitStatus = ConvertToCssClass(analysis.LineVisitStatus, false); |
| | | 740 | | |
| | 1330 | 741 | | this.reportTextWriter.Write( |
| | 1330 | 742 | | "<tr class=\"{0}\" title=\"{1}\" data-coverage=\"{{", |
| | 1330 | 743 | | analysis.LineVisitStatus > LineVisitStatus.NotCoverable ? "coverableline" : string.Empty, |
| | 1330 | 744 | | WebUtility.HtmlEncode(GetTooltip(analysis))); |
| | | 745 | | |
| | 1330 | 746 | | this.reportTextWriter.Write( |
| | 1330 | 747 | | "'AllTestMethods': {{'VC': '{0}', 'LVS': '{1}'}}", |
| | 1330 | 748 | | analysis.LineVisitStatus != LineVisitStatus.NotCoverable ? analysis.LineVisits.ToString() : string.Empty, |
| | 1330 | 749 | | lineVisitStatus); |
| | | 750 | | |
| | 8406 | 751 | | foreach (var coverageByTestMethod in analysis.LineCoverageByTestMethod) |
| | 2208 | 752 | | { |
| | 2208 | 753 | | this.reportTextWriter.Write( |
| | 2208 | 754 | | ", 'M{0}': {{'VC': '{1}', 'LVS': '{2}'}}", |
| | 2208 | 755 | | coverageByTestMethod.Key.Id.ToString(), |
| | 2208 | 756 | | coverageByTestMethod.Value.LineVisitStatus != LineVisitStatus.NotCoverable |
| | 2208 | 757 | | ? coverageByTestMethod.Value.LineVisits.ToString() |
| | 2208 | 758 | | : string.Empty, |
| | 2208 | 759 | | ConvertToCssClass(coverageByTestMethod.Value.LineVisitStatus, false)); |
| | 2208 | 760 | | } |
| | | 761 | | |
| | 1330 | 762 | | this.reportTextWriter.Write("}\">"); |
| | | 763 | | |
| | 1330 | 764 | | this.reportTextWriter.Write( |
| | 1330 | 765 | | "<td class=\"{0}\"> </td>", |
| | 1330 | 766 | | lineVisitStatus); |
| | 1330 | 767 | | this.reportTextWriter.Write( |
| | 1330 | 768 | | "<td class=\"leftmargin rightmargin right\">{0}</td>", |
| | 1330 | 769 | | analysis.LineVisitStatus != LineVisitStatus.NotCoverable ? analysis.LineVisits.ToString() : string.Empty); |
| | 1330 | 770 | | this.reportTextWriter.Write( |
| | 1330 | 771 | | "<td class=\"rightmargin right\"><a id=\"file{0}_line{1}\"></a><code>{1}</code></td>", |
| | 1330 | 772 | | fileIndex, |
| | 1330 | 773 | | analysis.LineNumber); |
| | | 774 | | |
| | 1330 | 775 | | if (analysis.CoveredBranches.HasValue && analysis.TotalBranches.HasValue && analysis.TotalBranches.Value > 0) |
| | 8 | 776 | | { |
| | 8 | 777 | | int branchCoverage = (int)(100 * (double)analysis.CoveredBranches.Value / analysis.TotalBranches.Value); |
| | 8 | 778 | | branchCoverage -= branchCoverage % 10; |
| | 8 | 779 | | this.reportTextWriter.Write("<td class=\"percentagebar percentagebar{0}\"><i class=\"icon-fork\"></i></td>", |
| | 8 | 780 | | } |
| | | 781 | | else |
| | 1322 | 782 | | { |
| | 1322 | 783 | | this.reportTextWriter.Write("<td></td>"); |
| | 1322 | 784 | | } |
| | | 785 | | |
| | 1330 | 786 | | this.reportTextWriter.Write( |
| | 1330 | 787 | | "<td class=\"{0}\"><code>{1}</code></td>", |
| | 1330 | 788 | | ConvertToCssClass(analysis.LineVisitStatus, true), |
| | 1330 | 789 | | formattedLine); |
| | | 790 | | |
| | 1330 | 791 | | this.reportTextWriter.WriteLine("</tr>"); |
| | 1330 | 792 | | } |
| | | 793 | | |
| | | 794 | | /// <inheritdoc /> |
| | | 795 | | public void FinishTable() |
| | 38 | 796 | | { |
| | 38 | 797 | | this.reportTextWriter.WriteLine("</tbody>"); |
| | 38 | 798 | | this.reportTextWriter.WriteLine("</table>"); |
| | 38 | 799 | | this.reportTextWriter.WriteLine("</div>"); |
| | 38 | 800 | | } |
| | | 801 | | |
| | | 802 | | /// <inheritdoc /> |
| | | 803 | | public void BeginRiskHotspots() |
| | 2 | 804 | | { |
| | 2 | 805 | | this.reportTextWriter.WriteLine("<risk-hotspots>"); |
| | 2 | 806 | | } |
| | | 807 | | |
| | | 808 | | /// <inheritdoc /> |
| | | 809 | | public void FinishRiskHotspots() |
| | 2 | 810 | | { |
| | 2 | 811 | | this.reportTextWriter.WriteLine("</risk-hotspots>"); |
| | 2 | 812 | | } |
| | | 813 | | |
| | | 814 | | /// <inheritdoc /> |
| | | 815 | | public void RiskHotspots(IEnumerable<RiskHotspot> riskHotspots) |
| | 0 | 816 | | { |
| | 0 | 817 | | var codeQualityMetrics = riskHotspots.First().MethodMetric.Metrics |
| | 0 | 818 | | .Where(m => m.MetricType == MetricType.CodeQuality) |
| | 0 | 819 | | .ToArray(); |
| | | 820 | | |
| | 0 | 821 | | this.reportTextWriter.WriteLine("<div class=\"table-responsive\">"); |
| | 0 | 822 | | this.reportTextWriter.WriteLine("<table class=\"overview table-fixed stripped\">"); |
| | | 823 | | |
| | 0 | 824 | | this.reportTextWriter.WriteLine("<colgroup>"); |
| | 0 | 825 | | this.reportTextWriter.WriteLine("<col class=\"column-min-200\" />"); |
| | 0 | 826 | | this.reportTextWriter.WriteLine("<col class=\"column-min-200\" />"); |
| | 0 | 827 | | this.reportTextWriter.WriteLine("<col class=\"column-min-200\" />"); |
| | | 828 | | |
| | 0 | 829 | | foreach (var met in codeQualityMetrics) |
| | 0 | 830 | | { |
| | 0 | 831 | | this.reportTextWriter.WriteLine("<col class=\"column105\" />"); |
| | 0 | 832 | | } |
| | | 833 | | |
| | 0 | 834 | | this.reportTextWriter.WriteLine("</colgroup>"); |
| | | 835 | | |
| | 0 | 836 | | this.reportTextWriter.Write("<thead><tr>"); |
| | | 837 | | |
| | 0 | 838 | | this.reportTextWriter.WriteLine("<th>{0}</th>", WebUtility.HtmlEncode("Assembly")); |
| | 0 | 839 | | this.reportTextWriter.WriteLine("<th>{0}</th>", WebUtility.HtmlEncode("Class")); |
| | 0 | 840 | | this.reportTextWriter.WriteLine("<th>{0}</th>", WebUtility.HtmlEncode("Method")); |
| | | 841 | | |
| | 0 | 842 | | foreach (var metric in codeQualityMetrics) |
| | 0 | 843 | | { |
| | 0 | 844 | | if (metric.ExplanationUrl == null) |
| | 0 | 845 | | { |
| | 0 | 846 | | this.reportTextWriter.WriteLine("<th>{0}</th>", WebUtility.HtmlEncode(metric.Name)); |
| | 0 | 847 | | } |
| | | 848 | | else |
| | 0 | 849 | | { |
| | 0 | 850 | | this.reportTextWriter.WriteLine( |
| | 0 | 851 | | "<th>{0} <a href=\"{1}\" target=\"_blank\"><i class=\"icon-info-circled\"></i></a></th>", |
| | 0 | 852 | | WebUtility.HtmlEncode(metric.Name), |
| | 0 | 853 | | WebUtility.HtmlEncode(metric.ExplanationUrl.OriginalString)); |
| | 0 | 854 | | } |
| | 0 | 855 | | } |
| | | 856 | | |
| | 0 | 857 | | this.reportTextWriter.WriteLine("</tr></thead>"); |
| | | 858 | | |
| | 0 | 859 | | this.reportTextWriter.WriteLine("<tbody>"); |
| | | 860 | | |
| | 0 | 861 | | foreach (var riskHotspot in riskHotspots.Take(20)) |
| | 0 | 862 | | { |
| | 0 | 863 | | string filenameColumn = riskHotspot.Class.Name; |
| | | 864 | | |
| | 0 | 865 | | if (!this.onlySummary) |
| | 0 | 866 | | { |
| | 0 | 867 | | filenameColumn = string.Format( |
| | 0 | 868 | | "<a href=\"{0}\">{1}</a>", |
| | 0 | 869 | | WebUtility.HtmlEncode(this.GetClassReportFilename(riskHotspot.Assembly, riskHotspot.Class.Name)), |
| | 0 | 870 | | WebUtility.HtmlEncode(riskHotspot.Class.DisplayName)); |
| | 0 | 871 | | } |
| | | 872 | | |
| | 0 | 873 | | this.reportTextWriter.WriteLine("<tr>"); |
| | 0 | 874 | | this.reportTextWriter.WriteLine("<td>{0}</td>", WebUtility.HtmlEncode(riskHotspot.Assembly.ShortName)); |
| | 0 | 875 | | this.reportTextWriter.WriteLine("<td>{0}</td>", filenameColumn); |
| | | 876 | | |
| | 0 | 877 | | if (!this.onlySummary && riskHotspot.MethodMetric.Line.HasValue) |
| | 0 | 878 | | { |
| | 0 | 879 | | this.reportTextWriter.Write( |
| | 0 | 880 | | "<td title=\"{0}\"><a href=\"{1}#file{2}_line{3}\">{4}</a></td>", |
| | 0 | 881 | | WebUtility.HtmlEncode(riskHotspot.MethodMetric.FullName), |
| | 0 | 882 | | WebUtility.HtmlEncode(this.GetClassReportFilename(riskHotspot.Assembly, riskHotspot.Class.Name)), |
| | 0 | 883 | | riskHotspot.FileIndex, |
| | 0 | 884 | | riskHotspot.MethodMetric.Line, |
| | 0 | 885 | | WebUtility.HtmlEncode(riskHotspot.MethodMetric.ShortName)); |
| | 0 | 886 | | } |
| | | 887 | | else |
| | 0 | 888 | | { |
| | 0 | 889 | | this.reportTextWriter.Write( |
| | 0 | 890 | | "<td title=\"{0}\">{1}</td>", |
| | 0 | 891 | | WebUtility.HtmlEncode(riskHotspot.MethodMetric.FullName), |
| | 0 | 892 | | WebUtility.HtmlEncode(riskHotspot.MethodMetric.ShortName)); |
| | 0 | 893 | | } |
| | | 894 | | |
| | 0 | 895 | | foreach (var statusMetric in riskHotspot.StatusMetrics) |
| | 0 | 896 | | { |
| | 0 | 897 | | this.reportTextWriter.WriteLine( |
| | 0 | 898 | | "<td class=\"{0} right\">{1}</td>", |
| | 0 | 899 | | statusMetric.Exceeded ? "lightred" : "lightgreen", |
| | 0 | 900 | | statusMetric.Metric.Value.HasValue ? statusMetric.Metric.Value.Value.ToString("0.##") : "-"); |
| | 0 | 901 | | } |
| | | 902 | | |
| | 0 | 903 | | this.reportTextWriter.WriteLine("</tr>"); |
| | 0 | 904 | | } |
| | | 905 | | |
| | 0 | 906 | | this.reportTextWriter.WriteLine("</tbody>"); |
| | 0 | 907 | | this.reportTextWriter.WriteLine("</table>"); |
| | 0 | 908 | | this.reportTextWriter.WriteLine("</div>"); |
| | 0 | 909 | | } |
| | | 910 | | |
| | | 911 | | /// <inheritdoc /> |
| | | 912 | | public void SummaryAssembly(Assembly assembly, bool branchCoverageAvailable) |
| | 2 | 913 | | { |
| | 2 | 914 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 915 | | |
| | 2 | 916 | | this.reportTextWriter.Write("<tr>"); |
| | 2 | 917 | | this.reportTextWriter.Write("<th>{0}</th>", WebUtility.HtmlEncode(assembly.Name)); |
| | 2 | 918 | | this.reportTextWriter.Write("<th class=\"right\">{0}</th>", assembly.CoveredLines); |
| | 2 | 919 | | this.reportTextWriter.Write("<th class=\"right\">{0}</th>", assembly.CoverableLines - assembly.CoveredLines); |
| | 2 | 920 | | this.reportTextWriter.Write("<th class=\"right\">{0}</th>", assembly.CoverableLines); |
| | 2 | 921 | | this.reportTextWriter.Write("<th class=\"right\">{0}</th>", assembly.TotalLines.GetValueOrDefault()); |
| | 2 | 922 | | this.reportTextWriter.Write( |
| | 2 | 923 | | "<th title=\"{0}\" class=\"right\">{1}</th>", |
| | 2 | 924 | | assembly.CoverageQuota.HasValue ? $"{assembly.CoveredLines}/{assembly.CoverableLines}" : string.Empty, |
| | 2 | 925 | | assembly.CoverageQuota.HasValue ? assembly.CoverageQuota.Value.ToString() + "%" : string.Empty); |
| | 2 | 926 | | WriteCoverageTable(this.reportTextWriter, "th", assembly.CoverageQuota); |
| | | 927 | | |
| | 2 | 928 | | if (branchCoverageAvailable) |
| | 2 | 929 | | { |
| | 2 | 930 | | this.reportTextWriter.Write("<th class=\"right\">{0}</th>", assembly.CoveredBranches); |
| | 2 | 931 | | this.reportTextWriter.Write("<th class=\"right\">{0}</th>", assembly.TotalBranches); |
| | 2 | 932 | | this.reportTextWriter.Write( |
| | 2 | 933 | | "<th class=\"right\" title=\"{0}\">{1}</th>", |
| | 2 | 934 | | assembly.BranchCoverageQuota.HasValue ? $"{assembly.CoveredBranches}/{assembly.TotalBranches}" : "-", |
| | 2 | 935 | | assembly.BranchCoverageQuota.HasValue ? assembly.BranchCoverageQuota.Value.ToString() + "%" : string.Empty); |
| | 2 | 936 | | WriteCoverageTable(this.reportTextWriter, "th", assembly.BranchCoverageQuota); |
| | 2 | 937 | | } |
| | | 938 | | |
| | 2 | 939 | | this.reportTextWriter.WriteLine("</tr>"); |
| | 2 | 940 | | } |
| | | 941 | | |
| | | 942 | | /// <inheritdoc /> |
| | | 943 | | public void SummaryClass(Class @class, bool branchCoverageAvailable) |
| | 36 | 944 | | { |
| | 36 | 945 | | ArgumentNullException.ThrowIfNull(@class); |
| | | 946 | | |
| | 36 | 947 | | string filenameColumn = @class.Name; |
| | | 948 | | |
| | 36 | 949 | | if (!this.onlySummary) |
| | 36 | 950 | | { |
| | 36 | 951 | | filenameColumn = string.Format( |
| | 36 | 952 | | "<a href=\"{0}\">{1}</a>", |
| | 36 | 953 | | WebUtility.HtmlEncode(this.GetClassReportFilename(@class.Assembly, @class.Name)), |
| | 36 | 954 | | WebUtility.HtmlEncode(@class.DisplayName)); |
| | 36 | 955 | | } |
| | | 956 | | |
| | 36 | 957 | | this.reportTextWriter.Write("<tr>"); |
| | 36 | 958 | | this.reportTextWriter.Write("<td>{0}</td>", filenameColumn); |
| | 36 | 959 | | this.reportTextWriter.Write("<td class=\"right\">{0}</td>", @class.CoveredLines); |
| | 36 | 960 | | this.reportTextWriter.Write("<td class=\"right\">{0}</td>", @class.CoverableLines - @class.CoveredLines); |
| | 36 | 961 | | this.reportTextWriter.Write("<td class=\"right\">{0}</td>", @class.CoverableLines); |
| | 36 | 962 | | this.reportTextWriter.Write("<td class=\"right\">{0}</td>", @class.TotalLines.GetValueOrDefault()); |
| | 36 | 963 | | this.reportTextWriter.Write( |
| | 36 | 964 | | "<td title=\"{0}\" class=\"right\">{1}{2}</td>", |
| | 36 | 965 | | @class.CoverageQuota.HasValue ? $"{@class.CoveredLines}/{@class.CoverableLines}" : string.Empty, |
| | 36 | 966 | | string.Empty, |
| | 36 | 967 | | @class.CoverageQuota.HasValue ? @class.CoverageQuota.Value.ToString() + "%" : string.Empty); |
| | | 968 | | |
| | 36 | 969 | | WriteCoverageTable(this.reportTextWriter, "td", @class.CoverageQuota); |
| | | 970 | | |
| | 36 | 971 | | if (branchCoverageAvailable) |
| | 36 | 972 | | { |
| | 36 | 973 | | this.reportTextWriter.Write("<td class=\"right\">{0}</td>", @class.CoveredBranches); |
| | 36 | 974 | | this.reportTextWriter.Write("<td class=\"right\">{0}</td>", @class.TotalBranches); |
| | 36 | 975 | | this.reportTextWriter.Write( |
| | 36 | 976 | | "<td class=\"right\" title=\"{0}\">{1}{2}</td>", |
| | 36 | 977 | | @class.BranchCoverageQuota.HasValue ? $"{@class.CoveredBranches}/{@class.TotalBranches}" : "-", |
| | 36 | 978 | | string.Empty, |
| | 36 | 979 | | @class.BranchCoverageQuota.HasValue ? @class.BranchCoverageQuota.Value.ToString() + "%" : string.Empty); |
| | 36 | 980 | | WriteCoverageTable(this.reportTextWriter, "td", @class.BranchCoverageQuota); |
| | 36 | 981 | | } |
| | | 982 | | |
| | 36 | 983 | | this.reportTextWriter.WriteLine("</tr>"); |
| | 36 | 984 | | } |
| | | 985 | | |
| | | 986 | | /// <inheritdoc /> |
| | | 987 | | public void AddFooter() |
| | 38 | 988 | | { |
| | 38 | 989 | | this.reportTextWriter.Write(string.Format( |
| | 38 | 990 | | "<div class=\"footer\">Generated by DirectSight {0}<br />{1}<br /><a href=\"https://github.com/joharasmus/Di |
| | 38 | 991 | | typeof(HtmlRenderer).Assembly.GetName().Version.ToString(3), |
| | 38 | 992 | | DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss 'UTC'zzz"))); |
| | 38 | 993 | | } |
| | | 994 | | |
| | | 995 | | /// <inheritdoc /> |
| | | 996 | | public void SaveSummaryReport(string targetDirectory) |
| | 2 | 997 | | { |
| | 2 | 998 | | this.SaveReport(); |
| | 2 | 999 | | this.SaveCss(targetDirectory); |
| | 2 | 1000 | | this.SaveJavaScript(targetDirectory); |
| | 2 | 1001 | | } |
| | | 1002 | | |
| | | 1003 | | /// <inheritdoc /> |
| | | 1004 | | public void SaveClassReport(string targetDirectory, string className) |
| | 36 | 1005 | | { |
| | 36 | 1006 | | this.SaveReport(); |
| | | 1007 | | |
| | 36 | 1008 | | if (!this.javaScriptGenerated) |
| | 36 | 1009 | | { |
| | 36 | 1010 | | this.SaveJavaScript(targetDirectory); |
| | 36 | 1011 | | this.javaScriptGenerated = true; |
| | 36 | 1012 | | } |
| | 36 | 1013 | | } |
| | | 1014 | | |
| | | 1015 | | /// <summary> |
| | | 1016 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 1017 | | /// </summary> |
| | | 1018 | | public void Dispose() |
| | 38 | 1019 | | { |
| | 38 | 1020 | | this.Dispose(true); |
| | 38 | 1021 | | GC.SuppressFinalize(this); |
| | 38 | 1022 | | } |
| | | 1023 | | |
| | | 1024 | | /// <summary> |
| | | 1025 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 1026 | | /// </summary> |
| | | 1027 | | /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release onl |
| | | 1028 | | protected virtual void Dispose(bool disposing) |
| | 38 | 1029 | | { |
| | 38 | 1030 | | if (disposing) |
| | 38 | 1031 | | { |
| | 38 | 1032 | | this.reportTextWriter?.Dispose(); |
| | 38 | 1033 | | StringBuilderCache.Return(this.javaScriptContent); |
| | 38 | 1034 | | } |
| | 38 | 1035 | | } |
| | | 1036 | | |
| | | 1037 | | /// <summary> |
| | | 1038 | | /// Writes a table showing the coverage quota with red and green bars to the TextWriter. |
| | | 1039 | | /// </summary> |
| | | 1040 | | /// <param name="writer"><see cref="TextWriter"/> to write to.</param> |
| | | 1041 | | /// <param name="surroundingElement">Html element to write around the table (e.g. 'td', or 'th').</param> |
| | | 1042 | | /// <param name="coverage">The coverage quota.</param> |
| | | 1043 | | private static void WriteCoverageTable(TextWriter writer, string surroundingElement, decimal? coverage) |
| | 76 | 1044 | | { |
| | 76 | 1045 | | writer.Write("<{0}><table class=\"coverage\"><tr>", surroundingElement); |
| | | 1046 | | |
| | 76 | 1047 | | if (coverage.HasValue) |
| | 44 | 1048 | | { |
| | 44 | 1049 | | int covered = (int)Math.Round(coverage.Value, 0); |
| | 44 | 1050 | | int uncovered = 100 - covered; |
| | | 1051 | | |
| | 44 | 1052 | | if (covered > 0) |
| | 38 | 1053 | | { |
| | 38 | 1054 | | writer.Write("<td class=\"green covered{0}\"> </td>", covered); |
| | 38 | 1055 | | } |
| | | 1056 | | |
| | 44 | 1057 | | if (uncovered > 0) |
| | 30 | 1058 | | { |
| | 30 | 1059 | | writer.Write("<td class=\"red covered{0}\"> </td>", uncovered); |
| | 30 | 1060 | | } |
| | 44 | 1061 | | } |
| | | 1062 | | else |
| | 32 | 1063 | | { |
| | 32 | 1064 | | writer.Write("<td class=\"gray covered100\"> </td>"); |
| | 32 | 1065 | | } |
| | | 1066 | | |
| | 76 | 1067 | | writer.Write("</tr></table></{0}>", surroundingElement); |
| | 76 | 1068 | | } |
| | | 1069 | | |
| | | 1070 | | /// <summary> |
| | | 1071 | | /// Converts the <see cref="LineVisitStatus" /> to the corresponding CSS class. |
| | | 1072 | | /// </summary> |
| | | 1073 | | /// <param name="lineVisitStatus">The line visit status.</param> |
| | | 1074 | | /// <param name="lightcolor">if set to <c>true</c> a CSS class representing a light color is returned.</param> |
| | | 1075 | | /// <returns>The corresponding CSS class.</returns> |
| | | 1076 | | private static string ConvertToCssClass(LineVisitStatus lineVisitStatus, bool lightcolor) |
| | 4868 | 1077 | | { |
| | 4868 | 1078 | | return lineVisitStatus switch |
| | 4868 | 1079 | | { |
| | 1012 | 1080 | | LineVisitStatus.Covered => lightcolor ? "lightgreen" : "green", |
| | 444 | 1081 | | LineVisitStatus.NotCovered => lightcolor ? "lightred" : "red", |
| | 24 | 1082 | | LineVisitStatus.PartiallyCovered => lightcolor ? "lightorange" : "orange", |
| | 3388 | 1083 | | _ => lightcolor ? "lightgray" : "gray", |
| | 4868 | 1084 | | }; |
| | | 1085 | | |
| | 4868 | 1086 | | } |
| | | 1087 | | |
| | | 1088 | | private static string GetTooltip(LineAnalysis analysis) |
| | 1330 | 1089 | | { |
| | 1330 | 1090 | | string branchRate = string.Empty; |
| | | 1091 | | |
| | 1330 | 1092 | | if (analysis.CoveredBranches.HasValue && analysis.TotalBranches.HasValue && analysis.TotalBranches.Value > 0) |
| | 8 | 1093 | | { |
| | 8 | 1094 | | branchRate = ", " + string.Format("{0} of {1} branches are covered", analysis.CoveredBranches, analysis.Tota |
| | 8 | 1095 | | } |
| | | 1096 | | |
| | | 1097 | | string result; |
| | | 1098 | | |
| | 1330 | 1099 | | if (analysis.LineVisitStatus == LineVisitStatus.Covered) |
| | 256 | 1100 | | { |
| | 256 | 1101 | | result = string.Format("Covered ({0} visits{1})", analysis.LineVisits, branchRate); |
| | 256 | 1102 | | } |
| | 1074 | 1103 | | else if (analysis.LineVisitStatus == LineVisitStatus.PartiallyCovered) |
| | 6 | 1104 | | { |
| | 6 | 1105 | | result = string.Format("Partially covered ({0} visits{1})", analysis.LineVisits, branchRate); |
| | 6 | 1106 | | } |
| | 1068 | 1107 | | else if (analysis.LineVisitStatus == LineVisitStatus.NotCovered) |
| | 126 | 1108 | | { |
| | 126 | 1109 | | result = string.Format("Not covered ({0} visits{1})", analysis.LineVisits, branchRate); |
| | 126 | 1110 | | } |
| | | 1111 | | else |
| | 942 | 1112 | | { |
| | 942 | 1113 | | result = "Not coverable"; |
| | 942 | 1114 | | return result; |
| | | 1115 | | } |
| | | 1116 | | |
| | 388 | 1117 | | return result; |
| | 1330 | 1118 | | } |
| | | 1119 | | |
| | | 1120 | | private static void WriteHtmlStart(TextWriter writer, string title, string subtitle) |
| | 38 | 1121 | | { |
| | 38 | 1122 | | writer.WriteLine($@"<!DOCTYPE html> |
| | 38 | 1123 | | <html> |
| | 38 | 1124 | | <head> |
| | 38 | 1125 | | <meta charset=""utf-8"" /> |
| | 38 | 1126 | | <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"" /> |
| | 38 | 1127 | | <meta http-equiv=""X-UA-Compatible"" content=""IE=EDGE,chrome=1"" /> |
| | 38 | 1128 | | <title>{WebUtility.HtmlEncode(title)} - {WebUtility.HtmlEncode(subtitle)}</title>"); |
| | | 1129 | | |
| | 38 | 1130 | | writer.WriteLine(CssLink); |
| | | 1131 | | |
| | 38 | 1132 | | writer.WriteLine("</head><body><div class=\"container\"><div class=\"containerleft\">"); |
| | 38 | 1133 | | } |
| | | 1134 | | |
| | | 1135 | | /// <summary> |
| | | 1136 | | /// Gets the file name of the report file for the given class. |
| | | 1137 | | /// </summary> |
| | | 1138 | | /// <param name="assembly">The assembly.</param> |
| | | 1139 | | /// <param name="className">Name of the class.</param> |
| | | 1140 | | /// <returns>The file name.</returns> |
| | | 1141 | | private string GetClassReportFilename(Assembly assembly, string className) |
| | 108 | 1142 | | { |
| | 108 | 1143 | | string assemblyName = assembly.ShortName; |
| | | 1144 | | |
| | 108 | 1145 | | string key = assembly.Name + "_" + className; |
| | | 1146 | | |
| | 108 | 1147 | | if (!this.fileNameByClass.TryGetValue(key, out string fileName)) |
| | 36 | 1148 | | { |
| | 36 | 1149 | | string shortClassName = null; |
| | | 1150 | | |
| | 36 | 1151 | | if (assemblyName == "Default" && className.Contains(Path.DirectorySeparatorChar)) |
| | 0 | 1152 | | { |
| | 0 | 1153 | | assemblyName = className[..className.LastIndexOf(Path.DirectorySeparatorChar)]; |
| | 0 | 1154 | | shortClassName = className[(className.LastIndexOf(Path.DirectorySeparatorChar) + 1)..]; |
| | 0 | 1155 | | } |
| | | 1156 | | else |
| | 36 | 1157 | | { |
| | 36 | 1158 | | if (className.EndsWith(".js", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 1159 | | { |
| | 0 | 1160 | | shortClassName = className[..className.LastIndexOf('.')]; |
| | 0 | 1161 | | } |
| | | 1162 | | else |
| | 36 | 1163 | | { |
| | 36 | 1164 | | shortClassName = className[(className.LastIndexOf('.') + 1)..]; |
| | 36 | 1165 | | } |
| | 36 | 1166 | | } |
| | | 1167 | | |
| | 36 | 1168 | | fileName = StringHelper.ReplaceInvalidPathChars(assemblyName + "_" + shortClassName) + ".html"; |
| | | 1169 | | |
| | 36 | 1170 | | if (fileName.Length > 100) |
| | 0 | 1171 | | { |
| | 0 | 1172 | | string firstPart = fileName[..50]; |
| | 0 | 1173 | | string lastPart = fileName.Substring(fileName.Length - 45, 45); |
| | | 1174 | | |
| | 0 | 1175 | | fileName = firstPart + lastPart; |
| | 0 | 1176 | | } |
| | | 1177 | | |
| | 342 | 1178 | | if (this.fileNameByClass.Values.Any(v => v.Equals(fileName, StringComparison.OrdinalIgnoreCase))) |
| | 0 | 1179 | | { |
| | 0 | 1180 | | int counter = 2; |
| | 0 | 1181 | | string fileNameWithoutExtension = fileName[..^4]; |
| | | 1182 | | |
| | | 1183 | | do |
| | 0 | 1184 | | { |
| | 0 | 1185 | | fileName = fileNameWithoutExtension + counter + ".html"; |
| | 0 | 1186 | | counter++; |
| | 0 | 1187 | | } |
| | 0 | 1188 | | while (this.fileNameByClass.Values.Any(v => v.Equals(fileName, StringComparison.OrdinalIgnoreCase))); |
| | 0 | 1189 | | } |
| | | 1190 | | |
| | 36 | 1191 | | this.fileNameByClass.Add(key, fileName); |
| | 36 | 1192 | | } |
| | | 1193 | | |
| | 108 | 1194 | | return fileName; |
| | 108 | 1195 | | } |
| | | 1196 | | |
| | | 1197 | | /// <summary> |
| | | 1198 | | /// Saves the CSS. |
| | | 1199 | | /// </summary> |
| | | 1200 | | /// <param name="targetDirectory">The target directory.</param> |
| | | 1201 | | private void SaveCss(string targetDirectory) |
| | 2 | 1202 | | { |
| | 2 | 1203 | | string targetPath = Path.Combine(targetDirectory, "report.css"); |
| | | 1204 | | |
| | 2 | 1205 | | if (WrittenCssAndJavascriptFiles.Contains(targetPath)) |
| | 0 | 1206 | | { |
| | 0 | 1207 | | return; |
| | | 1208 | | } |
| | | 1209 | | |
| | 2 | 1210 | | var builder = StringBuilderCache.Get(); |
| | 2 | 1211 | | using (var writer = new StringWriter(builder)) |
| | 2 | 1212 | | { |
| | 2 | 1213 | | this.WriteCss(writer); |
| | 2 | 1214 | | } |
| | | 1215 | | |
| | 2 | 1216 | | string css = StringBuilderCache.ToStringAndReturnToPool(builder); |
| | | 1217 | | |
| | 2 | 1218 | | System.IO.File.WriteAllText(targetPath, css); |
| | | 1219 | | |
| | 2 | 1220 | | var matches = Regex.Matches(css, @"url\(icon_(?<filename>.+).svg\),\surl\(data:image/svg\+xml;base64,(?<base64im |
| | | 1221 | | |
| | 106 | 1222 | | foreach (Match match in matches) |
| | 50 | 1223 | | { |
| | 50 | 1224 | | System.IO.File.WriteAllBytes( |
| | 50 | 1225 | | Path.Combine(targetDirectory, "icon_" + match.Groups["filename"].Value + ".svg"), |
| | 50 | 1226 | | Convert.FromBase64String(match.Groups["base64image"].Value)); |
| | 50 | 1227 | | } |
| | | 1228 | | |
| | 2 | 1229 | | WrittenCssAndJavascriptFiles.Add(targetPath); |
| | 2 | 1230 | | } |
| | | 1231 | | |
| | | 1232 | | /// <summary> |
| | | 1233 | | /// Saves the javascript. |
| | | 1234 | | /// </summary> |
| | | 1235 | | /// <param name="targetDirectory">The target directory.</param> |
| | | 1236 | | private void SaveJavaScript(string targetDirectory) |
| | 38 | 1237 | | { |
| | 38 | 1238 | | string targetPath = Path.Combine(targetDirectory, this.classReport ? "class.js" : "main.js"); |
| | | 1239 | | |
| | 38 | 1240 | | if (WrittenCssAndJavascriptFiles.Contains(targetPath)) |
| | 34 | 1241 | | { |
| | 34 | 1242 | | return; |
| | | 1243 | | } |
| | | 1244 | | |
| | 4 | 1245 | | using (var fs = new FileStream(targetPath, FileMode.Create)) |
| | 4 | 1246 | | using (var writer = new StreamWriter(fs)) |
| | 4 | 1247 | | { |
| | 4 | 1248 | | this.WriteCombinedJavascript(writer); |
| | 4 | 1249 | | } |
| | | 1250 | | |
| | 4 | 1251 | | WrittenCssAndJavascriptFiles.Add(targetPath); |
| | 38 | 1252 | | } |
| | | 1253 | | |
| | | 1254 | | /// <summary> |
| | | 1255 | | /// Writes CSS to the provided Text Writer. |
| | | 1256 | | /// </summary> |
| | | 1257 | | private void WriteCss(TextWriter writer) |
| | 2 | 1258 | | { |
| | | 1259 | | void WriteWithFilteredUrls(string resourceName) |
| | 8 | 1260 | | { |
| | 8 | 1261 | | var resource = ResourceStreamCache.Get(resourceName); |
| | 8 | 1262 | | writer.Write(resource); |
| | 8 | 1263 | | } |
| | | 1264 | | |
| | 2 | 1265 | | WriteWithFilteredUrls(this.cssFileResource); |
| | 2 | 1266 | | writer.WriteLine(); |
| | 2 | 1267 | | writer.WriteLine(); |
| | | 1268 | | |
| | 2 | 1269 | | if (this.additionalCssFileResources != null && this.additionalCssFileResources.Length > 0) |
| | 2 | 1270 | | { |
| | 14 | 1271 | | foreach (var additionalCssFileResource in this.additionalCssFileResources) |
| | 4 | 1272 | | { |
| | 4 | 1273 | | writer.WriteLine(); |
| | 4 | 1274 | | writer.WriteLine(); |
| | 4 | 1275 | | WriteWithFilteredUrls(additionalCssFileResource); |
| | 4 | 1276 | | } |
| | | 1277 | | |
| | 2 | 1278 | | writer.WriteLine(); |
| | 2 | 1279 | | writer.WriteLine(); |
| | 2 | 1280 | | } |
| | | 1281 | | |
| | 2 | 1282 | | WriteWithFilteredUrls("chartist.min.css"); |
| | 2 | 1283 | | } |
| | | 1284 | | |
| | | 1285 | | /// <summary> |
| | | 1286 | | /// Writes combined javascript to the provided TextWriter. |
| | | 1287 | | /// </summary> |
| | | 1288 | | private void WriteCombinedJavascript(TextWriter writer) |
| | 4 | 1289 | | { |
| | 4 | 1290 | | writer.WriteLine(ResourceStreamCache.Get("chartist.min.js")); |
| | 4 | 1291 | | writer.WriteLine(); |
| | | 1292 | | |
| | 4 | 1293 | | writer.Write(ResourceStreamCache.Get("custom.js")); |
| | | 1294 | | |
| | 4 | 1295 | | if (this.classReport) |
| | 2 | 1296 | | { |
| | 2 | 1297 | | return; |
| | | 1298 | | } |
| | | 1299 | | |
| | 2 | 1300 | | writer.WriteLine(); |
| | 2 | 1301 | | writer.WriteLine(); |
| | 2 | 1302 | | writer.Write(this.javaScriptContent); |
| | 2 | 1303 | | writer.WriteLine(); |
| | | 1304 | | |
| | 2 | 1305 | | writer.WriteLine("var translations = {"); |
| | 2 | 1306 | | writer.Write("'top': '{0}'", WebUtility.HtmlEncode("Top:")); |
| | 2 | 1307 | | writer.WriteLine(","); |
| | 2 | 1308 | | writer.Write("'all': '{0}'", WebUtility.HtmlEncode("All")); |
| | 2 | 1309 | | writer.WriteLine(","); |
| | 2 | 1310 | | writer.Write("'assembly': '{0}'", WebUtility.HtmlEncode("Assembly")); |
| | 2 | 1311 | | writer.WriteLine(","); |
| | 2 | 1312 | | writer.Write("'class': '{0}'", WebUtility.HtmlEncode("Class")); |
| | 2 | 1313 | | writer.WriteLine(","); |
| | 2 | 1314 | | writer.Write("'method': '{0}'", WebUtility.HtmlEncode("Method")); |
| | 2 | 1315 | | writer.WriteLine(","); |
| | 2 | 1316 | | writer.Write("'lineCoverage': '{0}'", WebUtility.HtmlEncode("Line coverage")); |
| | 2 | 1317 | | writer.WriteLine(","); |
| | 2 | 1318 | | writer.Write("'noGrouping': '{0}'", WebUtility.HtmlEncode("No grouping")); |
| | 2 | 1319 | | writer.WriteLine(","); |
| | 2 | 1320 | | writer.Write("'byAssembly': '{0}'", WebUtility.HtmlEncode("By assembly")); |
| | 2 | 1321 | | writer.WriteLine(","); |
| | 2 | 1322 | | writer.Write("'byNamespace': '{0}'", WebUtility.HtmlEncode("By namespace, Level:")); |
| | 2 | 1323 | | writer.WriteLine(","); |
| | 2 | 1324 | | writer.Write("'all': '{0}'", WebUtility.HtmlEncode("All")); |
| | 2 | 1325 | | writer.WriteLine(","); |
| | 2 | 1326 | | writer.Write("'collapseAll': '{0}'", WebUtility.HtmlEncode("Collapse all")); |
| | 2 | 1327 | | writer.WriteLine(","); |
| | 2 | 1328 | | writer.Write("'expandAll': '{0}'", WebUtility.HtmlEncode("Expand all")); |
| | 2 | 1329 | | writer.WriteLine(","); |
| | 2 | 1330 | | writer.Write("'grouping': '{0}'", WebUtility.HtmlEncode("Grouping:")); |
| | 2 | 1331 | | writer.WriteLine(","); |
| | 2 | 1332 | | writer.Write("'filter': '{0}'", WebUtility.HtmlEncode("Filter:")); |
| | 2 | 1333 | | writer.WriteLine(","); |
| | 2 | 1334 | | writer.Write("'name': '{0}'", WebUtility.HtmlEncode("Name")); |
| | 2 | 1335 | | writer.WriteLine(","); |
| | 2 | 1336 | | writer.Write("'covered': '{0}'", WebUtility.HtmlEncode("Covered")); |
| | 2 | 1337 | | writer.WriteLine(","); |
| | 2 | 1338 | | writer.Write("'uncovered': '{0}'", WebUtility.HtmlEncode("Uncovered")); |
| | 2 | 1339 | | writer.WriteLine(","); |
| | 2 | 1340 | | writer.Write("'coverable': '{0}'", WebUtility.HtmlEncode("Coverable")); |
| | 2 | 1341 | | writer.WriteLine(","); |
| | 2 | 1342 | | writer.Write("'total': '{0}'", WebUtility.HtmlEncode("Total")); |
| | 2 | 1343 | | writer.WriteLine(","); |
| | 2 | 1344 | | writer.Write("'coverage': '{0}'", WebUtility.HtmlEncode("Line coverage")); |
| | 2 | 1345 | | writer.WriteLine(","); |
| | 2 | 1346 | | writer.Write("'branchCoverage': '{0}'", WebUtility.HtmlEncode("Branch coverage")); |
| | 2 | 1347 | | writer.WriteLine(","); |
| | 2 | 1348 | | writer.Write("'methodCoverage': '{0}'", WebUtility.HtmlEncode("Method coverage")); |
| | 2 | 1349 | | writer.WriteLine(","); |
| | 2 | 1350 | | writer.Write("'fullMethodCoverage': '{0}'", WebUtility.HtmlEncode("Full method coverage")); |
| | 2 | 1351 | | writer.WriteLine(","); |
| | 2 | 1352 | | writer.Write("'percentage': '{0}'", WebUtility.HtmlEncode("Percentage")); |
| | 2 | 1353 | | writer.WriteLine(","); |
| | 2 | 1354 | | writer.Write("'date': '{0}'", WebUtility.HtmlEncode("Date")); |
| | 2 | 1355 | | writer.WriteLine(","); |
| | 2 | 1356 | | writer.Write("'allChanges': '{0}'", WebUtility.HtmlEncode("All changes")); |
| | 2 | 1357 | | writer.WriteLine(","); |
| | 2 | 1358 | | writer.Write("'selectCoverageTypes': '{0}'", WebUtility.HtmlEncode("Select coverage types")); |
| | 2 | 1359 | | writer.WriteLine(","); |
| | 2 | 1360 | | writer.Write("'selectCoverageTypesAndMetrics': '{0}'", "Select coverage types and metrics"); |
| | 2 | 1361 | | writer.WriteLine(","); |
| | 2 | 1362 | | writer.Write("'coverageTypes': '{0}'", WebUtility.HtmlEncode("Coverage types")); |
| | 2 | 1363 | | writer.WriteLine(","); |
| | 2 | 1364 | | writer.Write("'metrics': '{0}'", WebUtility.HtmlEncode("Metrics")); |
| | 2 | 1365 | | writer.WriteLine(","); |
| | 2 | 1366 | | writer.Write("'lineCoverageIncreaseOnly': '{0}'", WebUtility.HtmlEncode("Line coverage: Increase only")); |
| | 2 | 1367 | | writer.WriteLine(","); |
| | 2 | 1368 | | writer.Write("'lineCoverageDecreaseOnly': '{0}'", WebUtility.HtmlEncode("Line coverage: Decrease only")); |
| | 2 | 1369 | | writer.WriteLine(","); |
| | 2 | 1370 | | writer.Write("'branchCoverageIncreaseOnly': '{0}'", WebUtility.HtmlEncode("Branch coverage: Increase only")); |
| | 2 | 1371 | | writer.WriteLine(","); |
| | 2 | 1372 | | writer.Write("'branchCoverageDecreaseOnly': '{0}'", WebUtility.HtmlEncode("Branch coverage: Decrease only")); |
| | 2 | 1373 | | writer.WriteLine(","); |
| | 2 | 1374 | | writer.Write("'methodCoverageIncreaseOnly': '{0}'", WebUtility.HtmlEncode("Method coverage: Increase only")); |
| | 2 | 1375 | | writer.WriteLine(","); |
| | 2 | 1376 | | writer.Write("'methodCoverageDecreaseOnly': '{0}'", WebUtility.HtmlEncode("Method coverage: Decrease only")); |
| | 2 | 1377 | | writer.WriteLine(","); |
| | 2 | 1378 | | writer.Write("'fullMethodCoverageIncreaseOnly': '{0}'", WebUtility.HtmlEncode("Full method coverage: Increase on |
| | 2 | 1379 | | writer.WriteLine(","); |
| | 2 | 1380 | | writer.Write("'fullMethodCoverageDecreaseOnly': '{0}'", WebUtility.HtmlEncode("Full method coverage: Decrease on |
| | 2 | 1381 | | writer.WriteLine(); |
| | 2 | 1382 | | writer.WriteLine("};"); |
| | | 1383 | | |
| | 2 | 1384 | | writer.WriteLine(); |
| | 2 | 1385 | | writer.WriteLine(); |
| | | 1386 | | |
| | 2 | 1387 | | writer.WriteLine(ResourceStreamCache.Get("runtime.js")); |
| | 2 | 1388 | | writer.WriteLine(); |
| | | 1389 | | |
| | 2 | 1390 | | writer.WriteLine(ResourceStreamCache.Get("polyfills.js")); |
| | 2 | 1391 | | writer.WriteLine(); |
| | | 1392 | | |
| | 2 | 1393 | | writer.Write(ResourceStreamCache.Get("main.js")); |
| | 4 | 1394 | | } |
| | | 1395 | | |
| | | 1396 | | /// <summary> |
| | | 1397 | | /// Initializes the text writer. |
| | | 1398 | | /// </summary> |
| | | 1399 | | /// <param name="targetPath">The target path.</param> |
| | | 1400 | | private void CreateTextWriter(string targetPath) |
| | 38 | 1401 | | { |
| | 38 | 1402 | | this.reportTextWriter = new StreamWriter(new FileStream(targetPath, FileMode.Create)); |
| | 38 | 1403 | | } |
| | | 1404 | | |
| | | 1405 | | /// <summary> |
| | | 1406 | | /// Saves the report. |
| | | 1407 | | /// </summary> |
| | | 1408 | | private void SaveReport() |
| | 38 | 1409 | | { |
| | 38 | 1410 | | this.FinishReport(); |
| | | 1411 | | |
| | 38 | 1412 | | this.reportTextWriter.Flush(); |
| | 38 | 1413 | | this.reportTextWriter.Dispose(); |
| | | 1414 | | |
| | 38 | 1415 | | this.reportTextWriter = null; |
| | 38 | 1416 | | } |
| | | 1417 | | |
| | | 1418 | | /// <summary> |
| | | 1419 | | /// Finishes the report. |
| | | 1420 | | /// </summary> |
| | | 1421 | | private void FinishReport() |
| | 38 | 1422 | | { |
| | 38 | 1423 | | string fileName = this.classReport ? "class.js" : "main.js"; |
| | | 1424 | | |
| | 38 | 1425 | | this.reportTextWriter.WriteLine("</div></div>"); |
| | | 1426 | | |
| | 38 | 1427 | | this.reportTextWriter.WriteLine($"<script type=\"text/javascript\" src=\"{fileName}\"></script>"); |
| | | 1428 | | |
| | 38 | 1429 | | this.reportTextWriter.Write("</body></html>"); |
| | 38 | 1430 | | } |
| | | 1431 | | |
| | | 1432 | | private static class ResourceStreamCache |
| | | 1433 | | { |
| | 1 | 1434 | | private static readonly ConcurrentDictionary<string, string> Cache = new(); |
| | | 1435 | | |
| | 22 | 1436 | | public static string Get(string resourceName) => Cache.GetOrAdd(resourceName, v => |
| | 9 | 1437 | | { |
| | 9 | 1438 | | using Stream stream = |
| | 9 | 1439 | | typeof(HtmlRenderer) |
| | 9 | 1440 | | .Assembly |
| | 9 | 1441 | | .GetManifestResourceStream("DirectSight.Reporting.Builders.Rendering.resources." + resourceName); |
| | 9 | 1442 | | using var reader = new StreamReader(stream); |
| | 9 | 1443 | | return reader.ReadToEnd(); |
| | 31 | 1444 | | }); |
| | | 1445 | | } |
| | | 1446 | | |
| | | 1447 | | private static class StringBuilderCache |
| | | 1448 | | { |
| | 1 | 1449 | | private static readonly DefaultObjectPool<StringBuilder> Pool = new(new StringBuilderPooledObjectPolicy |
| | 1 | 1450 | | { |
| | 1 | 1451 | | InitialCapacity = 4096, |
| | 1 | 1452 | | MaximumRetainedCapacity = 1 * 1024 * 1024 |
| | 1 | 1453 | | }); |
| | | 1454 | | |
| | 40 | 1455 | | internal static StringBuilder Get() => Pool.Get(); |
| | | 1456 | | |
| | 38 | 1457 | | internal static void Return(StringBuilder builder) => Pool.Return(builder); |
| | | 1458 | | |
| | | 1459 | | internal static string ToStringAndReturnToPool(StringBuilder builder) |
| | 2 | 1460 | | { |
| | 2 | 1461 | | var result = builder.ToString(); |
| | 2 | 1462 | | Pool.Return(builder); |
| | 2 | 1463 | | return result; |
| | 2 | 1464 | | } |
| | | 1465 | | } |
| | | 1466 | | } |