OpenWalnut  1.5.0dev
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 <memory>
29 #include <string>
30 
31 #include <cxxtest/TestSuite.h>
32 
33 #include "../WException.h"
34 
35 /**
36  * Test WException
37  */
38 class WExceptionTest : public CxxTest::TestSuite
39 {
40 public:
41  /**
42  * An instantiation should never throw an exception.
43  */
44  void testInstantiation( void )
45  {
46  TS_ASSERT_THROWS_NOTHING( WException e );
47  TS_ASSERT_THROWS_NOTHING( WException e( std::string( "Some message" ) ) );
48  }
49 
50  /**
51  * Getting the message means every trace element should be returned.
52  */
53  void testGetMessage( void )
54  {
55  WException e( std::string( "Dummy exception" ) );
56  e.m_trace.push_back( "first" );
57  e.m_trace.push_back( "second" );
58  std::string expected = "Dummy exception\n\ntrace: first\ntrace: second";
59  TS_ASSERT_EQUALS( expected, e.getTrace() );
60  WException f;
61  TS_ASSERT_EQUALS( std::string(), f.getTrace() );
62  }
63 
64  /**
65  * Test backtrace. This test always passes on platforms other than Linux!
66  */
67  void testBacktrace( void )
68  {
69 #if( defined( __linux__ ) && defined( __GNUC__ ) )
70  try
71  {
72  new WException();
73  }
74  catch( const WException& e )
75  {
76  std::string bt = e.getBacktrace();
77  // how to test this? Since the trace is different in release and debug mode, we simply test for
78  // non empty string here.
79  TS_ASSERT( bt.length() );
80  }
81 #else
82  // every platform not Linux will pass this test since only Linux is supported
83  TS_ASSERT( true );
84 #endif
85  }
86 };
87 
88 #endif // WEXCEPTION_TEST_H
Test WException.
void testInstantiation(void)
An instantiation should never throw an exception.
void testBacktrace(void)
Test backtrace.
void testGetMessage(void)
Getting the message means every trace element should be returned.
Basic exception handler.
Definition: WException.h:39
std::list< std::string > m_trace
Stack trace for identifying the source where this exception came from.
Definition: WException.h:99
std::string getBacktrace() const
Returns a call stacktrace.
Definition: WException.cpp:107
std::string getTrace() const
Prints the trace of the call chain which caused this exception.
Definition: WException.cpp:96