CppTest  Version 2.0.0
cpptest-suite.h
Go to the documentation of this file.
1 // ---
2 //
3 // $Id: cpptest-suite.h,v 1.4 2010/03/26 04:45:14 hartwork 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 
29 #ifndef CPPTEST_SUITE_H
30 #define CPPTEST_SUITE_H
31 
32 #include <list>
33 #include <memory>
34 #include <string>
35 
36 #include "cpptest-time.h"
37 #include "cpptest-source.h"
38 
39 namespace Test
40 {
41  class Output;
42 
52  class Suite
53  {
54  public:
55  Suite();
56  virtual ~Suite();
57 
58  void add(std::unique_ptr<Suite> suite);
59 
60  bool run(Output& output, bool cont_after_fail = true);
61 
62  protected:
65  typedef void (Suite::*Func)();
66 
67  bool continue_after_failure() const { return _continue; }
68 
69  virtual void setup() {}
70  virtual void tear_down() {}
71 
72  void register_test(Func func, const std::string& name);
73  void assertment(Source s);
74 
75  private:
76  struct DoRun;
77  struct ExecTests;
78  struct SubSuiteTests;
79  struct SuiteTime;
80  struct SubSuiteTime;
81 
82  friend struct DoRun;
83  friend struct ExecTests;
84  friend struct SubSuiteTests;
85  friend struct SubSuiteTime;
86 
87  struct Data
88  {
89  Func _func;
90  std::string _name;
91  Time _time;
92 
93  Data(Func func, const std::string& name)
94  : _func(func), _name(name) {}
95  };
96 
97  typedef std::list<Data> Tests;
98  typedef std::list<Suite*> Suites;
99 
100  std::string _name; // Suite name
101  const std::string* _cur_test; // Current test func name
102  Suites _suites; // External test suites
103  Tests _tests; // All tests
104  Output* _output; // Output handler
105  bool _result : 1; // Test result
106  bool _success : 1; // Set if no test failed
107  bool _continue : 1; // Continue func after failures
108 
109  void do_run(Output* os, bool cont_after_fail);
110  int total_tests() const;
111  Time total_time(bool recursive) const;
112 
113  // Disable
114  //
115  Suite(const Suite&);
116  Suite& operator=(const Suite&);
117  };
118 
134  #define TEST_ADD(func)\
135  register_test(static_cast<Func>(&func), #func);
136 
137 } // namespace Test
138 
139 #endif // #ifndef CPPTEST_SUITE_H
140 
virtual void tear_down()
Definition: cpptest-suite.h:70
void(Suite::* Func)()
Definition: cpptest-suite.h:65
Time representation.
Definition: cpptest-time.h:43
Assertment source information.
Definition: cpptest-source.h:42
virtual void setup()
Definition: cpptest-suite.h:69
void register_test(Func func, const std::string &name)
Definition: suite.cpp:135
void add(std::unique_ptr< Suite > suite)
Definition: suite.cpp:120
void assertment(Source s)
Definition: suite.cpp:152
virtual ~Suite()
Definition: suite.cpp:71
Definition: collectoroutput.cpp:37
Unit testing suite.
Definition: cpptest-suite.h:52
Test suite output handler.
Definition: cpptest-output.h:55
bool run(Output &output, bool cont_after_fail=true)
Definition: suite.cpp:85
Suite()
Definition: suite.cpp:63