summaryrefslogtreecommitdiff
path: root/examples/cppunittest/ExceptionTest.cpp
blob: c56082ec68b84da9ecbb87f4ee35af1d70d8a2ea (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 "ExceptionTest.h"
#include <cppunit/Exception.h>
#include <cppunit/NotEqualException.h>
#include <memory>


CPPUNIT_TEST_SUITE_REGISTRATION( ExceptionTest );


ExceptionTest::ExceptionTest()
{
}


ExceptionTest::~ExceptionTest()
{
}


void 
ExceptionTest::setUp()
{
}


void 
ExceptionTest::tearDown()
{
}


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

  CPPUNIT_ASSERT_EQUAL( message, std::string( e.what() ) );
  CPPUNIT_ASSERT_EQUAL( lineNumber, int(e.lineNumber()) );
  CPPUNIT_ASSERT_EQUAL( fileName, e.fileName() );
}


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

  CPPUNIT_ASSERT_EQUAL( std::string(""), std::string( e.what() ) );
  CPPUNIT_ASSERT_EQUAL( CppUnit::Exception::UNKNOWNLINENUMBER, 
                        e.lineNumber() );
  CPPUNIT_ASSERT_EQUAL( CppUnit::Exception::UNKNOWNFILENAME, 
                        e.fileName() );
}


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


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


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


void 
ExceptionTest::testIsInstanceOf()
{
  CppUnit::Exception e( "message", 17, "fileName.cpp"  );
  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_EQUAL( e.lineNumber(), other.lineNumber() );
  CPPUNIT_ASSERT_EQUAL( e.fileName(), other.fileName() );
}