File indexing completed on 2024-12-15 04:02:45

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KDAB_NO_UNIT_TESTS
0010 
0011 #include "testregistry.h"
0012 
0013 #include "test.h"
0014 
0015 #include <memory>
0016 #include <iostream>
0017 #include <iomanip>
0018 
0019 KDAB::UnitTest::TestRegistry::TestRegistry()
0020     : mTests()
0021 {
0022 
0023 }
0024 
0025 KDAB::UnitTest::TestRegistry::~TestRegistry() {}
0026 
0027 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::mSelf = nullptr;
0028 
0029 // static
0030 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::instance() {
0031     if ( !mSelf )
0032         mSelf = new TestRegistry;
0033     return mSelf;
0034 }
0035 
0036 // static
0037 void KDAB::UnitTest::TestRegistry::deleteInstance() {
0038     delete mSelf; mSelf = nullptr;
0039 }
0040 
0041 void KDAB::UnitTest::TestRegistry::registerTestFactory( const TestFactory * tf, const char * group ) {
0042     assert( tf );
0043     mTests[group].push_back( tf );
0044 }
0045 
0046 unsigned int KDAB::UnitTest::TestRegistry::run() const {
0047   unsigned int failed = 0;
0048   for ( std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.begin() ; g != mTests.end() ; ++g ) {
0049     std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
0050     for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
0051       std::unique_ptr<Test> t( (*it)->create() );
0052       assert( t.get() );
0053       std::cerr << "  === \"" << t->name() << "\" ===" << std::endl;
0054       t->run();
0055       std::cerr << "    Succeeded: " << std::setw( 4 ) << t->succeeded()
0056                 << ";  failed: " << std::setw( 4 ) << t->failed() << std::endl;
0057       failed += t->failed();
0058     }
0059   }
0060   return failed;
0061 }
0062 
0063 
0064 unsigned int KDAB::UnitTest::TestRegistry::run( const char * group ) const {
0065   assert( group ); assert( *group );
0066   unsigned int failed = 0;
0067   const std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.find( group );
0068   if ( g == mTests.end() ) {
0069     std::cerr << "ERROR: No such group \"" << group << "\"" << std::endl;
0070     return 1;
0071   }
0072   std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
0073   for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
0074     std::unique_ptr<Test> t( (*it)->create() );
0075     assert( t.get() );
0076     std::cerr << "  === \"" << t->name() << "\" ===" << std::endl;
0077     t->run();
0078     std::cerr << "    Succeeded: " << t->succeeded() << ";  failed: " << t->failed() << std::endl;
0079     failed += t->failed();
0080   }
0081   return failed;
0082 }
0083 
0084 KDAB::UnitTest::Runner::~Runner()
0085 {
0086     TestRegistry::deleteInstance();
0087 }
0088 
0089 unsigned int KDAB::UnitTest::Runner::run( const char * group ) const
0090 {
0091   if ( group && *group )
0092     return TestRegistry::instance()->run( group );
0093   else
0094     return TestRegistry::instance()->run();
0095 }
0096 
0097 
0098 #endif // KDAB_NO_UNIT_TESTS