diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2012-05-05 21:52:20 +0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2012-05-06 16:38:21 +0200 |
commit | 4dbaa35920fa376c48eedf8cc3440b3965a01caa (patch) | |
tree | a57b25baae810fba7594b764628405fada72bac1 | |
parent | 7902eb988d167b6cddd88b6c5be4385b203080eb (diff) |
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
-rw-r--r-- | examples/cppunittest/TestAssertTest.cpp | 45 | ||||
-rw-r--r-- | examples/cppunittest/TestAssertTest.h | 8 | ||||
-rw-r--r-- | include/cppunit/Asserter.h | 104 | ||||
-rw-r--r-- | include/cppunit/TestAssert.h | 213 | ||||
-rw-r--r-- | src/cppunit/Asserter.cpp | 116 |
5 files changed, 472 insertions, 14 deletions
diff --git a/examples/cppunittest/TestAssertTest.cpp b/examples/cppunittest/TestAssertTest.cpp index 3960b8e..c990607 100644 --- a/examples/cppunittest/TestAssertTest.cpp +++ b/examples/cppunittest/TestAssertTest.cpp @@ -137,6 +137,51 @@ TestAssertTest::testAssertEqual() CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_EQUAL( 1, 2 ) ); } + +void +TestAssertTest::testAssertLess() +{ + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESS( 2, 1 ) ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESS( 12345679, 12345678 ) ); + + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_LESS( 1, 2 ) ); +} + + +void +TestAssertTest::testAssertGreater() +{ + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATER( 1, 2 ) ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATER( 12345678, 12345679 )); + + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_GREATER( 2, 1 ) ); + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_GREATER( 2, 2 ) ); +} + + +void +TestAssertTest::testAssertLessEqual() +{ + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESSEQUAL( 2, 1 ) ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESSEQUAL( 12345679, 12345678 )); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_LESSEQUAL( 2, 2 ) ); + + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_LESSEQUAL( 1, 2 ) ); +} + +void +TestAssertTest::testAssertGreaterEqual() +{ + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATEREQUAL( 1, 2 ) ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATEREQUAL( 12345678, 12345679 )); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATEREQUAL( 12345678, 12345678 )); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_GREATEREQUAL( 2, 2 ) ); + + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_GREATEREQUAL( 2, 1 ) ); +} + + + void TestAssertTest::testAssertMessageTrue() { diff --git a/examples/cppunittest/TestAssertTest.h b/examples/cppunittest/TestAssertTest.h index a672d38..01d303e 100644 --- a/examples/cppunittest/TestAssertTest.h +++ b/examples/cppunittest/TestAssertTest.h @@ -13,6 +13,10 @@ class TestAssertTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST( testAssertAssertionPass ); CPPUNIT_TEST( testAssert ); CPPUNIT_TEST( testAssertEqual ); + CPPUNIT_TEST( testAssertLess ); + CPPUNIT_TEST( testAssertGreater ); + CPPUNIT_TEST( testAssertLessEqual ); + CPPUNIT_TEST( testAssertGreaterEqual ); CPPUNIT_TEST( testAssertMessageTrue ); CPPUNIT_TEST( testAssertMessageFalse ); CPPUNIT_TEST( testAssertDoubleEquals ); @@ -39,6 +43,10 @@ public: void testAssert(); void testAssertEqual(); + void testAssertLess(); + void testAssertGreater(); + void testAssertLessEqual(); + void testAssertGreaterEqual(); void testAssertMessageTrue(); void testAssertMessageFalse(); 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 * diff --git a/src/cppunit/Asserter.cpp b/src/cppunit/Asserter.cpp index a9cf95c..00f4574 100644 --- a/src/cppunit/Asserter.cpp +++ b/src/cppunit/Asserter.cpp @@ -40,13 +40,41 @@ Asserter::failIf( bool shouldFail, failIf( shouldFail, Message( "assertion failed", message ), sourceLine ); } - std::string Asserter::makeExpected( const std::string &expectedValue ) { return "Expected: " + expectedValue; } +std::string +Asserter::makeExpectedEqual( const std::string &expectedValue ) +{ + return "Expected: " + expectedValue; +} + +std::string +Asserter::makeExpectedLess( const std::string& expectedValue ) +{ + return "Expected less than: " + expectedValue; +} + +std::string +Asserter::makeExpectedLessEqual( const std::string& expectedValue ) +{ + return "Expected less or equal than: " + expectedValue; +} + +std::string +Asserter::makeExpectedGreater( const std::string& expectedValue ) +{ + return "Expected greater than: " + expectedValue; +} + +std::string +Asserter::makeExpectedGreaterEqual( const std::string& expectedValue ) +{ + return "Expected greater or equal than: " + expectedValue; +} std::string Asserter::makeActual( const std::string &actualValue ) @@ -55,18 +83,28 @@ Asserter::makeActual( const std::string &actualValue ) } +Message +Asserter::makeMessage( const std::string& expectedMessage, + const std::string& actualMessage, + const std::string& shortDescription, + const AdditionalMessage& additionalMessage) +{ + Message message( shortDescription, + expectedMessage, + actualMessage ); + message.addDetail( additionalMessage ); + + return message; +} + + Message Asserter::makeNotEqualMessage( const std::string &expectedValue, const std::string &actualValue, const AdditionalMessage &additionalMessage, const std::string &shortDescription ) { - Message message( shortDescription, - makeExpected( expectedValue ), - makeActual( actualValue ) ); - message.addDetail( additionalMessage ); - - return message; + return makeMessage(makeExpectedEqual(expectedValue), makeActual(actualValue), shortDescription, additionalMessage); } @@ -77,15 +115,71 @@ Asserter::failNotEqual( std::string expected, const AdditionalMessage &additionalMessage, std::string shortDescription ) { - fail( makeNotEqualMessage( expected, - actual, - additionalMessage, - shortDescription ), + fail( makeMessage( makeExpectedEqual(expected), + makeActual(actual), + shortDescription, + additionalMessage ), sourceLine ); } void +Asserter::failNotLess( std::string expected, + std::string actual, + const SourceLine &sourceLine, + const AdditionalMessage &additionalMessage, + std::string shortDescription ) +{ + fail( makeMessage( makeExpectedLess(expected), + makeActual(actual), + shortDescription, + additionalMessage), + sourceLine ); +} + + +void +Asserter::failNotGreater( std::string expected, + std::string actual, + const SourceLine &sourceLine, + const AdditionalMessage &additionalMessage, + std::string shortDescription ) +{ + fail( makeMessage( makeExpectedGreater(expected), + makeActual(actual), + shortDescription, + additionalMessage), + sourceLine ); +} + +void +Asserter::failNotLessEqual( std::string expected, + std::string actual, + const SourceLine &sourceLine, + const AdditionalMessage &additionalMessage, + std::string shortDescription ) +{ + fail( makeMessage( makeExpectedLessEqual(expected), + makeActual(actual), + shortDescription, + additionalMessage ), + sourceLine ); +} + +void +Asserter::failNotGreaterEqual( std::string expected, + std::string actual, + const SourceLine &sourceLine, + const AdditionalMessage &additionalMessage, + std::string shortDescription ) +{ + fail( makeMessage( makeExpectedGreaterEqual(expected), + makeActual(actual), + shortDescription, + additionalMessage ), + sourceLine ); +} +void Asserter::failNotEqualIf( bool shouldFail, std::string expected, std::string actual, |