TestSuite.h
1 /*
2  * Copyright (C) 2005-2008 by Dr. Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef PT_UNIT_TESTSUITE_H
29 #define PT_UNIT_TESTSUITE_H
30 
31 #include <Pt/Unit/Api.h>
32 #include <Pt/Unit/Test.h>
33 #include <Pt/Unit/TestFixture.h>
34 #include <Pt/Unit/TestMethod.h>
35 #include <Pt/Unit/TestProtocol.h>
36 #include <map>
37 
38 namespace Pt {
39 
40  class SerializationInfo;
41 
42 namespace Unit {
43 
76  class PT_UNIT_API TestSuite : public Test
77  , public TestFixture
78  {
79  private:
81  class PT_UNIT_API Context : public TestContext
82  {
83  public:
84  Context(TestFixture& fixture, TestMethod& test, const SerializationInfo* args, std::size_t argCount)
85  : TestContext(fixture, test)
86  , _test(test)
87  {
88  test.setArgs(args, argCount);
89  }
90 
91  virtual ~Context()
92  {
93  _test.setArgs(0, 0);
94  }
95 
96  private:
97  TestMethod& _test;
98  };
99 
100  public:
110  explicit TestSuite(const std::string& name, TestProtocol& protocol = TestSuite::defaultProtocol);
111 
113  ~TestSuite();
114 
117  void setProtocol(TestProtocol* protocol);
118 
124  virtual void setUp();
125 
132  virtual void tearDown();
133 
139  virtual void run();
140 
153  void runTest( const std::string& name, const SerializationInfo* args = 0, std::size_t argCount = 0);
154 
156  void runAll();
157 
158  protected:
159  template <class ParentT>
160  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)() )
161  {
162  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT>(this->name() + "::" + name, parent, method);
163  this->registerTest(test);
164  }
165 
166  template <class ParentT, typename A1>
167  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1) )
168  {
169  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1>(this->name() + "::" + name, parent, method);
170  this->registerTest(test);
171  }
172 
173  template <class ParentT, typename A1, typename A2>
174  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1, A2) )
175  {
176  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1, A2>(this->name() + "::" + name, parent, method);
177  this->registerTest(test);
178  }
179 
180  template <class ParentT, typename A1, typename A2, typename A3>
181  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1, A2, A3) )
182  {
183  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1, A2, A3>(this->name() + "::" + name, parent, method);
184  this->registerTest(test);
185  }
186 
187  template <class ParentT, typename A1, typename A2, typename A3, typename A4>
188  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1, A2, A3, A4) )
189  {
190  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1, A2, A3, A4>(this->name() + "::" + name, parent, method);
191  this->registerTest(test);
192  }
193 
194  template <class ParentT, typename A1, typename A2, typename A3, typename A4, typename A5>
195  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1, A2, A3, A4, A5) )
196  {
197  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1, A2, A3, A4, A5>(this->name() + "::" + name, parent, method);
198  this->registerTest(test);
199  }
200 
201  private:
202  void registerTest(TestMethod* test);
203 
204  TestMethod* findTestMethod(const std::string& name);
205 
208  TestProtocol* _protocol;
209 
210  std::multimap<std::string, TestMethod*> _tests;
211 
212  public:
213  static TestProtocol defaultProtocol;
214  };
215 
216 } // namespace Unit
217 
218 } // namespace Pt
219 
220 #endif // for header
221 
Protocol and data driven testing.
Definition: TestSuite.h:76
Protocol for test suites.
Definition: TestProtocol.h:51
Context in which test are run.
Definition: TestContext.h:46
Fixture interface for tests.
Definition: TestFixture.h:40
Test base class
Definition: Test.h:54
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:58