|
GammaLib 2.2.0.dev
|
Abstract test suite class for unit testing on GammaLib fixtures. More...
#include <GTestSuite.hpp>
Public Member Functions | |
| GTestSuite (void) | |
| Void constructor. | |
| GTestSuite (const GTestSuite &testsuite) | |
| Copy constructor. | |
| GTestSuite (const std::string &name) | |
| Name constructor. | |
| virtual | ~GTestSuite (void) |
| Destructor. | |
| GTestSuite & | operator= (const GTestSuite &testsuite) |
| Assignment operator. | |
| GTestCase & | operator[] (const int &index) |
| Returns reference to test case. | |
| const GTestCase & | operator[] (const int &index) const |
| Returns reference to test case. | |
| virtual GTestSuite * | clone (void) const =0 |
| Clones object. | |
| virtual std::string | classname (void) const =0 |
| Return class name. | |
| virtual void | set (void)=0 |
| void | clear (void) |
| Clear test suite. | |
| int | size (void) const |
| Return number of tests in test suite. | |
| void | append (pfunction function, const std::string &name) |
| Append test functions to test suite. | |
| bool | run (void) |
| Run all tests in test suite. | |
| const std::string & | name (void) const |
| Return test suite name. | |
| void | name (const std::string &name) |
| Set Test Suite name. | |
| void | cout (const bool &flag) |
| Enables/disables logging into standard output stream. | |
| void | test_assert (const bool &result, const std::string &name, const std::string &message="") |
| Test an assert. | |
| void | test_value (const int &value, const int &expected, const std::string &name="", const std::string &message="") |
| Test an integer value. | |
| void | test_value (const double &value, const double &expected, const std::string &name="", const std::string &message="") |
| Test a double precision value. | |
| void | test_value (const double &value, const double &expected, const double &eps, const std::string &name="", const std::string &message="") |
| Test a double precision value. | |
| void | test_value (const std::complex< double > &value, const std::complex< double > &expected, const std::string &name="", const std::string &message="") |
| Test a complex value. | |
| void | test_value (const std::complex< double > &value, const std::complex< double > &expected, const double &eps, const std::string &name="", const std::string &message="") |
| Test a complex value. | |
| void | test_value (const std::string &value, const std::string &expected, const std::string &name="", const std::string &message="") |
| Test a string value. | |
| void | test_try (const std::string &name) |
| Test an try block. | |
| void | test_try_success (void) |
| Notice when a try block succeeded. | |
| void | test_try_failure (const std::string &message="", const std::string &type="") |
| Notice when a try block failed. | |
| void | test_try_failure (const std::exception &e) |
| Notice when a try block failed. | |
| GException::test_failure & | exception_failure (const std::string &message) |
| Return a failure exception. | |
| GException::test_error & | exception_error (const std::string &message) |
| Return an error exception. | |
| const int & | errors (void) const |
| Return the number of errors. | |
| const int & | failures (void) const |
| Return the number of failures. | |
| int | success (void) const |
| Return the number of successful tests. | |
| const time_t & | timestamp (void) const |
| Return the timestamp. | |
| double | duration (void) const |
| Return the total duration of all tests. | |
| std::string | print (const GChatter &chatter=NORMAL) const |
| Print test suite information. | |
Public Member Functions inherited from GBase | |
| virtual | ~GBase (void) |
| Destructor. | |
Protected Member Functions | |
| void | init_members (void) |
| Initialise class members. | |
| void | copy_members (const GTestSuite &testsuite) |
| Copy class members. | |
| void | free_members (void) |
| Delete class members. | |
| std::string | format_name (const std::string &name) |
| Format Name. | |
Protected Attributes | |
| std::string | m_name |
| Name of the test suite. | |
| std::vector< std::string > | m_names |
| Names of test functions. | |
| std::vector< pfunction > | m_functions |
| Test functions of this suite. | |
| std::vector< GTestCase * > | m_tests |
| List of test results. | |
| std::vector< GTestCase * > | m_stack_try |
| Stack for nested try blocks. | |
| int | m_index |
| Index of actual test function. | |
| int | m_failures |
| Number of failures. | |
| int | m_errors |
| Number of errors. | |
| GLog | m_log |
| Log. | |
| time_t | m_timestamp |
| Timestamp. | |
Abstract test suite class for unit testing on GammaLib fixtures.
Definition at line 53 of file GTestSuite.hpp.
| GTestSuite::GTestSuite | ( | void | ) |
| GTestSuite::GTestSuite | ( | const GTestSuite & | suite | ) |
Copy constructor.
| [in] | suite | Test Suite. |
Definition at line 83 of file GTestSuite.cpp.
References copy_members(), and init_members().
| GTestSuite::GTestSuite | ( | const std::string & | name | ) |
Name constructor.
| [in] | name | Test suite name. |
Definition at line 101 of file GTestSuite.cpp.
References init_members(), m_name, and name().
|
virtual |
| void GTestSuite::append | ( | pfunction | function, |
| const std::string & | name ) |
Append test functions to test suite.
| [in] | function | Test function pointer. |
| [in] | name | Test name. |
This method adds test functions to the test suite. The test functions will be executed when the run method is called.
Definition at line 234 of file GTestSuite.cpp.
References m_functions, m_names, and name().
|
pure virtual |
Return class name.
Returns the class name for non-abstract classes in a human readable way.
Implements GBase.
|
virtual |
Clear test suite.
Implements GBase.
Definition at line 212 of file GTestSuite.cpp.
References free_members(), and init_members().
|
pure virtual |
Clones object.
Creates a deep copy of the object and returns a pointer to the object.
Implements GBase.
Referenced by GTestSuites::append(), GTestSuites::insert(), and GTestSuites::set().
|
protected |
Copy class members.
| [in] | suite | Test suite. |
This method just clone the container not the test case.
Definition at line 1038 of file GTestSuite.cpp.
References m_errors, m_failures, m_functions, m_index, m_log, m_name, m_names, m_stack_try, m_tests, and m_timestamp.
Referenced by GTestSuite(), and operator=().
|
inline |
Enables/disables logging into standard output stream.
| [in] | flag | Enable/disable logging (true/false). |
Enables or disables logging into the standard output stream.
Definition at line 185 of file GTestSuite.hpp.
References GLog::cout(), and m_log.
Referenced by init_members().
| double GTestSuite::duration | ( | void | ) | const |
Return the total duration of all tests.
This method sums up all test durations and returns the result.
Definition at line 950 of file GTestSuite.cpp.
References duration(), and m_tests.
Referenced by duration(), and GTestSuites::write().
|
inline |
Return the number of errors.
Definition at line 198 of file GTestSuite.hpp.
References m_errors.
Referenced by print(), run(), and GTestSuites::write().
| GException::test_error & GTestSuite::exception_error | ( | const std::string & | message | ) |
Return an error exception.
| [in] | message | Message. |
It can be use in a try test
Example: test_try("Test a try block"); try { throw exception_error("an error"); test_try_success(); } catch(exception& e) { test_try_failure(e); }
Definition at line 928 of file GTestSuite.cpp.
References m_stack_try.
| GException::test_failure & GTestSuite::exception_failure | ( | const std::string & | message | ) |
Return a failure exception.
| [in] | message | Message. |
It can be use in a try test
Example: test_try("Test a try block"); try { throw exception_failure("a failure"); test_try_success(); } catch(exception& e) { test_try_failure(e); }
Definition at line 901 of file GTestSuite.cpp.
References m_stack_try.
|
inline |
Return the number of failures.
Definition at line 210 of file GTestSuite.hpp.
References m_failures.
Referenced by print(), run(), and GTestSuites::write().
|
protected |
Format Name.
Return a string with the format "TestFunctionName:TestTryname1:TestTryName2: name"
Definition at line 1096 of file GTestSuite.cpp.
References format_name(), m_index, m_names, m_stack_try, and name().
Referenced by format_name(), test_assert(), test_try(), test_value(), test_value(), test_value(), and test_value().
|
protected |
Delete class members.
Definition at line 1068 of file GTestSuite.cpp.
References GLog::close(), m_log, m_stack_try, and m_tests.
Referenced by clear(), operator=(), and ~GTestSuite().
|
protected |
Initialise class members.
Definition at line 1009 of file GTestSuite.cpp.
References GLog::buffer_size(), GLog::clear(), cout(), m_errors, m_failures, m_functions, m_index, m_log, m_name, m_names, m_stack_try, m_tests, and m_timestamp.
Referenced by clear(), GTestSuite(), GTestSuite(), GTestSuite(), and operator=().
|
inline |
Set Test Suite name.
| [in] | name | Test suite name. |
Definition at line 170 of file GTestSuite.hpp.
|
inline |
Return test suite name.
Definition at line 158 of file GTestSuite.hpp.
References m_name.
Referenced by append(), format_name(), GTestSuite(), name(), test_assert(), test_try(), test_value(), test_value(), test_value(), test_value(), test_value(), test_value(), and GTestSuites::write().
| GTestSuite & GTestSuite::operator= | ( | const GTestSuite & | suite | ) |
Assignment operator.
| [in] | suite | Test suite. |
Definition at line 139 of file GTestSuite.cpp.
References copy_members(), free_members(), and init_members().
| GTestCase & GTestSuite::operator[] | ( | const int & | index | ) |
Returns reference to test case.
| [in] | index | Test case index [0,...,size()-1]. |
| GException::out_of_range | Test case index is out of range. |
Definition at line 168 of file GTestSuite.cpp.
References G_OP_ACCESS, m_tests, and size().
| const GTestCase & GTestSuite::operator[] | ( | const int & | index | ) | const |
Returns reference to test case.
| [in] | index | Test case index [0,...,size()-1]. |
| GException::out_of_range | Test case index is out of range. |
Definition at line 189 of file GTestSuite.cpp.
References G_OP_ACCESS, m_tests, and size().
Print test suite information.
| [in] | chatter | Chattiness (defaults to NORMAL). |
Implements GBase.
Definition at line 971 of file GTestSuite.cpp.
References errors(), failures(), m_name, m_names, gammalib::parformat(), SILENT, size(), and gammalib::str().
| bool GTestSuite::run | ( | void | ) |
Run all tests in test suite.
Executes all test functions that have been appended to the test suite. For each test function a test case is added to the test suite.
Definition at line 253 of file GTestSuite.cpp.
References GTestCase::duration(), GTestCase::ERROR_TEST, errors(), failures(), GTestCase::has_passed(), m_errors, m_failures, m_functions, m_index, m_names, m_tests, GTestCase::message(), GTestCase::print(), set(), success(), and GTestCase::type().
|
pure virtual |
Referenced by run().
|
inline |
Return number of tests in test suite.
Definition at line 148 of file GTestSuite.hpp.
References m_tests.
Referenced by operator[](), operator[](), print(), success(), and GTestSuites::write().
| int GTestSuite::success | ( | void | ) | const |
Return the number of successful tests.
Definition at line 938 of file GTestSuite.cpp.
References m_errors, m_failures, and size().
Referenced by run().
| void GTestSuite::test_assert | ( | const bool & | assert, |
| const std::string & | name, | ||
| const std::string & | message = "" ) |
Test an assert.
| [in] | assert | Assert (true/false). |
| [in] | name | Test case name. |
| [in] | message | Test case name (defaults to ""). |
Tests if a condition is true or false. This method adds a test case of type "failure" to the test suite.
Examples: test_assert(x>3, "Test if x > 3"); test_assert(x>3 && x<10, "Test if 3 < x < 10 ");
Definition at line 368 of file GTestSuite.cpp.
References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), name(), and GTestCase::print().
| void GTestSuite::test_try | ( | const std::string & | name | ) |
Test an try block.
| [in] | name | Test case name (defaults to ""). |
Call before testing a try/catch block.
Example: test_try("Test a try block"); try { ... //someting to test test_try_success(); } catch(...) { test_try_failure(); }
Definition at line 727 of file GTestSuite.cpp.
References GTestCase::ERROR_TEST, format_name(), m_stack_try, and name().
| void GTestSuite::test_try_failure | ( | const std::exception & | e | ) |
Notice when a try block failed.
| [in] | e | Exception. |
| GException::test_nested_try_error | Test case index is out of range. |
Call this method in a catch block.
Example: test_try("Test a try block"); try { ... //someting to test test_try_success(); } catch(exception& e) { test_try_failure(e); }
Definition at line 871 of file GTestSuite.cpp.
References test_try_failure().
| void GTestSuite::test_try_failure | ( | const std::string & | message = "", |
| const std::string & | type = "" ) |
Notice when a try block failed.
| [in] | message | Message to explain why test failed (defaults to ""). |
| [in] | type | Type of message (defaults to ""). |
| GException::test_nested_try_error | Test case index is out of range. |
Call this method in the catch block.
Example: test_try("Test a try block"); try { ... //someting to test test_try_success(); } catch(...) { test_try_failure(); }
Definition at line 809 of file GTestSuite.cpp.
References GTestCase::ERROR_TEST, m_errors, m_stack_try, and m_tests.
Referenced by test_try_failure().
| void GTestSuite::test_try_success | ( | void | ) |
Notice when a try block succeeded.
| GException::test_nested_try_error | Test case index is out of range. |
Call this method at the last line of a try
Example: test_try("Test a try block"); try { ... //someting to test test_try_success(); } catch(...) { test_try_failure(); }
Definition at line 762 of file GTestSuite.cpp.
References G_TRY_SUCCESS, m_stack_try, and m_tests.
| void GTestSuite::test_value | ( | const double & | value, |
| const double & | expected, | ||
| const double & | eps, | ||
| const std::string & | name = "", | ||
| const std::string & | message = "" ) |
Test a double precision value.
| [in] | value | Double precision value to test. |
| [in] | expected | Expected double precision value. |
| [in] | eps | Precision of the test. |
| [in] | name | Test case name. |
| [in] | message | Test case message. |
Test if the value is comprised in the interval [expected-eps, expected+eps].
Definition at line 495 of file GTestSuite.cpp.
References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), name(), GTestCase::print(), and gammalib::str().
| void GTestSuite::test_value | ( | const double & | value, |
| const double & | expected, | ||
| const std::string & | name = "", | ||
| const std::string & | message = "" ) |
Test a double precision value.
| [in] | value | Double precision value to test. |
| [in] | expected | Expected double precision value. |
| [in] | name | Test case name. |
| [in] | message | Test case message. |
Test if the value is equal to the expected value within a relative precision of 1.0e-7.
Definition at line 467 of file GTestSuite.cpp.
References name(), and test_value().
| void GTestSuite::test_value | ( | const int & | value, |
| const int & | expected, | ||
| const std::string & | name = "", | ||
| const std::string & | message = "" ) |
Test an integer value.
| [in] | value | Integer value to test. |
| [in] | expected | Expected integer value. |
| [in] | name | Test case name. |
| [in] | message | Test case message. |
Test if integer value is the expected value.
Definition at line 406 of file GTestSuite.cpp.
References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), name(), GTestCase::print(), and gammalib::str().
Referenced by test_value(), and test_value().
| void GTestSuite::test_value | ( | const std::complex< double > & | value, |
| const std::complex< double > & | expected, | ||
| const double & | eps, | ||
| const std::string & | name = "", | ||
| const std::string & | message = "" ) |
Test a complex value.
| [in] | value | Complex value to test. |
| [in] | expected | Expected complex value. |
| [in] | eps | Precision of the test. |
| [in] | name | Test case name. |
| [in] | message | Test case message. |
Test if the value is comprised in the interval [expected-eps, expected+eps].
Definition at line 589 of file GTestSuite.cpp.
References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), name(), GTestCase::print(), and gammalib::str().
| void GTestSuite::test_value | ( | const std::complex< double > & | value, |
| const std::complex< double > & | expected, | ||
| const std::string & | name = "", | ||
| const std::string & | message = "" ) |
Test a complex value.
| [in] | value | Complex value to test. |
| [in] | expected | Expected complex value. |
| [in] | name | Test case name. |
| [in] | message | Test case message. |
Test if the value is equal to the expected value within a relative precision of 1.0e-7.
Definition at line 561 of file GTestSuite.cpp.
References name(), and test_value().
| void GTestSuite::test_value | ( | const std::string & | value, |
| const std::string & | expected, | ||
| const std::string & | name = "", | ||
| const std::string & | message = "" ) |
Test a string value.
| [in] | value | String value to test. |
| [in] | expected | Expected string value. |
| [in] | name | Test case name (defaults to ""). |
| [in] | message | Test case message (defaults to ""). |
Test if the string value corresponds to the expected value.
Definition at line 657 of file GTestSuite.cpp.
References GTestCase::FAIL_TEST, format_name(), GTestCase::has_passed(), m_failures, m_tests, GTestCase::message(), name(), and GTestCase::print().
|
inline |
Return the timestamp.
The timestamp is set at the construction of the object.
Definition at line 224 of file GTestSuite.hpp.
References m_timestamp.
Referenced by GTestSuites::write().
|
protected |
Number of errors.
Definition at line 138 of file GTestSuite.hpp.
Referenced by copy_members(), errors(), init_members(), run(), success(), and test_try_failure().
|
protected |
Number of failures.
Definition at line 137 of file GTestSuite.hpp.
Referenced by copy_members(), failures(), init_members(), run(), success(), test_assert(), test_value(), test_value(), test_value(), and test_value().
|
protected |
Test functions of this suite.
Definition at line 133 of file GTestSuite.hpp.
Referenced by append(), copy_members(), init_members(), and run().
|
protected |
Index of actual test function.
Definition at line 136 of file GTestSuite.hpp.
Referenced by copy_members(), format_name(), init_members(), and run().
|
protected |
Log.
Definition at line 139 of file GTestSuite.hpp.
Referenced by copy_members(), cout(), free_members(), and init_members().
|
protected |
Name of the test suite.
Definition at line 131 of file GTestSuite.hpp.
Referenced by copy_members(), GTestSuite(), init_members(), name(), name(), and print().
|
protected |
Names of test functions.
Definition at line 132 of file GTestSuite.hpp.
Referenced by append(), copy_members(), format_name(), init_members(), print(), and run().
|
protected |
Stack for nested try blocks.
Definition at line 135 of file GTestSuite.hpp.
Referenced by copy_members(), exception_error(), exception_failure(), format_name(), free_members(), init_members(), test_try(), test_try_failure(), and test_try_success().
|
protected |
List of test results.
Definition at line 134 of file GTestSuite.hpp.
Referenced by copy_members(), duration(), free_members(), init_members(), operator[](), operator[](), run(), size(), test_assert(), test_try_failure(), test_try_success(), test_value(), test_value(), test_value(), and test_value().
|
protected |
Timestamp.
Definition at line 140 of file GTestSuite.hpp.
Referenced by copy_members(), init_members(), and timestamp().