This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 764
/
CodeCoverageMethod.cs
273 lines (242 loc) · 7.52 KB
/
CodeCoverageMethod.cs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using ICSharpCode.Core;
namespace ICSharpCode.CodeCoverage
{
public class CodeCoverageMethod
{
string name = String.Empty;
string className = String.Empty;
string fullClassName = String.Empty;
string classNamespace = String.Empty;
int methodBodySize;
MethodAttributes methodAttributes;
List<CodeCoverageSequencePoint> sequencePoints = new List<CodeCoverageSequencePoint>();
public CodeCoverageMethod(string name, string className)
: this(name, className, MethodAttributes.Public)
{
}
public CodeCoverageMethod(string name, string className, MethodAttributes methodAttributes)
{
this.name = name;
this.fullClassName = className;
this.methodAttributes = methodAttributes;
int index = fullClassName.LastIndexOf('.');
if (index > 0) {
this.classNamespace = fullClassName.Substring(0, index);
this.className = fullClassName.Substring(index + 1);
} else {
this.className = fullClassName;
}
}
public CodeCoverageMethod(string className, XElement reader)
: this(GetMethodName(reader), className, GetMethodAttributes(reader))
{
ReadMethodBodySize(reader);
XAttribute isGetter = reader.Attribute("isGetter");
XAttribute isSetter = reader.Attribute("isSetter");
if (isGetter != null && isSetter != null && IsPropertyMethodName()) {
try {
IsProperty = Convert.ToBoolean(isGetter.Value) || Convert.ToBoolean(isSetter.Value);
} catch (FormatException) {
IsProperty = false;
}
}
else {
IsProperty = false;
}
}
static string GetMethodName(XElement reader)
{
return reader.Element("Name").Value;
}
static MethodAttributes GetMethodAttributes(XElement reader)
{
return MethodAttributes.Public;
}
void ReadMethodBodySize(XElement reader)
{
//string bodySizeAsString = reader.GetAttribute("bodysize");
//if (bodySizeAsString != null) {
// methodBodySize = Int32.Parse(bodySizeAsString);
//}
}
/// <summary>
/// Returns true if the method is a getter or setter method for a property.
/// </summary>
public bool IsProperty { get; private set; }
bool IsSpecialMethodName()
{
return (methodAttributes & MethodAttributes.SpecialName) == MethodAttributes.SpecialName;
}
bool IsPropertyMethodName()
{
return name.Contains("::get_") || name.Contains("::set_");
}
public string Name {
get { return name; }
}
public string ClassName {
get { return className; }
}
/// <summary>
/// Returns the full class name including the namespace prefix.
/// </summary>
public string FullClassName {
get { return fullClassName; }
}
public string ClassNamespace {
get { return classNamespace; }
}
public string RootNamespace {
get { return GetRootNamespace(classNamespace); }
}
public static string GetRootNamespace(string ns)
{
int index = ns.IndexOf('.');
if (index > 0) {
return ns.Substring(0, index);
}
return ns;
}
public List<CodeCoverageSequencePoint> SequencePoints {
get { return sequencePoints; }
}
public int VisitedSequencePointsCount {
get {
int count = 0;
foreach (CodeCoverageSequencePoint sequencePoint in sequencePoints) {
if (sequencePoint.VisitCount > 0) {
count++;
}
}
return count;
}
}
public int NotVisitedSequencePointsCount {
get {
int count = 0;
foreach (CodeCoverageSequencePoint sequencePoint in sequencePoints) {
if (sequencePoint.VisitCount == 0) {
count++;
}
}
return count;
}
}
public int GetVisitedCodeLength()
{
int total = 0;
foreach (CodeCoverageSequencePoint sequencePoint in sequencePoints) {
if (sequencePoint.VisitCount > 0) {
total += sequencePoint.Length;
}
}
return total;
}
public int GetUnvisitedCodeLength()
{
if (sequencePoints.Count == 0) {
return methodBodySize;
}
int total = 0;
foreach (CodeCoverageSequencePoint sequencePoint in sequencePoints) {
if (sequencePoint.VisitCount == 0) {
total += sequencePoint.Length;
}
}
return total;
}
public List<CodeCoverageSequencePoint> GetSequencePoints(string fileName)
{
List<CodeCoverageSequencePoint> matchedSequencePoints = new List<CodeCoverageSequencePoint>();
foreach (CodeCoverageSequencePoint sequencePoint in sequencePoints) {
if (FileUtility.IsEqualFileName(fileName, sequencePoint.Document)) {
matchedSequencePoints.Add(sequencePoint);
}
}
return matchedSequencePoints;
}
/// <summary>
/// Gets the next namespace level given the parent namespace.
/// </summary>
public static string GetChildNamespace(string fullNamespace, string parentNamespace)
{
string end = fullNamespace.Substring(parentNamespace.Length + 1);
return GetRootNamespace(end);
}
/// <summary>
/// Adds the child namespace to the namespace prefix.
/// </summary>
public static string GetFullNamespace(string prefix, string name)
{
if (prefix.Length > 0) {
return String.Concat(prefix, ".", name);
}
return name;
}
/// <summary>
/// Gets all child namespaces that starts with the specified string.
/// </summary>
/// <remarks>
/// If the starts with string is 'ICSharpCode' and there is a code coverage
/// method with a namespace of 'ICSharpCode.XmlEditor.Tests', then this
/// method will return 'XmlEditor' as one of its strings.
/// </remarks>
public static List<string> GetChildNamespaces(List<CodeCoverageMethod> methods, string parentNamespace) {
List<string> items = new List<string>();
foreach (CodeCoverageMethod method in methods) {
string classNamespace = method.ClassNamespace;
string dottedParentNamespace = parentNamespace + ".";
if (classNamespace.Length > parentNamespace.Length && classNamespace.StartsWith(dottedParentNamespace)) {
string ns = CodeCoverageMethod.GetChildNamespace(method.ClassNamespace, parentNamespace);
if (!items.Contains(ns)) {
items.Add(ns);
}
}
}
return items;
}
/// <summary>
/// Gets all methods whose namespaces starts with the specified string.
/// </summary>
public static List<CodeCoverageMethod> GetAllMethods(List<CodeCoverageMethod> methods, string namespaceStartsWith)
{
List<CodeCoverageMethod> matchedMethods = new List<CodeCoverageMethod>();
foreach (CodeCoverageMethod method in methods) {
if (method.ClassNamespace.StartsWith(namespaceStartsWith)) {
matchedMethods.Add(method);
}
}
return matchedMethods;
}
/// <summary>
/// Gets only those methods for the specified class.
/// </summary>
public static List<CodeCoverageMethod> GetMethods(List<CodeCoverageMethod> methods, string ns, string className)
{
List<CodeCoverageMethod> matchedMethods = new List<CodeCoverageMethod>();
foreach (CodeCoverageMethod method in methods) {
if (method.ClassName == className && method.ClassNamespace == ns) {
matchedMethods.Add(method);
}
}
return matchedMethods;
}
public static List<string> GetClassNames(List<CodeCoverageMethod> methods, string ns)
{
List<string> names = new List<string>();
foreach (CodeCoverageMethod method in methods) {
if (method.ClassNamespace == ns && !names.Contains(method.ClassName)) {
names.Add(method.ClassName);
}
}
return names;
}
}
}