You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.3KB

  1. // ---
  2. //
  3. // $Id: cpptest-suite.h,v 1.3 2005/06/08 08:08:06 nilu Exp $
  4. //
  5. // CppTest - A C++ Unit Testing Framework
  6. // Copyright (c) 2003 Niklas Lundell
  7. //
  8. // ---
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Lesser General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this library; if not, write to the
  22. // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. // Boston, MA 02111-1307, USA.
  24. //
  25. // ---
  26. /** \file */
  27. #ifndef CPPTEST_SUITE_H
  28. #define CPPTEST_SUITE_H
  29. #include "cpptest-time.h"
  30. namespace Test
  31. {
  32. class Output;
  33. /// \brief Unit testing suite.
  34. ///
  35. /// Base class for all suites. Derive from this class to create own
  36. /// test suites.
  37. ///
  38. /// %Test functions in derived classes are registered as tests using the
  39. /// TEST_ADD(func). Testing is started by run(). Note that suites may be
  40. /// embedded in other suites using add().
  41. ///
  42. class Suite
  43. {
  44. public:
  45. Suite();
  46. virtual ~Suite();
  47. void add(Suite* suite);
  48. bool run(Output& output, bool cont_after_fail = true);
  49. protected:
  50. /// Pointer to a test function.
  51. ///
  52. typedef void (Suite::*Func)();
  53. bool continue_after_failure() const { return _continue; }
  54. virtual void setup() {}
  55. virtual void tear_down() {}
  56. void register_test(Func func, const std::string& name);
  57. void assertment(Source s);
  58. private:
  59. struct DoRun;
  60. struct ExecTests;
  61. struct SubSuiteTests;
  62. struct SuiteTime;
  63. struct SubSuiteTime;
  64. friend struct DoRun;
  65. friend struct ExecTests;
  66. friend struct SubSuiteTests;
  67. friend struct SubSuiteTime;
  68. struct Data
  69. {
  70. Func _func;
  71. std::string _name;
  72. Time _time;
  73. Data(Func func, const std::string& name)
  74. : _func(func), _name(name) {}
  75. };
  76. typedef std::list<Data> Tests;
  77. typedef std::list<Suite*> Suites;
  78. std::string _name; // Suite name
  79. const std::string* _cur_test; // Current test func name
  80. Suites _suites; // External test suites
  81. Tests _tests; // All tests
  82. Output* _output; // Output handler
  83. bool _result : 1; // Test result
  84. bool _success : 1; // Set if no test failed
  85. bool _continue : 1; // Continue func after failures
  86. void do_run(Output* os, bool cont_after_fail);
  87. int total_tests() const;
  88. Time total_time(bool recursive) const;
  89. // Disable
  90. //
  91. Suite(const Suite&);
  92. Suite& operator=(const Suite&);
  93. };
  94. /// Adds a test function to the enclosing suite. Note that test functions
  95. /// should be added in the suites constructor.
  96. ///
  97. /// \param func Function to add, must be of type Suite::Func.
  98. ///
  99. /// \par Example:
  100. /// \code
  101. /// MySuite::MySuite()
  102. /// {
  103. /// TEST_ADD(&MySuite::test_1)
  104. /// TEST_ADD(&MySuite::test_2)
  105. /// ...
  106. /// }
  107. /// \endcode
  108. ///
  109. #define TEST_ADD(func)\
  110. register_test(static_cast<Func>(&func), #func);
  111. } // namespace Test
  112. #endif // #ifndef CPPTEST_SUITE_H