AGX Dynamics  2.35.0.1
unitAssert.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXUNIT_ASSERT_H
18 #define AGXUNIT_ASSERT_H
19 
20 #include <agx/config.h>
21 #include <agx/config/AGX_USE_UNIT_TESTS.h>
22 
23 #if AGX_USE_UNIT_TESTS()
24 
25 #include <agx/agxPhysics_export.h>
26 #include <exception>
27 #include <string>
28 #include <sstream>
29 #include <vector>
30 #include <agxUnit/SourceCodeInfo.h>
31 
33 
34 
35 #ifdef _MSC_VER
36 # pragma warning(push)
37 # pragma warning(disable: 4251) // warning C4251: class X needs to have dll-interface to be used by clients of class Y
38 # pragma warning(disable: 4275) // warning C4275: non dll-interface class 'std::exception' used as base for dll-interface class 'agxUnit::Exception'
39 #endif
40 
41 namespace agxUnit
42 {
43 
44  class AGXPHYSICS_EXPORT AssertMessage
45  {
46  public:
47 
48  AssertMessage() {}
49 
50  AssertMessage(const agx::String& brief, const agx::String& msg1) : m_brief(brief) { m_msg.push_back(msg1); }
51  AssertMessage(const agx::String& brief, const agx::String& msg1,
52  const agx::String& msg2) : m_brief(brief) { m_msg.push_back(msg1); m_msg.push_back(msg2); }
53  AssertMessage(const agx::String& brief, const agx::String& msg1,
54  const agx::String& msg2,
55  const agx::String& msg3) : m_brief(brief) { m_msg.push_back(msg1); m_msg.push_back(msg2); m_msg.push_back(msg3); }
56 
57  agx::String getMessage(size_t n) const { if (n >= m_msg.size()) return ""; return m_msg[n]; }
58  size_t numMessages() const { return m_msg.size(); }
59  void clear() { m_msg.clear(); }
60 
61  agx::String brief() const { return m_brief; }
62  agx::String detailed() const;
63  void addMessage( const agx::String& msg ) { m_msg.push_back( msg ); }
64  void addMessage( const SourceCodeInfo &info )
65  {
66  m_msg.push_back(agx::String("File: ")+ info.filename());
67  std::ostringstream str;
68  str << "Line: " << info.line();
69  m_msg.push_back(str.str());
70  m_msg.push_back(agx::String("Function: ")+ info.function());
71  }
72  void addMessage( const AssertMessage &message )
73  {
74  m_msg.insert( m_msg.end(),
75  message.m_msg.begin(),
76  message.m_msg.end() );
77  }
78 
79 
80  protected:
81 
82  typedef std::vector<agx::String> DetailedVector;
83  DetailedVector m_msg;
84  agx::String m_brief;
85  };
86 
87  class AGXPHYSICS_EXPORT AdditionalMessage : public AssertMessage
88  {
89  public:
90 
91  AdditionalMessage();
92 
93  AdditionalMessage( const agx::String &detail1 );
94 
95  AdditionalMessage( const char *detail1 );
96 
97  AdditionalMessage( const AssertMessage &other );
98 
99  AdditionalMessage &operator =( const AssertMessage &other );
100  };
101 
102 
103  class AGXPHYSICS_EXPORT Exception : public std::exception
104  {
105  public:
106  Exception( const AssertMessage& msg, const SourceCodeInfo &info=SourceCodeInfo() );
107 
108  virtual ~Exception() throw();
109 
110  Exception( const Exception &copy );
111 
113  Exception &operator =( const Exception &copy );
114 
116  const char *what() const throw();
117  const SourceCodeInfo& info() const { return m_info; }
118  const AssertMessage& message() const { return m_message; }
119 
120  protected:
121  AssertMessage m_message;
122  SourceCodeInfo m_info;
123  agx::String m_what;
124  };
125 
126  class AGXPHYSICS_EXPORT Error : public Exception
127  {
128  public:
129  Error( const AssertMessage& msg, const SourceCodeInfo &info=SourceCodeInfo() );
130 
131  virtual ~Error() throw();
132 
133  Error( const Error &copy );
134 
136  Error &operator =( const Error &copy );
137  };
138 
139 
140  struct Assert
141  {
144  static void AGXPHYSICS_EXPORT fail( const AssertMessage &message,
145  const SourceCodeInfo &sourceLine = SourceCodeInfo() );
146 
147 
154  static void AGXPHYSICS_EXPORT failIf( bool shouldFail,
155  const AssertMessage &message,
156  const SourceCodeInfo &sourceLine = SourceCodeInfo() );
157 
158 
168  static agx::String AGXPHYSICS_EXPORT makeExpected( const agx::String &expectedValue );
169 
179  static agx::String AGXPHYSICS_EXPORT makeActual( const agx::String &actualValue );
180 
181 #if 1
182  static AssertMessage AGXPHYSICS_EXPORT makeNotEqualMessage( const agx::String &expectedValue,
183  const agx::String &actualValue,
184  const AdditionalMessage &additionalMessage = AdditionalMessage(),
185  const agx::String &shortDescription = "equality agxAssertion failed");
186 
195  static void AGXPHYSICS_EXPORT failNotEqual( agx::String expected,
196  agx::String actual,
197  const SourceCodeInfo &sourceLine,
198  const AdditionalMessage &additionalMessage = AdditionalMessage(),
199  agx::String shortDescription = "equality agxAssertion failed" );
200 
211  static void AGXPHYSICS_EXPORT failNotEqualIf( bool shouldFail,
212  agx::String expected,
213  agx::String actual,
214  const SourceCodeInfo &sourceLine,
215  const AdditionalMessage &additionalMessage = AdditionalMessage(),
216  agx::String shortDescription = "equality agxAssertion failed" );
217 
218 #endif
219  };
220 }
221 
223 #ifdef _MSC_VER
224 # pragma warning(pop)
225 #endif
226 
227 
228 #endif // AGX_USE_UNIT_TESTS()
229 
230 #endif // AGXUNIT_UNITASSERT_H
#define AGXPHYSICS_EXPORT
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
AGXTERRAIN_EXPORT void clear()
std::runtime_error Error
Definition: debug.h:50