2011-01-31 14:25:51 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2016-01-01 14:34:45 +01:00
|
|
|
* Copyright (C) 2007-2016 Cppcheck team.
|
2011-01-31 14:25:51 +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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "pathmatch.h"
|
|
|
|
#include "testsuite.h"
|
|
|
|
|
2011-02-01 12:34:05 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2011-01-31 14:25:51 +01:00
|
|
|
|
2015-03-11 22:26:33 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class TestPathMatch : public TestFixture {
|
2011-01-31 14:25:51 +01:00
|
|
|
public:
|
2013-08-07 17:55:31 +02:00
|
|
|
TestPathMatch()
|
|
|
|
: TestFixture("TestPathMatch")
|
|
|
|
, emptyMatcher(std::vector<std::string>())
|
|
|
|
, srcMatcher(std::vector<std::string>(1, "src/"))
|
|
|
|
, fooCppMatcher(std::vector<std::string>(1, "foo.cpp"))
|
2014-11-20 14:20:09 +01:00
|
|
|
, srcFooCppMatcher(std::vector<std::string>(1, "src/foo.cpp")) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2011-01-31 14:25:51 +01:00
|
|
|
|
|
|
|
private:
|
2013-08-07 17:55:31 +02:00
|
|
|
const PathMatch emptyMatcher;
|
|
|
|
const PathMatch srcMatcher;
|
|
|
|
const PathMatch fooCppMatcher;
|
|
|
|
const PathMatch srcFooCppMatcher;
|
2011-01-31 14:25:51 +01:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void run() {
|
2011-01-31 14:25:51 +01:00
|
|
|
TEST_CASE(emptymaskemptyfile);
|
|
|
|
TEST_CASE(emptymaskpath1);
|
|
|
|
TEST_CASE(emptymaskpath2);
|
|
|
|
TEST_CASE(emptymaskpath3);
|
|
|
|
TEST_CASE(onemaskemptypath);
|
|
|
|
TEST_CASE(onemasksamepath);
|
2011-10-02 10:46:27 +02:00
|
|
|
TEST_CASE(onemasksamepathdifferentcase);
|
2011-01-31 14:25:51 +01:00
|
|
|
TEST_CASE(onemasksamepathwithfile);
|
2011-02-03 07:56:28 +01:00
|
|
|
TEST_CASE(onemaskdifferentdir1);
|
|
|
|
TEST_CASE(onemaskdifferentdir2);
|
|
|
|
TEST_CASE(onemaskdifferentdir3);
|
|
|
|
TEST_CASE(onemaskdifferentdir4);
|
2011-01-31 14:25:51 +01:00
|
|
|
TEST_CASE(onemasklongerpath1);
|
|
|
|
TEST_CASE(onemasklongerpath2);
|
|
|
|
TEST_CASE(onemasklongerpath3);
|
2011-12-15 20:18:52 +01:00
|
|
|
TEST_CASE(twomasklongerpath1);
|
|
|
|
TEST_CASE(twomasklongerpath2);
|
|
|
|
TEST_CASE(twomasklongerpath3);
|
|
|
|
TEST_CASE(twomasklongerpath4);
|
2011-02-03 13:43:42 +01:00
|
|
|
TEST_CASE(filemask1);
|
2011-10-02 10:46:27 +02:00
|
|
|
TEST_CASE(filemaskdifferentcase);
|
2011-02-03 13:43:42 +01:00
|
|
|
TEST_CASE(filemask2);
|
|
|
|
TEST_CASE(filemask3);
|
|
|
|
TEST_CASE(filemaskpath1);
|
|
|
|
TEST_CASE(filemaskpath2);
|
|
|
|
TEST_CASE(filemaskpath3);
|
|
|
|
TEST_CASE(filemaskpath4);
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:55:31 +02:00
|
|
|
// Test empty PathMatch
|
2014-11-20 14:20:09 +01:00
|
|
|
void emptymaskemptyfile() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!emptyMatcher.match(""));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void emptymaskpath1() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!emptyMatcher.match("src/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void emptymaskpath2() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!emptyMatcher.match("../src/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void emptymaskpath3() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!emptyMatcher.match("/home/user/code/src/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:55:31 +02:00
|
|
|
// Test PathMatch containing "src/"
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemaskemptypath() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!srcMatcher.match(""));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemasksamepath() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(srcMatcher.match("src/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemasksamepathdifferentcase() const {
|
2013-08-07 17:55:31 +02:00
|
|
|
std::vector<std::string> masks(1, "sRc/");
|
2012-02-19 17:22:59 +01:00
|
|
|
PathMatch match(masks, false);
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(match.match("srC/"));
|
2011-10-02 10:46:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemasksamepathwithfile() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(srcMatcher.match("src/file.txt"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemaskdifferentdir1() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!srcMatcher.match("srcfiles/file.txt"));
|
2011-02-03 07:56:28 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemaskdifferentdir2() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!srcMatcher.match("proj/srcfiles/file.txt"));
|
2011-02-03 07:56:28 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemaskdifferentdir3() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!srcMatcher.match("proj/mysrc/file.txt"));
|
2011-02-03 07:56:28 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemaskdifferentdir4() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!srcMatcher.match("proj/mysrcfiles/file.txt"));
|
2011-02-03 07:56:28 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemasklongerpath1() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(srcMatcher.match("/tmp/src/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemasklongerpath2() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(srcMatcher.match("src/module/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void onemasklongerpath3() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(srcMatcher.match("project/src/module/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void twomasklongerpath1() const {
|
2011-01-31 14:25:51 +01:00
|
|
|
std::vector<std::string> masks;
|
|
|
|
masks.push_back("src/");
|
|
|
|
masks.push_back("module/");
|
|
|
|
PathMatch match(masks);
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!match.match("project/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void twomasklongerpath2() const {
|
2011-01-31 14:25:51 +01:00
|
|
|
std::vector<std::string> masks;
|
|
|
|
masks.push_back("src/");
|
|
|
|
masks.push_back("module/");
|
|
|
|
PathMatch match(masks);
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(match.match("project/src/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void twomasklongerpath3() const {
|
2011-01-31 14:25:51 +01:00
|
|
|
std::vector<std::string> masks;
|
|
|
|
masks.push_back("src/");
|
|
|
|
masks.push_back("module/");
|
|
|
|
PathMatch match(masks);
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(match.match("project/module/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void twomasklongerpath4() const {
|
2011-01-31 14:25:51 +01:00
|
|
|
std::vector<std::string> masks;
|
|
|
|
masks.push_back("src/");
|
|
|
|
masks.push_back("module/");
|
|
|
|
PathMatch match(masks);
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(match.match("project/src/module/"));
|
2011-01-31 14:25:51 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:55:31 +02:00
|
|
|
// Test PathMatch containing "foo.cpp"
|
2014-11-20 14:20:09 +01:00
|
|
|
void filemask1() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(fooCppMatcher.match("foo.cpp"));
|
2011-02-03 13:43:42 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void filemaskdifferentcase() const {
|
2013-08-07 17:55:31 +02:00
|
|
|
std::vector<std::string> masks(1, "foo.cPp");
|
2012-02-19 17:22:59 +01:00
|
|
|
PathMatch match(masks, false);
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(match.match("fOo.cpp"));
|
2011-10-02 10:46:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void filemask2() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(fooCppMatcher.match("../foo.cpp"));
|
2011-02-03 13:43:42 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void filemask3() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(fooCppMatcher.match("src/foo.cpp"));
|
2011-02-03 13:43:42 +01:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:55:31 +02:00
|
|
|
// Test PathMatch containing "src/foo.cpp"
|
2014-11-20 14:20:09 +01:00
|
|
|
void filemaskpath1() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(srcFooCppMatcher.match("src/foo.cpp"));
|
2011-02-03 13:43:42 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void filemaskpath2() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(srcFooCppMatcher.match("proj/src/foo.cpp"));
|
2011-02-03 13:43:42 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void filemaskpath3() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!srcFooCppMatcher.match("foo.cpp"));
|
2011-02-03 13:43:42 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void filemaskpath4() const {
|
2017-07-28 11:27:04 +02:00
|
|
|
ASSERT(!srcFooCppMatcher.match("bar/foo.cpp"));
|
2011-02-03 13:43:42 +01:00
|
|
|
}
|
2011-01-31 14:25:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestPathMatch)
|