]> git.tdb.fi Git - libs/test.git/blob - source/function.h
Support passing a parameter to a test function
[libs/test.git] / source / function.h
1 #ifndef MSP_TEST_TESTFUNCTION_H_
2 #define MSP_TEST_TESTFUNCTION_H_
3
4 #include <string>
5 #include "exceptioncheck.h"
6
7 namespace Msp {
8 namespace Test {
9
10 class Test;
11
12 class Function
13 {
14 private:
15         std::string description;
16         ExceptionCheck *exc_check;
17
18 protected:
19         Function(const std::string &);
20 public:
21         virtual ~Function();
22
23         const std::string &get_description() const { return description; }
24
25         template<typename T>
26         Function &expect_throw() { exc_check = new ExceptionTypeCheck<T>; return *this; }
27
28         const ExceptionCheck *get_exception_check() const { return exc_check; }
29
30         virtual void run(Test &) const = 0;
31 };
32
33 template<typename T>
34 class TypedFunction: public Function
35 {
36 private:
37         typedef void (T::*FuncPtr)();
38
39         FuncPtr func;
40
41 public:
42         TypedFunction(FuncPtr f, const std::string &d):
43                 Function(d),
44                 func(f)
45         { }
46
47         virtual void run(Test &t) const
48         {
49                 (dynamic_cast<T &>(t).*func)();
50         }
51 };
52
53 template<typename T, typename A>
54 class TypedFunction1: public Function
55 {
56 private:
57         typedef void (T::*FuncPtr)(A);
58
59         FuncPtr func;
60         A arg;
61
62 public:
63         TypedFunction1(FuncPtr f, const A &a, const std::string &d):
64                 Function(d),
65                 func(f),
66                 arg(a)
67         { }
68
69         virtual void run(Test &t) const
70         {
71                 (dynamic_cast<T &>(t).*func)(arg);
72         }
73 };
74
75 } // namespace Test
76 } // namespace Msp
77
78 #endif