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