dodo  0.0.1
A C++ library to create containerized Linux services
unittest.hpp
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 puts.hpp
20  * Defines the dodo::common::Puts class.
21  */
22 
23 #ifndef common_unittest_hpp
24 #define common_unittest_hpp
25 
26 #include <iostream>
27 #include <string>
28 
29 namespace dodo::common {
30 
31  /**
32  * Unit test assitence class.
33  */
34  class UnitTest {
35  public:
36 
37  /**
38  * Create a test
39  * @param name The name of the test.
40  * @param description The description of the test.
41  * @param out The ostream to write to.
42  */
43  UnitTest( const std::string &name, const std::string &description, std::ostream *out );
44 
45  /**
46  * Destructor.
47  */
48  virtual ~UnitTest() {};
49 
50  /**
51  * Run the test.
52  * @return True if the test passed.
53  */
54  bool run();
55 
56  protected:
57 
58  /**
59  * Virtual to implement the test.
60  */
61  virtual void doRun() = 0;
62 
63  /**
64  * Write the subtest result.
65  * @param name The name of the test.
66  * @param description The description of the test.
67  * @param passed True if the test passed.
68  * @return The value of parameter passed.
69  */
70  bool writeSubTestResult( const std::string &name, const std::string& description, bool passed );
71 
72  private:
73 
74  /** Write the header. */
75  void writeUnitTestHeader();
76 
77  /** Write the summary. */
78  void writeUnitTestSummary();
79 
80  /** The test name. */
81  std::string name_;
82 
83  /** The test description. */
84  std::string description_;
85 
86  /** The output destination. */
87  std::ostream *out_;
88 
89  /** The total number of tests. */
90  size_t total_;
91 
92  /** The total number of failed tests. */
93  size_t failed_;
94  };
95 
96 }
97 
98 #endif
dodo::common::UnitTest::~UnitTest
virtual ~UnitTest()
Destructor.
Definition: unittest.hpp:48
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::UnitTest
Unit test assitence class.
Definition: unittest.hpp:34
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