forked from DelphiCodeCoverage/DelphiCodeCoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJacocoCoverageFileUnit.pas
More file actions
350 lines (286 loc) · 10.1 KB
/
Copy pathJacocoCoverageFileUnit.pas
File metadata and controls
350 lines (286 loc) · 10.1 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
(***********************************************************************)
(* Delphi Code Coverage *)
(* *)
(* A quick hack of a Code Coverage Tool for Delphi *)
(* by Christer Fahlgren and Nick Ring *)
(* *)
(* This Source Code Form is subject to the terms of the Mozilla Public *)
(* License, v. 2.0. If a copy of the MPL was not distributed with this *)
(* file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
unit JacocoCoverageFileUnit;
interface
uses
I_Report,
I_CoverageStats,
JclSimpleXml,
I_CoverageConfiguration,
ClassInfoUnit,
I_LogManager;
type
TJacocoCoverageReport = class(TInterfacedObject, IReport)
strict private
FCoverageConfiguration: ICoverageConfiguration;
procedure AddModuleInfo(
AAllElement: TJclSimpleXMLElem;
const AModuleInfo: TModuleInfo);
procedure AddClassInfo(
ASourceFileElement: TJclSimpleXMLElem;
const AClassInfo: TClassInfo);
procedure AddClassStats(
const ARootElement: TJclSimpleXMLElem;
const AClass: TClassInfo);
procedure AddMethodInfo(
AClassElement: TJclSimpleXMLElem;
const AMethod: TProcedureInfo);
procedure AddMethodStats(
const ARootElement: TJclSimpleXMLElem;
const AMethod: TProcedureInfo);
procedure AddSourceStats(
const ARootElement: TJclSimpleXMLElem;
const AModule: TModuleInfo);
procedure AddCoverageElement(const RootElement: TJclSimpleXMLElem;
const AType: string; const TotalCoveredCount, TotalUncoveredCount: Integer);
public
constructor Create(const ACoverageConfiguration: ICoverageConfiguration);
procedure Generate(
const ACoverage: ICoverageStats;
const AModuleInfoList: TModuleList;
const ALogManager: ILogManager);
end;
TJacocoCoverageReportMerger = class helper for TJacocoCoverageReport
class function MergeCoverageStatsForGenerics(const ACoverageStatsIn: ICoverageStats): ICoverageStats;
end;
implementation
uses
System.DateUtils,
System.StrUtils,
System.SysUtils,
System.Math,
JclFileUtils,
CoverageStats;
constructor TJacocoCoverageReport.Create(
const ACoverageConfiguration: ICoverageConfiguration);
begin
inherited Create;
FCoverageConfiguration := ACoverageConfiguration;
end;
procedure TJacocoCoverageReport.Generate(
const ACoverage: ICoverageStats;
const AModuleInfoList: TModuleList;
const ALogManager: ILogManager);
var
StatsElement: TJclSimpleXMLElem;
procedure AddValueElement(const AElementName: string; const AValue: Integer);
begin
StatsElement.Items
.Add(AElementName)
.Properties.Add('value', AValue);
end;
procedure AddElement(AElement: TJclSimpleXMLElem; const APropertyName: string; const AValue: Integer); overload;
begin
AElement.Properties.Add(APropertyName, AValue);
end;
procedure AddElement(AElement: TJclSimpleXMLElem; const APropertyName: string; const AValue: String); overload;
begin
AElement.Properties.Add(APropertyName, AValue);
end;
var
Uid: TGuid;
Result: HResult;
ModuleInfo: TModuleInfo;
XML: TJclSimpleXML;
SessionElement: TJclSimpleXMLElem;
begin
ALogManager.Log('Generating jacoco xml report');
XML := TJclSimpleXML.Create;
try
// Prolog doesn't seem to get written properly (with carriage returns)
XML.Prolog.AddDocType('report PUBLIC "-//JACOCO//DTD Report 1.0//EN" "report.dtd"');
XML.Prolog.Standalone := true;
XML.Root.Name := 'report';
AddElement(XML.Root, 'name', 'debug'); // For now
SessionElement := XML.Root.Items.Add('session');
Result := CreateGuid(Uid);
if Result = S_OK then
SessionElement.Properties.Add('id', GuidToString(Uid)); { TODO: Not sure of the format }
SessionElement.Properties.Add('start', DateTimeToUnix(now)); { TODO: Should be a start time }
SessionElement.Properties.Add('dump', DateTimeToUnix(now));
for ModuleInfo in AModuleInfoList do
begin
AddModuleInfo(XML.Root, ModuleInfo);
end;
XML.SaveToFile(
PathAppend(FCoverageConfiguration.OutputDir, 'jacoco.xml')
);
finally
XML.Free;
end;
end;
procedure TJacocoCoverageReport.AddModuleInfo(
AAllElement: TJclSimpleXMLElem;
const AModuleInfo: TModuleInfo);
var
PackageElement: TJclSimpleXMLElem;
SourceFileElement: TJclSimpleXMLElem;
ClassInfo: TClassInfo;
begin
PackageElement := AAllElement.Items.Add('package');
PackageElement.Properties.Add('name', AModuleInfo.ModuleName.Replace('.','/'));
for ClassInfo in AModuleInfo do
begin
AddClassInfo(PackageElement, ClassInfo);
end;
SourceFileElement := PackageElement.Items.Add('sourcefile');
SourceFileElement.Properties.Add('name', AModuleInfo.ModuleFileName);
{ TODO: Lines }
AddSourceStats(SourceFileElement, AModuleInfo);
end;
procedure TJacocoCoverageReport.AddSourceStats(
const ARootElement: TJclSimpleXMLElem; const AModule: TModuleInfo);
begin
AddCoverageElement(ARootElement,
'LINE',
AModule.CoveredLineCount,
AModule.LineCount - AModule.CoveredLineCount);
AddCoverageElement(ARootElement,
'METHOD',
AModule.CoveredMethodCount,
AModule.MethodCount - AModule.CoveredMethodCount);
AddCoverageElement(ARootElement,
'CLASS',
AModule.CoveredClassCount,
AModule.ClassCount - AModule.CoveredClassCount);
end;
procedure TJacocoCoverageReport.AddClassInfo(
ASourceFileElement: TJclSimpleXMLElem;
const AClassInfo: TClassInfo);
var
Method: TProcedureInfo;
ClassElement: TJclSimpleXMLElem;
begin
ClassElement := ASourceFileElement.Items.Add('class');
{ TODO: Check whether this is enough }
ClassElement.Properties.Add('name', AClassInfo.Module.Replace('.','/') + '/' + AClassInfo.TheClassName);
for Method in AClassInfo do
AddMethodInfo(ClassElement, Method);
AddClassStats(ClassElement, AClassInfo);
end;
procedure TJacocoCoverageReport.AddClassStats(
const ARootElement: TJclSimpleXMLElem;
const AClass: TClassInfo);
begin
AddCoverageElement(ARootElement,
'LINE',
AClass.CoveredLineCount,
AClass.LineCount - AClass.CoveredLineCount);
AddCoverageElement(ARootElement,
'METHOD',
AClass.CoveredProcedureCount,
AClass.ProcedureCount - AClass.CoveredProcedureCount);
// AddCoverageElement(ARootElement,
// 'CLASS',
// AClass.,
// 100 - AClass.PercentCovered);
end;
procedure TJacocoCoverageReport.AddMethodInfo(
AClassElement: TJclSimpleXMLElem;
const AMethod: TProcedureInfo);
var
MethodElement: TJclSimpleXMLElem;
begin
MethodElement := AClassElement.Items.Add('method');
MethodElement.Properties.Add('name', AMethod.Name);
MethodElement.Properties.Add('desc', '()'); {TODO: Not sure we can pull this out }
AddMethodStats(MethodElement, AMethod);
end;
procedure TJacocoCoverageReport.AddMethodStats(
const ARootElement: TJclSimpleXMLElem;
const AMethod: TProcedureInfo);
//var
// IsCovered: Integer;
begin
// IsCovered := IfThen(AMethod.PercentCovered > 0, 1, 0);
{ TODO: Not sure about these either! }
// INSTRUCTION
{ TODO: Is this the same as LINE? }
// AddCoverageElement(ARootElement,
// 'counter',
// 'INSTRUCTION',
// AMethod.CoveredLineCount,
// AMethod.LineCount - AMethod.CoveredLineCount);
// LINE
AddCoverageElement(ARootElement,
'LINE',
AMethod.CoveredLineCount,
AMethod.LineCount - AMethod.CoveredLineCount);
// AddCoverageElement(ARootElement,
// 'METHOD',
// AMethod.PercentCovered,
// 100 - AMethod.PercentCovered);
// AddCoverageElement(ARootElement,
// 'counter',
// 'INSTRUCTION',
// AMethod.CoveredLineCount,
// AMethod.LineCount - AMethod.CoveredLineCount);
// AddCoverageElement(ARootElement,
// 'counter',
// 'COMPLEXITY',
// AMethod.CoveredLineCount,
// AMethod.LineCount - AMethod.CoveredLineCount);
(*
AddCoverageElement(
ARootElement, 'counter',
AMethod.CoveredLineCount, AMethod.LineCount
);
AddCoverageElement(
ARootElement, 'counter',
AMethod.CoveredLineCount, AMethod.LineCount
);
AddCoverageElement(
ARootElement, 'counter',
AMethod.CoveredLineCount, AMethod.LineCount
);
*)
end;
procedure TJacocoCoverageReport.AddCoverageElement(
const RootElement: TJclSimpleXMLElem;
const AType: string;
const TotalCoveredCount, TotalUncoveredCount: Integer);
var
CoverageElement: TJclSimpleXMLElem;
begin
CoverageElement := RootElement.Items.Add('counter');
CoverageElement.Properties.Add('type', AType);
CoverageElement.Properties.Add('covered', TotalCoveredCount);
CoverageElement.Properties.Add('missed', TotalUncoveredCount);
end;
{$REGION 'TJacocoCoverageReportMerger'}
class function TJacocoCoverageReportMerger.MergeCoverageStatsForGenerics(
const ACoverageStatsIn: ICoverageStats): ICoverageStats;
var
i, j, line: Integer;
LModuleStats, LUnitStats, LResultStats: ICoverageStats;
FResultModuleName, FResultUnitName: String;
LCoverageLine: TCoverageLine;
begin
Result := TCoverageStats.Create(ACoverageStatsIn.Name, ACoverageStatsIn.Parent);
//Loop all modules
for i := 0 to ACoverageStatsIn.Count - 1 do begin
LModuleStats := ACoverageStatsIn.CoverageReport[i];
//Loop all units
for j := 0 to LModuleStats.Count - 1 do begin
LUnitStats := LModuleStats.CoverageReport[j];
FResultModuleName := LUnitStats.Name.Substring(0, LUnitStats.Name.LastIndexOf('.'));
FResultUnitName := LUnitStats.Name;
LResultStats := Result.CoverageReportByName[FResultModuleName].CoverageReportByName[FResultUnitName];
//Add all coverage lines
for line := 0 to ACoverageStatsIn.CoverageReport[i].CoverageReport[j].GetCoverageLineCount - 1 do begin
LCoverageLine := ACoverageStatsIn.CoverageReport[i].CoverageReport[j].CoverageLine[line];
LResultStats.AddLineCoverage(LCoverageLine.LineNumber, LCoverageLine.LineCount);
end;
end;
end;
Result.Calculate;
end;
{$REGION}
end.