From 4b0e3edfa42ef036d19455d4417a5053b29a235a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 9 Jul 2010 13:27:15 +0200 Subject: [PATCH] Unit Testing: Test that suppressions work --- Makefile | 4 +++ lib/settings.cpp | 16 +++++++----- lib/settings.h | 3 ++- test/testsettings.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 test/testsettings.cpp diff --git a/Makefile b/Makefile index 3242804ca..53e96e9d5 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,7 @@ TESTOBJ = test/testautovariables.o \ test/testpreprocessor.o \ test/testredundantif.o \ test/testrunner.o \ + test/testsettings.o \ test/testsimplifytokens.o \ test/teststl.o \ test/testsuite.o \ @@ -213,6 +214,9 @@ test/testredundantif.o: test/testredundantif.cpp lib/tokenize.h lib/classinfo.h test/testrunner.o: test/testrunner.cpp test/testsuite.h lib/errorlogger.h lib/settings.h $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testrunner.o test/testrunner.cpp +test/testsettings.o: test/testsettings.cpp lib/settings.h test/testsuite.h lib/errorlogger.h + $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testsettings.o test/testsettings.cpp + test/testsimplifytokens.o: test/testsimplifytokens.cpp test/testsuite.h lib/errorlogger.h lib/settings.h lib/tokenize.h lib/classinfo.h lib/token.h $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testsimplifytokens.o test/testsimplifytokens.cpp diff --git a/lib/settings.cpp b/lib/settings.cpp index 97d979946..f3a21ba37 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -53,6 +53,8 @@ bool Settings::Suppressions::parseFile(std::istream &istr) while (filedata.find("\r") != std::string::npos) filedata[filedata.find("\r")] = '\n'; + bool ret = true; + // Parse filedata.. std::istringstream istr2(filedata); while (std::getline(istr2, line)) @@ -74,36 +76,38 @@ bool Settings::Suppressions::parseFile(std::istream &istr) } // We could perhaps check if the id is valid and return error if it is not - addSuppression(id, file, lineNumber); + ret &= addSuppression(id, file, lineNumber); } - return true; + return ret; } -void Settings::Suppressions::addSuppression(const std::string &errorId, const std::string &file, unsigned int line) +bool Settings::Suppressions::addSuppression(const std::string &errorId, const std::string &file, unsigned int line) { // Check that errorId is valid.. if (errorId.empty()) { std::cerr << "Failed to add suppression. No id." << std::endl; - return; + return false; } for (std::string::size_type pos = 0; pos < errorId.length(); ++pos) { if (errorId[pos] < 0 || !std::isalnum(errorId[pos])) { std::cerr << "Failed to add suppression. Invalid id \"" << errorId << "\"" << std::endl; - return; + return false; } if (pos == 0 && std::isdigit(errorId[pos])) { std::cerr << "Failed to add suppression. Invalid id \"" << errorId << "\"" << std::endl; - return; + return false; } } _suppressions[errorId][file].push_back(line); _suppressions[errorId][file].sort(); + + return true; } bool Settings::Suppressions::isSuppressed(const std::string &errorId, const std::string &file, unsigned int line) diff --git a/lib/settings.h b/lib/settings.h index 4ce21d54a..32baeb2e4 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -145,8 +145,9 @@ public: * @param errorId the id for the error, e.g. "arrayIndexOutOfBounds" * @param file File name with the path, e.g. "src/main.cpp" * @param line number, e.g. "123" + * @return true on success, false in syntax error is noticed. */ - void addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0); + bool addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0); /** * @brief Returns true if this message should not be shown to the user. diff --git a/test/testsettings.cpp b/test/testsettings.cpp new file mode 100644 index 000000000..7abfde29c --- /dev/null +++ b/test/testsettings.cpp @@ -0,0 +1,57 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. + * + * 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 + * along with this program. If not, see . + */ + +#include "settings.h" +#include "testsuite.h" + +#include + +extern std::ostringstream errout; + +class TestSettings : public TestFixture +{ +public: + TestSettings() : TestFixture("TestSettings") + { } + +private: + + void run() + { + TEST_CASE(suppressionsBadId1); + TEST_CASE(suppressionsDosFormat); // Ticket #1836 + } + + void suppressionsBadId1() + { + Settings::Suppressions suppressions; + std::istringstream s("123"); + ASSERT_EQUALS(false, suppressions.parseFile(s)); + } + + void suppressionsDosFormat() + { + Settings::Suppressions suppressions; + std::istringstream s("abc\r\ndef\r\n"); + ASSERT_EQUALS(true, suppressions.parseFile(s)); + ASSERT_EQUALS(true, suppressions.isSuppressed("abc", "test.cpp", 1)); + ASSERT_EQUALS(true, suppressions.isSuppressed("def", "test.cpp", 1)); + } +}; + +REGISTER_TEST(TestSettings)