| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using DirectSight.Parser.Analysis; |
| | | 6 | | |
| | | 7 | | namespace DirectSight.Parser; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// The result on a <see cref="ParserBase"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | public class ParserResult |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The name of the parser or the merged parsers. |
| | | 16 | | /// </summary> |
| | | 17 | | private readonly List<string> parserNames; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The covered assemblies. |
| | | 21 | | /// </summary> |
| | | 22 | | private readonly List<Assembly> assemblies; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="ParserResult"/> class. |
| | | 26 | | /// </summary> |
| | 8 | 27 | | public ParserResult() |
| | 8 | 28 | | { |
| | 8 | 29 | | this.assemblies = []; |
| | 8 | 30 | | this.SupportsBranchCoverage = false; |
| | 8 | 31 | | this.parserNames = []; |
| | 8 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="ParserResult"/> class. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="assemblies">The assemblies.</param> |
| | | 38 | | /// <param name="supportsBranchCoverage">Indicates whether the used parser supports branch coverage.</param> |
| | | 39 | | /// <param name="parserName">The name of the parser.</param> |
| | 90 | 40 | | public ParserResult(List<Assembly> assemblies, bool supportsBranchCoverage, string parserName) |
| | 90 | 41 | | { |
| | 90 | 42 | | ArgumentNullException.ThrowIfNull(parserName); |
| | | 43 | | |
| | 90 | 44 | | this.assemblies = assemblies ?? throw new ArgumentNullException(nameof(assemblies)); |
| | 90 | 45 | | this.SupportsBranchCoverage = supportsBranchCoverage; |
| | 90 | 46 | | this.parserNames = |
| | 90 | 47 | | [ |
| | 90 | 48 | | parserName |
| | 90 | 49 | | ]; |
| | 90 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the assemblies that have been found in the report. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <value>The assemblies.</value> |
| | 82 | 56 | | public IReadOnlyCollection<Assembly> Assemblies => this.assemblies; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets a value indicating whether the used parser supports branch coverage. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <value> |
| | | 62 | | /// <c>true</c> if used parser supports branch coverage; otherwise, <c>false</c>. |
| | | 63 | | /// </value> |
| | 196 | 64 | | public bool SupportsBranchCoverage { get; private set; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the timestamp on which the coverage report was generated. |
| | | 68 | | /// </summary> |
| | 92 | 69 | | public DateTime? MinimumTimeStamp { get; internal set; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the timestamp on which the coverage report was generated. |
| | | 73 | | /// </summary> |
| | 92 | 74 | | public DateTime? MaximumTimeStamp { get; internal set; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the names of the parsers. |
| | | 78 | | /// </summary> |
| | | 79 | | public string ParserName |
| | | 80 | | { |
| | | 81 | | get |
| | 5 | 82 | | { |
| | 5 | 83 | | if (this.parserNames.Count == 0) |
| | 1 | 84 | | { |
| | 1 | 85 | | return string.Empty; |
| | | 86 | | } |
| | 4 | 87 | | else if (this.parserNames.Count == 1) |
| | 3 | 88 | | { |
| | 3 | 89 | | return this.parserNames[0]; |
| | | 90 | | } |
| | | 91 | | else |
| | 1 | 92 | | { |
| | 1 | 93 | | var sb = new StringBuilder("MultiReport ("); |
| | | 94 | | |
| | 4 | 95 | | var groupedParsers = this.parserNames.GroupBy(p => p).OrderBy(pg => pg.Key); |
| | | 96 | | |
| | 1 | 97 | | sb.Append(string.Join( |
| | 1 | 98 | | ", ", |
| | 2 | 99 | | groupedParsers.Select(pg => string.Format("{0}x {1}", pg.Count(), pg.Key)))); |
| | | 100 | | |
| | 1 | 101 | | sb.Append(')'); |
| | 1 | 102 | | return sb.ToString(); |
| | | 103 | | } |
| | 5 | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Merges the given parser result with the current instance. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="parserResult">The parser result to merge.</param> |
| | | 111 | | internal void Merge(ParserResult parserResult) |
| | 30 | 112 | | { |
| | 148 | 113 | | foreach (var assembly in parserResult.Assemblies) |
| | 29 | 114 | | { |
| | 52 | 115 | | var existingAssembly = this.assemblies.FirstOrDefault(a => a.Name == assembly.Name); |
| | | 116 | | |
| | 29 | 117 | | if (existingAssembly != null) |
| | 23 | 118 | | { |
| | 23 | 119 | | existingAssembly.Merge(assembly); |
| | 23 | 120 | | } |
| | | 121 | | else |
| | 6 | 122 | | { |
| | 6 | 123 | | this.assemblies.Add(assembly); |
| | 6 | 124 | | } |
| | 29 | 125 | | } |
| | | 126 | | |
| | 30 | 127 | | this.assemblies.Sort((x, y) => x.Name.CompareTo(y.Name)); |
| | | 128 | | |
| | 30 | 129 | | this.SupportsBranchCoverage |= parserResult.SupportsBranchCoverage; |
| | 30 | 130 | | this.parserNames.AddRange(parserResult.parserNames); |
| | | 131 | | |
| | 30 | 132 | | if (this.MinimumTimeStamp.HasValue) |
| | 0 | 133 | | { |
| | 0 | 134 | | if (parserResult.MinimumTimeStamp.HasValue) |
| | 0 | 135 | | { |
| | 0 | 136 | | this.MinimumTimeStamp = Min(this.MinimumTimeStamp.Value, parserResult.MinimumTimeStamp.Value); |
| | 0 | 137 | | } |
| | 0 | 138 | | } |
| | | 139 | | else |
| | 30 | 140 | | { |
| | 30 | 141 | | this.MinimumTimeStamp = parserResult.MinimumTimeStamp; |
| | 30 | 142 | | } |
| | | 143 | | |
| | 30 | 144 | | if (this.MaximumTimeStamp.HasValue) |
| | 0 | 145 | | { |
| | 0 | 146 | | if (parserResult.MaximumTimeStamp.HasValue) |
| | 0 | 147 | | { |
| | 0 | 148 | | this.MaximumTimeStamp = Max(this.MaximumTimeStamp.Value, parserResult.MaximumTimeStamp.Value); |
| | 0 | 149 | | } |
| | 0 | 150 | | } |
| | | 151 | | else |
| | 30 | 152 | | { |
| | 30 | 153 | | this.MaximumTimeStamp = parserResult.MaximumTimeStamp; |
| | 30 | 154 | | } |
| | 30 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Returns the minimum date. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="first">The first date.</param> |
| | | 161 | | /// <param name="second">The second date.</param> |
| | | 162 | | /// <returns>The minimum of the two dates.</returns> |
| | | 163 | | private static DateTime Min(DateTime first, DateTime second) |
| | 0 | 164 | | { |
| | 0 | 165 | | if (first < second) |
| | 0 | 166 | | { |
| | 0 | 167 | | return first; |
| | | 168 | | } |
| | | 169 | | else |
| | 0 | 170 | | { |
| | 0 | 171 | | return second; |
| | | 172 | | } |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Returns the maximum date. |
| | | 177 | | /// </summary> |
| | | 178 | | /// <param name="first">The first date.</param> |
| | | 179 | | /// <param name="second">The second date.</param> |
| | | 180 | | /// <returns>The maximum of the two dates.</returns> |
| | | 181 | | private static DateTime Max(DateTime first, DateTime second) |
| | 0 | 182 | | { |
| | 0 | 183 | | if (first > second) |
| | 0 | 184 | | { |
| | 0 | 185 | | return first; |
| | | 186 | | } |
| | | 187 | | else |
| | 0 | 188 | | { |
| | 0 | 189 | | return second; |
| | | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | } |