diff options
author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-10-02 05:49:27 +0000 |
---|---|---|
committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-10-02 05:49:27 +0000 |
commit | 9e5ff0f6a865524e5784450f26a1c74807dc0c80 (patch) | |
tree | 06537ff76603a0915252e3678fd922b2f00ecde4 /examples/cppunittest/TestAssertTest.cpp | |
parent | 0fe7e4fb21a7f2b096965dab4ad2ee19cc20522f (diff) | |
download | cppunit-9e5ff0f6a865524e5784450f26a1c74807dc0c80.tar.gz |
NEWS : updated.
NEWS : updated.
* doc/other_documentation.dox : added all the authors to the list of
authors.
* examples/cppunittest/HelperMacrosTest.*: added unit tests for
CPPUNIT_TEST_FAIL & CPPUNIT_TEST_EXCEPTION.
* examples/cppunittest/TestAssertTest.*: added unit tests for
CPPUNIT_FAIL. Corrected spelling error. Relaxed constraint on message
produced by CPPUNIT_ASSERT_MESSAGE. Refactored some tests.
* include/cppunit/extensions/HelperMacros.h : added macro
CPPUNIT_TEST_EXCEPTION to create a test case for the specified method
that must throw an exception of the specified type.
* include/cppunit/extensions/TestSuiteBuilder.h : made makeTestName() public.
Added addTestCallerForException() to add a test case expecting an exception of
the specified type to be caught.
* include/cppunit/TestAssert.h : added macro CPPUNIT_FAIL as a
shortcut for CPPUNIT_ASSERT_MESSAGE( message, false ).
Diffstat (limited to 'examples/cppunittest/TestAssertTest.cpp')
-rw-r--r-- | examples/cppunittest/TestAssertTest.cpp | 79 |
1 files changed, 39 insertions, 40 deletions
diff --git a/examples/cppunittest/TestAssertTest.cpp b/examples/cppunittest/TestAssertTest.cpp index 05f2b25..c87cdb9 100644 --- a/examples/cppunittest/TestAssertTest.cpp +++ b/examples/cppunittest/TestAssertTest.cpp @@ -1,4 +1,5 @@ #include "TestAssertTest.h" +#include <algorithm> /* Note: @@ -34,6 +35,7 @@ TestAssertTest::tearDown() { } + void TestAssertTest::testAssertTrue() { @@ -44,17 +46,7 @@ TestAssertTest::testAssertTrue() void TestAssertTest::testAssertFalse() { - bool exceptionCatched = false; - try - { - CPPUNIT_ASSERT( false ); - } - catch( CppUnit::Exception & ) - { - exceptionCatched = true; // ok, we were expecting an exception. - } - - CPPUNIT_ASSERT( exceptionCatched ); + CPPUNIT_ASSERT( false ); } @@ -64,8 +56,8 @@ static int foo() { return 1; } void TestAssertTest::testAssertEqual() { - CPPUNIT_ASSERT_EQUAL(1,1); - CPPUNIT_ASSERT_EQUAL( 1, foo() ); + CPPUNIT_ASSERT_EQUAL( 1, 1 ); + CPPUNIT_ASSERT_EQUAL( 1, foo() ); } @@ -79,19 +71,19 @@ TestAssertTest::testAssertMessageTrue() void TestAssertTest::testAssertMessageFalse() { - bool exceptionCatched = false; + bool exceptionCaught = false; std::string message( "This test message should not be seen" ); try { - CPPUNIT_ASSERT_MESSAGE( message, false ); + CPPUNIT_ASSERT_MESSAGE( message, 2==3 ); } catch( CppUnit::Exception &e ) { - CPPUNIT_ASSERT_EQUAL( message, std::string( e.what() ) ); - exceptionCatched = true; // ok, we were expecting an exception. + exceptionCaught = true; // ok, we were expecting an exception. + checkMessageContains( &e, message ); } - CPPUNIT_ASSERT( exceptionCatched ); + CPPUNIT_ASSERT( exceptionCaught ); } @@ -104,29 +96,16 @@ TestAssertTest::testAssertDoubleEquals() void -TestAssertTest::testAssertDoubleNotEquals() +TestAssertTest::testAssertDoubleNotEquals1() { - checkDoubleNotEquals( 1.1, 1.2, 0.09 ); - checkDoubleNotEquals( 1.2, 1.1, 0.09 ); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.09 ); } void -TestAssertTest::checkDoubleNotEquals( double expected, - double actual, - double delta ) +TestAssertTest::testAssertDoubleNotEquals2() { - bool exceptionCatched = false; - try - { - CPPUNIT_ASSERT_DOUBLES_EQUAL( expected, actual, delta ); - } - catch( CppUnit::Exception & ) - { - exceptionCatched = true; // ok, we were expecting an exception. - } - - CPPUNIT_ASSERT( exceptionCatched ); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.09 ); } @@ -140,15 +119,35 @@ TestAssertTest::testAssertLongEquals() void TestAssertTest::testAssertLongNotEquals() { - bool exceptionCatched = false; + CPPUNIT_ASSERT_EQUAL( 1, 2 ); +} + + +void +TestAssertTest::testFail() +{ + bool exceptionCaught = false; + std::string failure( "FailureMessage" ); try { - CPPUNIT_ASSERT_EQUAL( 1, 2 ); + CPPUNIT_FAIL( failure ); } - catch( CppUnit::Exception & ) + catch( CppUnit::Exception &e ) { - exceptionCatched = true; // ok, we were expecting an exception. + exceptionCaught = true; + checkMessageContains( &e, failure ); } + CPPUNIT_ASSERT( exceptionCaught ); +} + - CPPUNIT_ASSERT( exceptionCatched ); +void +TestAssertTest::checkMessageContains( CppUnit::Exception *e, + std::string expected ) +{ + std::string actual = e->what(); + CPPUNIT_ASSERT_MESSAGE( "Expected message not found: " + expected + + ", was: " + actual, + std::search( actual.begin(), actual.end(), + expected.begin(), expected.end() ) != actual.end() ); } |