OpenWalnut  1.4.0
WException_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WEXCEPTION_TEST_H
26 #define WEXCEPTION_TEST_H
27 
28 #include <string>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/shared_ptr.hpp>
32 #endif
33 #include <cxxtest/TestSuite.h>
34 
35 #include "../WException.h"
36 
37 /**
38  * Test WException
39  */
40 class WExceptionTest : public CxxTest::TestSuite
41 {
42 public:
43  /**
44  * An instantiation should never throw an exception.
45  */
46  void testInstantiation( void )
47  {
48  TS_ASSERT_THROWS_NOTHING( WException e() );
49  TS_ASSERT_THROWS_NOTHING( WException e( std::string( "Some message" ) ) );
50  }
51 
52  /**
53  * Getting the message means every trace element should be returned.
54  */
55  void testGetMessage( void )
56  {
57  WException e( std::string( "Dummy exception" ) );
58  e.m_trace.push_back( "first" );
59  e.m_trace.push_back( "second" );
60  std::string expected = "Dummy exception\n\ntrace: first\ntrace: second";
61  TS_ASSERT_EQUALS( expected, e.getTrace() );
62  WException f;
63  TS_ASSERT_EQUALS( std::string(), f.getTrace() );
64  }
65 
66  /**
67  * Test backtrace. This test always passes on platforms other than Linux!
68  */
69  void testBacktrace( void )
70  {
71 #if ( defined( __linux__ ) && defined( __GNUC__ ) )
72  try
73  {
74  new WException();
75  }
76  catch( const WException& e )
77  {
78  std::string bt = e.getBacktrace();
79  // how to test this? Since the trace is different in release and debug mode, we simply test for
80  // non empty string here.
81  TS_ASSERT( bt.length() );
82  }
83 #else
84  // every platform not Linux will pass this test since only Linux is supported
85  TS_ASSERT( true );
86 #endif
87  }
88 };
89 
90 #endif // WEXCEPTION_TEST_H
std::string getTrace() const
Prints the trace of the call chain which caused this exception.
Definition: WException.cpp:96
std::string getBacktrace() const
Returns a call stacktrace.
Definition: WException.cpp:107
void testGetMessage(void)
Getting the message means every trace element should be returned.
std::list< std::string > m_trace
Stack trace for identifying the source where this exception came from.
Definition: WException.h:99
void testInstantiation(void)
An instantiation should never throw an exception.
Basic exception handler.
Definition: WException.h:38
void testBacktrace(void)
Test backtrace.
Test WException.