[Cppunit-devel] Re: Possible error in CppUnit documentation
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-07-09 14:20:26
|
Tina wrote: > Hello, > > I'm a newbie of CppUnit, and I followed your "Money,a step by step > example" in the "CppUnit Documentation Version 1.10.2". > > The steps of "Setting up your project(VC++)", "Running our tests", > "Adding the TestFixture" and "Our first tests" went well. But in > "Adding more tests", when I added the test "testAdd", there were some > problems. In the documents, you wrote: > > MoneyTest.cpp > 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.getAmount() ); // += > works CPPUNIT_ASSERT( &money == &(money += money12FF) ); // += > returns ref. on 'this'. > > } > > It failed to compile. Because the CPPUNIT_ASSERT_EQUAL must have two > parameters, like CPPUNIT_ASSERT_EQUAL(expected,actual). Then I changed > it into CPPUNIT_ASSERT( expectedMoney.getAmount() == money.getAmount() > ), and it passed. Thanks, I reported the correction in the doc. > I want to know whether I could use > CPPUNIT_ASSERT_EQUAL(expected,actual) to finish my "testAdd"? And > how? Maybe I didn't really understand the meaning of > "CPPUNIT_ASSERT_EQUAL( expectedMoney == money.getAmount() )" you > wrote in your codes, so I hope you can help me:) No, the usage of CPPUNIT_ASSERT_EQUAL in the tutorial was a "bug". The following requirement applied to a type used in CPPUNIT_ASSERT_EQUAL (assuming there is no specialization of the template CppUnit::assertion_traits<T> for the type): - it must be comparable with == - it must be serializable in a std::ostream (e.g. the function "std::ostream &operator <<( std::ostream &os, const MyType &value )" must be defined). Since Money already have operator ==, the only thing we need to provide is std::ostream support: In Money.h: #include <iostream> // at the end of the file: inline std::ostream &operator <<( std::ostream &os, const Money &value ) { return os << "Money< value =" << value.getAmount() << "; currency = " << value.getCurrency() << ">"; } It is now possible to use the CPPUNIT_ASSERT_EQUAL macro: CPPUNIT_ASSERT_EQUAL( expectedMoney, money ); // add works Baptiste. |