diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-06-11 18:59:15 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-06-11 18:59:15 +0000 |
| commit | c7a4dccd9f1b1fadcd47afe482c8a8ff9e05ea8f (patch) | |
| tree | f0bb90d0fd555bbf07646380c6cab7bf06254620 /src/cppunit | |
| parent | 1806e9640462461fba3e1149c7b2c4a31805ec5e (diff) | |
| download | cppunit-c7a4dccd9f1b1fadcd47afe482c8a8ff9e05ea8f.tar.gz | |
Include/cppunit/NotEqualException.
include/cppunit/NotEqualException.cpp: addded, exception to be used
with assertEquals().
* src/cppunit/RepeatedTest.cpp: added to reduce header dependency
(TestResult.h was missing).
* src/cppunit/TestAssert.cpp: added to put non template functions
there.
* src/cppunit/TestCase.cpp: added std:: prefix to
catch (exception& e). Integrated a modified version of Tim Jansen
patch (#403745) for TestCase to make the unit test (TestCaseTest)
pass. If the setUp() fail then neither the runTest() nor
the tearDown() method is called.
Diffstat (limited to 'src/cppunit')
| -rw-r--r-- | src/cppunit/Exception.cpp | 79 | ||||
| -rw-r--r-- | src/cppunit/Makefile.am | 3 | ||||
| -rw-r--r-- | src/cppunit/NotEqualException.cpp | 64 | ||||
| -rw-r--r-- | src/cppunit/RepeatedTest.cpp | 37 | ||||
| -rw-r--r-- | src/cppunit/TestAssert.cpp | 36 | ||||
| -rw-r--r-- | src/cppunit/TestCase.cpp | 48 | ||||
| -rw-r--r-- | src/cppunit/cppunit.dsp | 20 |
7 files changed, 242 insertions, 45 deletions
diff --git a/src/cppunit/Exception.cpp b/src/cppunit/Exception.cpp index fa21f51..26a805e 100644 --- a/src/cppunit/Exception.cpp +++ b/src/cppunit/Exception.cpp @@ -1,37 +1,47 @@ #include "cppunit/Exception.h" -const std::string -CppUnit::Exception::UNKNOWNFILENAME = - "<unknown>"; -const int CppUnit::Exception::UNKNOWNLINENUMBER = -1; + +namespace CppUnit { + + +const std::string Exception::UNKNOWNFILENAME = "<unknown>"; + +const long Exception::UNKNOWNLINENUMBER = -1; + /// Construct the exception -CppUnit::Exception::Exception (const Exception& other) - : exception (other) +Exception::Exception (const Exception& other) : exception (other) { m_message = other.m_message; m_lineNumber = other.m_lineNumber; m_fileName = other.m_fileName; } -CppUnit::Exception::Exception (std::string message, long lineNumber, std::string fileName) - : m_message (message), m_lineNumber (lineNumber), m_fileName (fileName) + +Exception::Exception( std::string message, + long lineNumber, + std::string fileName ) : + m_message( message ), + m_lineNumber( lineNumber ), + m_fileName( fileName ) { } /// Destruct the exception -CppUnit::Exception::~Exception () -{} +Exception::~Exception () +{ +} /// Perform an assignment -CppUnit::Exception& -CppUnit::Exception::operator= (const Exception& other) +Exception& +Exception::operator=( const Exception& other ) { exception::operator= (other); - if (&other != this) { + if (&other != this) + { m_message = other.m_message; m_lineNumber = other.m_lineNumber; m_fileName = other.m_fileName; @@ -43,16 +53,47 @@ CppUnit::Exception::operator= (const Exception& other) /// Return descriptive message const char* -CppUnit::Exception::what() const throw () -{ return m_message.c_str (); } +Exception::what() const throw () +{ + return m_message.c_str (); +} + /// The line on which the error occurred long -CppUnit::Exception::lineNumber () -{ return m_lineNumber; } +Exception::lineNumber() +{ + return m_lineNumber; +} /// The file in which the error occurred std::string -CppUnit::Exception::fileName () -{ return m_fileName; } +Exception::fileName() +{ + return m_fileName; +} + + +Exception * +Exception::clone() const +{ + return new Exception( *this ); +} + + +bool +Exception::isInstanceOf( const Type &exceptionType ) const +{ + return exceptionType == type(); +} + + +Exception::Type +Exception::type() +{ + return Type( "CppUnit::Exception" ); +} + + +} // namespace CppUnit
\ No newline at end of file diff --git a/src/cppunit/Makefile.am b/src/cppunit/Makefile.am index f8933c7..f76ae21 100644 --- a/src/cppunit/Makefile.am +++ b/src/cppunit/Makefile.am @@ -1,5 +1,5 @@ # -# $Id: Makefile.am,v 1.5 2001-06-02 22:29:52 smr99 Exp $ +# $Id: Makefile.am,v 1.6 2001-06-11 19:59:15 blep Exp $ # EXTRA_DIST = cppunit.dsw cppunit.dsp @@ -8,6 +8,7 @@ INCLUDES = -I$(top_srcdir)/include lib_LTLIBRARIES = libcppunit.la libcppunit_la_SOURCES = \ + NotEqualException.cpp \ TestCase.cpp \ TestSuite.cpp \ TestResult.cpp \ diff --git a/src/cppunit/NotEqualException.cpp b/src/cppunit/NotEqualException.cpp new file mode 100644 index 0000000..2fe9e76 --- /dev/null +++ b/src/cppunit/NotEqualException.cpp @@ -0,0 +1,64 @@ +#include <cppunit/NotEqualException.h> + +namespace CppUnit { + + +NotEqualException::NotEqualException( std::string expected, + std::string actual, + long lineNumber, + std::string fileName ) : + Exception( "Expected: " + expected + ", but was:" + actual, + lineNumber, + fileName ) +{ +} + + +NotEqualException::NotEqualException( const NotEqualException &other ) : + Exception( other ), + m_expected( other.m_expected ), + m_actual( other.m_actual ) +{ +} + + +NotEqualException::~NotEqualException() +{ +} + + +NotEqualException & +NotEqualException::operator =( const NotEqualException &other ) +{ + if ( &other != this ) + { + m_expected = other.m_expected; + m_actual = other.m_actual; + } + return *this; +} + + +Exception * +NotEqualException::clone() const +{ + return new NotEqualException( *this ); +} + + +bool +NotEqualException::isInstanceOf( const Type &exceptionType ) const +{ + return exceptionType == type() || + Exception::isInstanceOf( exceptionType ); +} + + +Exception::Type +NotEqualException::type() +{ + return Type( "CppUnit::NotEqualException" ); +} + + +} // namespace CppUnit diff --git a/src/cppunit/RepeatedTest.cpp b/src/cppunit/RepeatedTest.cpp new file mode 100644 index 0000000..10f99d0 --- /dev/null +++ b/src/cppunit/RepeatedTest.cpp @@ -0,0 +1,37 @@ +#include <cppunit/extensions/RepeatedTest.h> +#include <cppunit/TestResult.h> + +namespace CppUnit { + + + +// Counts the number of test cases that will be run by this test. +int +RepeatedTest::countTestCases() +{ + return TestDecorator::countTestCases () * m_timesRepeat; +} + + +// Returns the name of the test instance. +std::string +RepeatedTest::toString() +{ + return TestDecorator::toString () + " (repeated)"; +} + +// Runs a repeated test +void +RepeatedTest::run( TestResult *result ) +{ + for ( int n = 0; n < m_timesRepeat; n++ ) + { + if ( result->shouldStop() ) + break; + + TestDecorator::run( result ); + } +} + + +} // namespace TestAssert diff --git a/src/cppunit/TestAssert.cpp b/src/cppunit/TestAssert.cpp new file mode 100644 index 0000000..2750023 --- /dev/null +++ b/src/cppunit/TestAssert.cpp @@ -0,0 +1,36 @@ +#include <cmath> + +#include "cppunit/TestAssert.h" +#include "estring.h" + +namespace CppUnit { + +/// Check for a failed general assertion +void TestAssert::assertImplementation (bool condition, + std::string conditionExpression, + long lineNumber, + std::string fileName) +{ + if (!condition) + throw Exception (conditionExpression, + lineNumber, + fileName); +} + + +/// Check for a failed equality assertion +void TestAssert::assertEquals (double expected, + double actual, + double delta, + long lineNumber, + std::string fileName) +{ + if (fabs (expected - actual) > delta) + assertImplementation (false, + notEqualsMessage(expected, actual), + lineNumber, + fileName); +} + + +} // namespace TestAssert diff --git a/src/cppunit/TestCase.cpp b/src/cppunit/TestCase.cpp index 66a2462..df5f96e 100644 --- a/src/cppunit/TestCase.cpp +++ b/src/cppunit/TestCase.cpp @@ -19,31 +19,37 @@ void TestCase::run (TestResult *result) { result->startTest (this); - - setUp (); - - try { - runTest (); - - } - catch (Exception& e) { - Exception *copy = new Exception (e); - result->addFailure (this, copy); - - } - catch (exception& e) { - result->addError (this, new Exception (e.what ())); - + try + { + setUp (); + + try { + runTest (); + } + catch (Exception& e) { + Exception *copy = e.clone(); + result->addFailure (this, copy); + } + catch (std::exception& e) { + result->addError (this, new Exception (e.what ())); + } + catch (...) { + Exception *e = new Exception ("caught unknown exception"); + result->addError (this, e); + } + + try { + tearDown (); + } + catch ( ... ) { + result->addError( this, new Exception( "tearDown() failed" ) ); + } } - catch (...) { - Exception *e = new Exception ("unknown exception"); - result->addError (this, e); - + catch ( ... ) { + result->addError( this, new Exception( "setUp() failed" ) ); } - tearDown (); - result->endTest (this); } diff --git a/src/cppunit/cppunit.dsp b/src/cppunit/cppunit.dsp index 1ac2f67..3a52746 100644 --- a/src/cppunit/cppunit.dsp +++ b/src/cppunit/cppunit.dsp @@ -122,6 +122,10 @@ SOURCE=..\..\include\cppunit\extensions\Orthodox.h # End Source File # Begin Source File +SOURCE=.\RepeatedTest.cpp +# End Source File +# Begin Source File + SOURCE=..\..\include\cppunit\extensions\RepeatedTest.h # End Source File # Begin Source File @@ -142,10 +146,6 @@ SOURCE=..\..\include\cppunit\extensions\TestFactoryRegistry.h # End Source File # Begin Source File -SOURCE=..\..\include\cppunit\extensions\TestSetup.h -# End Source File -# Begin Source File - SOURCE=..\..\include\cppunit\extensions\TestSuiteBuilder.h # End Source File # Begin Source File @@ -183,10 +183,22 @@ SOURCE=..\..\include\cppunit\Exception.h # End Source File # Begin Source File +SOURCE=.\NotEqualException.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\include\cppunit\NotEqualException.h +# End Source File +# Begin Source File + SOURCE=..\..\include\cppunit\Test.h # End Source File # Begin Source File +SOURCE=.\TestAssert.cpp +# End Source File +# Begin Source File + SOURCE=..\..\include\cppunit\TestAssert.h # End Source File # Begin Source File |
