]> git.stg.codes - stg.git/blob - tests/tut/tut_result.hpp
TUT framework updated to svn version
[stg.git] / tests / tut / tut_result.hpp
1 #ifndef TUT_RESULT_H_GUARD
2 #define TUT_RESULT_H_GUARD
3 #include <tut/tut_config.hpp>
4
5 #include <string>
6
7 #if defined(TUT_USE_RTTI)
8 #if (defined(_MSC_VER) && !defined(_CPPRTTI)) || (defined(__GNUC__) && !defined(__GXX_RTTI))
9 #undef TUT_USE_RTTI
10 #endif
11 #endif
12
13 #if defined(TUT_USE_RTTI)
14 #include <typeinfo>
15 #endif
16
17 namespace tut
18 {
19
20 #if defined(TUT_USE_RTTI)
21 template<typename T>
22 inline std::string type_name(const T& t)
23 {
24     return typeid(t).name();
25 }
26 #else
27 template<typename T>
28 inline std::string type_name(const T& t)
29 {
30     return "Unknown type, RTTI disabled";
31 }
32
33 inline std::string type_name(const std::exception&)
34 {
35     return "Unknown std::exception, RTTI disabled";
36 }
37 #endif
38
39
40 #if defined(TUT_USE_POSIX)
41 struct test_result_posix
42 {
43     test_result_posix()
44         : pid(getpid())
45     {
46     }
47
48     virtual ~test_result_posix()
49     {
50     }
51
52     pid_t pid;
53 };
54 #else
55 struct test_result_posix
56 {
57     virtual ~test_result_posix()
58     {
59     }
60 };
61 #endif
62
63 /**
64  * Return type of runned test/test group.
65  *
66  * For test: contains result of test and, possible, message
67  * for failure or exception.
68  */
69 struct test_result : public test_result_posix
70 {
71     /**
72      * Test group name.
73      */
74     std::string group;
75
76     /**
77      * Test number in group.
78      */
79     int test;
80
81     /**
82      * Test name (optional)
83      */
84     std::string name;
85
86     /**
87      * result of a test 
88      */
89     enum result_type
90     {
91         ok,       ///< test finished successfully
92         fail,     ///< test failed with ensure() or fail() methods
93         ex,       ///< test throwed an exceptions
94         warn,     ///< test finished successfully, but test destructor throwed
95         term,     ///< test forced test application to terminate abnormally
96         ex_ctor,  ///< 
97         rethrown, ///< 
98         skipped,  ///< 
99         dummy     ///< 
100     };
101
102     result_type result;
103
104     /**
105      * Exception message for failed test.
106      */
107     std::string message;
108     std::string exception_typeid;
109
110     /**
111      * Default constructor.
112      */
113     test_result()
114         : group(),
115           test(0),
116           name(),
117           result(ok),
118           message(),
119           exception_typeid()
120     {
121     }
122
123     /**
124      * Constructor.
125      */
126     test_result(const std::string& grp, int pos,
127                 const std::string& test_name, result_type res)
128         : group(grp),
129           test(pos),
130           name(test_name),
131           result(res),
132           message(),
133           exception_typeid()
134     {
135     }
136
137     /**
138      * Constructor with exception.
139      */
140     test_result(const std::string& grp,int pos,
141                 const std::string& test_name, result_type res,
142                 const std::exception& ex)
143         : group(grp),
144           test(pos),
145           name(test_name),
146           result(res),
147           message(ex.what()),
148           exception_typeid(type_name(ex))
149     {
150     }
151
152     /** Constructor with typeid.
153     */
154     test_result(const std::string& grp,int pos,
155                 const std::string& test_name, result_type res,
156                 const std::string& ex_typeid,
157                 const std::string& msg)
158         : group(grp),
159           test(pos),
160           name(test_name),
161           result(res),
162           message(msg),
163           exception_typeid(ex_typeid)
164     {
165     }
166
167     virtual ~test_result()
168     {
169     }
170 };
171
172 }
173
174 #endif