summaryrefslogtreecommitdiff
path: root/include/cppunit
diff options
context:
space:
mode:
authorMarkus Mohrhard <markus.mohrhard@googlemail.com>2012-05-05 21:52:20 +0200
committerMarkus Mohrhard <markus.mohrhard@googlemail.com>2012-05-06 16:38:21 +0200
commit4dbaa35920fa376c48eedf8cc3440b3965a01caa (patch)
treea57b25baae810fba7594b764628405fada72bac1 /include/cppunit
parent7902eb988d167b6cddd88b6c5be4385b203080eb (diff)
downloadcppunit-feature/new_assert_macros.tar.gz
add new assertion macros for <, <=, > and >=feature/new_assert_macros
Now we support the following new macros: - CPPUNIT_ASSERT_LESS - CPPUNIT_ASSERT_GREATER - CPPUNIT_ASSERT_LESSEQUAL - CPPUNIT_ASSERT_GREATEREQUAL
Diffstat (limited to 'include/cppunit')
-rw-r--r--include/cppunit/Asserter.h104
-rw-r--r--include/cppunit/TestAssert.h213
2 files changed, 314 insertions, 3 deletions
diff --git a/include/cppunit/Asserter.h b/include/cppunit/Asserter.h
index 94dadaa..1a333d2 100644
--- a/include/cppunit/Asserter.h
+++ b/include/cppunit/Asserter.h
@@ -76,16 +76,55 @@ struct Asserter
std::string message,
const SourceLine &sourceLine = SourceLine() );
- /*! \brief Returns a expected value string for a message.
+ /*! \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
@@ -98,11 +137,19 @@ struct Asserter
*/
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.
@@ -118,6 +165,61 @@ struct Asserter
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.
diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h
index 851a444..fa0bfa7 100644
--- a/include/cppunit/TestAssert.h
+++ b/include/cppunit/TestAssert.h
@@ -18,7 +18,7 @@
CPPUNIT_NS_BEGIN
-/*! \brief Traits used by CPPUNIT_ASSERT_EQUAL().
+/*! \brief Traits used by CPPUNIT_ASSERT* macros.
*
* Here is an example of specialising these traits:
*
@@ -30,7 +30,17 @@ CPPUNIT_NS_BEGIN
* {
* return x == y;
* }
- *
+ *
+ * static bool less( const std::string& x, const std::string& y )
+ * {
+ * return x < y;
+ * }
+ *
+ * static bool lessEqual( const std::string& x, const std::string& y )
+ * {
+ * return x <= y;
+ * }
+ *
* static std::string toString( const std::string& x )
* {
* std::string text = '"' + x + '"'; // adds quote around the string to see whitespace
@@ -49,6 +59,16 @@ struct assertion_traits
return x == y;
}
+ static bool less( const T& x, const T& y )
+ {
+ return x < y;
+ }
+
+ static bool lessEqual( const T& x, const T& y )
+ {
+ return x <= y;
+ }
+
static std::string toString( const T& x )
{
OStringStream ost;
@@ -83,6 +103,16 @@ struct assertion_traits<double>
return x == y;
}
+ static bool less( double x, double y )
+ {
+ return x < y;
+ }
+
+ static bool lessEqual( double x, double y )
+ {
+ return x <= y;
+ }
+
static std::string toString( double x )
{
#ifdef DBL_DIG
@@ -133,6 +163,82 @@ void CPPUNIT_API assertDoubleEquals( double expected,
const std::string &message );
+/*! \brief (Implementation) Asserts that an object is less than another one of the same type
+ * Use CPPUNIT_ASSERT_LESS, CPPUNIT_ASSERT_GREATER instead of this function.
+ * \sa assertion_traits, Asserter::failNotLess().
+ */
+template <class T>
+void assertLess( const T& expected,
+ const T& actual,
+ SourceLine sourceLine,
+ const std::string& message )
+{
+ if ( !assertion_traits<T>::less(actual,expected) )
+ {
+ Asserter::failNotLess( assertion_traits<T>::toString(expected),
+ assertion_traits<T>::toString(actual),
+ sourceLine,
+ message );
+ }
+}
+
+
+/*! \brief (Implementation) Asserts that an object is less than another one of the same type
+ * Use CPPUNIT_ASSERT_LESS, CPPUNIT_ASSERT_GREATER instead of this function.
+ * \sa assertion_traits, Asserter::failNotLess().
+ */
+template <class T>
+void assertGreater( const T& expected,
+ const T& actual,
+ SourceLine sourceLine,
+ const std::string& message )
+{
+ if ( !assertion_traits<T>::less(expected,actual) )
+ {
+ Asserter::failNotGreater( assertion_traits<T>::toString(expected),
+ assertion_traits<T>::toString(actual),
+ sourceLine,
+ message );
+ }
+}
+
+/*! \brief (Implementation) Asserts that two objects of the same type are equals.
+ * Use CPPUNIT_ASSERT_LESSEQUAL, CPPUNIT_ASSERT_GREATEREQUAL instead of this function.
+ * \sa assertion_traits, Asserter::failNotLessEqual().
+ */
+template <class T>
+void assertLessEqual( const T& expected,
+ const T& actual,
+ SourceLine sourceLine,
+ const std::string& message )
+{
+ if ( !assertion_traits<T>::lessEqual(actual,expected) )
+ {
+ Asserter::failNotLessEqual( assertion_traits<T>::toString(expected),
+ assertion_traits<T>::toString(actual),
+ sourceLine,
+ message );
+ }
+}
+
+/*! \brief (Implementation) Asserts that two objects of the same type are equals.
+ * Use CPPUNIT_ASSERT_LESSEQUAL, CPPUNIT_ASSERT_GREATEREQUAL instead of this function.
+ * \sa assertion_traits, Asserter::failNotLessEqual().
+ */
+template <class T>
+void assertGreaterEqual( const T& expected,
+ const T& actual,
+ SourceLine sourceLine,
+ const std::string& message )
+{
+ if ( !assertion_traits<T>::lessEqual(expected,actual) )
+ {
+ Asserter::failNotGreaterEqual( assertion_traits<T>::toString(expected),
+ assertion_traits<T>::toString(actual),
+ sourceLine,
+ message );
+ }
+}
/* A set of macros which allow us to get the line number
* and file name at the point of an error.
* Just goes to show that preprocessors do have some
@@ -232,6 +338,109 @@ void CPPUNIT_API assertDoubleEquals( double expected,
(message) ) )
#endif
+/** Asserts that actual is less than expected, provides additional message on failure.
+ * \ingroup Assertions
+ *
+ * Less and string representation can be defined with
+ * an appropriate assertion_traits class.
+ *
+ * A diagnostic is printed if actual is less than expected.
+ * The message is printed in addition to the expected and actual value
+ * to provide additional information.
+ *
+ * Requirement for \a expected and \a actual parameters:
+ * - They are exactly of the same type
+ * - They are serializable into a std::strstream using operator <<.
+ * - They can be compared using operator <.
+ *
+ * The last two requirements (serialization and comparison) can be
+ * removed by specializing the CppUnit::assertion_traits.
+ *
+ * \sa CPPUNIT_ASSERT_GREATER
+ */
+#define CPPUNIT_ASSERT_LESS(expected, actual) \
+ ( CPPUNIT_NS::assertLess( (expected), \
+ (actual), \
+ CPPUNIT_SOURCELINE(), \
+ "" ) )
+
+/** Asserts that actual is greater than expected, provides additional message on failure.
+ * \ingroup Assertions
+ *
+ * String representation can be defined with
+ * an appropriate assertion_traits class. For comparison assertLess is used.
+ *
+ * A diagnostic is printed if actual is less than expected.
+ * The message is printed in addition to the expected and actual value
+ * to provide additional information.
+ *
+ * Requirement for \a expected and \a actual parameters:
+ * - They are exactly of the same type
+ * - They are serializable into a std::strstream using operator <<.
+ * - They can be compared using operator<.
+ *
+ * The last two requirements (serialization and comparison) can be
+ * removed by specializing the CppUnit::assertion_traits.
+ *
+ * \sa CPPUNIT_ASSERT_LESS
+ */
+#define CPPUNIT_ASSERT_GREATER(expected, actual) \
+ ( CPPUNIT_NS::assertGreater( (expected), \
+ (actual), \
+ CPPUNIT_SOURCELINE(), \
+ "" ) )
+
+/** Asserts that actual is less or equal than expected, provides additional message on failure.
+ * \ingroup Assertions
+ *
+ * LessEqual and string representation can be defined with
+ * an appropriate assertion_traits class.
+ *
+ * A diagnostic is printed if actual is greater than expected.
+ * The message is printed in addition to the expected and actual value
+ * to provide additional information.
+ *
+ * Requirement for \a expected and \a actual parameters:
+ * - They are exactly of the same type
+ * - They are serializable into a std::strstream using operator <<.
+ * - They can be compared using operator <=.
+ *
+ * The last two requirements (serialization and comparison) can be
+ * removed by specializing the CppUnit::assertion_traits.
+ *
+ * \sa CPPUNIT_ASSERT_GREATEREQUAL
+ */
+#define CPPUNIT_ASSERT_LESSEQUAL(expected, actual) \
+ ( CPPUNIT_NS::assertLessEqual( (expected), \
+ (actual), \
+ CPPUNIT_SOURCELINE(), \
+ "" ) )
+
+/** Asserts that actual is greater than expected, provides additional message on failure.
+ * \ingroup Assertions
+ *
+ * String representation can be defined with
+ * an appropriate assertion_traits class. For comparison assertLess is used.
+ *
+ * A diagnostic is printed if actual is less than expected.
+ * The message is printed in addition to the expected and actual value
+ * to provide additional information.
+ *
+ * Requirement for \a expected and \a actual parameters:
+ * - They are exactly of the same type
+ * - They are serializable into a std::strstream using operator <<.
+ * - They can be compared using operator<=.
+ *
+ * The last two requirements (serialization and comparison) can be
+ * removed by specializing the CppUnit::assertion_traits.
+ *
+ * \sa CPPUNIT_ASSERT_LESSEQUAL
+ */
+#define CPPUNIT_ASSERT_GREATEREQUAL(expected, actual) \
+ ( CPPUNIT_NS::assertGreaterEqual( (expected), \
+ (actual), \
+ CPPUNIT_SOURCELINE(), \
+ "" ) )
/*! \brief Macro for primitive double value comparisons.
* \ingroup Assertions
*