Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/cxxtools/include/cxxtools/unit/assertion.h

00001 /*
00002  * Copyright (C) 2005-2008 by Dr. Marc Boris Duerner
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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 } // namespace unit
00110 
00111 } // namespace cxxtools
00112 
00113 #endif
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6