dodo  0.0.1
A C++ library to create containerized Linux services
unittest.cpp
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file socket.cpp
20  * Implements the dodo::network::Socket class.
21  */
22 
23 #include <common/unittest.hpp>
24 
25 namespace dodo::common {
26 
27  UnitTest::UnitTest( const std::string& name, const std::string& description, std::ostream *out ) :
28  name_(name),
29  description_(description),
30  out_(out),
31  total_(0),
32  failed_(0) {
33  }
34 
35  bool UnitTest::run() {
37  doRun();
39  return failed_ == 0;
40  }
41 
43  *out_ << std::string( 80, '=' ) << std::endl;
44  *out_ << name_ << " - " << description_ << std::endl;
45  *out_ << std::string( 80, '=' ) << std::endl;
46  *out_ << std::endl;
47  }
48 
50  *out_ << std::string( 80, '+' ) << std::endl;
51  *out_ << failed_ << " failed out of " << total_ << " tests." << std::endl;
52  *out_ << std::endl;
53  }
54 
55  bool UnitTest::writeSubTestResult( const std::string &name, const std::string& description, bool passed ) {
56  total_++;
57  if ( !passed ) failed_++;
58  *out_ << std::string( 80, '-' ) << std::endl;
59  *out_ << name << " - " << description << " : " << (passed?"passed":"failure") << std::endl;
60  *out_ << std::string( 80, '-' ) << std::endl;
61  *out_ << std::endl;
62  return passed;
63  }
64 
65 }
dodo::common::UnitTest::doRun
virtual void doRun()=0
Virtual to implement the test.
dodo::common::UnitTest::name_
std::string name_
The test name.
Definition: unittest.hpp:81
dodo::common::UnitTest::failed_
size_t failed_
The total number of failed tests.
Definition: unittest.hpp:93
dodo::common::UnitTest::writeUnitTestHeader
void writeUnitTestHeader()
Write the header.
Definition: unittest.cpp:42
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::UnitTest::writeSubTestResult
bool writeSubTestResult(const std::string &name, const std::string &description, bool passed)
Write the subtest result.
Definition: unittest.cpp:55
dodo::common::UnitTest::total_
size_t total_
The total number of tests.
Definition: unittest.hpp:90
dodo::common::UnitTest::description_
std::string description_
The test description.
Definition: unittest.hpp:84
dodo::common::UnitTest::UnitTest
UnitTest(const std::string &name, const std::string &description, std::ostream *out)
Create a test.
Definition: unittest.cpp:27
dodo::common::UnitTest::writeUnitTestSummary
void writeUnitTestSummary()
Write the summary.
Definition: unittest.cpp:49
dodo::common::UnitTest::run
bool run()
Run the test.
Definition: unittest.cpp:35
dodo::common::UnitTest::out_
std::ostream * out_
The output destination.
Definition: unittest.hpp:87