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

Commit 6880262

Browse files
author
Dragan
committed
CodeCoverage: Applied SD5 suggestions
1 parent 51fa8e6 commit 6880262

6 files changed

Lines changed: 48 additions & 71 deletions

File tree

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,17 @@
1919
using System;
2020
using System.Collections.Generic;
2121
using System.IO;
22-
using System.Text.RegularExpressions;
2322
using System.Windows.Forms;
2423
using System.Windows.Forms.Integration;
2524
using ICSharpCode.AvalonEdit;
2625
using ICSharpCode.AvalonEdit.AddIn;
27-
using ICSharpCode.AvalonEdit.Document;
2826
using ICSharpCode.AvalonEdit.Editing;
2927
using ICSharpCode.AvalonEdit.Highlighting;
3028
using ICSharpCode.Core;
3129
using ICSharpCode.Core.WinForms;
3230
using ICSharpCode.NRefactory;
3331
using ICSharpCode.SharpDevelop;
3432
using ICSharpCode.SharpDevelop.Editor;
35-
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
3633

3734
namespace ICSharpCode.CodeCoverage
3835
{
@@ -236,9 +233,9 @@ void UpdateListView(CodeCoverageTreeNode node)
236233
listView.BeginUpdate();
237234
try {
238235
listView.Items.Clear();
239-
CodeCoverageClassTreeNode classNode = node as CodeCoverageClassTreeNode;
240-
CodeCoverageMethodTreeNode methodNode = node as CodeCoverageMethodTreeNode;
241-
CodeCoveragePropertyTreeNode propertyNode = node as CodeCoveragePropertyTreeNode;
236+
var classNode = node as CodeCoverageClassTreeNode;
237+
var methodNode = node as CodeCoverageMethodTreeNode;
238+
var propertyNode = node as CodeCoveragePropertyTreeNode;
242239
if (classNode != null) {
243240
AddClassTreeNode(classNode);
244241
} else if (methodNode != null) {
@@ -253,9 +250,9 @@ void UpdateListView(CodeCoverageTreeNode node)
253250

254251
void UpdateTextEditor(CodeCoverageTreeNode node)
255252
{
256-
CodeCoverageClassTreeNode classNode = node as CodeCoverageClassTreeNode;
257-
CodeCoverageMethodTreeNode methodNode = node as CodeCoverageMethodTreeNode;
258-
CodeCoveragePropertyTreeNode propertyNode = node as CodeCoveragePropertyTreeNode;
253+
var classNode = node as CodeCoverageClassTreeNode;
254+
var methodNode = node as CodeCoverageMethodTreeNode;
255+
var propertyNode = node as CodeCoveragePropertyTreeNode;
259256
if (classNode != null && classNode.Nodes.Count > 0) {
260257
propertyNode = classNode.Nodes[0] as CodeCoveragePropertyTreeNode;
261258
methodNode = classNode.Nodes[0] as CodeCoverageMethodTreeNode;
@@ -280,8 +277,8 @@ void UpdateTextEditor(CodeCoverageTreeNode node)
280277
void AddClassTreeNode(CodeCoverageClassTreeNode node)
281278
{
282279
foreach (CodeCoverageTreeNode childNode in node.Nodes) {
283-
CodeCoverageMethodTreeNode method = childNode as CodeCoverageMethodTreeNode;
284-
CodeCoveragePropertyTreeNode property = childNode as CodeCoveragePropertyTreeNode;
280+
var method = childNode as CodeCoverageMethodTreeNode;
281+
var property = childNode as CodeCoveragePropertyTreeNode;
285282
if (method != null) {
286283
AddSequencePoints(method.Method);
287284
} else {
@@ -313,7 +310,7 @@ void AddSequencePoints(CodeCoverageMethod method)
313310

314311
void AddSequencePoint(CodeCoverageSequencePoint sequencePoint)
315312
{
316-
ListViewItem item = new ListViewItem(sequencePoint.VisitCount.ToString());
313+
var item = new ListViewItem(sequencePoint.VisitCount.ToString());
317314
item.SubItems.Add(sequencePoint.Line.ToString());
318315
item.SubItems.Add(sequencePoint.Column.ToString());
319316
item.SubItems.Add(sequencePoint.EndLine.ToString());
@@ -329,7 +326,7 @@ void AddSequencePoint(CodeCoverageSequencePoint sequencePoint)
329326
void ListViewItemActivate(object sender, EventArgs e)
330327
{
331328
if (listView.SelectedItems.Count > 0) {
332-
CodeCoverageSequencePoint sequencePoint = (CodeCoverageSequencePoint)listView.SelectedItems[0].Tag;
329+
var sequencePoint = (CodeCoverageSequencePoint)listView.SelectedItems[0].Tag;
333330
if (sequencePoint.Document.Length > 0) {
334331
FileService.JumpToFilePosition(sequencePoint.Document, sequencePoint.Line, sequencePoint.Column);
335332
}

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21-
using System.Windows.Media;
2221

2322
using ICSharpCode.NRefactory.Editor;
2423
using ICSharpCode.SharpDevelop;
@@ -50,7 +49,7 @@ public void AddMarker(IDocument document, CodeCoverageSequencePoint sequencePoin
5049
return;
5150
}
5251

53-
ITextMarkerService markerService = document.GetService(typeof(ITextMarkerService)) as ITextMarkerService;
52+
var markerService = document.GetService(typeof(ITextMarkerService)) as ITextMarkerService;
5453
if (markerService != null) {
5554
int startOffset = document.PositionToOffset(sequencePoint.Line, sequencePoint.Column);
5655
int endOffset = document.PositionToOffset(sequencePoint.EndLine, sequencePoint.EndColumn);
@@ -66,15 +65,15 @@ public void AddMarker(IDocument document, CodeCoverageSequencePoint sequencePoin
6665
/// </summary>
6766
public void RemoveMarkers(IDocument document)
6867
{
69-
ITextMarkerService markerService = document.GetService(typeof(ITextMarkerService)) as ITextMarkerService;
68+
var markerService = document.GetService(typeof(ITextMarkerService)) as ITextMarkerService;
7069
if (markerService != null) {
7170
markerService.RemoveAll(IsCodeCoverageTextMarker);
7271
}
7372
}
7473

7574
bool IsCodeCoverageTextMarker(ITextMarker marker)
7675
{
77-
Type type = marker.Tag as Type;
76+
var type = marker.Tag as Type;
7877
return type == typeof(CodeCoverageHighlighter);
7978
}
8079

@@ -109,21 +108,15 @@ bool IsValidSequencePoint(IDocument document, CodeCoverageSequencePoint sequence
109108
}
110109

111110
public static System.Drawing.Color GetSequencePointBackColor(CodeCoverageSequencePoint sequencePoint) {
112-
if (sequencePoint.VisitCount > 0) {
113-
if ( sequencePoint.BranchCoverage == true ) {
114-
return CodeCoverageOptions.VisitedColor;
115-
}
116-
return CodeCoverageOptions.PartVisitedColor;
111+
if (sequencePoint.VisitCount != 0) {
112+
return sequencePoint.BranchCoverage == true ? CodeCoverageOptions.VisitedColor : CodeCoverageOptions.PartVisitedColor;
117113
}
118114
return CodeCoverageOptions.NotVisitedColor;
119115
}
120116

121117
public static System.Drawing.Color GetSequencePointForeColor(CodeCoverageSequencePoint sequencePoint) {
122-
if (sequencePoint.VisitCount > 0) {
123-
if ( sequencePoint.BranchCoverage == true ) {
124-
return CodeCoverageOptions.VisitedForeColor;
125-
}
126-
return CodeCoverageOptions.PartVisitedForeColor;
118+
if (sequencePoint.VisitCount != 0) {
119+
return sequencePoint.BranchCoverage == true ? CodeCoverageOptions.VisitedForeColor : CodeCoverageOptions.PartVisitedForeColor;
127120
}
128121
return CodeCoverageOptions.NotVisitedForeColor;
129122
}

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21-
using System.Reflection;
22-
using System.Xml;
2321
using System.Xml.Linq;
2422
using ICSharpCode.Core;
2523

2624
namespace ICSharpCode.CodeCoverage
2725
{
2826
public class CodeCoverageMethod
2927
{
30-
string name = String.Empty;
31-
string className = String.Empty;
32-
string fullClassName = String.Empty;
33-
string classNamespace = String.Empty;
34-
List<CodeCoverageSequencePoint> sequencePoints = new List<CodeCoverageSequencePoint>();
28+
readonly string name = String.Empty;
29+
readonly string className = String.Empty;
30+
readonly string fullClassName = String.Empty;
31+
readonly string classNamespace = String.Empty;
32+
readonly List<CodeCoverageSequencePoint> sequencePoints = new List<CodeCoverageSequencePoint>();
3533

3634
public CodeCoverageMethod(string name, string className)
3735
{
@@ -111,10 +109,7 @@ public string RootNamespace {
111109
public static string GetRootNamespace(string ns)
112110
{
113111
int index = ns.IndexOf('.');
114-
if (index > 0) {
115-
return ns.Substring(0, index);
116-
}
117-
return ns;
112+
return index > 0 ? ns.Substring(0, index) : ns;
118113
}
119114

120115
public List<CodeCoverageSequencePoint> SequencePoints {
@@ -192,10 +187,7 @@ public static string GetChildNamespace(string fullNamespace, string parentNamesp
192187
/// </summary>
193188
public static string GetFullNamespace(string prefix, string name)
194189
{
195-
if (prefix.Length > 0) {
196-
return String.Concat(prefix, ".", name);
197-
}
198-
return name;
190+
return prefix.Length > 0 ? String.Concat(prefix, ".", name) : name;
199191
}
200192

201193
/// <summary>
@@ -207,7 +199,7 @@ public static string GetFullNamespace(string prefix, string name)
207199
/// method will return 'XmlEditor' as one of its strings.
208200
/// </remarks>
209201
public static List<string> GetChildNamespaces(List<CodeCoverageMethod> methods, string parentNamespace) {
210-
List<string> items = new List<string>();
202+
var items = new List<string>();
211203
foreach (CodeCoverageMethod method in methods) {
212204
string classNamespace = method.ClassNamespace;
213205
string dottedParentNamespace = parentNamespace + ".";
@@ -226,10 +218,10 @@ public static List<string> GetChildNamespaces(List<CodeCoverageMethod> methods,
226218
/// </summary>
227219
public static List<CodeCoverageMethod> GetAllMethods(List<CodeCoverageMethod> methods, string namespaceStartsWith)
228220
{
229-
List<CodeCoverageMethod> matchedMethods = new List<CodeCoverageMethod>();
221+
var matchedMethods = new List<CodeCoverageMethod>();
230222
namespaceStartsWith += ".";
231223
foreach (CodeCoverageMethod method in methods) {
232-
if ((method.ClassNamespace+".").StartsWith(namespaceStartsWith)) {
224+
if ((method.ClassNamespace + ".").StartsWith(namespaceStartsWith, StringComparison.Ordinal)) {
233225
matchedMethods.Add(method);
234226
}
235227
}
@@ -241,7 +233,7 @@ public static List<CodeCoverageMethod> GetAllMethods(List<CodeCoverageMethod> me
241233
/// </summary>
242234
public static List<CodeCoverageMethod> GetMethods(List<CodeCoverageMethod> methods, string ns, string className)
243235
{
244-
List<CodeCoverageMethod> matchedMethods = new List<CodeCoverageMethod>();
236+
var matchedMethods = new List<CodeCoverageMethod>();
245237
foreach (CodeCoverageMethod method in methods) {
246238
if (method.ClassName == className && method.ClassNamespace == ns) {
247239
matchedMethods.Add(method);
@@ -252,7 +244,7 @@ public static List<CodeCoverageMethod> GetMethods(List<CodeCoverageMethod> metho
252244

253245
public static List<string> GetClassNames(List<CodeCoverageMethod> methods, string ns)
254246
{
255-
List<string> names = new List<string>();
247+
var names = new List<string>();
256248
foreach (CodeCoverageMethod method in methods) {
257249
if (method.ClassNamespace == ns && !names.Contains(method.ClassName)) {
258250
names.Add(method.ClassName);

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ namespace ICSharpCode.CodeCoverage
2929
{
3030
public class CodeCoverageMethodElement
3131
{
32-
XElement element;
33-
CodeCoverageResults results;
32+
readonly XElement element;
33+
readonly CodeCoverageResults results;
3434

3535
/// <summary>Enables CodeCoverage.Test to compile</summary>
3636
/// <param name="element">XMLElement</param>
@@ -310,11 +310,13 @@ void GetBranchRatio () {
310310
// ie: static methods start sequence point "{" contains compiler generated branches
311311
// 3) Exclude Contract class (EnsuresOnThrow/Assert/Assume is inside method body)
312312
// 4) Exclude NUnit Assert(.Throws) class
313+
const string assert = "Assert";
314+
const string contract = "Contract";
313315
if (sp.Content == "in" || sp.Content == "{" || sp.Content == "}" ||
314-
sp.Content.StartsWith("Assert.") ||
315-
sp.Content.StartsWith("Assert ") ||
316-
sp.Content.StartsWith("Contract.") ||
317-
sp.Content.StartsWith("Contract ")
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)
318320
) {
319321
sp.BranchCoverage = true;
320322
continue; // skip
@@ -378,10 +380,7 @@ string GetFileRef() {
378380
string GetMethodName()
379381
{
380382
XElement nameElement = element.Element("Name");
381-
if (nameElement != null) {
382-
return GetMethodName(nameElement.Value);
383-
}
384-
return String.Empty;
383+
return nameElement != null ? GetMethodName(nameElement.Value) : String.Empty;
385384
}
386385

387386
string GetMethodName(string methodSignature)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace ICSharpCode.CodeCoverage
2323
{
2424
public class CodeCoverageModule : ICodeCoverageWithVisits
2525
{
26-
string name = String.Empty;
26+
readonly string name = String.Empty;
2727
List<CodeCoverageMethod> methods = new List<CodeCoverageMethod>();
2828
List<string> rootNamespaces;
2929

@@ -78,7 +78,7 @@ public decimal GetVisitedBranchCoverage() {
7878

7979
public List<CodeCoverageSequencePoint> GetSequencePoints(string fileName)
8080
{
81-
List<CodeCoverageSequencePoint> sequencePoints = new List<CodeCoverageSequencePoint>();
81+
var sequencePoints = new List<CodeCoverageSequencePoint>();
8282
foreach (CodeCoverageMethod method in methods) {
8383
sequencePoints.AddRange(method.GetSequencePoints(fileName));
8484
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ namespace ICSharpCode.CodeCoverage
2929
/// </summary>
3030
public class CodeCoverageResults
3131
{
32-
List<CodeCoverageModule> modules = new List<CodeCoverageModule>();
33-
Dictionary<string, string> fileNames = new Dictionary<string, string>();
34-
Dictionary<string, string> assemblies = new Dictionary<string, string>();
32+
readonly List<CodeCoverageModule> modules = new List<CodeCoverageModule>();
33+
readonly Dictionary<string, string> fileNames = new Dictionary<string, string>();
34+
readonly Dictionary<string, string> assemblies = new Dictionary<string, string>();
3535

3636
public CodeCoverageResults(string fileName)
3737
: this(new StreamReader(fileName, true))
@@ -50,7 +50,7 @@ public CodeCoverageResults(TextReader reader)
5050

5151
public List<CodeCoverageSequencePoint> GetSequencePoints(string fileName)
5252
{
53-
List<CodeCoverageSequencePoint> sequencePoints = new List<CodeCoverageSequencePoint>();
53+
var sequencePoints = new List<CodeCoverageSequencePoint>();
5454
foreach (CodeCoverageModule module in modules) {
5555
sequencePoints.AddRange(module.GetSequencePoints(fileName));
5656
}
@@ -63,7 +63,7 @@ public List<CodeCoverageModule> Modules {
6363

6464
void ReadResults(XContainer reader)
6565
{
66-
IEnumerable<XElement> modules = reader.Descendants("Module").Where(m => m.Attribute("skippedDueTo") == null);
66+
var modules = reader.Descendants("Module").Where(m => m.Attribute("skippedDueTo") == null);
6767
foreach (XElement file in reader.Descendants("File")) {
6868
AddFileName(file);
6969
}
@@ -79,7 +79,6 @@ private void RegisterAssembly(XElement assembly)
7979
assembly.Elements("Classes").Elements("Class").Where(
8080
c =>
8181
!c.Element("FullName").Value.Contains("__") &&
82-
//!c.Element("FullName").Value.Contains("<") &&
8382
c.Attribute("skippedDueTo") == null).Select(
8483
c => c.Element("FullName").Value).Distinct().OrderBy(name => name);
8584
foreach (string className in classNames) {
@@ -130,7 +129,7 @@ string GetAssemblyName(XElement reader)
130129
/// </summary>
131130
CodeCoverageMethod AddMethod(CodeCoverageModule module, string className, XElement reader)
132131
{
133-
CodeCoverageMethod method = new CodeCoverageMethod(className, reader, this);
132+
var method = new CodeCoverageMethod(className, reader, this);
134133
if (!method.Name.Contains("__")) {
135134
module.Methods.Add(method);
136135
}
@@ -163,12 +162,9 @@ string GetAssembly(string id)
163162
return GetDictionaryValue(assemblies, id);
164163
}
165164

166-
string GetDictionaryValue(Dictionary<string, string> dictionary, string key)
165+
string GetDictionaryValue(IReadOnlyDictionary<string, string> dictionary, string key)
167166
{
168-
if (dictionary.ContainsKey(key)) {
169-
return dictionary[key];
170-
}
171-
return String.Empty;
167+
return dictionary.ContainsKey(key) ? dictionary[key] : String.Empty;
172168
}
173169

174170
/// <summary>

0 commit comments

Comments
 (0)