1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KGantt library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef KDAB_NO_UNIT_TESTS
21 
22 #include "testregistry.h"
23 
24 #include "test.h"
25 
26 #include <memory>
27 #include <iostream>
28 #include <iomanip>
29 #include <cassert>
30 
TestRegistry()31 KDAB::UnitTest::TestRegistry::TestRegistry()
32     : mTests()
33 {
34 
35 }
36 
~TestRegistry()37 KDAB::UnitTest::TestRegistry::~TestRegistry() {}
38 
39 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::mSelf = nullptr;
40 
41 // static
instance()42 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::instance() {
43     if ( !mSelf )
44         mSelf = new TestRegistry;
45     return mSelf;
46 }
47 
48 // static
deleteInstance()49 void KDAB::UnitTest::TestRegistry::deleteInstance() {
50     delete mSelf; mSelf = nullptr;
51 }
52 
registerTestFactory(const TestFactory * tf,const char * group)53 void KDAB::UnitTest::TestRegistry::registerTestFactory( const TestFactory * tf, const char * group ) {
54     assert( tf );
55     mTests[group].push_back( tf );
56 }
57 
run() const58 unsigned int KDAB::UnitTest::TestRegistry::run() const {
59   unsigned int failed = 0;
60   for ( std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.begin() ; g != mTests.end() ; ++g ) {
61     std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
62     for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
63       // once ported to unique_ptr, remove special flag -Wno-deprecated-declarations for this file
64       std::auto_ptr<Test> t( (*it)->create() );
65       assert( t.get() );
66       std::cerr << "  === \"" << t->name() << "\" ===" << std::endl;
67       t->run();
68       std::cerr << "    Succeeded: " << std::setw( 4 ) << t->succeeded()
69                 << ";  failed: " << std::setw( 4 ) << t->failed() << std::endl;
70       failed += t->failed();
71     }
72   }
73   return failed;
74 }
75 
76 
run(const char * group) const77 unsigned int KDAB::UnitTest::TestRegistry::run( const char * group ) const {
78   assert( group ); assert( *group );
79   unsigned int failed = 0;
80   const std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.find( group );
81   if ( g == mTests.end() ) {
82     std::cerr << "ERROR: No such group \"" << group << "\"" << std::endl;
83     return 1;
84   }
85   std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
86   for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
87     std::auto_ptr<Test> t( (*it)->create() );
88     assert( t.get() );
89     std::cerr << "  === \"" << t->name() << "\" ===" << std::endl;
90     t->run();
91     std::cerr << "    Succeeded: " << t->succeeded() << ";  failed: " << t->failed() << std::endl;
92     failed += t->failed();
93   }
94   return failed;
95 }
96 
~Runner()97 KDAB::UnitTest::Runner::~Runner()
98 {
99 	TestRegistry::deleteInstance();
100 }
101 
run(const char * group) const102 unsigned int KDAB::UnitTest::Runner::run( const char * group ) const
103 {
104   if ( group && *group )
105     return TestRegistry::instance()->run( group );
106   else
107     return TestRegistry::instance()->run();
108 }
109 
110 
111 #endif // KDAB_NO_UNIT_TESTS
112