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 
82  class PT_UNIT_API TestSuite : public Test
83  , public TestFixture
84  {
85  private:
87  class PT_UNIT_API Context : public TestContext
88  {
89  public:
90  Context(TestFixture& fixture, TestMethod& test, const SerializationInfo* args, std::size_t argCount)
91  : TestContext(fixture, test)
92  , _test(test)
93  {
94  test.setArgs(args, argCount);
95  }
96 
97  virtual ~Context()
98  {
99  _test.setArgs(0, 0);
100  }
101 
102  private:
103  TestMethod& _test;
104  };
105 
106  public:
116  explicit TestSuite(const std::string& name, TestProtocol& protocol = TestSuite::defaultProtocol);
117 
120 
123  void setProtocol(TestProtocol* protocol);
124 
130  virtual void setUp();
131 
138  virtual void tearDown();
139 
145  virtual void run();
146 
159  void runTest( const std::string& name, const SerializationInfo* args = 0, std::size_t argCount = 0);
160 
162  void runAll();
163 
164  protected:
165  template <class ParentT>
166  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)() )
167  {
168  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT>(this->name() + "::" + name, parent, method);
169  this->registerTest(test);
170  }
171 
172  template <class ParentT, typename A1>
173  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1) )
174  {
175  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1>(this->name() + "::" + name, parent, method);
176  this->registerTest(test);
177  }
178 
179  template <class ParentT, typename A1, typename A2>
180  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1, A2) )
181  {
182  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1, A2>(this->name() + "::" + name, parent, method);
183  this->registerTest(test);
184  }
185 
186  template <class ParentT, typename A1, typename A2, typename A3>
187  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1, A2, A3) )
188  {
189  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1, A2, A3>(this->name() + "::" + name, parent, method);
190  this->registerTest(test);
191  }
192 
193  template <class ParentT, typename A1, typename A2, typename A3, typename A4>
194  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1, A2, A3, A4) )
195  {
196  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1, A2, A3, A4>(this->name() + "::" + name, parent, method);
197  this->registerTest(test);
198  }
199 
200  template <class ParentT, typename A1, typename A2, typename A3, typename A4, typename A5>
201  void registerMethod(const std::string& name, ParentT& parent, void (ParentT::*method)(A1, A2, A3, A4, A5) )
202  {
203  Pt::Unit::TestMethod* test = new BasicTestMethod<ParentT, A1, A2, A3, A4, A5>(this->name() + "::" + name, parent, method);
204  this->registerTest(test);
205  }
206 
207  private:
208  void registerTest(TestMethod* test);
209 
210  TestMethod* findTestMethod(const std::string& name);
211 
214  TestProtocol* _protocol;
215 
216  std::multimap<std::string, TestMethod*> _tests;
217 
218  public:
219  static TestProtocol defaultProtocol;
220  };
221 
222 } // namespace Unit
223 
224 } // namespace Pt
225 
226 #endif // for header
227 
Core module.
Definition: Allocator.h:33
Protocol for test suites.
Definition: TestProtocol.h:93
virtual void setUp()
Set up conText before running a test.
void runAll()
Runs all tests.
Protocol and data driven testing.
Definition: TestSuite.h:84
Fixture interface for tests.
Definition: TestFixture.h:42
void setProtocol(TestProtocol *protocol)
Sets the protocol.
Context in which test are run.
Definition: TestContext.h:47
~TestSuite()
Destructor.
void runTest(const std::string &name, const SerializationInfo *args=0, std::size_t argCount=0)
Runs a registered test.
TestSuite(const std::string &name, TestProtocol &protocol=TestSuite::defaultProtocol)
Construct by name and protocol.
virtual void run()
Runs the test suite.
Test base class
Definition: Test.h:55
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:59
virtual void tearDown()
Clean up after the test run.