Skip to content

Commit 4b0e3ed

Browse files
author
Daniel Marjamäki
committed
Unit Testing: Test that suppressions work
1 parent abbd557 commit 4b0e3ed

4 files changed

Lines changed: 73 additions & 7 deletions

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ TESTOBJ = test/testautovariables.o \
5252
test/testpreprocessor.o \
5353
test/testredundantif.o \
5454
test/testrunner.o \
55+
test/testsettings.o \
5556
test/testsimplifytokens.o \
5657
test/teststl.o \
5758
test/testsuite.o \
@@ -213,6 +214,9 @@ test/testredundantif.o: test/testredundantif.cpp lib/tokenize.h lib/classinfo.h
213214
test/testrunner.o: test/testrunner.cpp test/testsuite.h lib/errorlogger.h lib/settings.h
214215
$(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testrunner.o test/testrunner.cpp
215216

217+
test/testsettings.o: test/testsettings.cpp lib/settings.h test/testsuite.h lib/errorlogger.h
218+
$(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testsettings.o test/testsettings.cpp
219+
216220
test/testsimplifytokens.o: test/testsimplifytokens.cpp test/testsuite.h lib/errorlogger.h lib/settings.h lib/tokenize.h lib/classinfo.h lib/token.h
217221
$(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testsimplifytokens.o test/testsimplifytokens.cpp
218222

lib/settings.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ bool Settings::Suppressions::parseFile(std::istream &istr)
5353
while (filedata.find("\r") != std::string::npos)
5454
filedata[filedata.find("\r")] = '\n';
5555

56+
bool ret = true;
57+
5658
// Parse filedata..
5759
std::istringstream istr2(filedata);
5860
while (std::getline(istr2, line))
@@ -74,36 +76,38 @@ bool Settings::Suppressions::parseFile(std::istream &istr)
7476
}
7577

7678
// We could perhaps check if the id is valid and return error if it is not
77-
addSuppression(id, file, lineNumber);
79+
ret &= addSuppression(id, file, lineNumber);
7880
}
7981

80-
return true;
82+
return ret;
8183
}
8284

83-
void Settings::Suppressions::addSuppression(const std::string &errorId, const std::string &file, unsigned int line)
85+
bool Settings::Suppressions::addSuppression(const std::string &errorId, const std::string &file, unsigned int line)
8486
{
8587
// Check that errorId is valid..
8688
if (errorId.empty())
8789
{
8890
std::cerr << "Failed to add suppression. No id." << std::endl;
89-
return;
91+
return false;
9092
}
9193
for (std::string::size_type pos = 0; pos < errorId.length(); ++pos)
9294
{
9395
if (errorId[pos] < 0 || !std::isalnum(errorId[pos]))
9496
{
9597
std::cerr << "Failed to add suppression. Invalid id \"" << errorId << "\"" << std::endl;
96-
return;
98+
return false;
9799
}
98100
if (pos == 0 && std::isdigit(errorId[pos]))
99101
{
100102
std::cerr << "Failed to add suppression. Invalid id \"" << errorId << "\"" << std::endl;
101-
return;
103+
return false;
102104
}
103105
}
104106

105107
_suppressions[errorId][file].push_back(line);
106108
_suppressions[errorId][file].sort();
109+
110+
return true;
107111
}
108112

109113
bool Settings::Suppressions::isSuppressed(const std::string &errorId, const std::string &file, unsigned int line)

lib/settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ class Settings
145145
* @param errorId the id for the error, e.g. "arrayIndexOutOfBounds"
146146
* @param file File name with the path, e.g. "src/main.cpp"
147147
* @param line number, e.g. "123"
148+
* @return true on success, false in syntax error is noticed.
148149
*/
149-
void addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0);
150+
bool addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0);
150151

151152
/**
152153
* @brief Returns true if this message should not be shown to the user.

test/testsettings.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2010 Daniel Marjamäki and 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+
#include "settings.h"
20+
#include "testsuite.h"
21+
22+
#include <sstream>
23+
24+
extern std::ostringstream errout;
25+
26+
class TestSettings : public TestFixture
27+
{
28+
public:
29+
TestSettings() : TestFixture("TestSettings")
30+
{ }
31+
32+
private:
33+
34+
void run()
35+
{
36+
TEST_CASE(suppressionsBadId1);
37+
TEST_CASE(suppressionsDosFormat); // Ticket #1836
38+
}
39+
40+
void suppressionsBadId1()
41+
{
42+
Settings::Suppressions suppressions;
43+
std::istringstream s("123");
44+
ASSERT_EQUALS(false, suppressions.parseFile(s));
45+
}
46+
47+
void suppressionsDosFormat()
48+
{
49+
Settings::Suppressions suppressions;
50+
std::istringstream s("abc\r\ndef\r\n");
51+
ASSERT_EQUALS(true, suppressions.parseFile(s));
52+
ASSERT_EQUALS(true, suppressions.isSuppressed("abc", "test.cpp", 1));
53+
ASSERT_EQUALS(true, suppressions.isSuppressed("def", "test.cpp", 1));
54+
}
55+
};
56+
57+
REGISTER_TEST(TestSettings)

0 commit comments

Comments
 (0)