2
#include "TestAssertTest.h"
7
- tests need to be added to test asserEquals() template function and
8
use of assertion traits. Some check may need to be added to check
9
the message content in Exception.
10
- code need to be refactored with the use of a test caller that expect
15
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TestAssertTest,
19
TestAssertTest::TestAssertTest()
24
TestAssertTest::~TestAssertTest()
30
TestAssertTest::setUp()
36
TestAssertTest::tearDown()
42
TestAssertTest::testAssertThrow()
44
CPPUNIT_ASSERT_THROW( throw std::exception(), std::exception );
48
CPPUNIT_ASSERT_THROW( 1234, std::exception );
50
catch ( CPPUNIT_NS::Exception & )
55
throw std::exception();
60
TestAssertTest::testAssertNoThrow()
62
CPPUNIT_ASSERT_NO_THROW( 1234 );
66
CPPUNIT_ASSERT_NO_THROW( throw std::exception() );
68
catch ( CPPUNIT_NS::Exception & )
72
throw std::exception();
77
TestAssertTest::testAssertAssertionFail()
79
CPPUNIT_ASSERT_ASSERTION_FAIL( throw CPPUNIT_NS::Exception() );
83
CPPUNIT_ASSERT_ASSERTION_FAIL( 1234 );
85
catch ( CPPUNIT_NS::Exception & )
90
throw std::exception();
95
TestAssertTest::testAssertAssertionPass()
97
CPPUNIT_ASSERT_ASSERTION_PASS( 1234 );
101
CPPUNIT_ASSERT_ASSERTION_PASS( throw CPPUNIT_NS::Exception() );
103
catch ( CPPUNIT_NS::Exception & )
108
throw std::exception();
113
TestAssertTest::testAssert()
115
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( true ) );
117
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( false ) );
121
static int foo() { return 1; }
125
TestAssertTest::testAssertEqual()
127
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, 1 ) );
128
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, foo() ) );
129
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 12345678, 12345678 ) );
131
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_EQUAL( 1, 2 ) );
136
TestAssertTest::testAssertMessageTrue()
138
CPPUNIT_ASSERT_ASSERTION_PASS(
139
CPPUNIT_ASSERT_MESSAGE( "This test should not failed", true ) );
144
TestAssertTest::testAssertMessageFalse()
146
bool exceptionCaught = false;
147
std::string message( "This test message should not be seen" );
150
CPPUNIT_ASSERT_MESSAGE( message, 2==3 );
152
catch( CPPUNIT_NS::Exception &e )
154
exceptionCaught = true; // ok, we were expecting an exception.
155
checkMessageContains( &e, message );
158
CPPUNIT_ASSERT( exceptionCaught );
163
TestAssertTest::testAssertDoubleEquals()
165
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.101 ) );
166
CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.101 ) );
168
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.09 ) );
169
CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.09 ) );
174
TestAssertTest::testFail()
176
bool exceptionCaught = false;
177
std::string failure( "FailureMessage" );
180
CPPUNIT_FAIL( failure );
182
catch( CPPUNIT_NS::Exception &e )
184
exceptionCaught = true;
185
checkMessageContains( &e, failure );
187
CPPUNIT_ASSERT( exceptionCaught );
192
TestAssertTest::checkMessageContains( CPPUNIT_NS::Exception *e,
193
std::string expected )
195
std::string actual = e->what();
196
CPPUNIT_ASSERT_MESSAGE( "Expected message not found: " + expected +
198
std::search( actual.begin(), actual.end(),
199
expected.begin(), expected.end() ) != actual.end() );