From fbd454f554f13680fe62b36fb7a5829f6dc0c396 Mon Sep 17 00:00:00 2001 From: Baptiste Lepilleur Date: Fri, 5 Oct 2001 21:27:15 +0000 Subject: Include/cppunit/Asserter. include/cppunit/Asserter.h : * src/cppunit/Asserter.cpp : added. Helper to create assertion macros. * src/cppunit/cppunit.dsp : * src/cppunit/Makefile.am : * include/cppunit/Makefile.am : added Asserter.h and Asserter.cpp. * include/cppunit/Exception.h : * src/cppunit/Exception.cpp : added constructor that take a SourceLine argument. Deprecated static constant and old constructor. Fixed some constness issues. * examples/cppunittest/ExceptionTest.cpp : Refactored. * NEWS : partially updated (need to be more detailed) * include/cppunit/NotEqualException.h : * src/cppunit/NotEqualException.cpp : added constructor that take a SourceLine argument. Deprecated old constructor. Added a third element to compose message. * examples/cppunittest/NotEqualExceptionTest.cpp : moved to "Core" suite. Added test for SourceLine() and additionalMessage(). Refactored. * include/cppunit/SourceLine.h : * src/cppunit/SourceLine.cpp : added. Result of applying IntroduceParameterObject refactoring on filename & line number... * include/cppunit/TestAssert.h : * src/cppunit/TestAssert.cpp : deprecated old assert functions. added functions assertEquals() and assertDoubleEquals() which use SourceLine. * src/cppunit/TestCase.cpp : Modified for SourceLine. * include/cppunit/TestFailure.h : * src/cppunit/TestFailure.cpp : added failedTestName(), and sourceLine(). * src/msvc6/testrunner/TestRunnerDlg.cpp : modified to use SourceLine. * include/cppunit/TextTestResult.h : * src/cppunit/TextTestResult.cpp : corrected include order and switched to angled brackets. Refactored. Don't print failure location if not available. Not equal failure dump additional message if available. * src/cppunit/TextTestRunner.cpp : run() now returns a boolean to indicate if the run was sucessful. * src/cppunit/XmlTestResultOutputter.cpp : replaced itoa() with OStringStream. Refactored. * examples/cppunittest/XmlUniformiser.h : * examples/cppunittest/XmlUniformiser.cpp : CPPUNITTEST_ASSERT_XML_EQUAL capture failure location. Refactored checkXmlEqual(). * examples/cppunittest/XmlUniformiserTest.h : * examples/cppunittest/XmlUniformiserTest.cpp : added test for CPPUNITTEST_ASSERT_XML_EQUAL. * include/cppunit/XmlTestResultOutputter.h : * src/cppunit/XmlTestResultOutputter.cpp : updated to use SourceLine. --- src/cppunit/Asserter.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/cppunit/Asserter.cpp (limited to 'src/cppunit/Asserter.cpp') diff --git a/src/cppunit/Asserter.cpp b/src/cppunit/Asserter.cpp new file mode 100644 index 0000000..b6e5015 --- /dev/null +++ b/src/cppunit/Asserter.cpp @@ -0,0 +1,105 @@ +#include +#include + + +namespace CppUnit +{ + + +/*! Asserter creates and throws failure exception. + * + * The following example show how to create an assertion that + * checks if two XML strings are equivalent (extract from + * XmlUniformiser.cpp of CppUnit test suite): + * + * \code + * // Asserts that two XML strings are equivalent. + * #define CPPUNITTEST_ASSERT_XML_EQUAL( expected, actual ) \ + * ::CppUnitTest::checkXmlEqual( expected, actual, \ + * CPPUNIT_SOURCELINE() ) + * void + * checkXmlEqual( std::string expectedXml, + * std::string actualXml, + * CppUnit::SourceLine sourceLine ) + * { + * std::string expected = XmlUniformiser( expectedXml ).stripped(); + * std::string actual = XmlUniformiser( actualXml ).stripped(); + * + * if ( expected == actual ) + * return; + * + * int index = notEqualIndex( expected, actual ); + * CppUnit::OStringStream message; + * message << "differ at index: " << index << "\n" + * << "expected: " << expected.substr(index) << "\n" + * << "but was : " << actual.substr( index ); + * ::CppUnit::Asserter::failNotEqual( expected, + * actual, + * sourceLine, + * message.str() ); + * } + * + * + * int + * notEqualIndex( std::string expectedXml, + * std::string actualXml ) + * { + * int index = 0; + * while ( index < actualXml.length() && + * index < expectedXml.length() && + * actualXml[index] == expectedXml[index] ) + * ++index; + * + * return index; + * } + * \endcode + */ +namespace Asserter +{ + + +void +fail( std::string message, + SourceLine sourceLine ) +{ + throw Exception( message, sourceLine ); +} + + +void +failIf( bool shouldFail, + std::string message, + SourceLine location ) +{ + if ( shouldFail ) + fail( message, location ); +} + + +void +failNotEqual( std::string expected, + std::string actual, + SourceLine sourceLine, + std::string additionalMessage ) +{ + throw NotEqualException( expected, + actual, + sourceLine, + additionalMessage ); +} + + +void +failNotEqualIf( bool shouldFail, + std::string expected, + std::string actual, + SourceLine sourceLine, + std::string additionalMessage ) +{ + if ( shouldFail ) + failNotEqual( expected, actual, sourceLine, additionalMessage ); +} + + +} // namespace Asserter +} // namespace CppUnit -- cgit v1.2.1