-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCodeCoverageProcessor.cs
More file actions
106 lines (90 loc) · 4.79 KB
/
CodeCoverageProcessor.cs
File metadata and controls
106 lines (90 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using SQLCC.Core;
using SQLCC.Core.Helpers;
using SQLCC.Core.Objects;
namespace SQLCC
{
public class CodeCoverageProcessor
{
private readonly DbTraceCodeFormatter _dbCodeFormatter;
private readonly HighlightCodeProvider _highlightCodeProvider;
private readonly int _highlightMarkUpLength;
private readonly DataScrubber _dataScrubber;
public CodeCoverageProcessor(DbTraceCodeFormatter dbCodeFormatter, HighlightCodeProvider highlightCodeProvider)
{
_dbCodeFormatter = dbCodeFormatter;
_highlightCodeProvider = highlightCodeProvider;
_dataScrubber = new DataScrubber(dbCodeFormatter);
_highlightMarkUpLength = _dbCodeFormatter.StartHighlightMarkUp.Length + _dbCodeFormatter.EndHighlightMarkUp.Length;
}
private List<DbCodeSegment> ProcessRawCodeSegments(MatchCollection rawCodeSegments)
{
var coveredSegments = new List<DbCodeSegment>();
var countToSubtractFromPosition = 0;
for (var i = 0; i < rawCodeSegments.Count; i++)
{
var codeSegment = rawCodeSegments[i];
var dbCodeSegment = new DbCodeSegment();
dbCodeSegment.LinesOfCode = codeSegment.Groups[1].Value.Split('\n').Length;
dbCodeSegment.StartByte = codeSegment.Index - countToSubtractFromPosition;
dbCodeSegment.EndByte = dbCodeSegment.StartByte + codeSegment.Length - _highlightMarkUpLength;
coveredSegments.Add(dbCodeSegment);
countToSubtractFromPosition += _highlightMarkUpLength;
}
return coveredSegments;
}
private MatchCollection GetRawHighlightedCodeSegments(string code)
{
return Regex.Matches(code, string.Format("{0}(.*?){1}", _dbCodeFormatter.StartHighlightMarkUp, _dbCodeFormatter.EndHighlightMarkUp), RegexOptions.Singleline);
}
private List<DbCodeSegment> ProcessHighlightedCode(string highlightedCode)
{
return ProcessRawCodeSegments(GetRawHighlightedCodeSegments(highlightedCode));
}
public DbObject ProcessObjectCoverage(DbObject dbObject)
{
var dbObjectClone = dbObject.Get();
if (dbObjectClone.Code == null)
return dbObjectClone;
if (dbObjectClone.CoveredSegments.Count > 0)
{
var codeWithHighlights = _dbCodeFormatter.FormatCodeWithHighlights(dbObjectClone.Code,
dbObjectClone.CoveredSegments);
dbObjectClone.CoveredSegments = ProcessHighlightedCode(codeWithHighlights);
var functionalHighlightedCode = _dataScrubber.Scrub(codeWithHighlights, "floc.scrub");
var functionalSegments = ProcessHighlightedCode(functionalHighlightedCode);
foreach (var functionalSegment in functionalSegments)
{
dbObjectClone.CoveredCharacters += functionalSegment.EndByte - functionalSegment.StartByte;
dbObjectClone.CoveredLinesOfCode += functionalSegment.LinesOfCode;
}
}
var functionalCode = _dataScrubber.Scrub(dbObjectClone.Code, "floc.scrub");
dbObjectClone.TotalLoc = dbObjectClone.Code.Split('\n').Length;
dbObjectClone.TotalCharacters = dbObjectClone.Code.Length;
dbObjectClone.TotalFloc = functionalCode.Split('\n').Length;
dbObjectClone.TotalFunctionalCharacters = functionalCode.Length;
dbObjectClone.CoveredPercent = (decimal)dbObjectClone.CoveredCharacters / dbObjectClone.TotalFunctionalCharacters;
return dbObjectClone;
}
public void ProcessAllCoverage(DbCodeCoverage codeCover)
{
foreach (var obj in codeCover.TotalObjects)
{
obj.CoveredSegments = codeCover.TraceCodeSegments.Where(p => p.ObjectName.Equals(obj.Name.Split('.')[1]) && p.SchemaName.Equals(obj.Schema)).ToList();
obj.Set(ProcessObjectCoverage(obj));
obj.CodeHighlighted = _highlightCodeProvider.HighlightCode(obj.Code, obj.CoveredSegments);
codeCover.TotalLoc += obj.TotalLoc;
codeCover.TotalFloc += obj.TotalFloc;
codeCover.TotalCharacters += obj.TotalCharacters;
codeCover.TotalFunctionalCharacters += obj.TotalFunctionalCharacters;
codeCover.CoveredCharacters += (int)Math.Round(obj.TotalFunctionalCharacters * obj.CoveredPercent);
codeCover.CoveredLinesOfCode += (int)Math.Round(obj.TotalLoc * obj.CoveredPercent);
}
codeCover.CoveredPercent = ((decimal)codeCover.CoveredCharacters / codeCover.TotalFunctionalCharacters);
}
}
}