Re: [Cppunit-devel] templatized assertions
Brought to you by:
blep
|
From: Steve M. R. <ste...@vi...> - 2001-05-20 16:23:19
|
On Sun, May 20, 2001 at 11:44:44AM +0200, Baptiste Lepilleur wrote:
> > static std::string toString( const T& x )
> > {
> > ostringstream ost;
> shouldn't is be std::ostringstream ost; ?
Yes, thanks.
> Could you send the patch with the new assert/trait. I'd like to give it a
> try.
Here's what I'm currently using.
Index: src/cppunit/Makefile.am
===================================================================
RCS file: /cvsroot/cppunit/cppunit/src/cppunit/Makefile.am,v
retrieving revision 1.4
diff -u -b -B -r1.4 Makefile.am
--- src/cppunit/Makefile.am 2001/05/19 17:41:03 1.4
+++ src/cppunit/Makefile.am 2001/05/20 16:10:33
@@ -8,7 +8,6 @@
lib_LTLIBRARIES = libcppunit.la
libcppunit_la_SOURCES = \
- TestAssert.cpp \
TestCase.cpp \
TestSuite.cpp \
TestResult.cpp \
Index: include/cppunit/TestAssert.h
===================================================================
RCS file: /cvsroot/cppunit/cppunit/include/cppunit/TestAssert.h,v
retrieving revision 1.2
diff -u -b -B -r1.2 TestAssert.h
--- include/cppunit/TestAssert.h 2001/05/06 16:19:31 1.2
+++ include/cppunit/TestAssert.h 2001/05/20 16:10:33
@@ -2,10 +2,29 @@
#define CPPUNIT_TESTASSERT_H
#include <string>
+#include <sstream>
#include <cppunit/Exception.h>
+
namespace CppUnit {
+ template <class T>
+ struct assertion_traits
+ {
+ static bool equal( const T& x, const T& y )
+ {
+ return x == y;
+ }
+
+ static std::string toString( const T& x )
+ {
+ std::ostringstream ost;
+ ost << x;
+ return ost.str();
+ }
+ };
+
+
/*! \brief This class represents
*/
class TestAssert
@@ -17,24 +36,48 @@
bool condition,
std::string conditionExpression = "",
long lineNumber = Exception::UNKNOWNLINENUMBER,
- std::string fileName = Exception::UNKNOWNFILENAME);
+ std::string fileName = Exception::UNKNOWNFILENAME)
+ {
+ if (!condition)
+ throw Exception (conditionExpression,
+ lineNumber,
+ fileName);
+ }
+
- static void assertEquals (long expected,
- long actual,
+ template <class T>
+ static std::string notEqualsMessage (const T& expected,
+ const T& actual)
+ {
+ return "expected: " + assertion_traits<T>::toString(expected)
+ + " but was: " + assertion_traits<T>::toString(actual);
+ }
+
+
+ template <class T>
+ static void assertEquals (
+ const T& expected,
+ const T& actual,
long lineNumber = Exception::UNKNOWNLINENUMBER,
- std::string fileName = Exception::UNKNOWNFILENAME);
+ std::string fileName = Exception::UNKNOWNFILENAME)
+ {
+ assertImplementation( assertion_traits<T>::equal(expected,actual),
+ notEqualsMessage(expected, actual),
+ lineNumber,
+ fileName);
+ }
static void assertEquals (double expected,
double actual,
double delta,
long lineNumber = Exception::UNKNOWNLINENUMBER,
- std::string fileName = Exception::UNKNOWNFILENAME);
-
- static std::string notEqualsMessage (long expected,
- long actual);
-
- static std::string notEqualsMessage (double expected,
- double actual);
+ std::string fileName = Exception::UNKNOWNFILENAME)
+ {
+ assertImplementation( fabs(expected - actual) <= delta,
+ notEqualsMessage(expected, actual),
+ lineNumber,
+ fileName);
+ }
};
@@ -69,6 +112,15 @@
/// Macro for primitive value comparisons
#define assertLongsEqual(expected,actual)\
+(CppUnit::TestAssert::assertEquals ((expected),\
+ (actual),__LINE__,__FILE__))
+
+/// Generalized macro for primitive value comparisons
+/** Any type that implements operator== and operator<<
+ * can be compared. A diagnostic is printed if the
+ * actual and expected values disagree.
+ */
+#define assertEqual(expected,actual)\
(CppUnit::TestAssert::assertEquals ((expected),\
(actual),__LINE__,__FILE__))
--
by Rocket to the Moon,
by Airplane to the Rocket,
by Taxi to the Airport,
by Frontdoor to the Taxi,
by throwing back the blanket and laying down the legs ...
- They Might Be Giants
|