diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-06-12 05:59:26 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-06-12 05:59:26 +0000 |
| commit | bf78ed5d2c0f623efd08c277f97b240f2fef7ee6 (patch) | |
| tree | 6faa8ef2ae7d679cb14111211e8971f2deb9a54f /src/cppunit | |
| parent | 517df147c8508ec32c7ab780cc434d7f541e2bc7 (diff) | |
| download | cppunit-bf78ed5d2c0f623efd08c277f97b240f2fef7ee6.tar.gz | |
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.
Diffstat (limited to 'src/cppunit')
| -rw-r--r-- | src/cppunit/NotEqualException.cpp | 22 | ||||
| -rw-r--r-- | src/cppunit/TestAssert.cpp | 19 | ||||
| -rw-r--r-- | src/cppunit/TextTestResult.cpp | 19 |
3 files changed, 51 insertions, 9 deletions
diff --git a/src/cppunit/NotEqualException.cpp b/src/cppunit/NotEqualException.cpp index 2fe9e76..1615c2b 100644 --- a/src/cppunit/NotEqualException.cpp +++ b/src/cppunit/NotEqualException.cpp @@ -7,9 +7,11 @@ NotEqualException::NotEqualException( std::string expected, std::string actual, long lineNumber, std::string fileName ) : - Exception( "Expected: " + expected + ", but was:" + actual, + Exception( "Expected: " + expected + ", but was: " + actual, lineNumber, - fileName ) + fileName ), + m_expected( expected ), + m_actual( actual ) { } @@ -30,6 +32,8 @@ NotEqualException::~NotEqualException() NotEqualException & NotEqualException::operator =( const NotEqualException &other ) { + Exception::operator =( other ); + if ( &other != this ) { m_expected = other.m_expected; @@ -61,4 +65,18 @@ NotEqualException::type() } +std::string +NotEqualException::expectedValue() const +{ + return m_expected; +} + + +std::string +NotEqualException::actualValue() const +{ + return m_actual; +} + + } // namespace CppUnit diff --git a/src/cppunit/TestAssert.cpp b/src/cppunit/TestAssert.cpp index 2750023..f93c19b 100644 --- a/src/cppunit/TestAssert.cpp +++ b/src/cppunit/TestAssert.cpp @@ -2,6 +2,7 @@ #include "cppunit/TestAssert.h" #include "estring.h" +#include <cppunit/NotEqualException.h> namespace CppUnit { @@ -18,6 +19,16 @@ void TestAssert::assertImplementation (bool condition, } +/// Reports failed equality +void TestAssert::assertNotEqualImplementation( std::string expected, + std::string actual, + long lineNumber, + std::string fileName ) +{ + throw NotEqualException( expected, actual, lineNumber, fileName ); +} + + /// Check for a failed equality assertion void TestAssert::assertEquals (double expected, double actual, @@ -26,10 +37,10 @@ void TestAssert::assertEquals (double expected, std::string fileName) { if (fabs (expected - actual) > delta) - assertImplementation (false, - notEqualsMessage(expected, actual), - lineNumber, - fileName); + assertNotEqualImplementation( assertion_traits<double>::toString(expected), + assertion_traits<double>::toString(actual), + lineNumber, + fileName ); } diff --git a/src/cppunit/TextTestResult.cpp b/src/cppunit/TextTestResult.cpp index cbd226f..115da91 100644 --- a/src/cppunit/TextTestResult.cpp +++ b/src/cppunit/TextTestResult.cpp @@ -1,6 +1,7 @@ #include <iostream> #include "cppunit/TextTestResult.h" #include "cppunit/Exception.h" +#include "cppunit/NotEqualException.h" #include "cppunit/Test.h" #include "estring.h" @@ -82,9 +83,21 @@ TextTestResult::printFailures (std::ostream& stream) << ") " << "test: " << failure->failedTest()->getName() << " " << "line: " << (e ? estring (e->lineNumber ()) : "") << " " - << (e ? e->fileName () : "") << " " - << "\"" << failure->thrownException ()->what () << "\"" - << std::endl; + << (e ? e->fileName () : "") << " "; + + if ( failure->thrownException()->isInstanceOf( NotEqualException::type() ) ) + { + NotEqualException *e = (NotEqualException*)failure->thrownException(); + stream << std::endl + << "expected: " << e->expectedValue() << std::endl + << "but was: " << e->actualValue(); + } + else + { + stream << "\"" << failure->thrownException ()->what () << "\""; + } + + stream << std::endl; i++; } } |
