summaryrefslogtreecommitdiffstats
blob: 3321b8731eddfc8cfa3e2a6c42ff78431139acb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#ifndef CPPUNIT_ASSERTER_H
#define CPPUNIT_ASSERTER_H

#include <cppunit/AdditionalMessage.h>
#include <cppunit/SourceLine.h>
#include <string>

CPPUNIT_NS_BEGIN


class Message;


/*! \brief A set of functions to help writing assertion macros.
 * \ingroup CreatingNewAssertions
 *
 * Here is an example of assertion, a simplified version of the
 * actual assertion implemented in examples/cppunittest/XmlUniformiser.h:
 * \code
 * #include <cppunit/SourceLine.h>
 * #include <cppunit/TestAssert.h>
 * 
 * void 
 * checkXmlEqual( std::string expectedXml,
 *                std::string actualXml,
 *                CppUnit::SourceLine sourceLine )
 * {
 *   std::string expected = XmlUniformiser( expectedXml ).stripped();
 *   std::string actual = XmlUniformiser( actualXml ).stripped();
 * 
 *   if ( expected == actual )
 *     return;
 * 
 *   ::CppUnit::Asserter::failNotEqual( expected,
 *                                      actual,
 *                                      sourceLine );
 * }
 * 
 * /// Asserts that two XML strings are equivalent.
 * #define CPPUNITTEST_ASSERT_XML_EQUAL( expected, actual ) \
 *     checkXmlEqual( expected, actual,                     \
 *                    CPPUNIT_SOURCELINE() )
 * \endcode
 */

#if defined __GNUC__
#   define NORETURN __attribute__((noreturn))
#else
#   define NORETURN
#endif

struct Asserter
{
  /*! \brief Throws a Exception with the specified message and location.
   */
  NORETURN static void CPPUNIT_API fail( const Message &message, 
                                const SourceLine &sourceLine = SourceLine() );

  /*! \brief Throws a Exception with the specified message and location.
   * \deprecated Use fail( Message, SourceLine ) instead.
   */
  NORETURN static void CPPUNIT_API fail( std::string message, 
                                const SourceLine &sourceLine = SourceLine() );

  /*! \brief Throws a Exception with the specified message and location.
   * \param shouldFail if \c true then the exception is thrown. Otherwise
   *                   nothing happen.
   * \param message Message explaining the assertion failiure.
   * \param sourceLine Location of the assertion.
   */
  static void CPPUNIT_API failIf( bool shouldFail, 
                                  const Message &message, 
                                  const SourceLine &sourceLine = SourceLine() );

  /*! \brief Throws a Exception with the specified message and location.
   * \deprecated Use failIf( bool, Message, SourceLine ) instead.
   * \param shouldFail if \c true then the exception is thrown. Otherwise
   *                   nothing happen.
   * \param message Message explaining the assertion failiure.
   * \param sourceLine Location of the assertion.
   */
  static void CPPUNIT_API failIf( bool shouldFail, 
                                  std::string message, 
                                  const SourceLine &sourceLine = SourceLine() );

  /*! \brief Returns a expected value string for a message, case equal than
   * Typically used to create 'not equal' message, or to check that a message
   * contains the expected content when writing unit tests for your custom 
   * assertions.
   *
   * \param expectedValue String that represents the expected value.
   * \return \a expectedValue prefixed with "Expected: ".
   * \deprecated Use makeExpectedEqual instead
   * \see makeActual().
   */
  static std::string CPPUNIT_API makeExpected( const std::string &expectedValue );
  /*! \brief Returns a expected value string for a message, case equal than
   * Typically used to create 'not equal' message, or to check that a message
   * contains the expected content when writing unit tests for your custom 
   * assertions.
   *
   * \param expectedValue String that represents the expected value.
   * \return \a expectedValue prefixed with "Expected: ".
   * \see makeActual().
   */
  static std::string CPPUNIT_API makeExpectedEqual( const std::string &expectedValue );
  /*! \brief Returns a expected value string for a message, case less than.
   *
   * \param expectedValue String that represents the expected value.
   * \return \a expectedValue prefixed with "Expected less than: ".
   * \see makeExpectedEqual().
   */
  static std::string CPPUNIT_API makeExpectedLess( const std::string &expectedValue );
  /*! \brief Returns a expected value string for a message, case less or equal than.
   *
   * \param expectedValue String that represents the expected value.
   * \return \a expectedValue prefixed with "Expected: ".
   * \see makeExpectedEqual().
   */
  static std::string CPPUNIT_API makeExpectedLessEqual( const std::string &expectedValue );
  /*! \brief Returns a expected value string for a message, case greater than.
   *
   * \param expectedValue String that represents the expected value.
   * \return \a expectedValue prefixed with "Expected: ".
   * \see makeExpectedEqual().
   */
  static std::string CPPUNIT_API makeExpectedGreater( const std::string &expectedValue );
  /*! \brief Returns a expected value string for a message, greater or equal than.
   *
   * \param expectedValue String that represents the expected value.
   * \return \a expectedValue prefixed with "Expected: ".
   * \see makeExpectedEqual().
   */
  static std::string CPPUNIT_API makeExpectedGreaterEqual( const std::string &expectedValue );

