Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 164af14

Browse files
author
Dragan
committed
CodeCoverage: Apply C# filter only to .cs files
1 parent 9600529 commit 164af14

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

src/AddIns/Analysis/CodeCoverage/Project/Src/CodeCoverageMethodElement.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public CodeCoverageMethodElement(XElement element, CodeCoverageResults results)
5858

5959
public string FileID { get; private set; }
6060
public string FileName { get; private set; }
61+
public string FileNameExt { get; private set; }
6162
public bool IsVisited { get; private set; }
6263
public int CyclomaticComplexity { get; private set; }
6364
public decimal SequenceCoverage { get; private set; }
@@ -86,9 +87,14 @@ void Init()
8687

8788
this.FileID = GetFileRef();
8889
this.FileName = String.Empty;
90+
this.FileNameExt = String.Empty;
8991
if (!String.IsNullOrEmpty(this.FileID)) {
9092
if (results != null) {
9193
this.FileName = results.GetFileName(this.FileID);
94+
try {
95+
this.FileNameExt = Path.GetExtension(this.FileName);
96+
}
97+
catch {}
9298
if (cacheFileName != this.FileName) {
9399
cacheFileName = this.FileName;
94100
cacheDocument = GetSource (cacheFileName);
@@ -303,23 +309,26 @@ void GetBranchRatio () {
303309
continue; // skip
304310
}
305311

306-
// 1) Generated "in" code for IEnumerables contains hidden "try/catch/finally" branches that
307-
// one do not want or cannot cover by test-case because is handled earlier at same method.
308-
// ie: NullReferenceException in foreach loop is pre-handled at method entry, ie. by Contract.Require(items!=null)
309-
// 2) Branches within sequence points "{" and "}" are not source branches but compiler generated branches
310-
// ie: static methods start sequence point "{" contains compiler generated branches
311-
// 3) Exclude Contract class (EnsuresOnThrow/Assert/Assume is inside method body)
312-
// 4) Exclude NUnit Assert(.Throws) class
313-
const string assert = "Assert";
314-
const string contract = "Contract";
315-
if (sp.Content == "in" || sp.Content == "{" || sp.Content == "}" ||
316-
sp.Content.StartsWith(assert + ".", StringComparison.Ordinal) ||
317-
sp.Content.StartsWith(assert + " ", StringComparison.Ordinal) ||
318-
sp.Content.StartsWith(contract + ".", StringComparison.Ordinal) ||
319-
sp.Content.StartsWith(contract + " ", StringComparison.Ordinal)
320-
) {
321-
sp.BranchCoverage = true;
322-
continue; // skip
312+
if (this.FileNameExt == ".cs") {
313+
// 0) Only for C#
314+
// 1) Generated "in" code for IEnumerables contains hidden "try/catch/finally" branches that
315+
// one do not want or cannot cover by test-case because is handled earlier at same method.
316+
// ie: NullReferenceException in foreach loop is pre-handled at method entry, ie. by Contract.Require(items!=null)
317+
// 2) Branches within sequence points "{" and "}" are not source branches but compiler generated branches
318+
// ie: static methods start sequence point "{" contains compiler generated branches
319+
// 3) Exclude Contract class (EnsuresOnThrow/Assert/Assume is inside method body)
320+
// 4) Exclude NUnit Assert(.Throws) class
321+
const string assert = "Assert";
322+
const string contract = "Contract";
323+
if (sp.Content == "in" || sp.Content == "{" || sp.Content == "}" ||
324+
sp.Content.StartsWith(assert + ".", StringComparison.Ordinal) ||
325+
sp.Content.StartsWith(assert + " ", StringComparison.Ordinal) ||
326+
sp.Content.StartsWith(contract + ".", StringComparison.Ordinal) ||
327+
sp.Content.StartsWith(contract + " ", StringComparison.Ordinal)
328+
) {
329+
sp.BranchCoverage = true;
330+
continue; // skip
331+
}
323332
}
324333

325334
totalBranchCount += sp.BranchExitsCount;

0 commit comments

Comments
 (0)