KGantt

testregistry.cpp
1 /*
2  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KGantt library.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #ifndef KDAB_NO_UNIT_TESTS
10 
11 #include "testregistry.h"
12 
13 #include "test.h"
14 
15 #include <memory>
16 #include <iostream>
17 #include <iomanip>
18 #include <cassert>
19 
20 KDAB::UnitTest::TestRegistry::TestRegistry()
21  : mTests()
22 {
23 
24 }
25 
26 KDAB::UnitTest::TestRegistry::~TestRegistry() {}
27 
28 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::mSelf = nullptr;
29 
30 // static
31 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::instance() {
32  if ( !mSelf )
33  mSelf = new TestRegistry;
34  return mSelf;
35 }
36 
37 // static
38 void KDAB::UnitTest::TestRegistry::deleteInstance() {
39  delete mSelf; mSelf = nullptr;
40 }
41 
42 void KDAB::UnitTest::TestRegistry::registerTestFactory( const TestFactory * tf, const char * group ) {
43  assert( tf );
44  mTests[group].push_back( tf );
45 }
46 
47 unsigned int KDAB::UnitTest::TestRegistry::run() const {
48  unsigned int failed = 0;
49  for ( std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.begin() ; g != mTests.end() ; ++g ) {
50  std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
51  for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
52  // once ported to unique_ptr, remove special flag -Wno-deprecated-declarations for this file
53  std::auto_ptr<Test> t( (*it)->create() );
54  assert( t.get() );
55  std::cerr << " === \"" << t->name() << "\" ===" << std::endl;
56  t->run();
57  std::cerr << " Succeeded: " << std::setw( 4 ) << t->succeeded()
58  << "; failed: " << std::setw( 4 ) << t->failed() << std::endl;
59  failed += t->failed();
60  }
61  }
62  return failed;
63 }
64 
65 
66 unsigned int KDAB::UnitTest::TestRegistry::run( const char * group ) const {
67  assert( group ); assert( *group );
68  unsigned int failed = 0;
69  const std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.find( group );
70  if ( g == mTests.end() ) {
71  std::cerr << "ERROR: No such group \"" << group << "\"" << std::endl;
72  return 1;
73  }
74  std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
75  for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
76  std::auto_ptr<Test> t( (*it)->create() );
77  assert( t.get() );
78  std::cerr << " === \"" << t->name() << "\" ===" << std::endl;
79  t->run();
80  std::cerr << " Succeeded: " << t->succeeded() << "; failed: " << t->failed() << std::endl;
81  failed += t->failed();
82  }
83  return failed;
84 }
85 
86 KDAB::UnitTest::Runner::~Runner()
87 {
88  TestRegistry::deleteInstance();
89 }
90 
91 unsigned int KDAB::UnitTest::Runner::run( const char * group ) const
92 {
93  if ( group && *group )
94  return TestRegistry::instance()->run( group );
95  else
96  return TestRegistry::instance()->run();
97 }
98 
99 
100 #endif // KDAB_NO_UNIT_TESTS
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 22:32:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.