diff --git a/Makefile b/Makefile index c722663d1..a936e3945 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,8 @@ CLIOBJ = cli/cmdlineparser.o \ cli/main.o \ cli/threadexecutor.o -TESTOBJ = test/testautovariables.o \ +TESTOBJ = test/options.o \ + test/testautovariables.o \ test/testbufferoverrun.o \ test/testcharvar.o \ test/testclass.o \ @@ -64,6 +65,7 @@ TESTOBJ = test/testautovariables.o \ test/testmathlib.o \ test/testmemleak.o \ test/testobsoletefunctions.o \ + test/testoptions.o \ test/testother.o \ test/testpreprocessor.o \ test/testrunner.o \ @@ -96,6 +98,9 @@ testrunner: $(TESTOBJ) $(LIBOBJ) cli/threadexecutor.o cli/cmdlineparser.o test: all ./testrunner +check: all + ./testrunner -g -q + dmake: tools/dmake.cpp $(CXX) -o dmake tools/dmake.cpp lib/filelister*.cpp @@ -193,6 +198,9 @@ cli/main.o: cli/main.cpp cli/cppcheckexecutor.h lib/errorlogger.h lib/settings.h cli/threadexecutor.o: cli/threadexecutor.cpp cli/threadexecutor.h lib/settings.h lib/errorlogger.h lib/cppcheck.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/classinfo.h $(CXX) $(CXXFLAGS) -Ilib -c -o cli/threadexecutor.o cli/threadexecutor.cpp +test/options.o: test/options.cpp test/options.h + $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/options.o test/options.cpp + test/testautovariables.o: test/testautovariables.cpp lib/tokenize.h lib/classinfo.h lib/token.h lib/checkautovariables.h lib/check.h lib/settings.h lib/errorlogger.h test/testsuite.h $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testautovariables.o test/testautovariables.cpp @@ -235,13 +243,16 @@ test/testmemleak.o: test/testmemleak.cpp lib/tokenize.h lib/classinfo.h lib/toke test/testobsoletefunctions.o: test/testobsoletefunctions.cpp lib/tokenize.h lib/classinfo.h lib/token.h lib/checkobsoletefunctions.h lib/check.h lib/settings.h lib/errorlogger.h test/testsuite.h $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testobsoletefunctions.o test/testobsoletefunctions.cpp +test/testoptions.o: test/testoptions.cpp test/options.h test/testsuite.h lib/errorlogger.h + $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testoptions.o test/testoptions.cpp + test/testother.o: test/testother.cpp lib/tokenize.h lib/classinfo.h lib/token.h lib/checkother.h lib/check.h lib/settings.h lib/errorlogger.h test/testsuite.h $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testother.o test/testother.cpp test/testpreprocessor.o: test/testpreprocessor.cpp test/testsuite.h lib/errorlogger.h lib/preprocessor.h lib/tokenize.h lib/classinfo.h lib/token.h lib/settings.h $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/testpreprocessor.o test/testpreprocessor.cpp -test/testrunner.o: test/testrunner.cpp test/testsuite.h lib/errorlogger.h +test/testrunner.o: test/testrunner.cpp test/testsuite.h lib/errorlogger.h test/options.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 @@ -253,7 +264,7 @@ test/testsimplifytokens.o: test/testsimplifytokens.cpp test/testsuite.h lib/erro test/teststl.o: test/teststl.cpp lib/tokenize.h lib/classinfo.h lib/token.h lib/checkstl.h lib/check.h lib/settings.h lib/errorlogger.h test/testsuite.h $(CXX) $(CXXFLAGS) -Ilib -Icli -c -o test/teststl.o test/teststl.cpp -test/testsuite.o: test/testsuite.cpp test/testsuite.h lib/errorlogger.h +test/testsuite.o: test/testsuite.cpp test/testsuite.h lib/errorlogger.h test/options.h $(CXX) $(CXXFLAGS) -Ilib -Icli -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 diff --git a/test/options.cpp b/test/options.cpp new file mode 100644 index 000000000..bbadf71f7 --- /dev/null +++ b/test/options.cpp @@ -0,0 +1,47 @@ +// 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 "options.h" + +options::options(int argc, const char* argv[]) + :_options(&argv[1], &argv[0] + argc) + ,_gcc_style_errors(_options.count("-g") != 0) + ,_quiet(_options.count("-q") != 0) +{ + _options.erase("-g"); + _options.erase("-q"); +} + +bool options::quiet() const +{ + return _quiet; +} + +bool options::gcc_style_errors() const +{ + return _gcc_style_errors; +} + +const std::string& options::which_test() const +{ + static std::string ret = ""; + if (not _options.empty()) + { + ret = *_options.rbegin(); + } + return ret; +} + diff --git a/test/options.h b/test/options.h new file mode 100644 index 000000000..9cb6c73d5 --- /dev/null +++ b/test/options.h @@ -0,0 +1,42 @@ +// 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 . + +#ifndef OPTIONS_H +#define OPTIONS_H + +#include +#include + +class options +{ +public: + options(int argc, const char* argv[]); + bool quiet() const; + bool gcc_style_errors() const; + const std::string& which_test() const; + +private: + options(); + options(const options& non_copy); + const options& operator =(const options& non_assign); + +private: + std::set _options; + const bool _gcc_style_errors; + const bool _quiet; +}; + +#endif diff --git a/test/testoptions.cpp b/test/testoptions.cpp new file mode 100644 index 000000000..4e654e52c --- /dev/null +++ b/test/testoptions.cpp @@ -0,0 +1,103 @@ +// 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 "options.h" +#include "testsuite.h" +#include + +extern std::ostringstream errout; + +class TestOptions: public TestFixture +{ +public: + TestOptions() + :TestFixture("TestOptions") + { + } + + +private: + void run() + { + TEST_CASE(which_test); + TEST_CASE(which_test_method); + TEST_CASE(not_quiet); + TEST_CASE(quiet); + TEST_CASE(gcc_errors); + TEST_CASE(multiple_testcases); + TEST_CASE(invalid_switches); + } + + + void which_test() + { + const char* argv[] = {"./test_runner", "TestClass"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS("TestClass", args.which_test()); + } + + + void which_test_method() + { + const char* argv[] = {"./test_runner", "TestClass::TestMethod"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS("TestClass::TestMethod", args.which_test()); + } + + + void not_quiet() + { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS(false, args.quiet()); + } + + + void quiet() + { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-q"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS(true, args.quiet()); + } + + + void gcc_errors() + { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-g"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS(true, args.gcc_style_errors()); + } + + + void multiple_testcases() + { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "Ignore::ThisOne"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS("TestClass::TestMethod", args.which_test()); + } + + + void invalid_switches() + { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-a", "-v", "-q", "-g"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS("TestClass::TestMethod", args.which_test()); + ASSERT_EQUALS(true, args.gcc_style_errors()); + ASSERT_EQUALS(true, args.quiet()); + } +}; + +REGISTER_TEST(TestOptions) diff --git a/test/testrunner.cpp b/test/testrunner.cpp index 4fae432d3..b47163821 100644 --- a/test/testrunner.cpp +++ b/test/testrunner.cpp @@ -18,10 +18,13 @@ #include #include "testsuite.h" +#include "options.h" int main(int argc, const char *argv[]) { - size_t ret = TestFixture::runTests((argc == 2) ? argv[1] : NULL); + options args(argc, argv); + + size_t ret = TestFixture::runTests(args); return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 8407e8d04..f7b59ac1f 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -16,11 +16,8 @@ * along with this program. If not, see . */ - - - - #include "testsuite.h" +#include "options.h" #include #include @@ -68,7 +65,10 @@ unsigned int TestFixture::countTests; size_t TestFixture::fails_counter = 0; size_t TestFixture::todos_counter = 0; -TestFixture::TestFixture(const std::string &_name) : classname(_name) +TestFixture::TestFixture(const std::string &_name) + :classname(_name) + ,gcc_style_errors(false) + ,quiet_tests(false) { TestRegistry::theInstance().addTest(this); } @@ -79,7 +79,14 @@ bool TestFixture::runTest(const char testname[]) if (testToRun.empty() || testToRun == testname) { ++countTests; - std::cout << classname << "::" << testname << "\n"; + if (quiet_tests) + { + std::cout << '.'; + } + else + { + std::cout << classname << "::" << testname << "\n"; + } return true; } return false; @@ -110,7 +117,14 @@ void TestFixture::assert(const char *filename, int linenr, bool condition) if (!condition) { ++fails_counter; - errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl; + if (gcc_style_errors) + { + errmsg << filename << ':' << linenr << ": Assertion failed." << std::endl; + } + else + { + errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl; + } } } @@ -119,12 +133,24 @@ void TestFixture::assertEquals(const char *filename, int linenr, const std::stri if (expected != actual) { ++fails_counter; - - errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl - << "Expected:" << std::endl - << writestr(expected) << std::endl - << "Actual:" << std::endl - << writestr(actual) << std::endl; + if (gcc_style_errors) + { + errmsg << filename << ':' << linenr << ": Assertion failed. " + << "Expected: " + << writestr(expected) + << ". Actual: " + << writestr(actual) + << "." + << std::endl; + } + else + { + errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl + << "Expected:" << std::endl + << writestr(expected) << std::endl + << "Actual:" << std::endl + << writestr(actual) << std::endl; + } if (!msg.empty()) { errmsg << msg << std::endl; @@ -144,9 +170,13 @@ void TestFixture::assertEquals(const char *filename, int linenr, double expected void TestFixture::todoAssertEquals(const char *filename, int linenr, const std::string &expected, const std::string &actual) { if (expected == actual) + { assertEquals(filename, linenr, "TODO assertion", "The assertion succeeded"); + } else + { ++todos_counter; + } } void TestFixture::todoAssertEquals(const char *filename, int linenr, unsigned int expected, unsigned int actual) @@ -161,9 +191,16 @@ void TestFixture::todoAssertEquals(const char *filename, int linenr, unsigned in void TestFixture::assertThrowFail(const char *filename, int linenr) { ++fails_counter; - - errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl - << "The expected exception was not thrown" << std::endl; + if (gcc_style_errors) + { + errmsg << filename << ':' << linenr << " Assertion failed. " + << "The expected exception was not thrown" << std::endl; + } + else + { + errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl + << "The expected exception was not thrown" << std::endl; + } } void TestFixture::printTests() @@ -179,12 +216,22 @@ void TestFixture::printTests() void TestFixture::run(const std::string &str) { testToRun = str; + if (quiet_tests) + { + std::cout << '\n' << classname << ':'; + } run(); } -size_t TestFixture::runTests(const char cmd[]) +void TestFixture::processOptions(const options& args) { - std::string classname(cmd ? cmd : ""); + quiet_tests = args.quiet(); + gcc_style_errors = args.gcc_style_errors(); +} + +size_t TestFixture::runTests(const options& args) +{ + std::string classname(args.which_test()); std::string testname(""); if (classname.find("::") != std::string::npos) { @@ -201,6 +248,7 @@ size_t TestFixture::runTests(const char cmd[]) { if (classname.empty() || (*it)->classname == classname) { + (*it)->processOptions(args); (*it)->run(testname); } } diff --git a/test/testsuite.h b/test/testsuite.h index a4aed70de..747255192 100644 --- a/test/testsuite.h +++ b/test/testsuite.h @@ -22,6 +22,9 @@ #include #include "errorlogger.h" +#include "redirect.h" + +class options; class Token; @@ -36,6 +39,8 @@ private: protected: std::string classname; std::string testToRun; + bool gcc_style_errors; + bool quiet_tests; virtual void run() = 0; @@ -52,7 +57,7 @@ protected: void todoAssertEquals(const char *filename, int linenr, const std::string &expected, const std::string &actual); void todoAssertEquals(const char *filename, int linenr, unsigned int expected, unsigned int actual); void assertThrowFail(const char *filename, int linenr); - + void processOptions(const options& args); public: virtual void reportOut(const std::string &outmsg); virtual void reportErr(const ErrorLogger::ErrorMessage &msg); @@ -63,11 +68,10 @@ public: virtual ~TestFixture() { } static void printTests(); - static size_t runTests(const char cmd[]); + static size_t runTests(const options& args); }; - -#define TEST_CASE( NAME ) if ( runTest(#NAME) ) NAME (); +#define TEST_CASE( NAME ) if ( runTest(#NAME) ) { if (quiet_tests) { REDIRECT; NAME(); } else { NAME ();} } #define ASSERT( CONDITION ) assert(__FILE__, __LINE__, CONDITION) #define ASSERT_EQUALS( EXPECTED , ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL) #define ASSERT_EQUALS_MSG( EXPECTED , ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG) diff --git a/tools/dmake.cpp b/tools/dmake.cpp index 8be058093..d6bebbbf0 100644 --- a/tools/dmake.cpp +++ b/tools/dmake.cpp @@ -225,6 +225,8 @@ int main(int argc, char **argv) fout << "\t$(CXX) $(CXXFLAGS) -o testrunner $(TESTOBJ) $(LIBOBJ) cli/threadexecutor.o cli/cmdlineparser.o $(LDFLAGS)\n\n"; fout << "test:\tall\n"; fout << "\t./testrunner\n\n"; + fout << "check:\tall\n"; + fout << "\t./testrunner -g -q\n\n"; fout << "dmake:\ttools/dmake.cpp\n"; fout << "\t$(CXX) -o dmake tools/dmake.cpp lib/filelister*.cpp\n\n"; fout << "clean:\n";