  /*! \brief Returns an actual value string for a message.
   * Typically used to create 'not equal' message, or to check that a message
   * contains the expected content when writing unit tests for your custom 
   * assertions.
   *
   * \param actualValue String that represents the actual value.
   * \return \a actualValue prefixed with "Actual  : ".
   * \see makeExpected().
   */
  static std::string CPPUNIT_API makeActual( const std::string &actualValue );

  /*!
   * \deprecated Use makeMessage instead
   */ 
  static Message CPPUNIT_API makeNotEqualMessage( const std::string &expectedValue,
                                                  const std::string &actualValue,
                                                  const AdditionalMessage &additionalMessage = AdditionalMessage(),
                                                  const std::string &shortDescription = "equality assertion failed");

  static Message CPPUNIT_API makeMessage( const std::string &expectedValue,
                                                  const std::string &actualValue,
                                                  const std::string &shortDescription,
                                                  const AdditionalMessage &additionalMessage = AdditionalMessage());

  /*! \brief Throws an Exception with the specified message and location.
   * \param expected Text describing the expected value.
   * \param actual Text describing the actual value.
   * \param sourceLine Location of the assertion.
   * \param additionalMessage Additional message. Usually used to report
   *                          what are the differences between the expected and actual value.
   * \param shortDescription Short description for the failure message.
   */
  NORETURN static void CPPUNIT_API failNotEqual( std::string expected, 
                                        std::string actual, 
                                        const SourceLine &sourceLine,
                                        const AdditionalMessage &additionalMessage = AdditionalMessage(),
                                        std::string shortDescription = "equality assertion failed" );

  /*! \brief Throws an Exception with the specified message and location.
   * \param expected Text describing the expected value.
   * \param actual Text describing the actual value.
   * \param sourceLine Location of the assertion.
   * \param additionalMessage Additional message. Usually used to report
   *                          what are the differences between the expected and actual value.
   * \param shortDescription Short description for the failure message.
   */
  static void CPPUNIT_API failNotLess( std::string expected, 
                                        std::string actual, 
                                        const SourceLine &sourceLine,
                                        const AdditionalMessage &additionalMessage = AdditionalMessage(),
                                        std::string shortDescription = "less assertion failed" );

  /*! \brief Throws an Exception with the specified message and location.
   * \param expected Text describing the expected value.
   * \param actual Text describing the actual value.
   * \param sourceLine Location of the assertion.
   * \param additionalMessage Additional message. Usually used to report
   *                          what are the differences between the expected and actual value.
   * \param shortDescription Short description for the failure message.
   */
  static void CPPUNIT_API failNotGreater( std::string expected, 
                                        std::string actual, 
                                        const SourceLine &sourceLine,
                                        const AdditionalMessage &additionalMessage = AdditionalMessage(),
                                        std::string shortDescription = "greater assertion failed" );

  /*! \brief Throws an Exception with the specified message and location.
   * \param expected Text describing the expected value.
   * \param actual Text describing the actual value.
   * \param sourceLine Location of the assertion.
   * \param additionalMessage Additional message. Usually used to report
   *                          what are the differences between the expected and actual value.
   * \param shortDescription Short description for the failure message.
   */
  static void CPPUNIT_API failNotLessEqual( std::string expected, 
                                        std::string actual, 
                                        const SourceLine &sourceLine,
                                        const AdditionalMessage &additionalMessage = AdditionalMessage(),
                                        std::string shortDescription = "less equal assertion failed" );

  /*! \brief Throws an Exception with the specified message and location.
   * \param expected Text describing the expected value.
   * \param actual Text describing the actual value.
   * \param sourceLine Location of the assertion.
   * \param additionalMessage Additional message. Usually used to report
   *                          what are the differences between the expected and actual value.
   * \param shortDescription Short description for the failure message.
   */
  static void CPPUNIT_API failNotGreaterEqual( std::string expected, 
                                        std::string actual, 
                                        const SourceLine &sourceLine,
                                        const AdditionalMessage &additionalMessage = AdditionalMessage(),
                                        std::string shortDescription = "greater equal assertion failed" );  /*! \brief Throws an Exception with the specified message and location.

   * \param shouldFail if \c true then the exception is thrown. Otherwise
   *                   nothing happen.
   * \param expected Text describing the expected value.
   * \param actual Text describing the actual value.
   * \param sourceLine Location of the assertion.
   * \param additionalMessage Additional message. Usually used to report
   *                          where the "difference" is located.
   * \param shortDescription Short description for the failure message.
   */
  static void CPPUNIT_API failNotEqualIf( bool shouldFail,
                                          std::string expected, 
                                          std::string actual, 
                                          const SourceLine &sourceLine,
                                          const AdditionalMessage &additionalMessage = AdditionalMessage(),
                                          std::string shortDescription = "equality assertion failed" );

};


CPPUNIT_NS_END


#endif  // CPPUNIT_ASSERTER_H