summaryrefslogtreecommitdiff
path: root/examples/cppunittest/TestCaseTest.cpp
blob: d749938eaa977d0baf2180c57d5858ed261abd5b (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "TestCaseTest.h"
#include "FailingTestCase.h"
#include <cppunit/TestResult.h>

/*
 - test have been done to check exception management in run(). other 
   tests need to be added to check the other aspect of TestCase.
 */

CPPUNIT_TEST_SUITE_REGISTRATION( TestCaseTest );


TestCaseTest::TestCaseTest()
{
}


TestCaseTest::~TestCaseTest()
{
}


void 
TestCaseTest::setUp()
{
  m_result = new CppUnit::TestResult();
}


void 
TestCaseTest::tearDown()
{
  delete m_result;
}


void 
TestCaseTest::testSetUpFailure()
{
  checkFailure( true, false, false );
}


void 
TestCaseTest::testRunTestFailure()
{
  checkFailure( false, true, false );
}


void 
TestCaseTest::testTearDownFailure()
{
  checkFailure( false, false, true );
}


void 
TestCaseTest::testFailAll()
{
  checkFailure( true, true, true );
}


void 
TestCaseTest::testNoFailure()
{
  checkFailure( false, false, false );
}


void 
TestCaseTest::checkFailure( bool failSetUp, 
                            bool failRunTest,
                            bool failTearDown )
{
  try
  {
    FailingTestCase test( failSetUp, failRunTest, failTearDown );
    test.run( m_result );
    test.verify( !failSetUp, !failSetUp );
  }
  catch ( FailureException & )
  {
    CPPUNIT_ASSERT_MESSAGE( "exception should have been catched", false );
  }
}


void 
TestCaseTest::testCountTestCases()
{
  CppUnit::TestCase test;
  CPPUNIT_ASSERT_EQUAL( 1, test.countTestCases() );
}


void 
TestCaseTest::testDefaultConstructor()
{
  CppUnit::TestCase test;
  CPPUNIT_ASSERT_EQUAL( std::string(""), test.getName() );
}


void 
TestCaseTest::testConstructorWithName()
{
  std::string testName( "TestName" );
  CppUnit::TestCase test( testName );
  CPPUNIT_ASSERT_EQUAL( testName, test.getName() );
}


void 
TestCaseTest::testDefaultRun()
{
  CppUnit::TestCase test;
  std::auto_ptr<CppUnit::TestResult> result( test.run() );

  checkResult( 0, 0, 1, result.get() );
}


void 
TestCaseTest::testTwoRun()
{
  FailingTestCase test1( false, true, false );
  test1.run( m_result );
  test1.run( m_result );
  
  FailingTestCase test2( false, false, false );
  test2.run( m_result );

  checkResult( 2, 0, 1, m_result );
}


void 
TestCaseTest::checkResult( int failures,
                           int errors,
                           int testsRun,
                           CppUnit::TestResult *result )
{
  CPPUNIT_ASSERT_EQUAL( testsRun, result->runTests() );
  CPPUNIT_ASSERT_EQUAL( errors, result->testErrors() );
  CPPUNIT_ASSERT_EQUAL( failures, result->testFailures() );
}