From bf78ed5d2c0f623efd08c277f97b240f2fef7ee6 Mon Sep 17 00:00:00 2001 From: Baptiste Lepilleur Date: Tue, 12 Jun 2001 05:59:26 +0000 Subject: Include/cppunit/NotEqualException. include/cppunit/NotEqualException.h * src/cppunit/NotEqualException.h: Fixed constructor and operator = (aren't unit test nice?). Added methods expectedValue() and actualValue(). * include/cppunit/TestAssert.h: * src/cppunit/TestAssert.cpp: Use NotEqualException to report equality failure. * src/cppunit/TestResult.cpp: Report expect/was on different line for assertEquals failure. * examples/cppunittest/NotEqualExceptionTest.*: added unit tests for NotEqualException. --- include/cppunit/NotEqualException.h | 4 +++ include/cppunit/TestAssert.h | 71 ++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 37 deletions(-) (limited to 'include/cppunit') diff --git a/include/cppunit/NotEqualException.h b/include/cppunit/NotEqualException.h index ba39cd2..ca97364 100644 --- a/include/cppunit/NotEqualException.h +++ b/include/cppunit/NotEqualException.h @@ -19,6 +19,10 @@ namespace CppUnit { virtual ~NotEqualException(); + std::string expectedValue() const; + + std::string actualValue() const; + /*! Copy operator. * @param other Object to copy. * @return Reference on this object. diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h index c6ccb48..a117186 100644 --- a/include/cppunit/TestAssert.h +++ b/include/cppunit/TestAssert.h @@ -9,25 +9,23 @@ namespace CppUnit { - template - struct assertion_traits - { - static bool equal( const T& x, const T& y ) - { - return x == y; - } - - static std::string toString( const T& x ) - { - std::ostringstream ost; - ost << x; - return ost.str(); - } - }; - - - /*! \brief This class represents - */ + template + struct assertion_traits + { + static bool equal( const T& x, const T& y ) + { + return x == y; + } + + static std::string toString( const T& x ) + { + std::ostringstream ost; + ost << x; + return ost.str(); + } + }; + + namespace TestAssert { void assertImplementation( bool condition, @@ -35,28 +33,27 @@ namespace CppUnit { long lineNumber = Exception::UNKNOWNLINENUMBER, std::string fileName = Exception::UNKNOWNFILENAME ); + void assertNotEqualImplementation( std::string expected, + std::string actual, + long lineNumber = Exception::UNKNOWNLINENUMBER, + std::string fileName = Exception::UNKNOWNFILENAME ); + + template - std::string notEqualsMessage (const T& expected, - const T& actual) + void assertEquals( const T& expected, + const T& actual, + long lineNumber = Exception::UNKNOWNLINENUMBER, + std::string fileName = Exception::UNKNOWNFILENAME ) { - return "expected: " + assertion_traits::toString(expected) - + " but was: " + assertion_traits::toString(actual); + if ( !assertion_traits::equal(expected,actual) ) // lazy toString conversion... + { + assertNotEqualImplementation( assertion_traits::toString(expected), + assertion_traits::toString(actual), + lineNumber, + fileName ); + } } - - template - void assertEquals ( - const T& expected, - const T& actual, - long lineNumber = Exception::UNKNOWNLINENUMBER, - std::string fileName = Exception::UNKNOWNFILENAME) - { - assertImplementation( assertion_traits::equal(expected,actual), - notEqualsMessage(expected, actual), - lineNumber, - fileName); - } - void assertEquals( double expected, double actual, double delta, -- cgit v1.2.1