summaryrefslogtreecommitdiff
path: root/examples/cppunittest/ExceptionTest.cpp
blob: ab60eed70bad225cdaa4d2a68594ce671e9f7262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "CoreSuite.h"
#include "ExceptionTest.h"
#include <cppunit/Exception.h>
#include <cppunit/NotEqualException.h>
#include <memory>


CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ExceptionTest,
                                       CppUnitTest::coreSuiteName() );


ExceptionTest::ExceptionTest()
{
}


ExceptionTest::~ExceptionTest()
{
}


void 
ExceptionTest::setUp()
{
}


void 
ExceptionTest::tearDown()
{
}


void 
ExceptionTest::testConstructor()
{
  const std::string message( "a message" );
  const CppUnit::SourceLine sourceLine( "dir/afile.cpp", 17 );
  
  CppUnit::Exception e( message, sourceLine );

  CPPUNIT_ASSERT_EQUAL( message, std::string( e.what() ) );
  CPPUNIT_ASSERT( sourceLine == e.sourceLine() );
}


void 
ExceptionTest::testDefaultConstructor()
{
  CppUnit::Exception e;

  CPPUNIT_ASSERT_EQUAL( std::string(""), std::string( e.what() ) );
  CPPUNIT_ASSERT( !e.sourceLine().isValid() );
}


void 
ExceptionTest::testCopyConstructor()
{
  CppUnit::SourceLine sourceLine( "fileName.cpp", 123 );
  CppUnit::Exception e( "message", sourceLine  );
  CppUnit::Exception other( e );
  checkIsSame( e, other );
}


void 
ExceptionTest::testAssignment()
{
  CppUnit::SourceLine sourceLine( "fileName.cpp", 123 );
  CppUnit::Exception e( "message", sourceLine  );
  CppUnit::Exception other;
  other = e;
  checkIsSame( e, other );
}


void 
ExceptionTest::testClone()
{
  CppUnit::SourceLine sourceLine( "fileName.cpp", 123 );
  CppUnit::Exception e( "message", sourceLine  );
  std::auto_ptr<CppUnit::Exception> other( e.clone() );
  checkIsSame( e, *other.get() );
}


void 
ExceptionTest::testIsInstanceOf()
{
  CppUnit::SourceLine sourceLine( "fileName.cpp", 123 );
  CppUnit::Exception e( "message", sourceLine  );
  CPPUNIT_ASSERT( e.isInstanceOf( CppUnit::Exception::type() ) );
  CPPUNIT_ASSERT( !e.isInstanceOf( CppUnit::NotEqualException::type() ) );
}


void 
ExceptionTest::checkIsSame( CppUnit::Exception &e, 
                            CppUnit::Exception &other )
{
  std::string eWhat( e.what() );
  std::string otherWhat( other.what() );
  CPPUNIT_ASSERT_EQUAL( eWhat, otherWhat );
  CPPUNIT_ASSERT( e.sourceLine() == other.sourceLine() );
}