-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
370 lines (331 loc) · 10.8 KB
/
main.cpp
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <QDir>
#include <QFile>
#include <stdio.h>
#include <QRegExp>
#include <QtDebug>
#include <QTime>
#include <QList>
#include <QPair>
#include "dirtree.h"
unsigned int totalTestingFunCount = 0;
unsigned int totalVirtualCount = 0;
unsigned int totalDocumentedCount = 0;
QStringList ignoreFiles;
QString outputStats = "";
QStringList testpathes;
DirTree *dirTree = NULL;
QList<QPair<QString, QPair<int, bool> > > listOfTests;
QString warning = "";
QString colors[] = {"yellow", "white", "pink", "orange", "00CC66", "CC99CC", "00CCCC"};
QString colorToString(bgcolors color)
{
return colors[color];
}
bgcolors nextColor(bgcolors color)
{
return (bgcolors)(((int)color + 1) % 7);
}
QString commentFreeString(QString str)
{
// this counting is used and accurate because we have style guide
int docCount = str.count(QRegExp("///[^\\n]*\\n[^\\n/\\(]*\\("));
docCount += str.count(QString("/**"));
docCount += str.count(QRegExp("//![^\\n]*\\n[^/\\(]*\\("));
totalDocumentedCount += docCount;
QRegExp reg = QRegExp("/\\*.*\\*/");
reg.setMinimal(true);
str.remove(reg);
str.remove(QRegExp("//[^\\n$]*(\\n|$)"));
return str;
}
QString removeImplementations(QString str)
{
int publics = str.count(QString("public:"));
int index = str.indexOf(QString("public:"));
str.remove(0, index + 2);
if (publics > 1) {
QRegExp reg("class.*public:");
reg.setMinimal(true);
str.remove(reg);
}
int count = str.count("{");
for (int i = 1; i <= count; ++i) {
str.remove(QRegExp("\\{[^\\{\\}]*\\}"));
}
return str;
}
unsigned int virtualMethodsCount(QString str)
{
return str.count(QRegExp("\\bvirtual\\b"));
}
unsigned int macrosesWithParameters(QString str)
{
return str.count(QRegExp("Q_[A-Z]+(\\s)*\\([^\\)]*\\)"));
}
bool isIgnored(QString fileName)
{
bool isMatch = false;
for (int j = 0; j < ignoreFiles.length(); ++j)
{
QRegExp exReg(".*" + ignoreFiles.at(j) + ".*");
if (exReg.exactMatch(fileName)) {
isMatch = true;
break;
}
}
return isMatch;
}
unsigned int localFunctionCount(QString path, QString fileName)
{
QFile file(path + fileName + ".h");
// the second condition need for events when .h is interface
if (!file.open(QIODevice::ReadOnly | QIODevice::Text) || !QFile::exists(path + fileName + ".cpp")
|| isIgnored(path + fileName + ".h")) {
return 0;
}
QString header = file.readAll();
header = commentFreeString(header);
header = removeImplementations(header);
QRegExp functions("[^A-Za-z0-9~_]~?[_A-Za-z0-9]+(\\s)*\\([^\\)]*\\)");
QRegExp operators("[^A-Za-z0-9~_]operator[>!=<\\+\\*-\\|\\^&/]+(\\s)*\\([^\\)]*\\)");
unsigned int virtCount = virtualMethodsCount(header);
unsigned int localCount = header.count(functions) + header.count(operators) - virtCount - macrosesWithParameters(header);
totalVirtualCount += virtCount;
return localCount;
}
int localTests(QString fileName)
{
int testCount = 0;
// there are opportunity to binsearch
for (int i = 0; i < listOfTests.length(); ++i) {
QString nameTestFile = listOfTests.at(i).first;
if (nameTestFile.compare("/" + fileName + ".h", Qt::CaseInsensitive) == 0) {
if (listOfTests.at(i).second.second) {
warning = QString("There are the different classes with the same name in this project (they have unitTests) and (only) test\'s counting is wrong!") +
QString(" Solution: the new requests in style guide about the test\'s formalization and refactoring counting code.\n");
}
testCount = listOfTests.at(i).second.first;
listOfTests.removeAt(i);
listOfTests.insert(i, qMakePair(nameTestFile, qMakePair(testCount, true)));
break;
}
}
return testCount;
}
void dirTreeInitialization(QString name, int localTesting, int localDocumented, int localTests = 0, bool isIgnored = false, bgcolors color = white)
{
dirTree = new DirTree(name, localTesting, localDocumented, localTests, isIgnored, color);
}
// without interface methods
void totalFunctionCount(QString dir, bool dirIsIgnored, DirNode *parent)
{
DirNode *node;
QString filter = "*.h";
QDir directory(dir);
QStringList fileList = directory.entryList(QDir::Files);
QStringList filterFileList = fileList.filter(QRegExp(filter, Qt::CaseInsensitive, QRegExp::Wildcard));
QStringList dirList = directory.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
unsigned int localCount = 0;
int localTestCount = 0;
unsigned int oldDocumentedCount = totalDocumentedCount;
if (!parent || !dirIsIgnored) {
for (int i = 0; i < filterFileList.length(); ++i)
{
QString file = filterFileList.at(i);
file.chop(2);
localCount += localFunctionCount(dir, file);
localTestCount += localTests(file);
}
totalTestingFunCount += localCount;
if (!dirTree) {
dirTreeInitialization(dir, localCount, totalDocumentedCount - oldDocumentedCount, localTestCount, false, orange);
node = dirTree->getRoot();
} else {
node = DirTree::createNode(dir, localCount, totalDocumentedCount - oldDocumentedCount, localTestCount, false, nextColor(parent->color));
dirTree->addChild(node, parent);
}
for (int i = 0; i < dirList.length(); ++i)
{
bool followDirIsIgnored = false;
for (int j = 0; j < ignoreFiles.length(); ++j)
{
QRegExp exReg(".*" + ignoreFiles.at(j) + ".*");
if (exReg.exactMatch(dir + dirList.at(i) + "/")) {
followDirIsIgnored = true;
break;
}
}
totalFunctionCount(dir + dirList.at(i) + "/", followDirIsIgnored, node);
}
} else {
node = DirTree::createNode(dir, 0, 0, 0, true, nextColor(parent->color));
dirTree->addChild(node, parent);
for (int i = 0; i < dirList.length(); ++i)
{
totalFunctionCount(dir + dirList.at(i) + "/", true, node);
}
}
}
void fillIgnoredFiles()
{
QFile file("~testignore");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
ignoreFiles.append(line);
}
file.close();
}
void fillPathesForTest()
{
QFile file("testpathes");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
testpathes.append(line);
}
file.close();
}
void fillOutputStats(DirNode *node)
{
QString name1 = "<tr bgcolor =" + colorToString(node->color) + "><td>";
if (node->childNode) {
name1.append("<a href=\"#" + node->childNode->name + "\">");
}
name1.append((node->isIgnored ? "<i>~" : "") + node->name + (node->isIgnored ? "</i>" : "") + "</a></td>");
QString name2 = "<td> <a name=\"" + node->name + "\"></a>" + QString::number(node->localTesting) + "</td>";
QString name3 = "<td>" + QString::number(node->localDocumented)+ "</td>";
QString name4 = "<td>" + QString::number(node->localTests) + "</td>";
QString name5 = "<td>" + QString::number(node->totalTesting - node->localTesting) + "</td>";
QString name6 = "<td>" + QString::number(node->totalDocumented - node->localDocumented) + "</td>";
QString name7 = "<td>" + QString::number(node->totalTests - node->localTests) + "</td></tr>";
outputStats.append(name1 + name2 + name3 + name4 + name5 + name6 + name7);
}
void fillNodeLogHTML(DirNode *node)
{
if (node == dirTree->getRoot()) {
fillOutputStats(node);
}
DirNode *tmpNode = node->childNode;
if (tmpNode) {
if (tmpNode->rightNode) {
fillOutputStats(tmpNode);
while (tmpNode->rightNode != NULL) {
tmpNode = tmpNode->rightNode;
fillOutputStats(tmpNode);
}
tmpNode = node->childNode;
fillNodeLogHTML(tmpNode);
while (tmpNode->rightNode != NULL) {
tmpNode = tmpNode->rightNode;
fillNodeLogHTML(tmpNode);
}
} else {
fillOutputStats(node->childNode);
fillNodeLogHTML(node->childNode);
}
}
}
void fillLog(QString fileName)
{
QString frame1 = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">"
+ QString("<html><head><meta http-equiv=Content-Type content=text/html; charset=utf-8><title>method's ")
+ fileName + "</title><caption>" + "Project method's information" + "</caption></head><body><table border=1 "
+ "width=100% cellpadding=5 cols=7 bgcolor=white><tr bgcolor = pink><th width=40%>path</th><th width=10%>testing</th><th width=10%>doc</th><th width=10%>tests"
+ "</th><th width=10%>testing in subfolders</th><th width=10%>doc in subfolders</th><th width=10%>tests in subfolders</th width=10%></tr>";
QString frame2 = "</table></body></html>";
outputStats.append(frame1);
fillNodeLogHTML(dirTree->getRoot());
outputStats.append(frame2);
}
void localCountOfTests(QString cppFile)
{
QFile file(cppFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
QString impl = file.readAll();
int testCount = impl.count(QRegExp("TEST(\\s)?\\("));
testCount += impl.count(QRegExp("TEST_F(\\s)?\\("));
cppFile.chop(8);
cppFile.append(".h");
int lastSlash = cppFile.lastIndexOf("/");
QString hFile = cppFile.right(cppFile.length() - lastSlash);
listOfTests.append(qMakePair(hFile, qMakePair(testCount, false)));
}
void fillListOfTests(QString dir)
{
QDir directory(dir);
QString fileFilter = "*.cpp";
QStringList dirList = directory.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
QStringList fileList = directory.entryList(QDir::Files);
QStringList filterFileList = fileList.filter(QRegExp(fileFilter, Qt::CaseInsensitive, QRegExp::Wildcard));
for (int i = 0; i < filterFileList.length(); ++i)
{
QString file = filterFileList.at(i);
localCountOfTests(dir + file);
}
for (int i = 0; i < dirList.length(); ++i)
{
QString subDir = dirList.at(i);
fillListOfTests(dir + subDir + "/");
}
}
void fillListOfTestDirects(QString dir)
{
QDir directory(dir);
QString pathFilter = "*Tests";
QStringList dirList = directory.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
QStringList dirFilterList = dirList.filter(QRegExp(pathFilter, Qt::CaseSensitive, QRegExp::Wildcard));
for (int i = 0; i < dirFilterList.length(); ++i)
{
QString subDir = dirFilterList.at(i);
if (subDir.compare("exampleTests") != 0) {
fillListOfTests(dir + subDir + "/");
}
}
}
int main(int argc, char *argv[])
{
Q_UNUSED(argc);
Q_UNUSED(argv);
QTime time;
time.start();
fillIgnoredFiles();
fillPathesForTest();
for (int j = 0; j < testpathes.length(); ++j)
{
fillListOfTestDirects(testpathes.at(j) + "qrtest/unitTests/");
totalFunctionCount(testpathes.at(j), false, NULL);
dirTree->calculateTotalData();
QString fileName = "testCoverage";
QString num = (j) ? QString::number(j) : "";
QString extension = ".html";
QFile outputFile(fileName + num + extension);
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
return 0;
}
fillLog(fileName);
QTextStream out(&outputFile);
out << warning << outputStats;
totalTestingFunCount = 0;
totalVirtualCount = 0;
totalDocumentedCount = 0;
dirTree->~DirTree();
dirTree = NULL;
outputStats.clear();
outputFile.close();
listOfTests.clear();
warning.clear();
}
qDebug() << "time of execution: " + QString::number(time.elapsed()) + " ms";
return 0;
}