| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using DirectSight.Common; |
| | | 5 | | using DirectSight.Logging; |
| | | 6 | | using DirectSight.Parser.Analysis.LineCoverage; |
| | | 7 | | using DirectSight.Parser.FileReading; |
| | | 8 | | |
| | | 9 | | namespace DirectSight.Parser.Analysis; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a source code file. |
| | | 13 | | /// </summary> |
| | | 14 | | public class CodeFile |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The line coverage by test method. |
| | | 18 | | /// </summary> |
| | 1549 | 19 | | private readonly IDictionary<TestMethod, CoverageByTrackedMethod> lineCoveragesByTestMethod = new Dictionary<TestMet |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The method metrics of the class. |
| | | 23 | | /// </summary> |
| | 1549 | 24 | | private readonly HashSet<MethodMetric> methodMetrics = []; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The code elements. |
| | | 28 | | /// </summary> |
| | 1549 | 29 | | private readonly HashSet<CodeElement> codeElements = []; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Array containing the coverage information by line number. |
| | | 33 | | /// -1: Not coverable |
| | | 34 | | /// 0: Not visited |
| | | 35 | | /// >0: Number of visits |
| | | 36 | | /// </summary> |
| | | 37 | | private ILineInfo<int> lineCoverage; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Array containing the line visit status by line number. |
| | | 41 | | /// </summary> |
| | | 42 | | private ILineInfo<LineVisitStatus> lineVisitStatus; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The branches by line number. |
| | | 46 | | /// </summary> |
| | | 47 | | private IDictionary<int, ICollection<Branch>> branches; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// The optional additional file reader. |
| | | 51 | | /// </summary> |
| | | 52 | | private IFileReader additionalFileReader; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// The number of covered lines. |
| | | 56 | | /// </summary> |
| | | 57 | | private int coveredLines; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// The number of coverable lines. |
| | | 61 | | /// </summary> |
| | | 62 | | private int coverableLines; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Initializes a new instance of the <see cref="CodeFile" /> class. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="path">The path of the file.</param> |
| | | 68 | | /// <param name="lineCoverage">The line coverage.</param> |
| | | 69 | | /// <param name="lineVisitStatus">The line visit status.</param> |
| | | 70 | | internal CodeFile(string path, ILineInfo<int> lineCoverage, ILineInfo<LineVisitStatus> lineVisitStatus) |
| | 16 | 71 | | : this(path, lineCoverage, lineVisitStatus, null, null) |
| | 16 | 72 | | { |
| | 16 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Initializes a new instance of the <see cref="CodeFile" /> class. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="path">The path of the file.</param> |
| | | 79 | | /// <param name="lineCoverage">The line coverage.</param> |
| | | 80 | | /// <param name="lineVisitStatus">The line visit status.</param> |
| | | 81 | | /// <param name="additionalFileReader">The optional additional file reader.</param> |
| | | 82 | | internal CodeFile(string path, ILineInfo<int> lineCoverage, ILineInfo<LineVisitStatus> lineVisitStatus, IFileReader |
| | 2 | 83 | | : this(path, lineCoverage, lineVisitStatus, null, additionalFileReader) |
| | 2 | 84 | | { |
| | 2 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Initializes a new instance of the <see cref="CodeFile" /> class. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="path">The path.</param> |
| | | 91 | | /// <param name="lineCoverage">The line coverage.</param> |
| | | 92 | | /// <param name="lineVisitStatus">The line visit status.</param> |
| | | 93 | | /// <param name="branches">The branches.</param> |
| | | 94 | | internal CodeFile(string path, ILineInfo<int> lineCoverage, ILineInfo<LineVisitStatus> lineVisitStatus, IDictionary< |
| | 1531 | 95 | | : this(path, lineCoverage, lineVisitStatus, branches, null) |
| | 1531 | 96 | | { |
| | 1531 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Initializes a new instance of the <see cref="CodeFile" /> class. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="path">The path.</param> |
| | | 103 | | /// <param name="lineCoverage">The line coverage.</param> |
| | | 104 | | /// <param name="lineVisitStatus">The line visit status.</param> |
| | | 105 | | /// <param name="branches">The branches.</param> |
| | | 106 | | /// <param name="additionalFileReader">The optional additional file reader.</param> |
| | 1549 | 107 | | internal CodeFile( |
| | 1549 | 108 | | string path, |
| | 1549 | 109 | | ILineInfo<int> lineCoverage, |
| | 1549 | 110 | | ILineInfo<LineVisitStatus> lineVisitStatus, |
| | 1549 | 111 | | IDictionary<int, ICollection<Branch>> branches, |
| | 1549 | 112 | | IFileReader additionalFileReader) |
| | 1549 | 113 | | { |
| | 1549 | 114 | | if (lineCoverage == null) |
| | 0 | 115 | | { |
| | 0 | 116 | | throw new ArgumentNullException(nameof(lineCoverage)); |
| | | 117 | | } |
| | | 118 | | |
| | 1549 | 119 | | if (lineVisitStatus == null) |
| | 0 | 120 | | { |
| | 0 | 121 | | throw new ArgumentNullException(nameof(lineVisitStatus)); |
| | | 122 | | } |
| | | 123 | | |
| | 1549 | 124 | | if (lineCoverage.LongLength != lineVisitStatus.LongLength) |
| | 0 | 125 | | { |
| | 0 | 126 | | throw new ArgumentException("Length of 'lineCoverage' and 'lineVisitStatus' must match", nameof(lineVisitSta |
| | | 127 | | } |
| | | 128 | | |
| | 1549 | 129 | | this.Path = path ?? throw new ArgumentNullException(nameof(path)); |
| | 1549 | 130 | | this.lineCoverage = lineCoverage; |
| | 1549 | 131 | | this.lineVisitStatus = lineVisitStatus; |
| | 1549 | 132 | | this.branches = branches; |
| | 1549 | 133 | | this.additionalFileReader = additionalFileReader; |
| | | 134 | | |
| | 46475 | 135 | | this.coveredLines = this.lineCoverage.Count(l => l > 0); |
| | 46475 | 136 | | this.coverableLines = this.lineCoverage.Count(l => l >= 0); |
| | 1549 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Gets the path. |
| | | 141 | | /// </summary> |
| | | 142 | | /// <value>The path.</value> |
| | 5076 | 143 | | public string Path { get; } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets the test methods. |
| | | 147 | | /// </summary> |
| | | 148 | | /// <value> |
| | | 149 | | /// The test methods. |
| | | 150 | | /// </value> |
| | 104 | 151 | | public IEnumerable<TestMethod> TestMethods => this.lineCoveragesByTestMethod.Keys; |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Gets the method metrics. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <value>The method metrics.</value> |
| | 173 | 157 | | public IEnumerable<MethodMetric> MethodMetrics => this.methodMetrics; |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Gets the code elements. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <value> |
| | | 163 | | /// The code elements. |
| | | 164 | | /// </value> |
| | 689 | 165 | | public IEnumerable<CodeElement> CodeElements => this.codeElements; |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Gets the number of covered lines. |
| | | 169 | | /// </summary> |
| | | 170 | | /// <value>The number of covered lines.</value> |
| | 1589 | 171 | | public int CoveredLines => this.coveredLines; |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Gets the number of coverable lines. |
| | | 175 | | /// </summary> |
| | | 176 | | /// <value>The number of coverable lines.</value> |
| | 2528 | 177 | | public int CoverableLines => this.coverableLines; |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Gets the number of total lines. |
| | | 181 | | /// </summary> |
| | | 182 | | /// <value>The number of total lines.</value> |
| | 480 | 183 | | public int? TotalLines { get; private set; } |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Gets line coverage information by line number for this file. |
| | | 187 | | /// </summary> |
| | 0 | 188 | | public IReadOnlyLineInfo<int> LineCoverage => this.lineCoverage; |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Gets line visit status by line number for this file. |
| | | 192 | | /// </summary> |
| | 2 | 193 | | public IReadOnlyLineInfo<LineVisitStatus> LineVisitStatus => this.lineVisitStatus; |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Gets the branches by line number. |
| | | 197 | | /// </summary> |
| | 0 | 198 | | public IDictionary<int, ICollection<Branch>> BranchesByLine => this.branches ?? new Dictionary<int, ICollection<Bran |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Gets the number of covered branches. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <value> |
| | | 204 | | /// The number of covered branches. |
| | | 205 | | /// </value> |
| | | 206 | | public int? CoveredBranches |
| | | 207 | | { |
| | | 208 | | get |
| | 1191 | 209 | | { |
| | 1191 | 210 | | if (this.branches == null) |
| | 2 | 211 | | { |
| | 2 | 212 | | return null; |
| | | 213 | | } |
| | | 214 | | |
| | 2255 | 215 | | return this.branches.SafeSum(l => l.Value.Count(b => b.BranchVisits > 0)); |
| | 1191 | 216 | | } |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Gets the number of total branches. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <value> |
| | | 223 | | /// The number of total branches. |
| | | 224 | | /// </value> |
| | | 225 | | public int? TotalBranches |
| | | 226 | | { |
| | | 227 | | get |
| | 2023 | 228 | | { |
| | 2023 | 229 | | if (this.branches == null) |
| | 2 | 230 | | { |
| | 2 | 231 | | return null; |
| | | 232 | | } |
| | | 233 | | |
| | 2584 | 234 | | return this.branches.SafeSum(l => l.Value.Count); |
| | 2023 | 235 | | } |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// Gets the number of covered code elements. |
| | | 240 | | /// </summary> |
| | | 241 | | /// <value> |
| | | 242 | | /// The number of covered code elements. |
| | | 243 | | /// </value> |
| | | 244 | | public int CoveredCodeElements |
| | | 245 | | { |
| | | 246 | | get |
| | 325 | 247 | | { |
| | 325 | 248 | | return this.CodeElements.Count( |
| | 1318 | 249 | | x => x.CoverageQuota.GetValueOrDefault() > 0); |
| | 325 | 250 | | } |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Gets the number of fully covered code elements. |
| | | 255 | | /// </summary> |
| | | 256 | | /// <value> |
| | | 257 | | /// The number of fully covered code elements. |
| | | 258 | | /// </value> |
| | | 259 | | public int FullCoveredCodeElements |
| | | 260 | | { |
| | | 261 | | get |
| | 324 | 262 | | { |
| | 324 | 263 | | return this.CodeElements.Count( |
| | 1314 | 264 | | x => this.lineCoverage.Skip(x.FirstLine) |
| | 1314 | 265 | | .Take(x.LastLine - x.FirstLine + 1) |
| | 4608 | 266 | | .All(y => y != 0)); |
| | 324 | 267 | | } |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Gets the number of total code elements. |
| | | 272 | | /// </summary> |
| | | 273 | | /// <value> |
| | | 274 | | /// The number of total code elements. |
| | | 275 | | /// </value> |
| | 972 | 276 | | public int TotalCodeElements => this.codeElements.Count; |
| | | 277 | | |
| | | 278 | | /// <summary> |
| | | 279 | | /// Gets the coverage quota of the file. |
| | | 280 | | /// </summary> |
| | | 281 | | /// <value>The coverage quota.</value> |
| | | 282 | | public decimal? CoverageQuota |
| | | 283 | | { |
| | | 284 | | get |
| | 0 | 285 | | { |
| | 0 | 286 | | return (this.CoverableLines == 0) ? (decimal?)null : MathExtensions.CalculatePercentage(this.CoveredLines, t |
| | 0 | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Returns a <see cref="string" /> that represents this instance. |
| | | 292 | | /// </summary> |
| | | 293 | | /// <returns> |
| | | 294 | | /// A <see cref="string" /> that represents this instance. |
| | | 295 | | /// </returns> |
| | | 296 | | public override string ToString() |
| | 0 | 297 | | { |
| | 0 | 298 | | return this.Path; |
| | 0 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Determines whether the specified <see cref="object"/> is equal to this instance. |
| | | 303 | | /// </summary> |
| | | 304 | | /// <param name="obj">The <see cref="object"/> to compare with this instance.</param> |
| | | 305 | | /// <returns> |
| | | 306 | | /// <c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>. |
| | | 307 | | /// </returns> |
| | | 308 | | public override bool Equals(object obj) |
| | 449 | 309 | | { |
| | 449 | 310 | | if (obj == null || !obj.GetType().Equals(typeof(CodeFile))) |
| | 2 | 311 | | { |
| | 2 | 312 | | return false; |
| | | 313 | | } |
| | | 314 | | else |
| | 447 | 315 | | { |
| | 447 | 316 | | var codeFile = (CodeFile)obj; |
| | | 317 | | |
| | 447 | 318 | | string fileNameToCompare = codeFile.Path.Substring(codeFile.Path.LastIndexOf('/') + 1); |
| | 447 | 319 | | int indexOfSlash = codeFile.Path.LastIndexOf('/'); |
| | | 320 | | |
| | 447 | 321 | | if (indexOfSlash != -1) |
| | 447 | 322 | | { |
| | 447 | 323 | | fileNameToCompare = codeFile.Path.Substring(indexOfSlash + 1); |
| | 447 | 324 | | } |
| | | 325 | | |
| | 447 | 326 | | string fileName = this.Path.Substring(this.Path.LastIndexOf('/') + 1); |
| | 447 | 327 | | indexOfSlash = this.Path.LastIndexOf('/'); |
| | 447 | 328 | | if (indexOfSlash != -1) |
| | 447 | 329 | | { |
| | 447 | 330 | | fileName = this.Path.Substring(this.Path.LastIndexOf('/') + 1); |
| | 447 | 331 | | } |
| | | 332 | | |
| | 447 | 333 | | return fileName.Equals(fileNameToCompare, StringComparison.OrdinalIgnoreCase); |
| | | 334 | | } |
| | 449 | 335 | | } |
| | | 336 | | |
| | | 337 | | /// <summary> |
| | | 338 | | /// Returns a hash code for this instance. |
| | | 339 | | /// </summary> |
| | | 340 | | /// <returns> |
| | | 341 | | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |
| | | 342 | | /// </returns> |
| | 108 | 343 | | public override int GetHashCode() => this.Path.GetHashCode(); |
| | | 344 | | |
| | | 345 | | /// <summary> |
| | | 346 | | /// Calculates the coverage quota in a given range of lines. |
| | | 347 | | /// </summary> |
| | | 348 | | /// <param name="firstLine">The first line.</param> |
| | | 349 | | /// <param name="lastLine">The last line.</param> |
| | | 350 | | /// <returns>The coverage quota or <code>null</code> if not applicable.</returns> |
| | | 351 | | internal decimal? CoverageQuotaInRange(int firstLine, int lastLine) |
| | 5520 | 352 | | { |
| | 5520 | 353 | | if (firstLine < 0 |
| | 5520 | 354 | | || firstLine >= this.lineVisitStatus.Length |
| | 5520 | 355 | | || lastLine < 0 |
| | 5520 | 356 | | || lastLine >= this.lineVisitStatus.Length |
| | 5520 | 357 | | || firstLine > lastLine) |
| | 0 | 358 | | { |
| | 0 | 359 | | return null; |
| | | 360 | | } |
| | | 361 | | |
| | 5520 | 362 | | int coverableLines = 0; |
| | 5520 | 363 | | int coveredLines = 0; |
| | | 364 | | |
| | 62040 | 365 | | for (int i = firstLine; i <= lastLine; i++) |
| | 25500 | 366 | | { |
| | 25500 | 367 | | if (this.lineVisitStatus[i] != Analysis.LineVisitStatus.NotCoverable) |
| | 22215 | 368 | | { |
| | 22215 | 369 | | coverableLines++; |
| | 22215 | 370 | | } |
| | | 371 | | |
| | 25500 | 372 | | if (this.lineVisitStatus[i] > Analysis.LineVisitStatus.NotCovered) |
| | 13989 | 373 | | { |
| | 13989 | 374 | | coveredLines++; |
| | 13989 | 375 | | } |
| | 25500 | 376 | | } |
| | | 377 | | |
| | 5520 | 378 | | return (coverableLines == 0) ? (decimal?)null : MathExtensions.CalculatePercentage(coveredLines, coverableLines) |
| | 5520 | 379 | | } |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// Adds the coverage by test method. |
| | | 383 | | /// </summary> |
| | | 384 | | /// <param name="testMethod">The test method.</param> |
| | | 385 | | /// <param name="trackedMethodCoverage">The coverage by for test method.</param> |
| | | 386 | | internal void AddCoverageByTestMethod(TestMethod testMethod, CoverageByTrackedMethod trackedMethodCoverage) |
| | 456 | 387 | | { |
| | 456 | 388 | | if (testMethod == null) |
| | 0 | 389 | | { |
| | 0 | 390 | | throw new ArgumentNullException(nameof(testMethod)); |
| | | 391 | | } |
| | | 392 | | |
| | 456 | 393 | | if (trackedMethodCoverage == null) |
| | 0 | 394 | | { |
| | 0 | 395 | | throw new ArgumentNullException(nameof(trackedMethodCoverage)); |
| | | 396 | | } |
| | | 397 | | |
| | 456 | 398 | | if (!this.lineCoveragesByTestMethod.TryGetValue(testMethod, out CoverageByTrackedMethod existingTrackedMethodCov |
| | 455 | 399 | | { |
| | 455 | 400 | | this.lineCoveragesByTestMethod.Add(testMethod, trackedMethodCoverage); |
| | 455 | 401 | | } |
| | | 402 | | else |
| | 1 | 403 | | { |
| | 1 | 404 | | this.lineCoveragesByTestMethod[testMethod] = MergeCoverageByTrackedMethod(existingTrackedMethodCoverage, tra |
| | 1 | 405 | | } |
| | 456 | 406 | | } |
| | | 407 | | |
| | | 408 | | /// <summary> |
| | | 409 | | /// Adds the given method metric. |
| | | 410 | | /// </summary> |
| | | 411 | | /// <param name="methodMetric">The method metric.</param> |
| | | 412 | | internal void AddMethodMetric(MethodMetric methodMetric) |
| | 2988 | 413 | | { |
| | 2988 | 414 | | this.methodMetrics.Add(methodMetric); |
| | 2988 | 415 | | } |
| | | 416 | | |
| | | 417 | | /// <summary> |
| | | 418 | | /// Adds the code element. |
| | | 419 | | /// </summary> |
| | | 420 | | /// <param name="codeElement">The code element.</param> |
| | | 421 | | internal void AddCodeElement(CodeElement codeElement) |
| | 4426 | 422 | | { |
| | 4426 | 423 | | this.codeElements.Add(codeElement); |
| | 4426 | 424 | | } |
| | | 425 | | |
| | | 426 | | /// <summary> |
| | | 427 | | /// Performs the analysis of the source file. |
| | | 428 | | /// </summary> |
| | | 429 | | /// <param name="fileReader">The file reader.</param> |
| | | 430 | | /// <returns>The analysis result.</returns> |
| | | 431 | | internal FileAnalysis AnalyzeFile(IFileReader fileReader) |
| | 65 | 432 | | { |
| | 65 | 433 | | string error = null; |
| | | 434 | | |
| | 65 | 435 | | string[] lines = null; |
| | | 436 | | |
| | 65 | 437 | | if (this.additionalFileReader != null) |
| | 2 | 438 | | { |
| | 2 | 439 | | lines = this.additionalFileReader.LoadFile(this.Path, out error); |
| | 2 | 440 | | } |
| | | 441 | | |
| | 65 | 442 | | if (this.additionalFileReader == null || error != null) |
| | 64 | 443 | | { |
| | 64 | 444 | | error = null; |
| | 64 | 445 | | lines = fileReader.LoadFile(this.Path, out error); |
| | 64 | 446 | | } |
| | | 447 | | |
| | 65 | 448 | | if (error != null) |
| | 2 | 449 | | { |
| | 2 | 450 | | ConsoleLogger.Error(error); |
| | 2 | 451 | | lines = this.lineCoverage |
| | 8 | 452 | | .Select(l => string.Empty) |
| | 2 | 453 | | .ToArray(); |
| | 2 | 454 | | } |
| | | 455 | | |
| | 65 | 456 | | this.TotalLines = lines.Length; |
| | | 457 | | |
| | 65 | 458 | | int currentLineNumber = 0; |
| | | 459 | | |
| | 65 | 460 | | var result = new FileAnalysis(this.Path, error); |
| | 65 | 461 | | ICollection<Branch> branchesOfLine = null; |
| | | 462 | | |
| | 5279 | 463 | | foreach (var line in lines) |
| | 2542 | 464 | | { |
| | 2542 | 465 | | currentLineNumber++; |
| | 2542 | 466 | | int visits = this.lineCoverage.Length > currentLineNumber ? this.lineCoverage[currentLineNumber] : -1; |
| | 2542 | 467 | | LineVisitStatus lineVisitStatus = this.lineVisitStatus.Length > currentLineNumber ? this.lineVisitStatus[cur |
| | | 468 | | |
| | 2542 | 469 | | var lineCoverageByTestMethod = this.lineCoveragesByTestMethod |
| | 2542 | 470 | | .ToDictionary( |
| | 2524 | 471 | | l => l.Key, |
| | 2542 | 472 | | l => |
| | 2524 | 473 | | { |
| | 2524 | 474 | | if (l.Value.Coverage.Length > currentLineNumber) |
| | 1908 | 475 | | { |
| | 1908 | 476 | | return new ShortLineAnalysis(l.Value.Coverage[currentLineNumber], l.Value.LineVisitStatus[curren |
| | 2542 | 477 | | } |
| | 2542 | 478 | | else |
| | 616 | 479 | | { |
| | 616 | 480 | | return new ShortLineAnalysis(-1, Analysis.LineVisitStatus.NotCoverable); |
| | 2542 | 481 | | } |
| | 5066 | 482 | | }); |
| | | 483 | | |
| | 2542 | 484 | | if (this.branches != null && this.branches.TryGetValue(currentLineNumber, out branchesOfLine)) |
| | 25 | 485 | | { |
| | 25 | 486 | | result.AddLineAnalysis( |
| | 25 | 487 | | new LineAnalysis( |
| | 25 | 488 | | visits, |
| | 25 | 489 | | lineVisitStatus, |
| | 25 | 490 | | lineCoverageByTestMethod, |
| | 25 | 491 | | currentLineNumber, |
| | 25 | 492 | | line.TrimEnd(), |
| | 50 | 493 | | branchesOfLine.Count(b => b.BranchVisits > 0), |
| | 25 | 494 | | branchesOfLine.Count)) |
| | 25 | 495 | | ; |
| | 25 | 496 | | } |
| | | 497 | | else |
| | 2517 | 498 | | { |
| | 2517 | 499 | | result.AddLineAnalysis( |
| | 2517 | 500 | | new LineAnalysis( |
| | 2517 | 501 | | visits, |
| | 2517 | 502 | | lineVisitStatus, |
| | 2517 | 503 | | lineCoverageByTestMethod, |
| | 2517 | 504 | | currentLineNumber, |
| | 2517 | 505 | | line.TrimEnd())); |
| | 2517 | 506 | | } |
| | 2542 | 507 | | } |
| | | 508 | | |
| | 65 | 509 | | return result; |
| | 65 | 510 | | } |
| | | 511 | | |
| | | 512 | | /// <summary> |
| | | 513 | | /// Merges the given file with the current instance. |
| | | 514 | | /// </summary> |
| | | 515 | | /// <param name="file">The file to merge.</param> |
| | | 516 | | internal void Merge(CodeFile file) |
| | 390 | 517 | | { |
| | 390 | 518 | | if (file == null) |
| | 0 | 519 | | { |
| | 0 | 520 | | throw new ArgumentNullException(nameof(file)); |
| | | 521 | | } |
| | | 522 | | |
| | | 523 | | // Resize coverage array if necessary |
| | 390 | 524 | | if (file.lineCoverage.LongLength > this.lineCoverage.LongLength) |
| | 4 | 525 | | { |
| | 4 | 526 | | this.lineCoverage = this.lineCoverage.Resize(file.lineCoverage.LongLength); |
| | 4 | 527 | | } |
| | | 528 | | |
| | | 529 | | // Resize line visit status array if necessary |
| | 390 | 530 | | if (file.lineVisitStatus.LongLength > this.lineVisitStatus.LongLength) |
| | 4 | 531 | | { |
| | 4 | 532 | | this.lineVisitStatus = this.lineVisitStatus.Resize(file.lineVisitStatus.LongLength); |
| | 4 | 533 | | } |
| | | 534 | | |
| | 390 | 535 | | if (file.branches != null) |
| | 387 | 536 | | { |
| | 387 | 537 | | if (this.branches == null) |
| | 1 | 538 | | { |
| | 1 | 539 | | this.branches = new Dictionary<int, ICollection<Branch>>(); |
| | 1 | 540 | | } |
| | | 541 | | |
| | 1369 | 542 | | foreach (var branchByLine in file.branches) |
| | 104 | 543 | | { |
| | 104 | 544 | | if (this.branches.TryGetValue(branchByLine.Key, out ICollection<Branch> existingBranches)) |
| | 101 | 545 | | { |
| | 719 | 546 | | foreach (var branch in branchByLine.Value) |
| | 208 | 547 | | { |
| | 532 | 548 | | Branch existingBranch = existingBranches.FirstOrDefault(b => b.Equals(branch)); |
| | 208 | 549 | | if (existingBranch != null) |
| | 207 | 550 | | { |
| | 207 | 551 | | existingBranch.BranchVisits += branch.BranchVisits; |
| | 207 | 552 | | } |
| | | 553 | | else |
| | 1 | 554 | | { |
| | 1 | 555 | | existingBranches.Add(branch); |
| | 1 | 556 | | } |
| | 208 | 557 | | } |
| | 101 | 558 | | } |
| | | 559 | | else |
| | 3 | 560 | | { |
| | 3 | 561 | | this.branches.Add(branchByLine); |
| | 3 | 562 | | } |
| | 104 | 563 | | } |
| | 387 | 564 | | } |
| | | 565 | | |
| | 23500 | 566 | | for (long i = 0; i < file.lineCoverage.LongLength; i++) |
| | 11360 | 567 | | { |
| | 11360 | 568 | | int coverage = this.lineCoverage[i]; |
| | | 569 | | |
| | 11360 | 570 | | if (coverage < 0) |
| | 7192 | 571 | | { |
| | 7192 | 572 | | coverage = file.lineCoverage[i]; |
| | 7192 | 573 | | } |
| | 4168 | 574 | | else if (file.lineCoverage[i] > 0) |
| | 2654 | 575 | | { |
| | 2654 | 576 | | coverage += file.lineCoverage[i]; |
| | 2654 | 577 | | } |
| | | 578 | | |
| | 11360 | 579 | | this.lineCoverage[i] = coverage; |
| | 11360 | 580 | | } |
| | | 581 | | |
| | 11750 | 582 | | this.coveredLines = this.lineCoverage.Count(l => l > 0); |
| | 11750 | 583 | | this.coverableLines = this.lineCoverage.Count(l => l >= 0); |
| | | 584 | | |
| | 23500 | 585 | | for (long i = 0; i < file.lineVisitStatus.LongLength; i++) |
| | 11360 | 586 | | { |
| | 11360 | 587 | | int lineVisitStatus = Math.Max((int)this.lineVisitStatus[i], (int)file.lineVisitStatus[i]); |
| | | 588 | | |
| | 11360 | 589 | | this.lineVisitStatus[i] = (LineVisitStatus)lineVisitStatus; |
| | | 590 | | |
| | 11360 | 591 | | if (this.lineVisitStatus[i] == Analysis.LineVisitStatus.PartiallyCovered |
| | 11360 | 592 | | && this.branches != null |
| | 11360 | 593 | | && this.branches.TryGetValue((int)i, out ICollection<Branch> branches)) |
| | 74 | 594 | | { |
| | 218 | 595 | | if (branches.All(b => b.BranchVisits > 0)) |
| | 1 | 596 | | { |
| | 1 | 597 | | this.lineVisitStatus[i] = Analysis.LineVisitStatus.Covered; |
| | 1 | 598 | | } |
| | 74 | 599 | | } |
| | 11360 | 600 | | } |
| | | 601 | | |
| | 1174 | 602 | | foreach (var lineCoverageByTestMethod in file.lineCoveragesByTestMethod) |
| | 2 | 603 | | { |
| | 2 | 604 | | this.lineCoveragesByTestMethod.TryGetValue(lineCoverageByTestMethod.Key, out CoverageByTrackedMethod existin |
| | | 605 | | |
| | 2 | 606 | | if (existingTrackedMethodCoverage == null) |
| | 1 | 607 | | { |
| | 1 | 608 | | this.lineCoveragesByTestMethod.Add(lineCoverageByTestMethod); |
| | 1 | 609 | | } |
| | | 610 | | else |
| | 1 | 611 | | { |
| | 1 | 612 | | this.lineCoveragesByTestMethod[lineCoverageByTestMethod.Key] = MergeCoverageByTrackedMethod(existingTrac |
| | 1 | 613 | | } |
| | 2 | 614 | | } |
| | | 615 | | |
| | 2672 | 616 | | foreach (var methodMetric in file.methodMetrics) |
| | 751 | 617 | | { |
| | 2419 | 618 | | var existingMethodMetric = this.methodMetrics.FirstOrDefault(m => m.Equals(methodMetric)); |
| | 751 | 619 | | if (existingMethodMetric != null) |
| | 751 | 620 | | { |
| | 751 | 621 | | existingMethodMetric.Merge(methodMetric); |
| | 751 | 622 | | } |
| | | 623 | | else |
| | 0 | 624 | | { |
| | 0 | 625 | | this.AddMethodMetric(methodMetric); |
| | 0 | 626 | | } |
| | 751 | 627 | | } |
| | | 628 | | |
| | 3364 | 629 | | foreach (var codeElement in file.codeElements) |
| | 1097 | 630 | | { |
| | 1097 | 631 | | this.codeElements.Add(codeElement); |
| | 1097 | 632 | | } |
| | | 633 | | |
| | 3364 | 634 | | foreach (var codeElement in this.codeElements) |
| | 1097 | 635 | | { |
| | 1097 | 636 | | codeElement.ApplyMaximumCoverageQuota(this.CoverageQuotaInRange(codeElement.FirstLine, codeElement.LastLine) |
| | 1097 | 637 | | } |
| | | 638 | | |
| | 390 | 639 | | if (file.additionalFileReader == null) |
| | 390 | 640 | | { |
| | 390 | 641 | | file.additionalFileReader = this.additionalFileReader; |
| | 390 | 642 | | } |
| | 390 | 643 | | } |
| | | 644 | | |
| | | 645 | | /// <summary> |
| | | 646 | | /// Merges the two tracked method coverage. |
| | | 647 | | /// </summary> |
| | | 648 | | /// <param name="existingTrackedMethodCoverage">The existing tracked method coverage.</param> |
| | | 649 | | /// <param name="lineCoverageByTestMethod">The new line coverage by test method.</param> |
| | | 650 | | /// <returns>The merged tracked method coverage.</returns> |
| | | 651 | | private static CoverageByTrackedMethod MergeCoverageByTrackedMethod(CoverageByTrackedMethod existingTrackedMethodCov |
| | 2 | 652 | | { |
| | | 653 | | // Resize coverage array if neccessary |
| | 2 | 654 | | if (lineCoverageByTestMethod.Coverage.LongLength > existingTrackedMethodCoverage.Coverage.LongLength) |
| | 1 | 655 | | { |
| | 1 | 656 | | existingTrackedMethodCoverage.Coverage = lineCoverageByTestMethod.Coverage.Resize(lineCoverageByTestMethod.C |
| | 1 | 657 | | } |
| | | 658 | | |
| | | 659 | | // Resize line visit status array if neccessary |
| | 2 | 660 | | if (lineCoverageByTestMethod.LineVisitStatus.LongLength > existingTrackedMethodCoverage.LineVisitStatus.LongLeng |
| | 1 | 661 | | { |
| | 1 | 662 | | existingTrackedMethodCoverage.LineVisitStatus = lineCoverageByTestMethod.LineVisitStatus.Resize(lineCoverage |
| | 1 | 663 | | } |
| | | 664 | | |
| | 48 | 665 | | for (long i = 0; i < lineCoverageByTestMethod.Coverage.LongLength; i++) |
| | 22 | 666 | | { |
| | 22 | 667 | | int coverage = existingTrackedMethodCoverage.Coverage[i]; |
| | | 668 | | |
| | 22 | 669 | | if (coverage < 0) |
| | 8 | 670 | | { |
| | 8 | 671 | | coverage = lineCoverageByTestMethod.Coverage[i]; |
| | 8 | 672 | | } |
| | 14 | 673 | | else if (lineCoverageByTestMethod.Coverage[i] > 0) |
| | 7 | 674 | | { |
| | 7 | 675 | | coverage += lineCoverageByTestMethod.Coverage[i]; |
| | 7 | 676 | | } |
| | | 677 | | |
| | 22 | 678 | | existingTrackedMethodCoverage.Coverage[i] = coverage; |
| | 22 | 679 | | } |
| | | 680 | | |
| | 48 | 681 | | for (long i = 0; i < lineCoverageByTestMethod.LineVisitStatus.LongLength; i++) |
| | 22 | 682 | | { |
| | 22 | 683 | | int lineVisitStatus = Math.Max((int)existingTrackedMethodCoverage.LineVisitStatus[i], (int)lineCoverageByTes |
| | | 684 | | |
| | 22 | 685 | | existingTrackedMethodCoverage.LineVisitStatus[i] = (LineVisitStatus)lineVisitStatus; |
| | 22 | 686 | | } |
| | | 687 | | |
| | 2 | 688 | | return existingTrackedMethodCoverage; |
| | 2 | 689 | | } |
| | | 690 | | } |