[Cppunit-devel] Assertion, ASSERT_EQUAL & co...
Brought to you by:
blep
From: Baptiste L. <gai...@fr...> - 2001-10-05 12:30:47
|
I've been thinking about all our discussion about ASSERT_EQUAL and others assertion. I've come to the following conclusion: - CppUnit can not provide an ASSERT_EQUAL that satify everybody - CppUnit users need to create custome assertion (checking that two XML strings are identical for example ;-) ) So, the path I wish to take is: 1) propose a reasonable default ASSERT_EQUAL (to discuss later) 2) provide all the tools for user to easily implement a new assert macro I won't discuss (1) now since it is long winded discussed, with little value at the end. I'll tackle (2) right now which offer the most value for us. Here is the list of what a ASSERT macro typically do: a) capture filename and line where the macro was expanded for easy localisation of failure b) throw an Exception if failure occurs. The Exception convey a message describing the failure, and the location of the failure captured in (a). That means CppUnit should help the user with both step. At the current time, it means relying on TestAssert::assertImplemention and a lot of passing filename and line around as parameter. First I apply IntroduceParameterObject and create a SourceLine class that contains the filename and the line, and a nice default constructor when those are not available. Refactor all CppUnit to use SourceLine (Exception, TestAssert) Introduce a new macro: #define CPPUNIT_SOURCELINE() CppUnit::SourceLine( __FILE__, __LINE__ ) Introduce helper methods in TestAssert to factor out code that throw exception for failure. (That last one is not well refined yet, but it would be something like: void fail( std::string message, SourceLine location = SourceLine() ); void failIf( bool shouldFail, std::string message, SourceLine location = SourceLine() ); void failNotEqual( std::string expected, std::string actual, SourceLine location = SourceLine(), std::string additionalMessage ="" ); // additionalMessage is a message that is append after the // 'expect ... but was ...'. I noticed that it is really helpful // to have a precise report indicating why both are not equals. See // the CPPUNITTEST_ASSERT_XML macro used to compare two XML string // for example. void failNotEqual( bool shouldFail, std::string expected, std::string actual, SourceLine location = SourceLine(), std::string additionalMessage ="" ); Note that some of those function probably exist in TestAssert). The next step would be to move assertion code outside of TestAssert (assertEquals...). TestAssert would provide service to report failed assertion, but would not implement those assertions (and therefore making no choice about how those assertion are implemented). This would leave the user with a very simple way to write new assertion MACRO. For example: MYASSSERT_XML_EQUAL( expected, actual ) \ checkXmlEqual( expected, actual, CPPUNIT_SOURCELINE() ); checkXmlEqual( std::string expected, std::string actual, SourceLine location ) { if ( expected != actual ) // Whatever you want there CppUnit::TestAssert( fail, expected, actual, location ); } I'll try to tackle the first part this evening (applying the IntroduceParameteObject refactoring). Does the change to TestAssert sound good for you ? Baptiste. --- Baptiste Lepilleur <gai...@fr...> http://gaiacrtn.free.fr/index.html Language: English, French |