void
MoneyTest::testConstructor()
{
// Set up
const std::string currencyFF( "FF" );
const double longNumber = 1234.5678;
// Process
Money money( longNumber, currencyFF );
// Check
CPPUNIT_ASSERT_EQUAL( longNumber, money.getAmount() );
CPPUNIT_ASSERT_EQUAL( currencyFF, money.getCurrency() );
}
void
MoneyTest::testEqual()
{
// Set up
const Money money123FF( 123, "FF" );
const Money money123USD( 123, "USD" );
const Money money12FF( 12, "FF" );
const Money money12USD( 12, "USD" );
// Process & Check
CPPUNIT_ASSERT( money123FF == money123FF ); // ==
CPPUNIT_ASSERT( money12FF != money123FF ); // != amount
CPPUNIT_ASSERT( money123USD != money123FF ); // != currency
CPPUNIT_ASSERT( money12USD != money123FF ); // != currency and != amount
}
void
MoneyTest::testAdd()
{
// Set up
const Money money12FF( 12, "FF" );
const Money expectedMoney( 135, "FF" );
// Process
Money money( 123, "FF" );
money += money12FF;
// Check
CPPUNIT_ASSERT_EQUAL( expectedMoney, money ); // add works
CPPUNIT_ASSERT( &money == &(money += money12FF) ); // add returns ref. on 'this'.
}
这里我们常常用到的还有
CPPUNIT_TEST_SUITE_REGISTRATION这个是注册MoneyTest到测试框架中,这个时必须的。
CPPUNIT_ASSERT_EQUAL(期望值,实际值)具有相同类型(能用==)比较,如果比较不相等会fail
CPPUNIT_ASSERT(bool量)里面的变量如果为false会fail结果
CPPUNIT_ASSERT_MESSAGE(message,condition) 当condition为false的时候fail,输出message信息到结果中
CPPUNIT_FAIL( message ) // 表示失败,message中指明了诊断信息
CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual)// 断言两个值(expected和actual)是否相等,如果不等输出message中指明了附加的诊断信息
CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) 判断两个浮点数是否精确相等,这个用来判断浮点数是否近似相等必须要用到的,我们不能用CPPUNIT_ASSERT_EQUAL(期望值,实际值)来判断了。