Re: [Cppunit-devel] templatized assertions
Brought to you by:
blep
|
From: Baptiste L. <bl...@cl...> - 2001-05-16 19:12:15
|
I would use a trait to add the object to the stream. The trait generic
implementation would use '<<', but could be specialized when needed.
Also, since it is a template member, if should be protected with a define.
VC++ 5.0 (and many other compiler I guess) does not support template member
for example (though it a static member so I'm not sure).
In JUnit, there is also a assertion of the form
assert( bool, message )...
----- Original Message -----
From: "Steve M. Robbins" <ste...@vi...>
To: <cpp...@li...>
Sent: Tuesday, May 15, 2001 10:43 PM
Subject: [Cppunit-devel] templatized assertions
> 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
>
>
> _______________________________________________
> Cppunit-devel mailing list
> Cpp...@li...
> http://lists.sourceforge.net/lists/listinfo/cppunit-devel
>
|