KD Chart 2  [rev.2.6]
testregistry.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2019 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #ifndef KDAB_NO_UNIT_TESTS
24 
25 #include "testregistry.h"
26 
27 #include "test.h"
28 
29 #include <memory>
30 #include <iostream>
31 #include <iomanip>
32 #include <cassert>
33 
34 KDAB::UnitTest::TestRegistry::TestRegistry()
35  : mTests()
36 {
37 
38 }
39 
40 KDAB::UnitTest::TestRegistry::~TestRegistry() {}
41 
42 KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::mSelf = 0;
43 
44 // static
46  if ( !mSelf )
47  mSelf = new TestRegistry;
48  return mSelf;
49 }
50 
51 // static
53  delete mSelf; mSelf = 0;
54 }
55 
56 void KDAB::UnitTest::TestRegistry::registerTestFactory( const TestFactory * tf, const char * group ) {
57  assert( tf );
58  mTests[group].push_back( tf );
59 }
60 
61 unsigned int KDAB::UnitTest::TestRegistry::run() const {
62  unsigned int failed = 0;
63  for ( std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.begin() ; g != mTests.end() ; ++g ) {
64  std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
65  for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
66  std::auto_ptr<Test> t( (*it)->create() );
67  assert( t.get() );
68  std::cerr << " === \"" << t->name() << "\" ===" << std::endl;
69  t->run();
70  std::cerr << " Succeeded: " << std::setw( 4 ) << t->succeeded()
71  << "; failed: " << std::setw( 4 ) << t->failed() << std::endl;
72  failed += t->failed();
73  }
74  }
75  return failed;
76 }
77 
78 
79 unsigned int KDAB::UnitTest::TestRegistry::run( const char * group ) const {
80  assert( group ); assert( *group );
81  unsigned int failed = 0;
82  const std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.find( group );
83  if ( g == mTests.end() ) {
84  std::cerr << "ERROR: No such group \"" << group << "\"" << std::endl;
85  return 1;
86  }
87  std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
88  for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
89  std::auto_ptr<Test> t( (*it)->create() );
90  assert( t.get() );
91  std::cerr << " === \"" << t->name() << "\" ===" << std::endl;
92  t->run();
93  std::cerr << " Succeeded: " << t->succeeded() << "; failed: " << t->failed() << std::endl;
94  failed += t->failed();
95  }
96  return failed;
97 }
98 
100 {
102 }
103 
104 unsigned int KDAB::UnitTest::Runner::run( const char * group ) const
105 {
106  if ( group && *group )
107  return TestRegistry::instance()->run( group );
108  else
109  return TestRegistry::instance()->run();
110 }
111 
112 
113 #endif // KDAB_NO_UNIT_TESTS
static TestRegistry * instance()
void registerTestFactory(const TestFactory *tf, const char *group)
unsigned int run(const char *group=0) const
unsigned int run() const

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
https://www.kdab.com/
https://www.kdab.com/products/kd-chart/