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
19KDAB::UnitTest::TestRegistry::TestRegistry()
20 : mTests()
21{
22
23}
24
25KDAB::UnitTest::TestRegistry::~TestRegistry() {}
26
27KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::mSelf = nullptr;
28
29// static
30KDAB::UnitTest::TestRegistry * KDAB::UnitTest::TestRegistry::instance() {
31 if ( !mSelf )
32 mSelf = new TestRegistry;
33 return mSelf;
34}
35
36// static
37void KDAB::UnitTest::TestRegistry::deleteInstance() {
38 delete mSelf; mSelf = nullptr;
39}
40
41void KDAB::UnitTest::TestRegistry::registerTestFactory( const TestFactory * tf, const char * group ) {
42 assert( tf );
43 mTests[group].push_back( tf );
44}
45
46unsigned int KDAB::UnitTest::TestRegistry::run() const {
47 unsigned int failed = 0;
48 for ( std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.begin() ; g != mTests.end() ; ++g ) {
49 std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
50 for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
51 std::unique_ptr<Test> t( (*it)->create() );
52 assert( t.get() );
53 std::cerr << " === \"" << t->name() << "\" ===" << std::endl;
54 t->run();
55 std::cerr << " Succeeded: " << std::setw( 4 ) << t->succeeded()
56 << "; failed: " << std::setw( 4 ) << t->failed() << std::endl;
57 failed += t->failed();
58 }
59 }
60 return failed;
61}
62
63
64unsigned int KDAB::UnitTest::TestRegistry::run( const char * group ) const {
65 assert( group ); assert( *group );
66 unsigned int failed = 0;
67 const std::map< std::string, std::vector<const TestFactory*> >::const_iterator g = mTests.find( group );
68 if ( g == mTests.end() ) {
69 std::cerr << "ERROR: No such group \"" << group << "\"" << std::endl;
70 return 1;
71 }
72 std::cerr << "===== GROUP \"" << g->first << "\" =========" << std::endl;
73 for ( std::vector<const TestFactory*>::const_iterator it = g->second.begin() ; it != g->second.end() ; ++it ) {
74 std::unique_ptr<Test> t( (*it)->create() );
75 assert( t.get() );
76 std::cerr << " === \"" << t->name() << "\" ===" << std::endl;
77 t->run();
78 std::cerr << " Succeeded: " << t->succeeded() << "; failed: " << t->failed() << std::endl;
79 failed += t->failed();
80 }
81 return failed;
82}
83
84KDAB::UnitTest::Runner::~Runner()
85{
86 TestRegistry::deleteInstance();
87}
88
89unsigned int KDAB::UnitTest::Runner::run( const char * group ) const
90{
91 if ( group && *group )
92 return TestRegistry::instance()->run( group );
93 else
94 return TestRegistry::instance()->run();
95}
96
97
98#endif // KDAB_NO_UNIT_TESTS
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Sep 27 2024 11:49:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.