00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CXXTOOLS_ASSERTION_H
00029 #define CXXTOOLS_ASSERTION_H
00030
00031 #include "cxxtools/sourceinfo.h"
00032 #include <stdexcept>
00033 #include <iostream>
00034 #include <sstream>
00035
00036 namespace cxxtools {
00037
00038 namespace unit {
00039
00059 class Assertion
00060 {
00061 public:
00072 Assertion(const std::string& what, const SourceInfo& si);
00073
00074 const SourceInfo& sourceInfo() const;
00075
00076 const char* what() const { return _what.c_str(); }
00077
00078 private:
00079 SourceInfo _sourceInfo;
00080 std::string _what;
00081 };
00082
00083 #define CXXTOOLS_UNIT_ASSERT(cond) \
00084 if( !(cond) ) throw cxxtools::unit::Assertion(#cond, CXXTOOLS_SOURCEINFO);
00085
00086 #define CXXTOOLS_UNIT_ASSERT_MSG(cond, what) \
00087 if( !(cond) ) throw cxxtools::unit::Assertion((what), CXXTOOLS_SOURCEINFO);
00088
00089 #define CXXTOOLS_UNIT_ASSERT_EQUALS(value1, value2) \
00090 if( ! ((value1) == (value2)) ) \
00091 { \
00092 std::ostringstream _cxxtools_msg; \
00093 _cxxtools_msg << "not equal: value1 (" #value1 ")=<" << value1 << "> value2 (" #value2 ")=<" << value2 << '>'; \
00094 throw cxxtools::unit::Assertion(_cxxtools_msg.str(), CXXTOOLS_SOURCEINFO); \
00095 }
00096
00097 #define CXXTOOLS_UNIT_ASSERT_THROW(cond, EX) \
00098 try { \
00099 cond; \
00100 throw std::string("Exception expected."); \
00101 } \
00102 catch(const std::string & s) \
00103 { \
00104 throw cxxtools::unit::Assertion(s, CXXTOOLS_SOURCEINFO); \
00105 } \
00106 catch(const EX &) \
00107 {} \
00108
00109 }
00110
00111 }
00112
00113 #endif