From 224cf85f2b7fd7ec47cda4788902996349f8c754 Mon Sep 17 00:00:00 2001 From: Baptiste Lepilleur Date: Sat, 13 Mar 2004 11:52:57 +0000 Subject: Examples/cppunittest/TestAssertTest. examples/cppunittest/TestAssertTest.h: * examples/cppunittest/TestAssertTest.cpp: * examples/cppunittest/XmlUniformiserTest.h: * examples/cppunittest/XmlUniformiserTest.cpp: * include/cppunit/TestAssert.h: add the exception assertion macros from cppunit 2: CPPUNIT_ASSERT_THROW, CPPUNIT_ASSERT_NO_THROW, CPPUNIT_ASSERT_ASSERTION_FAIL, CPPUNIT_ASSERT_ASSERTION_PASS. Updated unit test to use and test the new macros. * include/cppunit/extensions/HelperMacros.h: deprecated the test case factory that check for exception (CPPUNIT_TEST_FAIL & CPPUNIT_TEST_EXCEPTION). --- examples/cppunittest/TestAssertTest.cpp | 117 ++++++++++++++++++++++---------- 1 file changed, 81 insertions(+), 36 deletions(-) (limited to 'examples/cppunittest/TestAssertTest.cpp') diff --git a/examples/cppunittest/TestAssertTest.cpp b/examples/cppunittest/TestAssertTest.cpp index e3b57ac..9481184 100644 --- a/examples/cppunittest/TestAssertTest.cpp +++ b/examples/cppunittest/TestAssertTest.cpp @@ -39,16 +39,82 @@ TestAssertTest::tearDown() void -TestAssertTest::testAssertTrue() +TestAssertTest::testAssertThrow() { - CPPUNIT_ASSERT( true ); + CPPUNIT_ASSERT_THROW( throw std::exception( "dummy" ), std::exception ); + + try + { + CPPUNIT_ASSERT_THROW( 1234, std::exception ); + } + catch ( CPPUNIT_NS::Exception & ) + { + return; + } + + throw std::exception( "CPPUT_ASSERT_THROW( 1234, std::exception ) did not fail." ); +} + + +void +TestAssertTest::testAssertNoThrow() +{ + CPPUNIT_ASSERT_NO_THROW( 1234 ); + + try + { + CPPUNIT_ASSERT_NO_THROW( throw std::exception( "dummy" ) ); + } + catch ( CPPUNIT_NS::Exception & ) + { + return; + } + throw std::exception( "CPPUT_ASSERT_NO_THROW( throw std::exception( \"dummy\" ) ) did not fail." ); +} + + +void +TestAssertTest::testAssertAssertionFail() +{ + CPPUNIT_ASSERT_ASSERTION_FAIL( throw CPPUNIT_NS::Exception() ); + + try + { + CPPUNIT_ASSERT_ASSERTION_FAIL( 1234 ); + } + catch ( CPPUNIT_NS::Exception & ) + { + return; + } + + throw std::exception( "CPPUNIT_ASSERT_ASSERTION_FAIL( 1234 ) did not fail." ); +} + + +void +TestAssertTest::testAssertAssertionPass() +{ + CPPUNIT_ASSERT_ASSERTION_PASS( 1234 ); + + try + { + CPPUNIT_ASSERT_ASSERTION_PASS( throw CPPUNIT_NS::Exception() ); + } + catch ( CPPUNIT_NS::Exception & ) + { + return; + } + + throw std::exception( "CPPUNIT_ASSERT_ASSERTION_PASS did not fail." ); } void -TestAssertTest::testAssertFalse() +TestAssertTest::testAssert() { - CPPUNIT_ASSERT( false ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( true ) ); + + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( false ) ); } @@ -58,15 +124,19 @@ static int foo() { return 1; } void TestAssertTest::testAssertEqual() { - CPPUNIT_ASSERT_EQUAL( 1, 1 ); - CPPUNIT_ASSERT_EQUAL( 1, foo() ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, 1 ) ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, foo() ) ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 12345678, 12345678 ) ); + + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_EQUAL( 1, 2 ) ); } void TestAssertTest::testAssertMessageTrue() { - CPPUNIT_ASSERT_MESSAGE( "This test should not failed", true ); + CPPUNIT_ASSERT_ASSERTION_PASS( + CPPUNIT_ASSERT_MESSAGE( "This test should not failed", true ) ); } @@ -92,36 +162,11 @@ TestAssertTest::testAssertMessageFalse() void TestAssertTest::testAssertDoubleEquals() { - CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.101 ); - CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.101 ); -} + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.101 ) ); + CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.101 ) ); - -void -TestAssertTest::testAssertDoubleNotEquals1() -{ - CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.09 ); -} - - -void -TestAssertTest::testAssertDoubleNotEquals2() -{ - CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.09 ); -} - - -void -TestAssertTest::testAssertLongEquals() -{ - CPPUNIT_ASSERT_EQUAL( 12345678, 12345678 ); -} - - -void -TestAssertTest::testAssertLongNotEquals() -{ - CPPUNIT_ASSERT_EQUAL( 1, 2 ); + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.09 ) ); + CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.09 ) ); } -- cgit v1.2.1