|
1 | | -/* |
2 | | - * Cppcheck - A tool for static C/C++ code analysis |
3 | | - * Copyright (C) 2007-2021 Cppcheck team. |
4 | | - * |
5 | | - * This program is free software: you can redistribute it and/or modify |
6 | | - * it under the terms of the GNU General Public License as published by |
7 | | - * the Free Software Foundation, either version 3 of the License, or |
8 | | - * (at your option) any later version. |
9 | | - * |
10 | | - * This program is distributed in the hope that it will be useful, |
11 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | - * GNU General Public License for more details. |
14 | | - * |
15 | | - * You should have received a copy of the GNU General Public License |
16 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | - */ |
18 | | - |
19 | 1 | #include "testresultstree.h" |
20 | 2 |
|
21 | | -#include "resultstree.h" |
22 | | - |
23 | | -// headers that declare mocked functions/variables |
24 | | -#include "applicationlist.h" |
25 | | -#include "common.h" |
26 | | -#include "projectfile.h" |
27 | | -#include "threadhandler.h" |
28 | | -#include "threadresult.h" |
29 | | - |
30 | | -#include "application.h" |
31 | | -#include "checkers.h" |
32 | | -#include "erroritem.h" |
33 | | -#include "errorlogger.h" |
34 | | -#include "errortypes.h" |
35 | | -#include "report.h" |
36 | | -#include "showtypes.h" |
37 | | -#include "suppressions.h" |
38 | | -#include "xmlreport.h" |
| 3 | +#include <iostream> |
39 | 4 |
|
40 | | -#include <cstddef> |
41 | | -#include <string> |
42 | | -#include <utility> |
43 | | - |
44 | | -#include <QList> |
45 | | -#include <QModelIndex> |
46 | | -#include <QString> |
| 5 | +#include <QStandardItemModel> |
47 | 6 | #include <QtTest> |
48 | 7 |
|
49 | | -class TestReport : public Report { |
| 8 | +class QCustomItem : public QStandardItem |
| 9 | +{ |
50 | 10 | public: |
51 | | - explicit TestReport(QString format) : Report(QString()), format(std::move(format)) {} |
52 | | - void writeHeader() override { |
53 | | - output.clear(); |
| 11 | + explicit QCustomItem(QString str) |
| 12 | + : mStr(std::move(str)) |
| 13 | + {} |
| 14 | + ~QCustomItem() override { |
| 15 | + std::cout << "~QCustomItem(" << mStr.toStdString() << ")" << std::endl; |
54 | 16 | } |
55 | | - void writeFooter() override {} |
56 | | - void writeError(const ErrorItem &error) override { |
57 | | - QString line = format; |
58 | | - line.replace("{id}", error.errorId); |
59 | | - line.replace("{classification}", error.classification); |
60 | | - line.replace("{guideline}", error.guideline); |
61 | | - output += (output.isEmpty() ? "" : "\n") + line; |
62 | | - } |
63 | | - QString format; |
64 | | - QString output; |
| 17 | +private: |
| 18 | + QString mStr; |
65 | 19 | }; |
66 | 20 |
|
67 | | -// Mock GUI... |
68 | | -ProjectFile::ProjectFile(QObject *parent) : QObject(parent) {} |
69 | | -ProjectFile *ProjectFile::mActiveProject; |
70 | | -void ProjectFile::addSuppression(const SuppressionList::Suppression & /*unused*/) {} |
71 | | -QString ProjectFile::getWarningTags(std::size_t /*unused*/) const { |
72 | | - return QString(); |
73 | | -} |
74 | | -void ProjectFile::setWarningTags(std::size_t /*unused*/, const QString& /*unused*/) {} |
75 | | -bool ProjectFile::write(const QString & /*unused*/) { |
76 | | - return true; |
77 | | -} |
78 | | -ApplicationList::ApplicationList(QObject *parent) : QObject(parent) {} |
79 | | -ApplicationList::~ApplicationList() = default; |
80 | | -int ApplicationList::getApplicationCount() const { |
81 | | - return 0; |
82 | | -} |
83 | | -ThreadHandler::ThreadHandler(QObject *parent) : QObject(parent) {} |
84 | | -ThreadHandler::~ThreadHandler() = default; |
85 | | -bool ThreadHandler::isChecking() const { |
86 | | - return false; |
87 | | -} |
88 | | -void ThreadHandler::stop() { |
89 | | - throw 1; |
90 | | -} |
91 | | -void ThreadHandler::threadDone() { |
92 | | - throw 1; |
93 | | -} |
94 | | -Application& ApplicationList::getApplication(const int /*unused*/) { |
95 | | - throw 1; |
96 | | -} |
97 | | -const Application& ApplicationList::getApplication(const int index) const { |
98 | | - return mApplications.at(index); |
99 | | -} |
100 | | -QString getPath(const QString &type) { |
101 | | - return "/" + type; |
102 | | -} |
103 | | -void setPath(const QString & /*unused*/, const QString & /*unused*/) {} |
104 | | -QString XmlReport::quoteMessage(const QString &message) { |
105 | | - return message; |
106 | | -} |
107 | | -QString XmlReport::unquoteMessage(const QString &message) { |
108 | | - return message; |
109 | | -} |
110 | | -XmlReport::XmlReport(const QString& filename) : Report(filename) {} |
111 | | -void ThreadResult::fileChecked(const QString & /*unused*/) { |
112 | | - throw 1; |
113 | | -} |
114 | | -void ThreadResult::reportOut(const std::string & /*unused*/, Color /*unused*/) { |
115 | | - throw 1; |
116 | | -} |
117 | | -void ThreadResult::reportErr(const ErrorMessage & /*unused*/) { |
118 | | - throw 1; |
119 | | -} |
120 | | - |
121 | | -// Test... |
122 | | - |
123 | 21 | void TestResultsTree::test1() const |
124 | 22 | { |
125 | | - // #12772 : GUI: information messages are shown even though information tool button is deselected |
126 | | - ResultsTree tree(nullptr); |
127 | | - tree.showResults(ShowTypes::ShowType::ShowInformation, false); |
128 | | - ErrorItem errorItem; |
129 | | - errorItem.errorPath << QErrorPathItem(); |
130 | | - errorItem.severity = Severity::information; |
131 | | - tree.addErrorItem(errorItem); |
132 | | - QCOMPARE(tree.isRowHidden(0,QModelIndex()), true); // Added item is hidden |
133 | | - tree.showResults(ShowTypes::ShowType::ShowInformation, true); |
134 | | - QCOMPARE(tree.isRowHidden(0,QModelIndex()), false); // Show item |
135 | | -} |
136 | | - |
137 | | -void TestResultsTree::testReportType() const |
138 | | -{ |
139 | | - TestReport report("{id},{classification},{guideline}"); |
140 | | - |
141 | | - int msgCount = 0; |
142 | | - auto createErrorItem = [&msgCount](const Severity severity, const QString& errorId) -> ErrorItem { |
143 | | - ++msgCount; |
144 | | - ErrorItem errorItem; |
145 | | - errorItem.errorPath << QErrorPathItem(ErrorMessage::FileLocation("file1.c", msgCount, 1)); |
146 | | - errorItem.severity = severity; |
147 | | - errorItem.errorId = errorId; |
148 | | - errorItem.summary = "test summary " + QString::number(msgCount); |
149 | | - return errorItem; |
150 | | - }; |
151 | | - |
152 | | - // normal report with 2 errors |
153 | | - ResultsTree tree(nullptr); |
154 | | - tree.updateSettings(false, false, false, false, false); |
155 | | - tree.addErrorItem(createErrorItem(Severity::style, "id1")); |
156 | | - tree.addErrorItem(createErrorItem(Severity::style, "unusedVariable")); // Misra C 2.8 |
157 | | - tree.saveResults(&report); |
158 | | - QCOMPARE(report.output, "id1,,\nunusedVariable,,"); |
159 | | - |
160 | | - // switch to Misra C report and check that "id1" is not shown |
161 | | - tree.setReportType(ReportType::misraC2012); |
162 | | - tree.saveResults(&report); |
163 | | - QCOMPARE(report.output, "unusedVariable,Advisory,2.8"); |
164 | | - |
165 | | - // add "missingReturn" and check that it is added properly |
166 | | - tree.addErrorItem(createErrorItem(Severity::warning, "missingReturn")); // Misra C 17.4 |
167 | | - tree.saveResults(&report); |
168 | | - QCOMPARE(report.output, |
169 | | - "unusedVariable,Advisory,2.8\n" |
170 | | - "missingReturn,Mandatory,17.4"); |
171 | | -} |
172 | | - |
173 | | - |
174 | | -void TestResultsTree::testGetGuidelineError() const |
175 | | -{ |
176 | | - TestReport report("{id},{classification},{guideline}"); |
177 | | - |
178 | | - int msgCount = 0; |
179 | | - auto createErrorItem = [&msgCount](const Severity severity, const QString& errorId) -> ErrorItem { |
180 | | - ++msgCount; |
181 | | - ErrorItem errorItem; |
182 | | - errorItem.errorPath << QErrorPathItem(ErrorMessage::FileLocation("file1.c", msgCount, 1)); |
183 | | - errorItem.severity = severity; |
184 | | - errorItem.errorId = errorId; |
185 | | - errorItem.summary = "test summary " + QString::number(msgCount); |
186 | | - return errorItem; |
187 | | - }; |
188 | | - |
189 | | - // normal report with 2 errors |
190 | | - ResultsTree tree(nullptr); |
191 | | - tree.setReportType(ReportType::misraC2012); |
192 | | - tree.addErrorItem(createErrorItem(Severity::error, "id1")); // error severity => guideline 1.3 |
193 | | - tree.saveResults(&report); |
194 | | - QCOMPARE(report.output, "id1,Required,1.3"); |
| 23 | + auto* model = new QStandardItemModel; |
| 24 | + auto *item = new QCustomItem("0"); |
| 25 | + { |
| 26 | + QMap<QString, QVariant> itemdata; |
| 27 | + itemdata["file"] = "file1"; |
| 28 | + item->setData(itemdata); |
| 29 | + itemdata["file"] = "file2"; |
| 30 | + item->setData(itemdata); // crash |
| 31 | + } |
| 32 | + model/*->invisibleRootItem()*/->appendRow(item); |
| 33 | + model->clear(); |
| 34 | + delete model; |
195 | 35 | } |
196 | 36 |
|
197 | 37 | QTEST_MAIN(TestResultsTree) |
198 | | - |
0 commit comments