diff --git a/Makefile b/Makefile index f8f245fd8..a9b53ed22 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ TESTOBJ = test/testautovariables.o \ test/testsimplifytokens.o \ test/teststl.o \ test/testsuite.o \ + test/testthreadexecutor.o \ test/testtoken.o \ test/testtokenize.o \ test/testunusedfunctions.o \ @@ -225,6 +226,9 @@ test/teststl.o: test/teststl.cpp lib/tokenize.h lib/classinfo.h lib/token.h lib/ test/testsuite.o: test/testsuite.cpp test/testsuite.h lib/errorlogger.h lib/settings.h $(CXX) $(CXXFLAGS) -Ilib -c -o test/testsuite.o test/testsuite.cpp +test/testthreadexecutor.o: test/testthreadexecutor.cpp lib/cppcheck.h lib/settings.h lib/errorlogger.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/classinfo.h test/testsuite.h + $(CXX) $(CXXFLAGS) -Ilib -c -o test/testthreadexecutor.o test/testthreadexecutor.cpp + test/testtoken.o: test/testtoken.cpp test/testsuite.h lib/errorlogger.h lib/settings.h lib/tokenize.h lib/classinfo.h lib/token.h $(CXX) $(CXXFLAGS) -Ilib -c -o test/testtoken.o test/testtoken.cpp diff --git a/cli/threadexecutor.cpp b/cli/threadexecutor.cpp index 5960f459a..0c1588b63 100644 --- a/cli/threadexecutor.cpp +++ b/cli/threadexecutor.cpp @@ -40,6 +40,11 @@ ThreadExecutor::~ThreadExecutor() //dtor } +void ThreadExecutor::addFileContent(const std::string &path, const std::string &content) +{ + _fileContents[ path ] = content; +} + /////////////////////////////////////////////////////////////////////////////// ////// This code is for __GNUC__ and __sun only /////////////////////////////// /////////////////////////////////////////////////////////////////////////////// @@ -155,7 +160,18 @@ unsigned int ThreadExecutor::check() { CppCheck fileChecker(*this); fileChecker.settings(_settings); - fileChecker.addFile(_filenames[i]); + + if (_fileContents.size() > 0 && _fileContents.find(_filenames[i]) != _fileContents.end()) + { + // File content was given as a string + fileChecker.addFile(_filenames[i], _fileContents[ _filenames[i] ]); + } + else + { + // Read file from a file + fileChecker.addFile(_filenames[i]); + } + unsigned int resultOfCheck = fileChecker.check(); std::ostringstream oss; oss << resultOfCheck; diff --git a/cli/threadexecutor.h b/cli/threadexecutor.h index 50db1cd0f..7a3e5b8fc 100644 --- a/cli/threadexecutor.h +++ b/cli/threadexecutor.h @@ -38,6 +38,14 @@ public: virtual void reportOut(const std::string &outmsg); virtual void reportErr(const ErrorLogger::ErrorMessage &msg); virtual void reportStatus(unsigned int index, unsigned int max); + /** + * @brief Add content to a file, to be used in unit testing. + * + * @param path File name (used as a key to link with real file). + * @param content If the file would be a real file, this should be + * the content of the file. + */ + void addFileContent(const std::string &path, const std::string &content); private: const std::vector &_filenames; @@ -45,6 +53,9 @@ private: ErrorLogger &_errorLogger; unsigned int _fileCount; + /** @brief Key is file name, and value is the content of the file */ + std::map _fileContents; + #if (defined(__GNUC__) || defined(__sun)) && !defined(__MINGW32__) private: bool handleRead(unsigned int &result); diff --git a/cppcheck.cbp b/cppcheck.cbp index 0e650d10d..f15ed8b6a 100644 --- a/cppcheck.cbp +++ b/cppcheck.cbp @@ -137,6 +137,7 @@ + diff --git a/test/testthreadexecutor.cpp b/test/testthreadexecutor.cpp new file mode 100644 index 000000000..a062026e6 --- /dev/null +++ b/test/testthreadexecutor.cpp @@ -0,0 +1,61 @@ +/* + * 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 . + */ + + +// The preprocessor that Cppcheck uses is a bit special. Instead of generating +// the code for a known configuration, it generates the code for each configuration. + + +#include "cppcheck.h" +#include "testsuite.h" + +#include +#include +#include +#include + +extern std::ostringstream errout; +extern std::ostringstream output; + +class TestThreadExecutor : public TestFixture +{ +public: + TestThreadExecutor() : TestFixture("TestThreadExecutor") + { } + +private: + + void check(const std::string &data) + { + errout.str(""); + output.str(""); + } + + void run() + { + TEST_CASE(jobs); + } + + void jobs() + { + errout.str(""); + output.str(""); + } +}; + +REGISTER_TEST(TestThreadExecutor)