9#ifndef KDAB_NO_UNIT_TESTS
11#include "testregistry.h"
19KDAB::UnitTest::TestRegistry::TestRegistry()
25KDAB::UnitTest::TestRegistry::~TestRegistry() {}
27KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::mSelf =
nullptr;
30KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::instance() {
32 mSelf =
new TestRegistry;
37void KDAB::UnitTest::TestRegistry::deleteInstance() {
38 delete mSelf; mSelf =
nullptr;
41void KDAB::UnitTest::TestRegistry::registerTestFactory(
const TestFactory * tf,
const char * group ) {
43 mTests[group].push_back( tf );
46unsigned int KDAB::UnitTest::TestRegistry::run()
const {
47 unsigned int failed = 0;
48 for ( std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.begin() ; g != mTests.end() ; ++g ) {
49 std::cerr <<
"===== GROUP \"" << g->first <<
"\" =========" << std::endl;
50 for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
51 std::unique_ptr<Test> t( (*it)->create() );
53 std::cerr <<
" === \"" << t->name() <<
"\" ===" << std::endl;
55 std::cerr <<
" Succeeded: " << std::setw( 4 ) << t->succeeded()
56 <<
"; failed: " << std::setw( 4 ) << t->failed() << std::endl;
57 failed += t->failed();
64unsigned int KDAB::UnitTest::TestRegistry::run(
const char * group )
const {
65 assert( group ); assert( *group );
66 unsigned int failed = 0;
67 const std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.find( group );
68 if ( g == mTests.end() ) {
69 std::cerr <<
"ERROR: No such group \"" << group <<
"\"" << std::endl;
72 std::cerr <<
"===== GROUP \"" << g->first <<
"\" =========" << std::endl;
73 for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
74 std::unique_ptr<Test> t( (*it)->create() );
76 std::cerr <<
" === \"" << t->name() <<
"\" ===" << std::endl;
78 std::cerr <<
" Succeeded: " << t->succeeded() <<
"; failed: " << t->failed() << std::endl;
79 failed += t->failed();
84KDAB::UnitTest::Runner::~Runner()
86 TestRegistry::deleteInstance();
89unsigned int KDAB::UnitTest::Runner::run(
const char * group )
const
91 if ( group && *group )
92 return TestRegistry::instance()->run( group );
94 return TestRegistry::instance()->run();