[Cppunit-devel] templatized assertions
Brought to you by:
blep
|
From: Steve M. R. <ste...@vi...> - 2001-05-15 20:41:25
|
Hello,
Using 1.5.5, I can see two flavours of assertions:
assert( boolean )
and assertDoublesEqual( double, double ) [ also assertLongsEqual() ]
The second form is potentially very useful, since it allows the
diagnostic message to say "expected X, got Y". I'd like to extend
this form of assertion to arbitrary types.
To this end, I've added templatized assertEquals() and
notEqualsMessage() member functions to class TestAssert, and a new
assertion macro "assertEqual(x,y)". The assertion will work on any
type that has operator=() and operator<<().
The patch below does work. Before I go further down this road and
submit a full patch with doc changes, and the whole 9 yards, I thought
I'd check to see whether you folks see a problem with this approach.
Perhaps this approach has been previously considered and discarded?
Comments?
-Steve
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/15 20:20:50
@@ -2,6 +2,7 @@
#define CPPUNIT_TESTASSERT_H
#include <string>
+#include <sstream>
#include <cppunit/Exception.h>
namespace CppUnit {
@@ -19,22 +20,34 @@
long lineNumber = Exception::UNKNOWNLINENUMBER,
std::string fileName = Exception::UNKNOWNFILENAME);
- static void assertEquals (long expected,
- long 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)
+ {
+ if (expected != actual)
+ assertImplementation (false, 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);
+ template <class T>
+ static std::string notEqualsMessage (const T& expected,
+ const T& actual)
+ {
+ ostringstream ost;
+ ost << "expected: " << expected
+ << " but was: " << actual;
+ return ost.str();
+ }
- static std::string notEqualsMessage (double expected,
- double actual);
};
@@ -69,6 +82,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
|