switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / auto_unit_test.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Boost.Test auto unit test compatibility across Boost V 1.33 and 1.34
30
31     This header file will allows to consistently use the Boost Version 1.33 syntax for defining auto
32     unit tests. If the available Boost version is 1.34, this file will automatically take care of
33     any necessary workarounds.
34
35     So, instead of <tt>\#include <boost/test/auto_unit_test.hpp></tt>, you should always write
36     <pre>
37     \#include "../Utils/auto_unit_test.hh"
38     </pre> (with possibliy adjusted path).
39  */
40
41 #ifndef HH_SENF_Utils_auto_unit_test_
42 #define HH_SENF_Utils_auto_unit_test_ 1
43
44 // Custom includes
45 #include <iostream>
46 #include <boost/version.hpp>
47
48 //#include "auto_unit_test.mpp"
49 #include "auto_unit_test.ih"
50 //-/////////////////////////////////////////////////////////////////////////////////////////////////
51
52 #if BOOST_VERSION >= 103400
53
54 #   define BOOST_AUTO_UNIT_TEST BOOST_AUTO_TEST_CASE
55
56 #endif
57
58 #include <boost/test/auto_unit_test.hpp>
59
60 /** \defgroup unittest Boost.Test extensions
61
62     This module defines some additional extensions to Boost.Test
63  */
64
65 /** \brief Check for compile failure
66
67     \c COMPILE_FAIL() is used to check, that a certain piece of code will produce a compile
68     time failure.
69
70     \code
71     #ifdef COMPILE_CHECK
72
73     COMPILE_FAIL(foo)
74     {
75         // fails to compile ....
76         int x = "foo";
77     }
78
79     COMPILE_FAIL(bar)
80     { ... }
81
82     #endif
83     \endcode
84
85     This check is performed by the extended unit-test builder in \c senfscons.
86
87     \ingroup unittest
88  */
89 #define COMPILE_FAIL(n) void n()
90
91 /** \brief Show exception information
92
93     \c SENF_CHECK_NO_THROW() is an extension to \c BOOST_CHECK_NO_THROW() which will write out the
94     unabridged exception information when an exception is thrown in addition to signaling an error.
95
96     \ingroup unittest
97  */
98 #define SENF_CHECK_NO_THROW(expr)                                                               \
99     BOOST_CHECK_NO_THROW(                                                                       \
100         try { expr ; }                                                                          \
101         catch (std::exception & e) { std::cerr << e.what() << std::endl; throw; } )
102
103 namespace senf {
104 namespace test {
105
106     template <class Iterator>
107     typename detail::NoCharIterator<Iterator>::type nocharIterator(Iterator i);
108
109 }}
110
111 /** \brief Check ranges for equality, writing char's as int's
112
113     \c SENF_CHECK_EQUAL_COLLECTIONS() is like \c BOOST_CHECK_EQUAL_COLLECTIONS(). The only
114     difference is, that \c SENF_CHECK_EQUAL_COLLECTIONS() will write out collection values in
115     numeric form (and not as single characters) if the elements are of any char type. Other types
116     are not affected.
117
118     \hideinitializer
119     \ingroup unittest
120  */
121 #define SENF_CHECK_EQUAL_COLLECTIONS(left_begin, left_end, right_begin, right_end)              \
122     BOOST_CHECK_EQUAL_COLLECTIONS(                                                              \
123         senf::test::nocharIterator(left_begin), senf::test::nocharIterator(left_end),           \
124         senf::test::nocharIterator(right_begin), senf::test::nocharIterator(right_end))
125
126 /** \brief Define new test case
127
128     \c SENF_AUTO_UNIT_TEST() is like \c BOOST_AUTO_UNIT_TEST(). The only difference is, that
129     if an exception is thrown in the test case \c SENF_AUTO_UNIT_TEST() will write out the
130     complete what-message of the exception.
131
132     \hideinitializer
133     \ingroup unittest
134  */
135 #define SENF_AUTO_UNIT_TEST(name)                                                               \
136     void senf_test_body_##name();                                                               \
137     BOOST_AUTO_UNIT_TEST(name)                                                                  \
138     {                                                                                           \
139         try {                                                                                   \
140             senf_test_body_##name();                                                            \
141         }                                                                                       \
142         catch (std::exception & e) {                                                            \
143             std::cerr << e.what() << std::endl;                                                 \
144             throw;                                                                              \
145         }                                                                                       \
146     }                                                                                           \
147     void senf_test_body_##name()
148
149
150 #ifdef DOXYGEN
151     /** \brief Check for non-equality
152
153         \c SENF_CHECK_NOT_EQUAL() is the opposite of \c BOOST_CHECK_EQUAL.
154         \hideinitializer
155         \ingroup unittest
156      */
157     #define SENF_CHECK_NOT_EQUAL( Left, Rigth)
158 #else
159 #ifdef BOOST_CHECK_NE
160     #define SENF_CHECK_NOT_EQUAL( Left, Right) BOOST_CHECK_NE( Left, Right)
161 #else
162     #define SENF_CHECK_NOT_EQUAL( Left, Right) BOOST_CHECK( Left != Right)
163 #endif
164 #endif
165
166 //-/////////////////////////////////////////////////////////////////////////////////////////////////
167 //#include "auto_unit_test.cci"
168 //#include "auto_unit_test.ct"
169 #include "auto_unit_test.cti"
170 #endif
171
172 \f
173 // Local Variables:
174 // mode: c++
175 // fill-column: 100
176 // comment-column: 40
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"
181 // End: