nut-debian/tests/cpputest.cpp

94 lines
3.3 KiB
C++
Raw Permalink Normal View History

2012-08-13 00:39:31 +03:00
/* cpputest - basic runner for unit tests
Copyright (C)
2012 Emilien Kia <emilienkia-guest@alioth.debian.org>
2022-07-10 10:23:45 +03:00
2020 Jim Klimov <jimklimov@gmail.com>
2012-08-13 00:39:31 +03: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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2022-07-10 10:23:45 +03:00
#include <stdexcept>
#include <cppunit/TestResult.h>
2012-08-13 00:39:31 +03:00
#include <cppunit/CompilerOutputter.h>
2022-07-10 10:23:45 +03:00
#include <cppunit/TextTestProgressListener.h>
2012-08-13 00:39:31 +03:00
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
2022-07-10 10:23:45 +03:00
#include "common.h"
2012-08-13 00:39:31 +03:00
2022-07-10 10:23:45 +03:00
// Inspired by https://stackoverflow.com/a/66702001
class MyCustomProgressTestListener : public CppUnit::TextTestProgressListener {
public:
virtual void startTest(CppUnit::Test *test) override;
};
// Implement out of class declaration to avoid
// error: 'MyCustomProgressTestListener' has no out-of-line virtual method
// definitions; its vtable will be emitted in every translation unit
// [-Werror,-Wweak-vtables]
void MyCustomProgressTestListener::startTest(CppUnit::Test *test) {
//fprintf(stderr, "starting test %s\n", test->getName().c_str());
std::cerr << "starting test " << test->getName() << std::endl;
}
2012-08-13 00:39:31 +03:00
int main(int argc, char* argv[])
{
2022-07-10 10:23:45 +03:00
bool verbose = false;
if (argc > 1) {
if (strcmp("-v", argv[1]) == 0 || strcmp("--verbose", argv[1]) == 0 ) {
verbose = true;
}
}
2012-08-13 00:39:31 +03:00
/* Get the top level suite from the registry */
2022-07-10 10:23:45 +03:00
std::cerr << "D: Getting test suite..." << std::endl;
2012-08-13 00:39:31 +03:00
CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
/* Adds the test to the list of test to run */
2022-07-10 10:23:45 +03:00
std::cerr << "D: Preparing test runner..." << std::endl;
2012-08-13 00:39:31 +03:00
CppUnit::TextUi::TestRunner runner;
runner.addTest( suite );
/* Change the default outputter to a compiler error format outputter */
2022-07-10 10:23:45 +03:00
std::cerr << "D: Setting test runner outputter..." << std::endl;
2012-08-13 00:39:31 +03:00
runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
std::cerr ) );
2022-07-10 10:23:45 +03:00
if (verbose) {
/* Add a listener to report test names */
std::cerr << "D: Setting test runner listener for test names..." << std::endl;
MyCustomProgressTestListener progress;
runner.eventManager().addListener(&progress);
}
2012-08-13 00:39:31 +03:00
/* Run the tests. */
2022-07-10 10:23:45 +03:00
bool wasSucessful = false;
try {
std::cerr << "D: Launching the test run..." << std::endl;
wasSucessful = runner.run();
}
catch ( std::invalid_argument &e ) // Test path not resolved
{
std::cerr << std::endl
<< "ERROR: " << e.what()
<< std::endl;
wasSucessful = false;
}
2012-08-13 00:39:31 +03:00
/* Return error code 1 if the one of test failed. */
2022-07-10 10:23:45 +03:00
std::cerr << "D: Got to the end of test suite with code " <<
"'" << ( wasSucessful ? "true" : "false" ) << "'" << std::endl;
2012-08-13 00:39:31 +03:00
return wasSucessful ? 0 : 1;
}