| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Xml.Linq; |
| | | 4 | | |
| | | 5 | | namespace DirectSight.Parser.Preprocessing; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Preprocessor for OpenCover reports. |
| | | 9 | | /// </summary> |
| | | 10 | | internal class OpenCoverReportPreprocessor |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Executes the preprocessing of the report. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="report">The report.</param> |
| | | 16 | | internal void Execute(XContainer report) |
| | 56 | 17 | | { |
| | 1924 | 18 | | foreach (var module in report.Descendants("Module").ToArray()) |
| | 878 | 19 | | { |
| | 878 | 20 | | ApplyClassNameToStartupCodeElements(module); |
| | 878 | 21 | | } |
| | 56 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Applies the class name of the parent class to startup code elements. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="module">The module.</param> |
| | | 28 | | private static void ApplyClassNameToStartupCodeElements(XElement module) |
| | 878 | 29 | | { |
| | 878 | 30 | | var startupCodeClasses = module |
| | 878 | 31 | | .Elements("Classes") |
| | 878 | 32 | | .Elements("Class") |
| | 1390 | 33 | | .Where(c => c.Element("FullName").Value.StartsWith("<StartupCode$", StringComparison.OrdinalIgnoreCase) |
| | 1390 | 34 | | && c.Element("FullName").Value.Contains("/")) |
| | 878 | 35 | | .ToArray(); |
| | | 36 | | |
| | 878 | 37 | | var classesInModule = module |
| | 878 | 38 | | .Elements("Classes") |
| | 878 | 39 | | .Elements("Class") |
| | 1390 | 40 | | .Where(c => !c.Element("FullName").Value.StartsWith("<StartupCode$", StringComparison.OrdinalIgnoreCase)) |
| | 878 | 41 | | .ToArray(); |
| | | 42 | | |
| | 2634 | 43 | | foreach (var startupCodeClass in startupCodeClasses) |
| | 0 | 44 | | { |
| | 0 | 45 | | var methods = startupCodeClass |
| | 0 | 46 | | .Elements("Methods") |
| | 0 | 47 | | .Elements("Method") |
| | 0 | 48 | | .Where(c => c.Element("FileRef") != null) |
| | 0 | 49 | | .ToArray(); |
| | | 50 | | |
| | 0 | 51 | | var fileIds = methods.Elements("FileRef") |
| | 0 | 52 | | .Select(e => e.Attribute("uid").Value) |
| | 0 | 53 | | .Distinct() |
| | 0 | 54 | | .ToArray(); |
| | | 55 | | |
| | 0 | 56 | | if (fileIds.Length != 1) |
| | 0 | 57 | | { |
| | 0 | 58 | | continue; |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | var lineNumbers = methods |
| | 0 | 62 | | .Elements("SequencePoints") |
| | 0 | 63 | | .Elements("SequencePoint") |
| | 0 | 64 | | .Where(s => s.Attribute("sl") != null) |
| | 0 | 65 | | .Select(s => int.Parse(s.Attribute("sl").Value)) |
| | 0 | 66 | | .OrderBy(v => v) |
| | 0 | 67 | | .Take(1) |
| | 0 | 68 | | .ToArray(); |
| | | 69 | | |
| | 0 | 70 | | if (lineNumbers.Length != 1) |
| | 0 | 71 | | { |
| | 0 | 72 | | continue; |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | XElement closestClass = null; |
| | 0 | 76 | | int closestLineNumber = 0; |
| | | 77 | | |
| | 0 | 78 | | foreach (var @class in classesInModule) |
| | 0 | 79 | | { |
| | 0 | 80 | | var methodsOfClass = @class |
| | 0 | 81 | | .Elements("Methods") |
| | 0 | 82 | | .Elements("Method") |
| | 0 | 83 | | .Where(c => c.Element("FileRef") != null) |
| | 0 | 84 | | .ToArray(); |
| | | 85 | | |
| | 0 | 86 | | var fileIdsOfClass = methodsOfClass |
| | 0 | 87 | | .Elements("FileRef") |
| | 0 | 88 | | .Select(e => e.Attribute("uid").Value) |
| | 0 | 89 | | .Distinct() |
| | 0 | 90 | | .ToArray(); |
| | | 91 | | |
| | 0 | 92 | | if (fileIdsOfClass.Length != 1 || fileIdsOfClass[0] != fileIds[0]) |
| | 0 | 93 | | { |
| | 0 | 94 | | continue; |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | var lineNumbersOfClass = methodsOfClass |
| | 0 | 98 | | .Elements("SequencePoints") |
| | 0 | 99 | | .Elements("SequencePoint") |
| | 0 | 100 | | .Where(s => s.Attribute("sl") != null) |
| | 0 | 101 | | .Select(s => int.Parse(s.Attribute("sl").Value)) |
| | 0 | 102 | | .OrderBy(v => v) |
| | 0 | 103 | | .Take(1) |
| | 0 | 104 | | .ToArray(); |
| | | 105 | | |
| | | 106 | | /* Conditions: |
| | | 107 | | * 1) No line numbers available |
| | | 108 | | * 2) Class comes after current class |
| | | 109 | | * 3) Closer class has already been found */ |
| | 0 | 110 | | if (lineNumbersOfClass.Length != 1 |
| | 0 | 111 | | || lineNumbersOfClass[0] > lineNumbers[0] |
| | 0 | 112 | | || closestLineNumber > lineNumbersOfClass[0]) |
| | 0 | 113 | | { |
| | 0 | 114 | | continue; |
| | | 115 | | } |
| | | 116 | | else |
| | 0 | 117 | | { |
| | 0 | 118 | | closestClass = @class; |
| | 0 | 119 | | closestLineNumber = lineNumbersOfClass[0]; |
| | 0 | 120 | | } |
| | 0 | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | if (closestClass != null) |
| | 0 | 124 | | { |
| | 0 | 125 | | startupCodeClass.Element("FullName").Value = closestClass.Element("FullName").Value + "/" + startupCodeC |
| | 0 | 126 | | } |
| | 0 | 127 | | } |
| | 878 | 128 | | } |
| | | 129 | | } |