diff options
| author | Steve M. Robbins <smr@sumost.ca> | 2001-06-02 21:29:52 +0000 |
|---|---|---|
| committer | Steve M. Robbins <smr@sumost.ca> | 2001-06-02 21:29:52 +0000 |
| commit | cdbca4119defbc5f9698906633eec05b5dc8272a (patch) | |
| tree | 6f0fc91b8cb7cc7b361966ffc8ab2e401e1e4c6e /include/cppunit | |
| parent | 99f54c0f4b53debc49f2081ce01158b2ed200c30 (diff) | |
| download | cppunit-cdbca4119defbc5f9698906633eec05b5dc8272a.tar.gz | |
Change to templatized TestAssert::assertEquals() and the new CPPUNIT_ASSERT* macros
Diffstat (limited to 'include/cppunit')
| -rw-r--r-- | include/cppunit/Makefile.am | 1 | ||||
| -rw-r--r-- | include/cppunit/TestAssert.h | 109 | ||||
| -rw-r--r-- | include/cppunit/TestCase.h | 4 | ||||
| -rw-r--r-- | include/cppunit/config.h.in | 21 | ||||
| -rw-r--r-- | include/cppunit/extensions/HelperMacros.h | 13 | ||||
| -rw-r--r-- | include/cppunit/extensions/Orthodox.h | 10 | ||||
| -rw-r--r-- | include/cppunit/extensions/TestSuiteBuilder.h | 9 | ||||
| -rw-r--r-- | include/cppunit/extensions/TypeInfoHelper.h | 4 |
8 files changed, 126 insertions, 45 deletions
diff --git a/include/cppunit/Makefile.am b/include/cppunit/Makefile.am index a5f31ef..57143bf 100644 --- a/include/cppunit/Makefile.am +++ b/include/cppunit/Makefile.am @@ -2,6 +2,7 @@ SUBDIRS = extensions libcppunitincludedir = $(includedir)/cppunit libcppunitinclude_HEADERS = \ + config.h \ Exception.h \ Test.h \ TestAssert.h \ diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h index 88ee195..9127780 100644 --- a/include/cppunit/TestAssert.h +++ b/include/cppunit/TestAssert.h @@ -2,10 +2,30 @@ #define CPPUNIT_TESTASSERT_H #include <string> +#include <sstream> +#include <cppunit/config.h> #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 +37,48 @@ namespace CppUnit { bool condition, std::string conditionExpression = "", long lineNumber = Exception::UNKNOWNLINENUMBER, - std::string fileName = Exception::UNKNOWNFILENAME); - - static void assertEquals (long expected, - long actual, + std::string fileName = Exception::UNKNOWNFILENAME) + { + if (!condition) + throw Exception (conditionExpression, + lineNumber, + fileName); + } + + + 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); + } }; @@ -43,34 +87,47 @@ namespace CppUnit { * Just goes to show that preprocessors do have some * redeeming qualities. */ -#define CPPUNIT_SOURCEANNOTATION - -#ifdef CPPUNIT_SOURCEANNOTATION +#if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION - #undef assert - #define assert(condition)\ +# define CPPUNIT_ASSERT(condition)\ (CppUnit::TestAssert::assertImplementation ((condition),(#condition),\ __LINE__, __FILE__)) #else - #undef assert - #define assert(condition)\ +# define CPPUNIT_ASSERT(condition)\ (CppUnit::TestAssert::assertImplementation ((condition),"",\ __LINE__, __FILE__)) #endif -/// Macro for primitive value comparisons -#define assertDoublesEqual(expected,actual,delta)\ -(CppUnit::TestAssert::assertEquals ((expected),\ - (actual),(delta),__LINE__,__FILE__)) +/// Generalized macro for primitive value comparisons +/** Equality and string representation can be defined with + * an appropriate assertion_traits class. + * A diagnostic is printed if actual and expected values disagree. + */ +#define CPPUNIT_ASSERT_EQUAL(expected,actual)\ + (CppUnit::TestAssert::assertEquals ((expected),\ + (actual),__LINE__,__FILE__)) /// Macro for primitive value comparisons -#define assertLongsEqual(expected,actual)\ -(CppUnit::TestAssert::assertEquals ((expected),\ - (actual),__LINE__,__FILE__)) +#define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)\ + (CppUnit::TestAssert::assertEquals ((expected),\ + (actual),(delta),__LINE__,__FILE__)) + + +// Backwards compatibility + +#if CPPUNIT_ENABLE_NAKED_ASSERT + +#undef assert +#define assert(c) CPPUNIT_ASSERT(c) +#define assertEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a) +#define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d) +#define assertLongsEqual(e,a) CPPUNIT_ASSERT_EQUAL(e,a) + +#endif } // namespace CppUnit diff --git a/include/cppunit/TestCase.h b/include/cppunit/TestCase.h index ebbd135..e27ba31 100644 --- a/include/cppunit/TestCase.h +++ b/include/cppunit/TestCase.h @@ -41,12 +41,12 @@ namespace CppUnit { * * For each test implement a method which interacts * with the fixture. Verify the expected results with assertions specified - * by calling assert on the expression you want to test: + * by calling CPPUNIT_ASSERT on the expression you want to test: * * \code * protected: void testAdd () { * int result = value1 + value2; - * assert (result == 5); + * CPPUNIT_ASSERT (result == 5); * } * \endcode * diff --git a/include/cppunit/config.h.in b/include/cppunit/config.h.in new file mode 100644 index 0000000..9d697d0 --- /dev/null +++ b/include/cppunit/config.h.in @@ -0,0 +1,21 @@ +#ifndef CPPUNIT_CONFIG_H +#define CPPUNIT_CONFIG_H + + +/* Define to 1 if the compiler supports Run-Time Type Identification */ +#define CPPUNIT_USE_TYPEINFO @CPPUNIT_USE_TYPEINFO@ + +/* Define to 1 if you wish to have the old-style macros + assert(), assertEqual(), assertDoublesEqual(), and assertLongsEqual() */ +#define CPPUNIT_ENABLE_NAKED_ASSERT 0 + +/* Define to 1 if the preprocessor expands (#foo) to "foo" (quotes incl.) */ +#define CPPUNIT_HAVE_CPP_SOURCEANNOTATION 1 + +/** FIXME: determine using autoconf. **/ +/* Define to 1 if std::string::compare(string,pos,len) is defined, + rather than std::string::compare(pos,len,string) */ +#define CPPUNIT_STRING_COMPARE_STRING_FIRST 1 + + +#endif diff --git a/include/cppunit/extensions/HelperMacros.h b/include/cppunit/extensions/HelperMacros.h index 12defe8..17784ea 100644 --- a/include/cppunit/extensions/HelperMacros.h +++ b/include/cppunit/extensions/HelperMacros.h @@ -7,6 +7,7 @@ #define CPPUNIT_EXTENSIONS_HELPERMACROS_H #include <string> +#include <cppunit/config.h> #include <cppunit/extensions/AutoRegisterSuite.h> #include <cppunit/extensions/TestSuiteBuilder.h> @@ -17,13 +18,11 @@ // defined, otherwise it is extracted from the macro parameter // // This macro is for cppunit internal and should not be use otherwise. -#ifdef CPPUNIT_USE_TYPEINFO -#define __CPPUNIT_SUITE_CTOR_ARGS( ATestCaseType ) - -#else // CPPUNIT_USE_TYPEINFO -#define __CPPUNIT_SUITE_CTOR_ARGS( ATestCaseType ) (std::string(#ATestCaseType)) - -#endif // CPPUNIT_USE_TYPEINFO +#if CPPUNIT_USE_TYPEINFO +# define __CPPUNIT_SUITE_CTOR_ARGS( ATestCaseType ) +#else +# define __CPPUNIT_SUITE_CTOR_ARGS( ATestCaseType ) (std::string(#ATestCaseType)) +#endif /** \file diff --git a/include/cppunit/extensions/Orthodox.h b/include/cppunit/extensions/Orthodox.h index e7667d1..39506f1 100644 --- a/include/cppunit/extensions/Orthodox.h +++ b/include/cppunit/extensions/Orthodox.h @@ -58,25 +58,25 @@ template <typename ClassUnderTest> void Orthodox<ClassUnderTest>::runTest () ClassUnderTest a, b, c; // make sure we have an equality operator - assert (a == b); + CPPUNIT_ASSERT (a == b); // check the inverse b.operator= (a.operator! ()); - assert (a != b); + CPPUNIT_ASSERT (a != b); // double inversion b = !!a; - assert (a == b); + CPPUNIT_ASSERT (a == b); // invert again b = !a; // check calls c = a; - assert (c == call (a)); + CPPUNIT_ASSERT (c == call (a)); c = b; - assert (c == call (b)); + CPPUNIT_ASSERT (c == call (b)); } diff --git a/include/cppunit/extensions/TestSuiteBuilder.h b/include/cppunit/extensions/TestSuiteBuilder.h index c1c6906..8536f1c 100644 --- a/include/cppunit/extensions/TestSuiteBuilder.h +++ b/include/cppunit/extensions/TestSuiteBuilder.h @@ -2,12 +2,13 @@ #define CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H #include <memory> +#include <cppunit/config.h> #include <cppunit/TestSuite.h> #include <cppunit/TestCaller.h> -#ifdef CPPUNIT_USE_TYPEINFO -#include <cppunit/extensions/TypeInfoHelper.h> -#endif // CPPUNIT_USE_TYPEINFO +#if CPPUNIT_USE_TYPEINFO +# include <cppunit/extensions/TypeInfoHelper.h> +#endif namespace CppUnit { @@ -17,7 +18,7 @@ namespace CppUnit { public: typedef void (Fixture::*TestMethod)(); -#ifdef CPPUNIT_USE_TYPEINFO +#if CPPUNIT_USE_TYPEINFO TestSuiteBuilder() : m_suite( new TestSuite( TypeInfoHelper::getClassName( typeid(Fixture) ) ) ) diff --git a/include/cppunit/extensions/TypeInfoHelper.h b/include/cppunit/extensions/TypeInfoHelper.h index 6dd6ae8..1b5cdb7 100644 --- a/include/cppunit/extensions/TypeInfoHelper.h +++ b/include/cppunit/extensions/TypeInfoHelper.h @@ -1,7 +1,9 @@ #ifndef CPPUNIT_TYPEINFOHELPER_H #define CPPUNIT_TYPEINFOHELPER_H -#ifdef CPPUNIT_USE_TYPEINFO +#include <cppunit/config.h> + +#if CPPUNIT_USE_TYPEINFO #include <typeinfo> |
