summaryrefslogtreecommitdiff
path: root/src/cppunit
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2001-10-05 21:27:15 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2001-10-05 21:27:15 +0000
commitfbd454f554f13680fe62b36fb7a5829f6dc0c396 (patch)
tree4ef06f5085f7e68ebc144dd78560afec3e3aadc2 /src/cppunit
parent6a1755ef0e36aeb1ac2df0a46e5dafe08a4699ee (diff)
downloadcppunit-fbd454f554f13680fe62b36fb7a5829f6dc0c396.tar.gz
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.
Diffstat (limited to 'src/cppunit')
-rw-r--r--src/cppunit/Asserter.cpp105
-rw-r--r--src/cppunit/Exception.cpp65
-rw-r--r--src/cppunit/Makefile.am4
-rw-r--r--src/cppunit/NotEqualException.cpp31
-rw-r--r--src/cppunit/SourceLine.cpp62
-rw-r--r--src/cppunit/TestAssert.cpp18
-rw-r--r--src/cppunit/TestFailure.cpp17
-rw-r--r--src/cppunit/TextTestResult.cpp60
-rw-r--r--src/cppunit/TextTestRunner.cpp34
-rw-r--r--src/cppunit/XmlTestResultOutputter.cpp25
-rw-r--r--src/cppunit/cppunit.dsp20
11 files changed, 381 insertions, 60 deletions
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 <cppunit/Asserter.h>
+#include <cppunit/NotEqualException.h>
+
+
+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
diff --git a/src/cppunit/Exception.cpp b/src/cppunit/Exception.cpp
index 0246c98..3a4efd0 100644
--- a/src/cppunit/Exception.cpp
+++ b/src/cppunit/Exception.cpp
@@ -4,28 +4,51 @@
namespace CppUnit {
+#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
+/*!
+ * \deprecated Use SourceLine::isValid() instead.
+ */
const std::string Exception::UNKNOWNFILENAME = "<unknown>";
+/*!
+ * \deprecated Use SourceLine::isValid() instead.
+ */
const long Exception::UNKNOWNLINENUMBER = -1;
+#endif
/// Construct the exception
-Exception::Exception (const Exception& other) : std::exception (other)
+Exception::Exception( const Exception &other ) :
+ std::exception( other )
{
- m_message = other.m_message;
- m_lineNumber = other.m_lineNumber;
- m_fileName = other.m_fileName;
+ m_message = other.m_message;
+ m_sourceLine = other.m_sourceLine;
}
+/*!
+ * \deprecated Use other constructor instead.
+ */
+Exception::Exception( std::string message,
+ SourceLine sourceLine ) :
+ m_message( message ),
+ m_sourceLine( sourceLine )
+{
+}
+
+
+#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
+/*!
+ * \deprecated Use other constructor instead.
+ */
Exception::Exception( std::string message,
long lineNumber,
std::string fileName ) :
m_message( message ),
- m_lineNumber( lineNumber ),
- m_fileName( fileName )
+ m_sourceLine( fileName, lineNumber )
{
}
+#endif
/// Destruct the exception
@@ -36,15 +59,14 @@ Exception::~Exception () throw()
/// Perform an assignment
Exception&
-Exception::operator=( const Exception& other )
+Exception::operator =( const Exception& other )
{
- SuperClass::operator= (other);
+ SuperClass::operator =(other);
- if (&other != this)
+ if ( &other != this )
{
- m_message = other.m_message;
- m_lineNumber = other.m_lineNumber;
- m_fileName = other.m_fileName;
+ m_message = other.m_message;
+ m_sourceLine = other.m_sourceLine;
}
return *this;
@@ -58,21 +80,32 @@ Exception::what() const throw ()
return m_message.c_str ();
}
+/// Location where the error occured
+SourceLine
+Exception::sourceLine() const
+{
+ return m_sourceLine;
+}
+
+#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
/// The line on which the error occurred
long
-Exception::lineNumber()
+Exception::lineNumber() const
{
- return m_lineNumber;
+ return m_sourceLine.isValid() ? m_sourceLine.lineNumber() :
+ UNKNOWNLINENUMBER;
}
/// The file in which the error occurred
std::string
-Exception::fileName()
+Exception::fileName() const
{
- return m_fileName;
+ return m_sourceLine.isValid() ? m_sourceLine.fileName() :
+ UNKNOWNFILENAME;
}
+#endif
Exception *
diff --git a/src/cppunit/Makefile.am b/src/cppunit/Makefile.am
index 669b449..7dc8752 100644
--- a/src/cppunit/Makefile.am
+++ b/src/cppunit/Makefile.am
@@ -1,5 +1,5 @@
#
-# $Id: Makefile.am,v 1.11 2001-10-05 08:06:28 blep Exp $
+# $Id: Makefile.am,v 1.12 2001-10-05 22:27:14 blep Exp $
#
EXTRA_DIST = cppunit.dsw cppunit.dsp
@@ -8,8 +8,10 @@ INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include
lib_LTLIBRARIES = libcppunit.la
libcppunit_la_SOURCES = \
+ Asserter.cpp \
NotEqualException.cpp \
RepeatedTest.cpp \
+ SourceLine.cpp \
TestAssert.cpp \
TestCase.cpp \
TestSuite.cpp \
diff --git a/src/cppunit/NotEqualException.cpp b/src/cppunit/NotEqualException.cpp
index 1711c0d..738961d 100644
--- a/src/cppunit/NotEqualException.cpp
+++ b/src/cppunit/NotEqualException.cpp
@@ -4,6 +4,25 @@ namespace CppUnit {
NotEqualException::NotEqualException( std::string expected,
+ std::string actual,
+ SourceLine sourceLine ,
+ std::string additionalMessage ) :
+ Exception( "Expected: " + expected +
+ ", but was: " + actual +
+ "." + additionalMessage ,
+ sourceLine),
+ m_expected( expected ),
+ m_actual( actual ),
+ m_additionalMessage( additionalMessage )
+{
+}
+
+
+#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
+/*!
+ * \deprecated Use other constructor instead.
+ */
+NotEqualException::NotEqualException( std::string expected,
std::string actual,
long lineNumber,
std::string fileName ) :
@@ -14,12 +33,14 @@ NotEqualException::NotEqualException( std::string expected,
m_actual( actual )
{
}
+#endif
NotEqualException::NotEqualException( const NotEqualException &other ) :
Exception( other ),
m_expected( other.m_expected ),
- m_actual( other.m_actual )
+ m_actual( other.m_actual ),
+ m_additionalMessage( other.m_additionalMessage )
{
}
@@ -38,6 +59,7 @@ NotEqualException::operator =( const NotEqualException &other )
{
m_expected = other.m_expected;
m_actual = other.m_actual;
+ m_additionalMessage = other.m_additionalMessage;
}
return *this;
}
@@ -79,4 +101,11 @@ NotEqualException::actualValue() const
}
+std::string
+NotEqualException::additionalMessage() const
+{
+ return m_additionalMessage;
+}
+
+
} // namespace CppUnit
diff --git a/src/cppunit/SourceLine.cpp b/src/cppunit/SourceLine.cpp
new file mode 100644
index 0000000..feb356e
--- /dev/null
+++ b/src/cppunit/SourceLine.cpp
@@ -0,0 +1,62 @@
+#include <cppunit/SourceLine.h>
+
+
+namespace CppUnit
+{
+
+SourceLine::SourceLine() :
+ m_lineNumber( -1 )
+{
+}
+
+
+SourceLine::SourceLine( const std::string &fileName,
+ int lineNumber ) :
+ m_fileName( fileName ),
+ m_lineNumber( lineNumber )
+{
+}
+
+
+SourceLine::~SourceLine()
+{
+}
+
+
+bool
+SourceLine::isValid() const
+{
+ return !m_fileName.empty();
+}
+
+
+int
+SourceLine::lineNumber() const
+{
+ return m_lineNumber;
+}
+
+
+std::string
+SourceLine::fileName() const
+{
+ return m_fileName;
+}
+
+
+bool
+SourceLine::operator ==( const SourceLine &other ) const
+{
+ return m_fileName == other.m_fileName &&
+ m_lineNumber == other.m_lineNumber;
+}
+
+
+bool
+SourceLine::operator !=( const SourceLine &other ) const
+{
+ return !( *this == other );
+}
+
+
+} // namespace CppUnit
diff --git a/src/cppunit/TestAssert.cpp b/src/cppunit/TestAssert.cpp
index 72d48d9..802d5e4 100644
--- a/src/cppunit/TestAssert.cpp
+++ b/src/cppunit/TestAssert.cpp
@@ -11,6 +11,7 @@
namespace CppUnit {
+#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
/// Check for a failed general assertion
void TestAssert::assertImplementation (bool condition,
std::string conditionExpression,
@@ -48,5 +49,22 @@ void TestAssert::assertEquals (double expected,
fileName );
}
+#else
+
+void
+TestAssert::assertDoubleEquals( double expected,
+ double actual,
+ double delta,
+ SourceLine sourceLine )
+{
+ Asserter::failNotEqualIf( fabs( expected - actual ) > delta,
+ assertion_traits<double>::toString(expected),
+ assertion_traits<double>::toString(actual),
+ sourceLine );
+}
+
+
+#endif
+
}
diff --git a/src/cppunit/TestFailure.cpp b/src/cppunit/TestFailure.cpp
index 0db1628..51cd8cd 100644
--- a/src/cppunit/TestFailure.cpp
+++ b/src/cppunit/TestFailure.cpp
@@ -36,6 +36,14 @@ TestFailure::thrownException() const
}
+/// Gets the failure location.
+SourceLine
+TestFailure::sourceLine() const
+{
+ return m_thrownException->sourceLine();
+}
+
+
/// Indicates if the failure is a failed assertion or an error.
bool
TestFailure::isError() const
@@ -43,6 +51,15 @@ TestFailure::isError() const
return m_isError;
}
+
+/// Gets the name of the failed test.
+std::string
+TestFailure::failedTestName() const
+{
+ return m_failedTest->getName();
+}
+
+
/// Returns a short description of the failure.
std::string
TestFailure::toString() const
diff --git a/src/cppunit/TextTestResult.cpp b/src/cppunit/TextTestResult.cpp
index bfd6fb9..44a8fa6 100644
--- a/src/cppunit/TextTestResult.cpp
+++ b/src/cppunit/TextTestResult.cpp
@@ -1,8 +1,9 @@
+#include <cppunit/Exception.h>
+#include <cppunit/NotEqualException.h>
+#include <cppunit/Test.h>
+#include <cppunit/TextTestResult.h>
#include <iostream>
-#include "cppunit/TextTestResult.h"
-#include "cppunit/Exception.h"
-#include "cppunit/NotEqualException.h"
-#include "cppunit/Test.h"
+
namespace CppUnit {
@@ -39,7 +40,10 @@ TextTestResult::printFailures( std::ostream &stream )
TestFailures::const_iterator itFailure = failures().begin();
int failureNumber = 1;
while ( itFailure != failures().end() )
+ {
+ stream << std::endl;
printFailure( *itFailure++, failureNumber++, stream );
+ }
}
@@ -54,7 +58,7 @@ TextTestResult::printFailure( TestFailure *failure,
stream << ' ';
printFailureType( failure, stream );
stream << ' ';
- printFailureLocation( failure->thrownException(), stream );
+ printFailureLocation( failure->sourceLine(), stream );
stream << std::endl;
printFailureDetail( failure->thrownException(), stream );
stream << std::endl;
@@ -88,11 +92,14 @@ TextTestResult::printFailureType( TestFailure *failure,
void
-TextTestResult::printFailureLocation( Exception *thrownException,
+TextTestResult::printFailureLocation( SourceLine sourceLine,
std::ostream &stream )
{
- stream << "line: " << thrownException->lineNumber()
- << ' ' << thrownException->fileName();
+ if ( !sourceLine.isValid() )
+ return;
+
+ stream << "line: " << sourceLine.lineNumber()
+ << ' ' << sourceLine.fileName();
}
@@ -105,6 +112,12 @@ TextTestResult::printFailureDetail( Exception *thrownException,
NotEqualException *e = (NotEqualException*)thrownException;
stream << "expected: " << e->expectedValue() << std::endl
<< "but was: " << e->actualValue();
+ if ( !e->additionalMessage().empty() )
+ {
+ stream << std::endl;
+ stream << "additional message:" << std::endl
+ << e->additionalMessage();
+ }
}
else
{
@@ -129,13 +142,30 @@ TextTestResult::printHeader( std::ostream &stream )
stream << std::endl << "OK (" << runTests () << " tests)"
<< std::endl;
else
- stream << std::endl
- << "!!!FAILURES!!!" << std::endl
- << "Test Results:" << std::endl
- << "Run: " << runTests()
- << " Failures: " << testFailures()
- << " Errors: " << testErrors()
- << std::endl;
+ {
+ stream << std::endl;
+ printFailureWarning( stream );
+ printStatistics( stream );
+ }
+}
+
+
+void
+TextTestResult::printFailureWarning( std::ostream &stream )
+{
+ stream << "!!!FAILURES!!!" << std::endl;
+}
+
+
+void
+TextTestResult::printStatistics( std::ostream &stream )
+{
+ stream << "Test Results:" << std::endl;
+
+ stream << "Run: " << runTests()
+ << " Failures: " << testFailures()
+ << " Errors: " << testErrors()
+ << std::endl;
}
diff --git a/src/cppunit/TextTestRunner.cpp b/src/cppunit/TextTestRunner.cpp
index aaa6c86..c00c456 100644
--- a/src/cppunit/TextTestRunner.cpp
+++ b/src/cppunit/TextTestRunner.cpp
@@ -37,34 +37,31 @@ TextTestRunner::addTest( Test *test )
* of an added test.
* \param doWait if \c true then the user must press the RETURN key
* before the run() method exit.
+ * \return \c true is the test was successful, \c false if the test
+ * failed or was not found.
*/
-
-void
+bool
TextTestRunner::run( std::string testName,
bool doWait )
{
- runTestByName( testName );
+ bool sucessful = runTestByName( testName );
wait( doWait );
+ return sucessful;
}
-void
+bool
TextTestRunner::runTestByName( std::string testName )
{
if ( testName.empty() )
- {
- runTest( m_suite );
- }
- else
- {
- Test *test = findTestByName( testName );
- if ( test != NULL )
- runTest( test );
- else
- {
- std::cout << "Test " << testName << " not found." << std::endl;
- }
- }
+ return runTest( m_suite );
+
+ Test *test = findTestByName( testName );
+ if ( test != NULL )
+ return runTest( test );
+
+ std::cout << "Test " << testName << " not found." << std::endl;
+ return false;
}
@@ -94,12 +91,13 @@ TextTestRunner::findTestByName( std::string name ) const
}
-void
+bool
TextTestRunner::runTest( Test *test )
{
TextTestResult result;
test->run( &result );
std::cout << result << std::endl;
+ return result.wasSuccessful();
}
diff --git a/src/cppunit/XmlTestResultOutputter.cpp b/src/cppunit/XmlTestResultOutputter.cpp
index 590c617..7e81c7c 100644
--- a/src/cppunit/XmlTestResultOutputter.cpp
+++ b/src/cppunit/XmlTestResultOutputter.cpp
@@ -139,8 +139,9 @@ XmlTestResultOutputter::Node::escape( std::string value ) const
std::string
XmlTestResultOutputter::Node::asString( int value )
{
- char buffer[16];
- return ::itoa( value, buffer, 10 );
+ OStringStream stream;
+ stream << value;
+ return stream.str();
}
@@ -286,18 +287,24 @@ XmlTestResultOutputter::makeFailedTestNode( Test *test,
testNode->addNode( new Node( "FailureType",
failure->isError() ? "Error" : "Assertion" ) );
- if ( thrownException->lineNumber() != Exception::UNKNOWNLINENUMBER )
- {
- Node *locationNode = new Node( "Location" );
- testNode->addNode( locationNode );
- locationNode->addNode( new Node( "File", thrownException->fileName() ) );
- locationNode->addNode( new Node( "Line", thrownException->lineNumber() ) );
- }
+ if ( failure->sourceLine().isValid() )
+ testNode->addNode( makeFailureLocationNode( failure ) );
return testNode;
}
XmlTestResultOutputter::Node *
+XmlTestResultOutputter::makeFailureLocationNode( TestFailure *failure )
+{
+ Node *locationNode = new Node( "Location" );
+ SourceLine sourceLine = failure->sourceLine();
+ locationNode->addNode( new Node( "File", sourceLine.fileName() ) );
+ locationNode->addNode( new Node( "Line", sourceLine.lineNumber() ) );
+ return locationNode;
+}
+
+
+XmlTestResultOutputter::Node *
XmlTestResultOutputter::makeSucessfulTestNode( Test *test,
int testNumber )
{
diff --git a/src/cppunit/cppunit.dsp b/src/cppunit/cppunit.dsp
index 7ff4e72..234f6cd 100644
--- a/src/cppunit/cppunit.dsp
+++ b/src/cppunit/cppunit.dsp
@@ -178,6 +178,14 @@ SOURCE=..\..\include\cppunit\extensions\TypeInfoHelper.h
# PROP Default_Filter ""
# Begin Source File
+SOURCE=.\Asserter.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\..\include\cppunit\Asserter.h
+# End Source File
+# Begin Source File
+
SOURCE=.\Exception.cpp
# End Source File
# Begin Source File
@@ -194,6 +202,14 @@ SOURCE=..\..\include\cppunit\NotEqualException.h
# End Source File
# Begin Source File
+SOURCE=.\SourceLine.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\..\include\cppunit\SourceLine.h
+# End Source File
+# Begin Source File
+
SOURCE=..\..\include\cppunit\Test.h
# End Source File
# Begin Source File
@@ -287,6 +303,10 @@ SOURCE=..\..\ChangeLog
# End Source File
# Begin Source File
+SOURCE=..\..\include\cppunit\Makefile.am
+# End Source File
+# Begin Source File
+
SOURCE=.\Makefile.am
# End Source File
# Begin Source File