2009-02-05 21:06:39 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2020-05-10 11:16:32 +02:00
|
|
|
* Copyright (C) 2007-2020 Cppcheck team.
|
2009-02-05 21:06:39 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-05 21:06:39 +01:00
|
|
|
*/
|
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "check.h"
|
2009-10-25 12:49:06 +01:00
|
|
|
#include "cppcheck.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "errorlogger.h"
|
2009-02-05 21:06:39 +01:00
|
|
|
#include "testsuite.h"
|
|
|
|
|
2010-05-17 19:18:05 +02:00
|
|
|
#include <algorithm>
|
2011-12-23 22:31:48 +01:00
|
|
|
#include <list>
|
2009-02-05 21:06:39 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class TestCppcheck : public TestFixture {
|
2009-02-05 21:06:39 +01:00
|
|
|
public:
|
2014-11-20 14:20:09 +01:00
|
|
|
TestCppcheck() : TestFixture("TestCppcheck") {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2009-02-05 21:06:39 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class ErrorLogger2 : public ErrorLogger {
|
2011-01-16 17:06:07 +01:00
|
|
|
public:
|
|
|
|
std::list<std::string> id;
|
2010-05-13 22:14:29 +02:00
|
|
|
|
2019-12-27 19:05:10 +01:00
|
|
|
void reportOut(const std::string & /*outmsg*/) OVERRIDE {}
|
2020-01-18 07:25:39 +01:00
|
|
|
void bughuntingReport(const std::string & /*str*/) OVERRIDE {}
|
2010-05-13 22:14:29 +02:00
|
|
|
|
2019-09-20 21:57:16 +02:00
|
|
|
void reportErr(const ErrorLogger::ErrorMessage &msg) OVERRIDE {
|
2019-08-18 12:19:05 +02:00
|
|
|
id.push_back(msg.id);
|
2011-01-16 17:06:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-12 15:45:25 +01:00
|
|
|
void run() OVERRIDE {
|
2011-02-02 09:57:08 +01:00
|
|
|
TEST_CASE(instancesSorted);
|
2012-08-26 16:22:46 +02:00
|
|
|
TEST_CASE(classInfoFormat);
|
2011-01-16 17:06:07 +01:00
|
|
|
TEST_CASE(getErrorMessages);
|
2010-05-13 22:14:29 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void instancesSorted() const {
|
2012-08-26 16:22:46 +02:00
|
|
|
for (std::list<Check *>::const_iterator i = Check::instances().begin(); i != Check::instances().end(); ++i) {
|
|
|
|
std::list<Check *>::const_iterator j = i;
|
2011-02-02 09:57:08 +01:00
|
|
|
++j;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (j != Check::instances().end()) {
|
2011-02-02 09:57:08 +01:00
|
|
|
ASSERT_EQUALS(true, (*i)->name() < (*j)->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void classInfoFormat() const {
|
2012-08-26 16:22:46 +02:00
|
|
|
for (std::list<Check *>::const_iterator i = Check::instances().begin(); i != Check::instances().end(); ++i) {
|
|
|
|
const std::string info = (*i)->classInfo();
|
|
|
|
if (!info.empty()) {
|
2015-07-25 17:19:53 +02:00
|
|
|
ASSERT('\n' != info[0]); // No \n in the beginning
|
|
|
|
ASSERT('\n' == info.back()); // \n at end
|
2012-08-26 16:22:46 +02:00
|
|
|
if (info.size() > 1)
|
|
|
|
ASSERT('\n' != info[info.length()-2]); // Only one \n at end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void getErrorMessages() const {
|
2011-01-16 17:06:07 +01:00
|
|
|
ErrorLogger2 errorLogger;
|
2020-05-19 16:04:25 +02:00
|
|
|
CppCheck cppCheck(errorLogger, true, nullptr);
|
2010-04-03 21:53:06 +02:00
|
|
|
cppCheck.getErrorMessages();
|
2011-01-16 17:07:12 +01:00
|
|
|
ASSERT(!errorLogger.id.empty());
|
2010-05-16 07:38:29 +02:00
|
|
|
|
2015-11-06 21:58:49 +01:00
|
|
|
// Check if there are duplicate error ids in errorLogger.id
|
2011-01-16 18:45:05 +01:00
|
|
|
std::string duplicate;
|
|
|
|
for (std::list<std::string>::iterator it = errorLogger.id.begin();
|
|
|
|
it != errorLogger.id.end();
|
2011-10-13 20:53:06 +02:00
|
|
|
++it) {
|
|
|
|
if (std::find(errorLogger.id.begin(), it, *it) != it) {
|
2011-01-16 18:45:05 +01:00
|
|
|
duplicate = "Duplicate ID: " + *it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_EQUALS("", duplicate);
|
2018-02-10 22:30:49 +01:00
|
|
|
|
|
|
|
// Check for error ids from this class.
|
|
|
|
bool foundPurgedConfiguration = false;
|
|
|
|
bool foundTooManyConfigs = false;
|
2019-09-19 20:29:33 +02:00
|
|
|
for (const std::string & it : errorLogger.id) {
|
|
|
|
if (it == "purgedConfiguration")
|
2018-02-10 22:30:49 +01:00
|
|
|
foundPurgedConfiguration = true;
|
2019-09-19 20:29:33 +02:00
|
|
|
else if (it == "toomanyconfigs")
|
2018-02-10 22:30:49 +01:00
|
|
|
foundTooManyConfigs = true;
|
|
|
|
}
|
|
|
|
ASSERT(foundPurgedConfiguration);
|
|
|
|
ASSERT(foundTooManyConfigs);
|
2011-01-16 17:06:07 +01:00
|
|
|
}
|
2009-02-05 21:06:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestCppcheck)
|