00001 #ifndef KDAB_NO_UNIT_TESTS
00002
00003 #include "testregistry.h"
00004
00005 #include "test.h"
00006
00007 #include <memory>
00008 #include <iostream>
00009 #include <iomanip>
00010 #include <cassert>
00011
00012 KDAB::UnitTest::TestRegistry::TestRegistry()
00013 : mTests()
00014 {
00015
00016 }
00017
00018 KDAB::UnitTest::TestRegistry::~TestRegistry() {}
00019
00020 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::mSelf = 0;
00021
00022
00023 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::instance() {
00024 if ( !mSelf )
00025 mSelf = new TestRegistry;
00026 return mSelf;
00027 }
00028
00029
00030 void KDAB::UnitTest::TestRegistry::deleteInstance() {
00031 delete mSelf; mSelf = 0;
00032 }
00033
00034 void KDAB::UnitTest::TestRegistry::registerTestFactory( const TestFactory * tf, const char * group ) {
00035 assert( tf );
00036 mTests[group].push_back( tf );
00037 }
00038
00039 unsigned int KDAB::UnitTest::TestRegistry::run() const {
00040 unsigned int failed = 0;
00041 for ( std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.begin() ; g != mTests.end() ; ++g ) {
00042 std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
00043 for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
00044 std::auto_ptr<Test> t( (*it)->create() );
00045 assert( t.get() );
00046 std::cerr << " === \"" << t->name() << "\" ===" << std::endl;
00047 t->run();
00048 std::cerr << " Succeeded: " << std::setw( 4 ) << t->succeeded()
00049 << "; failed: " << std::setw( 4 ) << t->failed() << std::endl;
00050 failed += t->failed();
00051 }
00052 }
00053 return failed;
00054 }
00055
00056
00057 unsigned int KDAB::UnitTest::TestRegistry::run( const char * group ) const {
00058 assert( group ); assert( *group );
00059 unsigned int failed = 0;
00060 const std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.find( group );
00061 if ( g == mTests.end() ) {
00062 std::cerr << "ERROR: No such group \"" << group << "\"" << std::endl;
00063 return 1;
00064 }
00065 std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
00066 for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
00067 std::auto_ptr<Test> t( (*it)->create() );
00068 assert( t.get() );
00069 std::cerr << " === \"" << t->name() << "\" ===" << std::endl;
00070 t->run();
00071 std::cerr << " Succeeded: " << t->succeeded() << "; failed: " << t->failed() << std::endl;
00072 failed += t->failed();
00073 }
00074 return failed;
00075 }
00076
00077 KDAB::UnitTest::Runner::~Runner()
00078 {
00079 TestRegistry::deleteInstance();
00080 }
00081
00082 unsigned int KDAB::UnitTest::Runner::run( const char * group ) const
00083 {
00084 if ( group && *group )
00085 return TestRegistry::instance()->run( group );
00086 else
00087 return TestRegistry::instance()->run();
00088 }
00089
00090
00091 #endif // KDAB_NO_UNIT_TESTS