xref: /haiku/headers/tools/cppunit/cppunit/TestAssert.h (revision 002f37b0cca92e4cf72857c72ac95db5a8b09615)
1 #ifndef CPPUNIT_TESTASSERT_H
2 #define CPPUNIT_TESTASSERT_H
3 
4 #include <cppunit/Portability.h>
5 #include <cppunit/Exception.h>
6 #include <cppunit/Asserter.h>
7 
8 
9 namespace CppUnit {
10 
11   /*! \brief Traits used by CPPUNIT_ASSERT_EQUAL().
12    *
13    * Here is an example of specialization of that traits:
14    *
15    * \code
16    * template<>
17    * struct assertion_traits<string>   // specialization for the string type
18    * {
19    *   static bool equal( const string& x, const string& y )
20    *   {
21    *     return x == y;
22    *   }
23    *
24    *   static string toString( const string& x )
25    *   {
26    *     string text = '"' + x + '"';    // adds quote around the string to see whitespace
27    *     OStringStream ost;
28    *     ost << text;
29    *     return ost.str();
30    *   }
31    * };
32    * \endcode
33    */
34   template <class T>
35   struct assertion_traits
36   {
equalassertion_traits37       static bool equal( const T& x, const T& y )
38       {
39           return x == y;
40       }
41 
toStringassertion_traits42       static std::string toString( const T& x )
43       {
44           OStringStream ost;
45           ost << x;
46           return ost.str();
47       }
48   };
49 
50 
51   namespace TestAssert
52   {
53 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
54     void CPPUNIT_API assertImplementation( bool         condition,
55                                            std::string  conditionExpression = "",
56                                            long lineNumber,
57                                            std::string  fileName );
58 
59     void CPPUNIT_API assertNotEqualImplementation( std::string expected,
60                                                    std::string actual,
61                                                    long lineNumber,
62                                                    std::string fileName );
63 
64 
65     template <class T>
assertEquals(const T & expected,const T & actual,long lineNumber,std::string fileName)66     void assertEquals( const T& expected,
67                        const T& actual,
68                        long lineNumber,
69                        std::string fileName )
70     {
71       if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
72       {
73         assertNotEqualImplementation( assertion_traits<T>::toString(expected),
74                                       assertion_traits<T>::toString(actual),
75                                       lineNumber,
76                                       fileName );
77       }
78     }
79 
80     void CPPUNIT_API assertEquals( double expected,
81                                    double actual,
82                                    double delta,
83                                    long lineNumber,
84                                    std::string fileName );
85 
86 #else   //                  using SourceLine
87 
88     template <class T>
89     void assertEquals( const T& expected,
90                        const T& actual,
91                        SourceLine sourceLine,
92                        const std::string &message ="" )
93     {
94       if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
95       {
96         Asserter::failNotEqual( assertion_traits<T>::toString(expected),
97                                 assertion_traits<T>::toString(actual),
98                                 sourceLine,
99                                 message );
100       }
101     }
102 
103     void CPPUNIT_API assertDoubleEquals( double expected,
104                                          double actual,
105                                          double delta,
106                                          SourceLine sourceLine );
107 
108 #endif
109   }
110 
111 
112 /* A set of macros which allow us to get the line number
113  * and file name at the point of an error.
114  * Just goes to show that preprocessors do have some
115  * redeeming qualities.
116  */
117 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
118 /** Assertions that a condition is \c true.
119  * \ingroup Assertions
120  */
121 #define CPPUNIT_ASSERT(condition)                          \
122   ( ::CppUnit::Asserter::failIf( !(condition),             \
123                                  (#condition),             \
124                                  CPPUNIT_SOURCELINE() ) )
125 #else
126 #define CPPUNIT_ASSERT(condition)                          \
127   ( ::CppUnit::Asserter::failIf( !(condition),             \
128                                  "",                       \
129                                  CPPUNIT_SOURCELINE() ) )
130 #endif
131 
132 /** Assertion with a user specified message.
133  * \ingroup Assertions
134  * \param message Message reported in diagnostic if \a condition evaluates
135  *                to \c false.
136  * \param condition If this condition evaluates to \c false then the
137  *                  test failed.
138  */
139 #define CPPUNIT_ASSERT_MESSAGE(message,condition)          \
140   ( ::CppUnit::Asserter::failIf( !(condition),             \
141                                  (message),                \
142                                  CPPUNIT_SOURCELINE() ) )
143 
144 /** Fails with the specified message.
145  * \ingroup Assertions
146  * \param message Message reported in diagnostic.
147  */
148 #define CPPUNIT_FAIL( message )                            \
149   ( ::CppUnit::Asserter::fail( message,                    \
150                                CPPUNIT_SOURCELINE() ) )
151 
152 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
153 /// Generalized macro for primitive value comparisons
154 #define CPPUNIT_ASSERT_EQUAL(expected,actual)                    \
155   ( ::CppUnit::TestAssert::assertEquals( (expected),             \
156                                          (actual),               \
157                                          __LINE__, __FILE__ ) )
158 #else
159 /** Asserts that two values are equals.
160  * \ingroup Assertions
161  *
162  * Equality and string representation can be defined with
163  * an appropriate CppUnit::assertion_traits class.
164  *
165  * A diagnostic is printed if actual and expected values disagree.
166  *
167  * Requirement for \a expected and \a actual parameters:
168  * - They are exactly of the same type
169  * - They are serializable into a strstream using operator <<.
170  * - They can be compared using operator ==.
171  *
172  * The last two requirements (serialization and comparison) can be
173  * removed by specializing the CppUnit::assertion_traits.
174  */
175 #define CPPUNIT_ASSERT_EQUAL(expected,actual)                     \
176   ( ::CppUnit::TestAssert::assertEquals<typeof(expected)>( (expected), \
177                                          (actual),                \
178                                          CPPUNIT_SOURCELINE() ) )
179 
180 /** Asserts that two values are equals, provides additional messafe on failure.
181  * \ingroup Assertions
182  *
183  * Equality and string representation can be defined with
184  * an appropriate assertion_traits class.
185  *
186  * A diagnostic is printed if actual and expected values disagree.
187  * The message is printed in addition to the expected and actual value
188  * to provide additional information.
189  *
190  * Requirement for \a expected and \a actual parameters:
191  * - They are exactly of the same type
192  * - They are serializable into a strstream using operator <<.
193  * - They can be compared using operator ==.
194  *
195  * The last two requirements (serialization and comparison) can be
196  * removed by specializing the CppUnit::assertion_traits.
197  */
198 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual)     \
199   ( ::CppUnit::TestAssert::assertEquals<typeof(expected)>( (expected),  \
200                                          (actual),                \
201                                          CPPUNIT_SOURCELINE(),    \
202                                          (message) ) )
203 #endif
204 
205 /*! \brief Macro for primitive value comparisons
206  * \ingroup Assertions
207  */
208 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)       \
209   ( ::CppUnit::TestAssert::assertDoubleEquals( (expected),        \
210                                                (actual),          \
211                                                (delta),           \
212                                                CPPUNIT_SOURCELINE() ) )
213 
214 // Backwards compatibility
215 
216 #if CPPUNIT_ENABLE_NAKED_ASSERT
217 
218 #undef assert
219 #define assert(c)                 CPPUNIT_ASSERT(c)
220 #define assertEqual(e,a)          CPPUNIT_ASSERT_EQUAL(e,a)
221 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
222 #define assertLongsEqual(e,a)     CPPUNIT_ASSERT_EQUAL(e,a)
223 
224 #endif
225 
226 
227 } // namespace CppUnit
228 
229 #endif  // CPPUNIT_TESTASSERT_H
230