|
|
@ -29,22 +29,33 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
{ |
|
|
|
{ |
|
|
|
public class CodeCoverageMethodElement |
|
|
|
public class CodeCoverageMethodElement |
|
|
|
{ |
|
|
|
{ |
|
|
|
XElement element; |
|
|
|
readonly XElement element; |
|
|
|
CodeCoverageResults parent; |
|
|
|
readonly CodeCoverageResults results; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Enables CodeCoverage.Test to compile</summary>
|
|
|
|
|
|
|
|
/// <param name="element">XMLElement</param>
|
|
|
|
public CodeCoverageMethodElement(XElement element) |
|
|
|
public CodeCoverageMethodElement(XElement element) |
|
|
|
: this (element, null) {} |
|
|
|
: this (element, null) {} |
|
|
|
public CodeCoverageMethodElement(XElement element, CodeCoverageResults parent) |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Create Method Element</summary>
|
|
|
|
|
|
|
|
/// <param name="element">XMLElement</param>
|
|
|
|
|
|
|
|
/// <param name="results">has .GetFileName(FileID)</param>
|
|
|
|
|
|
|
|
public CodeCoverageMethodElement(XElement element, CodeCoverageResults results) |
|
|
|
{ |
|
|
|
{ |
|
|
|
this.parent = parent; |
|
|
|
this.results = results; |
|
|
|
this.element = element; |
|
|
|
this.element = element; |
|
|
|
this.SequencePoints = new List<CodeCoverageSequencePoint>(); |
|
|
|
this.SequencePoints = new List<CodeCoverageSequencePoint>(); |
|
|
|
this.BranchPoints = new List<CodeCoverageBranchPoint>(); |
|
|
|
|
|
|
|
Init(); |
|
|
|
Init(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Primary TextSource cache
|
|
|
|
private static string cacheFileName = String.Empty; |
|
|
|
private static string cacheFileName = String.Empty; |
|
|
|
private static CodeCoverageStringTextSource cacheDocument = null; |
|
|
|
private static CodeCoverageStringTextSource cacheDocument = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Secondary TextSource cache
|
|
|
|
|
|
|
|
private static string cache2FileName = String.Empty; |
|
|
|
|
|
|
|
private static CodeCoverageStringTextSource cache2Document = null; |
|
|
|
|
|
|
|
|
|
|
|
public string FileID { get; private set; } |
|
|
|
public string FileID { get; private set; } |
|
|
|
public string FileName { get; private set; } |
|
|
|
public string FileName { get; private set; } |
|
|
|
public bool IsVisited { get; private set; } |
|
|
|
public bool IsVisited { get; private set; } |
|
|
@ -58,7 +69,6 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
public List<CodeCoverageSequencePoint> SequencePoints { get; private set; } |
|
|
|
public List<CodeCoverageSequencePoint> SequencePoints { get; private set; } |
|
|
|
public CodeCoverageSequencePoint BodyStartSP { get; private set; } |
|
|
|
public CodeCoverageSequencePoint BodyStartSP { get; private set; } |
|
|
|
public CodeCoverageSequencePoint BodyFinalSP { get; private set; } |
|
|
|
public CodeCoverageSequencePoint BodyFinalSP { get; private set; } |
|
|
|
public List<CodeCoverageBranchPoint> BranchPoints { get; private set; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsGetter { get; private set; } |
|
|
|
public bool IsGetter { get; private set; } |
|
|
|
public bool IsSetter { get; private set; } |
|
|
|
public bool IsSetter { get; private set; } |
|
|
@ -77,22 +87,11 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
this.FileID = GetFileRef(); |
|
|
|
this.FileID = GetFileRef(); |
|
|
|
this.FileName = String.Empty; |
|
|
|
this.FileName = String.Empty; |
|
|
|
if (!String.IsNullOrEmpty(this.FileID)) { |
|
|
|
if (!String.IsNullOrEmpty(this.FileID)) { |
|
|
|
if (parent != null) { |
|
|
|
if (results != null) { |
|
|
|
this.FileName = parent.GetFileName(this.FileID); |
|
|
|
this.FileName = results.GetFileName(this.FileID); |
|
|
|
if ( File.Exists(this.FileName) ) { |
|
|
|
|
|
|
|
if (cacheFileName != this.FileName) { |
|
|
|
if (cacheFileName != this.FileName) { |
|
|
|
cacheFileName = this.FileName; |
|
|
|
cacheFileName = this.FileName; |
|
|
|
cacheDocument = null; |
|
|
|
cacheDocument = GetSource (cacheFileName); |
|
|
|
try { |
|
|
|
|
|
|
|
using (Stream stream = new FileStream(this.FileName, FileMode.Open, FileAccess.Read)) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
stream.Position = 0; |
|
|
|
|
|
|
|
string textSource = ICSharpCode.AvalonEdit.Utils.FileReader.ReadFileContent(stream, Encoding.Default); |
|
|
|
|
|
|
|
cacheDocument = new CodeCoverageStringTextSource(textSource); |
|
|
|
|
|
|
|
} catch {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -106,22 +105,43 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
if ( !String.IsNullOrEmpty( this.FileID ) ) { |
|
|
|
if ( !String.IsNullOrEmpty( this.FileID ) ) { |
|
|
|
this.GetSequencePoints(); |
|
|
|
this.GetSequencePoints(); |
|
|
|
this.GetSequencePointsContent(); |
|
|
|
this.GetSequencePointsContent(); |
|
|
|
this.getBodyStartSP(); // before OrderBy Line/Col
|
|
|
|
this.getBodyStartSP(); |
|
|
|
this.getBodyFinalSP(); // before orderBy Line/Col
|
|
|
|
this.getBodyFinalSP(); |
|
|
|
this.FilterSequencePoints(); // before orderBy Line/Col
|
|
|
|
|
|
|
|
this.GetBranchPoints(); |
|
|
|
|
|
|
|
this.GetBranchRatio(); |
|
|
|
this.GetBranchRatio(); |
|
|
|
this.GetBranchCoverage(); |
|
|
|
this.GetBranchCoverage(); |
|
|
|
|
|
|
|
|
|
|
|
// SP's are originaly ordered by CIL offset
|
|
|
|
// SP's are originaly ordered by CIL offset
|
|
|
|
// but ccrewrite can move offset of
|
|
|
|
// because ccrewrite can move offset of
|
|
|
|
// Contract.Requires before method signature SP { and
|
|
|
|
// Contract.Requires before method start ({) SP offset
|
|
|
|
// Contract.Ensures after method closing SP }
|
|
|
|
// Contract.Ensures after method final (}) SP offset
|
|
|
|
// So sort SP's back by line/column
|
|
|
|
// So sort SP's back by line/column
|
|
|
|
this.SequencePoints.OrderBy(item => item.Line).OrderBy(item => item.Column); |
|
|
|
this.SequencePoints.OrderBy(item => item.Line).OrderBy(item => item.Column); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string cacheGetSource_LastFileName = null; |
|
|
|
|
|
|
|
private static CodeCoverageStringTextSource cacheGetSource_LastSource = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static CodeCoverageStringTextSource GetSource(string filename) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filename == cacheGetSource_LastFileName) return cacheGetSource_LastSource; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var retSource = (CodeCoverageStringTextSource)null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
stream.Position = 0; |
|
|
|
|
|
|
|
string textSource = ICSharpCode.AvalonEdit.Utils.FileReader.ReadFileContent(stream, Encoding.Default); |
|
|
|
|
|
|
|
retSource = new CodeCoverageStringTextSource(textSource); |
|
|
|
|
|
|
|
} catch (Exception) {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception) {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cacheGetSource_LastFileName = filename; |
|
|
|
|
|
|
|
cacheGetSource_LastSource = retSource; |
|
|
|
|
|
|
|
return retSource; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void GetSequencePoints() { |
|
|
|
void GetSequencePoints() { |
|
|
|
|
|
|
|
|
|
|
|
var xSPoints = this.element |
|
|
|
var xSPoints = this.element |
|
|
@ -129,16 +149,32 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
.Elements("SequencePoint"); |
|
|
|
.Elements("SequencePoint"); |
|
|
|
|
|
|
|
|
|
|
|
foreach (XElement xSPoint in xSPoints) { |
|
|
|
foreach (XElement xSPoint in xSPoints) { |
|
|
|
CodeCoverageSequencePoint sp = new CodeCoverageSequencePoint(); |
|
|
|
var sp = new CodeCoverageSequencePoint(); |
|
|
|
sp.FileID = this.FileID; |
|
|
|
|
|
|
|
sp.Document = this.FileName; |
|
|
|
|
|
|
|
sp.Line = (int)GetDecimalAttributeValue(xSPoint.Attribute("sl")); |
|
|
|
sp.Line = (int)GetDecimalAttributeValue(xSPoint.Attribute("sl")); |
|
|
|
sp.EndLine = (int)GetDecimalAttributeValue(xSPoint.Attribute("el")); |
|
|
|
sp.EndLine = (int)GetDecimalAttributeValue(xSPoint.Attribute("el")); |
|
|
|
sp.Column = (int)GetDecimalAttributeValue(xSPoint.Attribute("sc")); |
|
|
|
sp.Column = (int)GetDecimalAttributeValue(xSPoint.Attribute("sc")); |
|
|
|
sp.EndColumn = (int)GetDecimalAttributeValue(xSPoint.Attribute("ec")); |
|
|
|
sp.EndColumn = (int)GetDecimalAttributeValue(xSPoint.Attribute("ec")); |
|
|
|
sp.VisitCount = (int)GetDecimalAttributeValue(xSPoint.Attribute("vc")); |
|
|
|
sp.VisitCount = (int)GetDecimalAttributeValue(xSPoint.Attribute("vc")); |
|
|
|
sp.Offset = (int)GetDecimalAttributeValue(xSPoint.Attribute("offset")); |
|
|
|
sp.Offset = (int)GetDecimalAttributeValue(xSPoint.Attribute("offset")); |
|
|
|
sp.BranchCoverage = true; |
|
|
|
sp.BranchExitsCount = (int)GetDecimalAttributeValue(xSPoint.Attribute("bec")); |
|
|
|
|
|
|
|
sp.BranchExitsVisit = (int)GetDecimalAttributeValue(xSPoint.Attribute("bev")); |
|
|
|
|
|
|
|
sp.FileID = xSPoint.Attribute("fileid") != null? xSPoint.Attribute("fileid").Value : "0"; |
|
|
|
|
|
|
|
if (sp.FileID == "0") { |
|
|
|
|
|
|
|
// SequencePoint from not covered (not runnable) file
|
|
|
|
|
|
|
|
// ie: interface with CodeContractClass/CodeContractClassFor
|
|
|
|
|
|
|
|
sp.Document = xSPoint.Attribute("fileid") != null? xSPoint.Attribute("fileid").Value : ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (sp.FileID == this.FileID) { |
|
|
|
|
|
|
|
// This method SequencePoint (from this.FileName)
|
|
|
|
|
|
|
|
sp.Document = this.FileName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
// SequencePoint from another method/file
|
|
|
|
|
|
|
|
// ie: ccrewriten CodeContractClass/CodeContractClassFor
|
|
|
|
|
|
|
|
// [or dependency-injected or fody-weaved???]
|
|
|
|
|
|
|
|
sp.Document = results.GetFileName(sp.FileID); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sp.BranchCoverage = (sp.BranchExitsCount == sp.BranchExitsVisit); |
|
|
|
sp.Content = String.Empty; |
|
|
|
sp.Content = String.Empty; |
|
|
|
sp.Length = 0; |
|
|
|
sp.Length = 0; |
|
|
|
|
|
|
|
|
|
|
@ -148,114 +184,81 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
|
|
|
|
|
|
|
|
void GetSequencePointsContent() |
|
|
|
void GetSequencePointsContent() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (cacheFileName == this.FileName && cacheDocument != null) { |
|
|
|
|
|
|
|
foreach (var sp in this.SequencePoints) { |
|
|
|
foreach (var sp in this.SequencePoints) { |
|
|
|
GetSequencePointContent(sp); |
|
|
|
GetSequencePointContent(sp); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GetSequencePointContent(CodeCoverageSequencePoint sp) |
|
|
|
void GetSequencePointContent(CodeCoverageSequencePoint sp) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// ccrewrite will cause lots of invalid calls to GetText()!
|
|
|
|
if (cacheFileName == sp.Document) { |
|
|
|
if (cacheFileName == sp.Document && cacheDocument != null) { |
|
|
|
// check primary cache (this.Filename)
|
|
|
|
sp.Content = cacheDocument.GetText(sp); // never returns null
|
|
|
|
sp.Content = cacheDocument == null? "" : cacheDocument.GetText(sp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
// check & update secondary cache
|
|
|
|
|
|
|
|
if (cache2FileName == sp.Document) { |
|
|
|
|
|
|
|
sp.Content = cache2Document == null? "" : cache2Document.GetText(sp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
cache2FileName = sp.Document; |
|
|
|
|
|
|
|
cache2Document = GetSource (cache2FileName); |
|
|
|
|
|
|
|
sp.Content = cache2Document == null? "" : cache2Document.GetText(sp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
if (sp.Content != String.Empty) { |
|
|
|
if (sp.Content != String.Empty) { |
|
|
|
if (sp.Line != sp.EndLine) { |
|
|
|
if (sp.Line != sp.EndLine) { |
|
|
|
// merge lines to single line
|
|
|
|
// merge multiple lines to single line
|
|
|
|
sp.Content = Regex.Replace(sp.Content, @"\s+", " "); |
|
|
|
sp.Content = Regex.Replace(sp.Content, @"\s+", " "); |
|
|
|
} |
|
|
|
} |
|
|
|
// SequencePoint.Length counts all but whitespace
|
|
|
|
// SequencePoint.Length counts all but whitespace
|
|
|
|
sp.Length = Regex.Replace(sp.Content, @"\s", "").Length; |
|
|
|
sp.Length = Regex.Replace(sp.Content, @"\s", "").Length; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Find method-body start SequencePoint "{" (sp.Content required)
|
|
|
|
// Find method-body first SequencePoint
|
|
|
|
// Sequence points expected to be ordered by Offset
|
|
|
|
// -> this method SP with lowest Line/Column
|
|
|
|
// Cannot just get first one because of ccrewrite&ContractClassFor
|
|
|
|
|
|
|
|
void getBodyStartSP() { |
|
|
|
void getBodyStartSP() { |
|
|
|
bool startPointFound = false; |
|
|
|
if (this.SequencePoints.Count != 0) { |
|
|
|
CodeCoverageSequencePoint startSeqPoint = null; |
|
|
|
|
|
|
|
foreach (CodeCoverageSequencePoint sp in this.SequencePoints) { |
|
|
|
foreach (CodeCoverageSequencePoint sp in this.SequencePoints) { |
|
|
|
if ( sp.Content == "{") { |
|
|
|
if (sp.FileID != this.FileID) continue; |
|
|
|
if ( this.IsConstructor ) { |
|
|
|
if (this.BodyStartSP == null || (sp.Line < this.BodyStartSP.Line) || |
|
|
|
// take previous/last one if not null
|
|
|
|
(sp.Line == this.BodyStartSP.Line && sp.Column < this.BodyStartSP.Column) |
|
|
|
startSeqPoint = startSeqPoint?? sp; |
|
|
|
) { |
|
|
|
} |
|
|
|
this.BodyStartSP = sp; |
|
|
|
else { |
|
|
|
|
|
|
|
startSeqPoint = sp; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
startPointFound = true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
startSeqPoint = sp; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
this.BodyStartSP = startPointFound? startSeqPoint: null; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Find method-body final SequencePoint "}" (sp.Content required)
|
|
|
|
// Find method-body last SequencePoint
|
|
|
|
// Sequence points expected to be ordered by Offset
|
|
|
|
// -> this method SP.Content=="}" with highest Line/Column
|
|
|
|
|
|
|
|
// and lowest Offset (when duplicated bw ccrewrite)
|
|
|
|
void getBodyFinalSP() { |
|
|
|
void getBodyFinalSP() { |
|
|
|
CodeCoverageSequencePoint finalSeqPoint = null; |
|
|
|
if (this.SequencePoints.Count != 0) { |
|
|
|
foreach (CodeCoverageSequencePoint sp in ((IEnumerable<CodeCoverageSequencePoint>)this.SequencePoints).Reverse()) { |
|
|
|
for (int i = this.SequencePoints.Count-1; i > 0; i--) { |
|
|
|
if ( sp.Content == "}") { |
|
|
|
var sp = this.SequencePoints[i]; |
|
|
|
if (finalSeqPoint == null) { |
|
|
|
if (sp.FileID != this.FileID) continue; |
|
|
|
finalSeqPoint = sp; |
|
|
|
if (sp.Content != "}") continue; |
|
|
|
} |
|
|
|
if (this.BodyFinalSP == null || (sp.Line > this.BodyFinalSP.Line) || |
|
|
|
// check for ccrewrite duplicate
|
|
|
|
(sp.Line == this.BodyFinalSP.Line && sp.Column >= this.BodyFinalSP.Column) |
|
|
|
else if (sp.Line == finalSeqPoint.Line && |
|
|
|
|
|
|
|
sp.Column == finalSeqPoint.Column && |
|
|
|
|
|
|
|
sp.EndLine == finalSeqPoint.EndLine && |
|
|
|
|
|
|
|
sp.EndColumn == finalSeqPoint.EndColumn && |
|
|
|
|
|
|
|
sp.Offset < finalSeqPoint.Offset) { |
|
|
|
|
|
|
|
finalSeqPoint = sp; |
|
|
|
|
|
|
|
// duplicate found, so far no reason to expect "triplicate" :)
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.BodyFinalSP = finalSeqPoint; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FilterSequencePoints() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.SequencePoints.Count != 0 && |
|
|
|
|
|
|
|
this.BodyStartSP != null && |
|
|
|
|
|
|
|
this.BodyFinalSP != null ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// After ccrewrite ContractClass/ContractClassFor
|
|
|
|
|
|
|
|
// sequence point(s) from another file/class/method
|
|
|
|
|
|
|
|
// is inserted into this method sequence points
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// To remove alien sequence points, all sequence points on lines
|
|
|
|
|
|
|
|
// before method signature and after end-brackets xxx{} are removed
|
|
|
|
|
|
|
|
// If ContractClassFor is in another file but interleaves this method lines
|
|
|
|
|
|
|
|
// then, afaik, not much can be done to remove inserted alien SP's
|
|
|
|
|
|
|
|
var selected = new List<CodeCoverageSequencePoint>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var point in this.SequencePoints) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if Content.Length is 0, GetText() is failed by ccrewrite inserted invalid SequencePoint
|
|
|
|
|
|
|
|
if (point.Content.Length != 0 |
|
|
|
|
|
|
|
&& (point.Line > BodyStartSP.Line || (point.Line == BodyStartSP.Line && point.Column >= BodyStartSP.Column)) |
|
|
|
|
|
|
|
&& (point.Line < BodyFinalSP.Line || (point.Line == BodyFinalSP.Line && point.Column < BodyFinalSP.Column)) |
|
|
|
|
|
|
|
) { |
|
|
|
) { |
|
|
|
selected.Add (point); |
|
|
|
// ccrewrite ContractClass/ContractClassFor
|
|
|
|
} |
|
|
|
// adds duplicate method end-sequence-point "}"
|
|
|
|
// After ccrewrite ContractClass/ContractClassFor
|
|
|
|
|
|
|
|
// duplicate method end-sequence-point "}" is added
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Add only first finalSP (can be a duplicate)
|
|
|
|
// Take duplicate BodyFinalSP with lower Offset
|
|
|
|
// Note: IL.Offset of second duplicate finalSP will
|
|
|
|
// Because IL.Offset of second duplicate
|
|
|
|
// extend branch coverage outside method-end "}",
|
|
|
|
// will extend branch coverage of this method
|
|
|
|
// and that can lead to wrong branch coverage display!
|
|
|
|
// by coverage of ContractClassFor inserted SequencePoint!
|
|
|
|
if (object.ReferenceEquals (point, this.BodyFinalSP)) { |
|
|
|
if (this.BodyFinalSP != null && |
|
|
|
selected.Add (point); |
|
|
|
sp.Line == this.BodyFinalSP.Line && |
|
|
|
|
|
|
|
sp.Column == this.BodyFinalSP.Column && |
|
|
|
|
|
|
|
sp.Offset < this.BodyFinalSP.Offset) { |
|
|
|
|
|
|
|
this.SequencePoints.Remove(this.BodyFinalSP); // remove duplicate
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.BodyFinalSP = sp; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.SequencePoints = selected; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -270,82 +273,35 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void GetBranchPoints() { |
|
|
|
|
|
|
|
// get all BranchPoints
|
|
|
|
|
|
|
|
var xBPoints = this.element |
|
|
|
|
|
|
|
.Elements("BranchPoints") |
|
|
|
|
|
|
|
.Elements("BranchPoint"); |
|
|
|
|
|
|
|
foreach (XElement xBPoint in xBPoints) { |
|
|
|
|
|
|
|
CodeCoverageBranchPoint bp = new CodeCoverageBranchPoint(); |
|
|
|
|
|
|
|
bp.VisitCount = (int)GetDecimalAttributeValue(xBPoint.Attribute("vc")); |
|
|
|
|
|
|
|
bp.Offset = (int)GetDecimalAttributeValue(xBPoint.Attribute("offset")); |
|
|
|
|
|
|
|
bp.Path = (int)GetDecimalAttributeValue(xBPoint.Attribute("path")); |
|
|
|
|
|
|
|
bp.OffsetEnd = (int)GetDecimalAttributeValue(xBPoint.Attribute("offsetend")); |
|
|
|
|
|
|
|
this.BranchPoints.Add(bp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GetBranchRatio () { |
|
|
|
void GetBranchRatio () { |
|
|
|
|
|
|
|
|
|
|
|
// goal: Get branch ratio, merge branch-exits and exclude (rewriten) Code Contracts branches
|
|
|
|
|
|
|
|
this.BranchCoverageRatio = null; |
|
|
|
this.BranchCoverageRatio = null; |
|
|
|
|
|
|
|
|
|
|
|
if ( this.BranchPoints == null |
|
|
|
Debug.Assert (this.SequencePoints != null); |
|
|
|
|| this.BranchPoints.Count == 0 |
|
|
|
if ( this.SequencePoints.Count == 0 ) return; |
|
|
|
|| this.SequencePoints == null |
|
|
|
|
|
|
|
|| this.SequencePoints.Count == 0 |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This sequence point offset is used to skip CCRewrite(n) BranchPoint's (Requires)
|
|
|
|
// This sequence point offset is used to skip CCRewrite(n) BranchPoint's (Requires)
|
|
|
|
// and '{' branches at static methods
|
|
|
|
// and '{' branches at static methods
|
|
|
|
if (this.BodyStartSP == null) { return; } // empty body
|
|
|
|
if (this.BodyStartSP == null) return; // empty body
|
|
|
|
|
|
|
|
|
|
|
|
// This sequence point offset is used to skip CCRewrite(n) BranchPoint's (Ensures)
|
|
|
|
// This sequence point offset is used to skip CCRewrite(n) BranchPoint's (Ensures)
|
|
|
|
if (this.BodyFinalSP == null) { return; } // empty body
|
|
|
|
if (this.BodyFinalSP == null) return; // empty body
|
|
|
|
|
|
|
|
|
|
|
|
// Connect Sequence & Branches
|
|
|
|
|
|
|
|
IEnumerator<CodeCoverageSequencePoint> SPEnumerator = this.SequencePoints.GetEnumerator(); |
|
|
|
|
|
|
|
CodeCoverageSequencePoint currSeqPoint = this.BodyStartSP; |
|
|
|
|
|
|
|
int nextSeqPointOffset = BodyStartSP.Offset; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var bp in this.BranchPoints) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ignore branches outside of method body offset range
|
|
|
|
|
|
|
|
if (bp.Offset < BodyStartSP.Offset) |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
if (bp.Offset > BodyFinalSP.Offset) |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Sync with SequencePoint
|
|
|
|
|
|
|
|
while ( nextSeqPointOffset < bp.Offset ) { |
|
|
|
|
|
|
|
currSeqPoint = SPEnumerator.Current; |
|
|
|
|
|
|
|
if ( SPEnumerator.MoveNext() ) { |
|
|
|
|
|
|
|
nextSeqPointOffset = SPEnumerator.Current.Offset; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
nextSeqPointOffset = int.MaxValue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (currSeqPoint.Branches == null) { |
|
|
|
|
|
|
|
currSeqPoint.Branches = new List<CodeCoverageBranchPoint>(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Add Branch to Branches
|
|
|
|
|
|
|
|
currSeqPoint.Branches.Add(bp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Merge sp.Branches on exit-offset
|
|
|
|
|
|
|
|
// Calculate Method Branch coverage
|
|
|
|
// Calculate Method Branch coverage
|
|
|
|
int totalBranchVisit = 0; |
|
|
|
int totalBranchVisit = 0; |
|
|
|
int totalBranchCount = 0; |
|
|
|
int totalBranchCount = 0; |
|
|
|
int pointBranchVisit = 0; |
|
|
|
|
|
|
|
int pointBranchCount = 0; |
|
|
|
|
|
|
|
Dictionary<int, CodeCoverageBranchPoint> bpExits = new Dictionary<int, CodeCoverageBranchPoint>(); |
|
|
|
|
|
|
|
foreach (var sp in this.SequencePoints) { |
|
|
|
foreach (var sp in this.SequencePoints) { |
|
|
|
|
|
|
|
|
|
|
|
// SequencePoint covered & has branches?
|
|
|
|
// SequencePoint is visited and belongs to this method?
|
|
|
|
if (sp.VisitCount != 0 && sp.Branches != null) { |
|
|
|
if (sp.VisitCount != 0 && sp.FileID == this.FileID) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Don't want branch coverage of ccrewrite(n)
|
|
|
|
|
|
|
|
// SequencePoint's with offset before and after method body
|
|
|
|
|
|
|
|
if (sp.Offset < BodyStartSP.Offset || |
|
|
|
|
|
|
|
sp.Offset > BodyFinalSP.Offset) { |
|
|
|
|
|
|
|
sp.BranchCoverage = true; |
|
|
|
|
|
|
|
continue; // skip
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 1) Generated "in" code for IEnumerables contains hidden "try/catch/finally" branches that
|
|
|
|
// 1) Generated "in" code for IEnumerables contains hidden "try/catch/finally" branches that
|
|
|
|
// one do not want or cannot cover by test-case because is handled earlier at same method.
|
|
|
|
// one do not want or cannot cover by test-case because is handled earlier at same method.
|
|
|
@ -354,44 +310,23 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
// ie: static methods start sequence point "{" contains compiler generated branches
|
|
|
|
// ie: static methods start sequence point "{" contains compiler generated branches
|
|
|
|
// 3) Exclude Contract class (EnsuresOnThrow/Assert/Assume is inside method body)
|
|
|
|
// 3) Exclude Contract class (EnsuresOnThrow/Assert/Assume is inside method body)
|
|
|
|
// 4) Exclude NUnit Assert(.Throws) class
|
|
|
|
// 4) Exclude NUnit Assert(.Throws) class
|
|
|
|
|
|
|
|
const string assert = "Assert"; |
|
|
|
|
|
|
|
const string contract = "Contract"; |
|
|
|
if (sp.Content == "in" || sp.Content == "{" || sp.Content == "}" || |
|
|
|
if (sp.Content == "in" || sp.Content == "{" || sp.Content == "}" || |
|
|
|
sp.Content.StartsWith("Assert.") || |
|
|
|
sp.Content.StartsWith(assert + ".", StringComparison.Ordinal) || |
|
|
|
sp.Content.StartsWith("Assert ") || |
|
|
|
sp.Content.StartsWith(assert + " ", StringComparison.Ordinal) || |
|
|
|
sp.Content.StartsWith("Contract.") || |
|
|
|
sp.Content.StartsWith(contract + ".", StringComparison.Ordinal) || |
|
|
|
sp.Content.StartsWith("Contract ") |
|
|
|
sp.Content.StartsWith(contract + " ", StringComparison.Ordinal) |
|
|
|
) { |
|
|
|
) { |
|
|
|
sp.Branches = null; |
|
|
|
sp.BranchCoverage = true; |
|
|
|
continue; // skip
|
|
|
|
continue; // skip
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Merge sp.Branches on OffsetEnd using bpExits key
|
|
|
|
totalBranchCount += sp.BranchExitsCount; |
|
|
|
bpExits.Clear(); |
|
|
|
totalBranchVisit += sp.BranchExitsVisit; |
|
|
|
foreach (var bp in sp.Branches) { |
|
|
|
|
|
|
|
if (!bpExits.ContainsKey(bp.OffsetEnd)) { |
|
|
|
|
|
|
|
bpExits[bp.OffsetEnd] = bp; // insert branch
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
bpExits[bp.OffsetEnd].VisitCount += bp.VisitCount; // update branch
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Compute branch coverage
|
|
|
|
|
|
|
|
pointBranchVisit = 0; |
|
|
|
|
|
|
|
pointBranchCount = 0; |
|
|
|
|
|
|
|
foreach (var bp in bpExits.Values) { |
|
|
|
|
|
|
|
pointBranchVisit += bp.VisitCount == 0? 0 : 1 ; |
|
|
|
|
|
|
|
pointBranchCount += 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Not full coverage?
|
|
|
|
|
|
|
|
if (pointBranchVisit != pointBranchCount) { |
|
|
|
|
|
|
|
sp.BranchCoverage = false; // => part-covered
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
totalBranchVisit += pointBranchVisit; |
|
|
|
|
|
|
|
totalBranchCount += pointBranchCount; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (sp.Branches != null) |
|
|
|
|
|
|
|
sp.Branches = null; // release memory
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.BranchCoverageRatio = (totalBranchCount!=0) ? new Tuple<int,int>(totalBranchVisit,totalBranchCount) : null; |
|
|
|
this.BranchCoverageRatio = (totalBranchCount!=0) ? new Tuple<int,int>(totalBranchVisit,totalBranchCount) : null; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
@ -445,10 +380,7 @@ namespace ICSharpCode.CodeCoverage |
|
|
|
string GetMethodName() |
|
|
|
string GetMethodName() |
|
|
|
{ |
|
|
|
{ |
|
|
|
XElement nameElement = element.Element("Name"); |
|
|
|
XElement nameElement = element.Element("Name"); |
|
|
|
if (nameElement != null) { |
|
|
|
return nameElement != null ? GetMethodName(nameElement.Value) : String.Empty; |
|
|
|
return GetMethodName(nameElement.Value); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return String.Empty; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
string GetMethodName(string methodSignature) |
|
|
|
string GetMethodName(string methodSignature) |
|
|
|