Assertion.h
1 /*
2  * Copyright (C) 2005-2008 by Dr. Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef PT_UNIT_ASSERTION_H
29 #define PT_UNIT_ASSERTION_H
30 
31 #include <Pt/Unit/Api.h>
32 #include "Pt/SourceInfo.h"
33 #include <stdexcept>
34 #include <iostream>
35 
36 namespace Pt {
37 
38 namespace Unit {
39 
60  class PT_UNIT_API Assertion
61  {
62  public:
73  Assertion(const std::string& what, const SourceInfo& si);
74 
77  const Pt::SourceInfo& sourceInfo() const;
78 
79  const char* what() const { return _what.c_str(); }
80 
81  private:
82  Pt::SourceInfo _sourceInfo;
83  std::string _what;
84  };
85 
86  namespace { bool testCond = false; }
87 
88  inline bool getFalse()
89  { return testCond; }
90 
91  #define PT_UNIT_ASSERT(cond) \
92  do { \
93  if( !(cond) ) \
94  throw Pt::Unit::Assertion(#cond, PT_SOURCEINFO); \
95  } while (::Pt::Unit::testCond)
96 
97  #define PT_UNIT_ASSERT_MSG(cond, what) \
98  do { \
99  if( !(cond) ) \
100  { \
101  std::ostringstream _pt_msg; \
102  _pt_msg << what; \
103  throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
104  } \
105  } while (::Pt::Unit::testCond)
106 
107  // TODO: deprecated
108  #define PT_UNIT_ASSERT_EQUALS(value1, value2) \
109  do { \
110  if( ! ((value1) == (value2)) ) \
111  { \
112  std::ostringstream _pt_msg; \
113  _pt_msg << "not equal: value1 (" #value1 ")=<" << value1 << "> value2 (" #value2 ")=<" << value2 << '>'; \
114  throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
115  } \
116  } while (::Pt::Unit::testCond)
117 
118  #define PT_UNIT_ASSERT_EQUAL(value1, value2) \
119  do { \
120  if( ! ((value1) == (value2)) ) \
121  { \
122  std::ostringstream _pt_msg; \
123  _pt_msg << "not equal: (" #value1 ")=<" << value1 << ">, (" #value2 ")=<" << value2 << '>'; \
124  throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
125  } \
126  } while (::Pt::Unit::testCond)
127 
128  #define PT_UNIT_ASSERT_THROW(cond, EX) \
129  do { \
130  struct _pt_ex { }; \
131  try \
132  { \
133  cond; \
134  throw _pt_ex(); \
135  } \
136  catch(const _pt_ex &) \
137  { \
138  std::ostringstream _pt_msg; \
139  _pt_msg << "exception of type " #EX " expected in " #cond; \
140  throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
141  } \
142  catch(const EX &) \
143  {} \
144  } while (::Pt::Unit::testCond)
145 
146  #define PT_UNIT_ASSERT_NOTHROW(cond) \
147  do { \
148  try { \
149  \
150  cond; \
151  } \
152  catch(const std::exception& e) \
153  { \
154  throw Pt::Unit::Assertion( \
155  std::string("unexpected exception of type ") + typeid(e).name() + ": " + e.what(), \
156  PT_SOURCEINFO); \
157  } \
158  catch(...) \
159  { \
160  throw Pt::Unit::Assertion("unexpected exception." , PT_SOURCEINFO); \
161  } \
162  } while (::Pt::Unit::testCond)
163 
164  #define PT_UNIT_FAIL(what) \
165  do { \
166  std::ostringstream _pt_msg; \
167  _pt_msg << what; \
168  throw Pt::Unit::Assertion(_pt_msg.str(), PT_SOURCEINFO); \
169  } while (::Pt::Unit::testCond)
170 
171 } // namespace Unit
172 
173 } // namespace Pt
174 
175 #endif // PTV_UNIT_ASSERTION_H
Test Assertion exception
Definition: Assertion.h:60
Source code info class.
Definition: SourceInfo.h:100