TestMethod.h
1 /*
2  * Copyright (C) 2005-2006 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_TESTMETHOD_H
29 #define PT_UNIT_TESTMETHOD_H
30 
31 #include <Pt/Unit/Api.h>
32 #include <Pt/Unit/Test.h>
33 #include <Pt/SerializationInfo.h>
34 #include <Pt/TypeTraits.h>
35 #include <Pt/Void.h>
36 #include <Pt/Method.h>
37 #include <stdexcept>
38 #include <cstddef>
39 
40 namespace Pt {
41 
42 namespace Unit {
43 
44  class TestMethod : public Pt::Unit::Test
45  {
46  public:
47  TestMethod(const std::string& name)
48  : Pt::Unit::Test(name)
49  , _args(0)
50  , _argCount(0)
51  {}
52 
53  virtual ~TestMethod()
54  {}
55 
56  void setArgs(const SerializationInfo* si, std::size_t argCount)
57  {
58  _args = si;
59  _argCount = argCount;
60  }
61 
62  const SerializationInfo* args() const
63  { return _args; }
64 
65  std::size_t argCount() const
66  { return _argCount; }
67 
68  private:
69  const SerializationInfo* _args;
70  std::size_t _argCount;
71  };
72 
73 
74  template < class C,
75  typename A1 = Pt::Void,
76  typename A2 = Pt::Void,
77  typename A3 = Pt::Void,
78  typename A4 = Pt::Void,
79  typename A5 = Pt::Void,
80  typename A6 = Pt::Void,
81  typename A7 = Pt::Void,
82  typename A8 = Pt::Void >
83  class BasicTestMethod : public Pt::Method<void, C, A1, A2, A3, A4, A5, A6, A7, A8>
84  , public TestMethod
85  {
86  public:
87  typedef C ClassT;
88  typedef void (C::*MemFuncT)(A1, A2, A3, A4, A5, A6, A7, A8);
89 
90  typedef typename TypeTraits<A1>::Value V1;
91  typedef typename TypeTraits<A2>::Value V2;
92  typedef typename TypeTraits<A3>::Value V3;
93  typedef typename TypeTraits<A4>::Value V4;
94  typedef typename TypeTraits<A5>::Value V5;
95  typedef typename TypeTraits<A6>::Value V6;
96  typedef typename TypeTraits<A7>::Value V7;
97  typedef typename TypeTraits<A8>::Value V8;
98 
99  public:
100  BasicTestMethod(const std::string& name, C& object, MemFuncT ptr)
101  : Pt::Method<void, C, A1, A2, A3, A4, A5, A6, A7, A8>(object, ptr)
102  , TestMethod(name)
103  {}
104 
105  void run()
106  {
107  const SerializationInfo* args = this->args();
108  std::size_t argCount = this->argCount();
109 
110  if(argCount != 8)
111  throw std::invalid_argument("invalid number of arguments");
112 
113  V1 v1 = V1();
114  args[0] >>= v1;
115 
116  V2 v2 = V2();
117  args[1] >>= v2;
118 
119  V3 v3 = V3();
120  args[2] >>= v3;
121 
122  V4 v4 = V4();
123  args[3] >>= v4;
124 
125  V5 v5 = V5();
126  args[4] >>= v5;
127 
128  V6 v6 = V6();
129  args[5] >>= v6;
130 
131  V7 v7 = V7();
132  args[6] >>= v7;
133 
134  V8 v8 = V8();
135  args[7] >>= v8;
136 
137  Pt::Method<void, C>::call(v1, v2, v3, v4, v5, v6, v7, v8);
138  }
139  };
140 
141 
142  template < class C,
143  typename A1,
144  typename A2,
145  typename A3,
146  typename A4,
147  typename A5>
148  class BasicTestMethod<C,
149  A1,
150  A2,
151  A3,
152  A4,
153  A5,
154  Pt::Void,
155  Pt::Void,
156  Pt::Void> : public Pt::Method<void, C, A1, A2, A3, A4, A5>
157  , public TestMethod
158  {
159  public:
160  typedef C ClassT;
161  typedef void (C::*MemFuncT)(A1, A2, A3, A4, A5);
162 
163  typedef typename TypeTraits<A1>::Value V1;
164  typedef typename TypeTraits<A2>::Value V2;
165  typedef typename TypeTraits<A3>::Value V3;
166  typedef typename TypeTraits<A4>::Value V4;
167  typedef typename TypeTraits<A5>::Value V5;
168 
169  public:
170  BasicTestMethod(const std::string& name, C& object, MemFuncT ptr)
171  : Pt::Method<void, C, A1, A2, A3, A4, A5>(object, ptr)
172  , TestMethod(name)
173  {}
174 
175  void run()
176  {
177  const SerializationInfo* args = this->args();
178  std::size_t argCount = this->argCount();
179 
180  if(argCount != 5)
181  throw std::invalid_argument("invalid number of arguments");
182 
183  V1 v1 = V1();
184  args[0] >>= v1;
185 
186  V2 v2 = V2();
187  args[1] >>= v2;
188 
189  V3 v3 = V3();
190  args[2] >>= v3;
191 
192  V4 v4 = V4();
193  args[3] >>= v4;
194 
195  V5 v5 = V5();
196  args[4] >>= v5;
197 
199  }
200  };
201 
202 
203  template < class C,
204  typename A1,
205  typename A2,
206  typename A3,
207  typename A4>
208  class BasicTestMethod<C,
209  A1,
210  A2,
211  A3,
212  A4,
213  Pt::Void,
214  Pt::Void,
215  Pt::Void,
216  Pt::Void> : public Pt::Method<void, C, A1, A2, A3, A4>
217  , public TestMethod
218  {
219  public:
220  typedef C ClassT;
221  typedef void (C::*MemFuncT)(A1, A2, A3, A4);
222 
223  typedef typename TypeTraits<A1>::Value V1;
224  typedef typename TypeTraits<A2>::Value V2;
225  typedef typename TypeTraits<A3>::Value V3;
226  typedef typename TypeTraits<A4>::Value V4;
227 
228  public:
229  BasicTestMethod(const std::string& name, C& object, MemFuncT ptr)
230  : Pt::Method<void, C, A1, A2, A3, A4>(object, ptr)
231  , TestMethod(name)
232  {}
233 
234  void run()
235  {
236  const SerializationInfo* args = this->args();
237  std::size_t argCount = this->argCount();
238 
239  if(argCount != 4)
240  throw std::invalid_argument("invalid number of arguments");
241 
242  V1 v1 = V1();
243  args[0] >>= v1;
244 
245  V2 v2 = V2();
246  args[1] >>= v2;
247 
248  V3 v3 = V3();
249  args[2] >>= v3;
250 
251  V4 v4 = V4();
252  args[3] >>= v4;
253 
255  }
256  };
257 
258 
259  template < class C,
260  typename A1,
261  typename A2,
262  typename A3>
263  class BasicTestMethod<C,
264  A1,
265  A2,
266  A3,
267  Pt::Void,
268  Pt::Void,
269  Pt::Void,
270  Pt::Void,
271  Pt::Void> : public Pt::Method<void, C, A1, A2, A3>
272  , public TestMethod
273  {
274  public:
275  typedef C ClassT;
276  typedef void (C::*MemFuncT)(A1, A2, A3);
277 
278  typedef typename TypeTraits<A1>::Value V1;
279  typedef typename TypeTraits<A2>::Value V2;
280  typedef typename TypeTraits<A3>::Value V3;
281 
282  public:
283  BasicTestMethod(const std::string& name, C& object, MemFuncT ptr)
284  : Pt::Method<void, C, A1, A2, A3>(object, ptr)
285  , TestMethod(name)
286  {}
287 
288  void run()
289  {
290  const SerializationInfo* args = this->args();
291  std::size_t argCount = this->argCount();
292 
293  if(argCount != 3)
294  throw std::invalid_argument("invalid number of arguments");
295 
296  V1 v1 = V1();
297  args[0] >>= v1;
298 
299  V2 v2 = V2();
300  args[1] >>= v2;
301 
302  V3 v3 = V3();
303  args[2] >>= v3;
304 
306  }
307  };
308 
309 
310  template < class C,
311  typename A1,
312  typename A2>
313  class BasicTestMethod<C,
314  A1,
315  A2,
316  Pt::Void,
317  Pt::Void,
318  Pt::Void,
319  Pt::Void,
320  Pt::Void,
321  Pt::Void> : public Pt::Method<void, C, A1, A2>
322  , public TestMethod
323  {
324  public:
325  typedef C ClassT;
326  typedef void (C::*MemFuncT)(A1, A2);
327 
328  typedef typename TypeTraits<A1>::Value V1;
329  typedef typename TypeTraits<A2>::Value V2;
330 
331  public:
332  BasicTestMethod(const std::string& name, C& object, MemFuncT ptr)
333  : Pt::Method<void, C, A1, A2>(object, ptr)
334  , TestMethod(name)
335  {}
336 
337  void run()
338  {
339  const SerializationInfo* args = this->args();
340  std::size_t argCount = this->argCount();
341 
342  if(argCount != 2)
343  throw std::invalid_argument("invalid number of arguments");
344 
345  V1 v1 = V1();
346  args[0] >>= v1;
347 
348  V2 v2 = V2();
349  args[1] >>= v2;
350 
352  }
353  };
354 
355 
356  template < class C,
357  typename A1>
358  class BasicTestMethod<C,
359  A1,
360  Pt::Void,
361  Pt::Void,
362  Pt::Void,
363  Pt::Void,
364  Pt::Void,
365  Pt::Void,
366  Pt::Void> : public Pt::Method<void, C, A1>
367  , public TestMethod
368  {
369  public:
370  typedef C ClassT;
371  typedef void (C::*MemFuncT)(A1);
372 
373  typedef typename TypeTraits<A1>::Value V1;
374 
375  public:
376  BasicTestMethod(const std::string& name, C& object, MemFuncT ptr)
377  : Pt::Method<void, C, A1>(object, ptr)
378  , TestMethod(name)
379  {}
380 
381  void run()
382  {
383  const SerializationInfo* args = this->args();
384  std::size_t argCount = this->argCount();
385 
386  if(argCount != 1)
387  throw std::invalid_argument("invalid number of arguments");
388 
389  V1 v1 = V1();
390  args[0] >>= v1;
392  }
393  };
394 
395 
396  template < class C >
397  class BasicTestMethod<C,
398  Pt::Void,
399  Pt::Void,
400  Pt::Void,
401  Pt::Void,
402  Pt::Void,
403  Pt::Void,
404  Pt::Void,
405  Pt::Void> : public Pt::Method<void, C>
406  , public TestMethod
407  {
408  public:
409  typedef C ClassT;
410  typedef void (C::*MemFuncT)();
411 
412  public:
413  BasicTestMethod(const std::string& name, C& object, MemFuncT ptr)
414  : Pt::Method<void, C>(object, ptr)
415  , TestMethod(name)
416  {}
417 
418  void run()
419  {
421  }
422  };
423 
424 } // namespace Unit
425 
426 } // namespace Pt
427 
428 #endif // for header
429 
Method(C &object, MemFuncT ptr)
Wraps the given object/member pair.
Adapter for class methods.
Definition: Method.h:19
R call(ARGUMENTS) const
Same as operator().
IMPLEMENTATION_DEFINED Value
The derived value type.
Definition: TypeTraits.h:21
Test base class
Definition: Test.h:54
Void type.
Definition: Void.h:43
Test(const std::string &name)
Construct a test by name.
Definition: Test.h:145