Skip to content

Commit

Permalink
TestCppcheck: improved testing
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Dec 2, 2023
1 parent 408cf3c commit 2cfb0a4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ test/testcondition.o: test/testcondition.cpp externals/simplecpp/simplecpp.h lib
test/testconstructors.o: test/testconstructors.cpp lib/addoninfo.h lib/check.h lib/checkclass.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testconstructors.cpp

test/testcppcheck.o: test/testcppcheck.cpp lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h
test/testcppcheck.o: test/testcppcheck.cpp lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testcppcheck.cpp

test/testerrorlogger.o: test/testerrorlogger.cpp externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h lib/xml.h test/fixture.h
Expand Down
60 changes: 53 additions & 7 deletions test/testcppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "cppcheck.h"
#include "errorlogger.h"
#include "fixture.h"
#include "helpers.h"

#include <algorithm>
#include <list>
Expand All @@ -34,30 +35,33 @@ class TestCppcheck : public TestFixture {

class ErrorLogger2 : public ErrorLogger {
public:
std::list<std::string> id;
std::list<std::string> ids;

private:
void reportOut(const std::string & /*outmsg*/, Color /*c*/ = Color::Reset) override {}

void reportErr(const ErrorMessage &msg) override {
id.push_back(msg.id);
ids.push_back(msg.id);
}
};

void run() override {
TEST_CASE(getErrorMessages);
TEST_CASE(check);
TEST_CASE(suppress_error_library);
}

void getErrorMessages() const {
ErrorLogger2 errorLogger;
CppCheck::getErrorMessages(errorLogger);
ASSERT(!errorLogger.id.empty());
ASSERT(!errorLogger.ids.empty());

// Check if there are duplicate error ids in errorLogger.id
std::string duplicate;
for (std::list<std::string>::const_iterator it = errorLogger.id.cbegin();
it != errorLogger.id.cend();
for (std::list<std::string>::const_iterator it = errorLogger.ids.cbegin();
it != errorLogger.ids.cend();
++it) {
if (std::find(errorLogger.id.cbegin(), it, *it) != it) {
if (std::find(errorLogger.ids.cbegin(), it, *it) != it) {
duplicate = "Duplicate ID: " + *it;
break;
}
Expand All @@ -67,7 +71,7 @@ class TestCppcheck : public TestFixture {
// Check for error ids from this class.
bool foundPurgedConfiguration = false;
bool foundTooManyConfigs = false;
for (const std::string & it : errorLogger.id) {
for (const std::string & it : errorLogger.ids) {
if (it == "purgedConfiguration")
foundPurgedConfiguration = true;
else if (it == "toomanyconfigs")
Expand All @@ -76,6 +80,48 @@ class TestCppcheck : public TestFixture {
ASSERT(foundPurgedConfiguration);
ASSERT(foundTooManyConfigs);
}

void check()
{
ScopedFile file("test.cpp",
"int main()\n"
"{\n"
" int i = *((int*)0);\n"
" return 0;\n"
"}");

ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});
ASSERT_EQUALS(1, cppcheck.check(file.path()));
// TODO: how to properly disable these warnings?
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
return id == "logChecker";
}), errorLogger.ids.end());
ASSERT_EQUALS(1, errorLogger.ids.size());
ASSERT_EQUALS("nullPointer", *errorLogger.ids.cbegin());
}

void suppress_error_library()
{
ScopedFile file("test.cpp",
"int main()\n"
"{\n"
" int i = *((int*)0);\n"
" return 0;\n"
"}");

ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});
const char xmldata[] = R"(<def format="2"><markup ext=".cpp" reporterrors="false"/></def>)";
const Settings s = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
cppcheck.settings() = s;
ASSERT_EQUALS(0, cppcheck.check(file.path()));
// TODO: how to properly disable these warnings?
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
return id == "logChecker";
}), errorLogger.ids.end());
ASSERT_EQUALS(0, errorLogger.ids.size());
}
};

REGISTER_TEST(TestCppcheck)

0 comments on commit 2cfb0a4

Please sign in to comment.