-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTest_cppunit.cpp
More file actions
157 lines (127 loc) · 4.31 KB
/
Test_cppunit.cpp
File metadata and controls
157 lines (127 loc) · 4.31 KB
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
149
150
151
152
153
154
155
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/TestCase.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/TextTestProgressListener.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#define XMLOUT
#include "Money.h"
class MoneyTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( MoneyTest );
CPPUNIT_TEST( testConstructor );
CPPUNIT_TEST( testEqual );
CPPUNIT_TEST( testAdd );
CPPUNIT_TEST_EXCEPTION( testAddThrow, IncompatibleMoneyError );
CPPUNIT_TEST_SUITE_END();
public:
void testConstructor();
void testEqual();
void testAdd();
void testAddThrow();
};
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( MoneyTest );
void
MoneyTest::testConstructor()
{
// Set up
const std::string currencyFF( "FF" );
const double longNumber = 12345678.90123;
// Process
Money money( longNumber, currencyFF );
// Check
CPPUNIT_ASSERT_EQUAL( longNumber, money.getAmount() );
CPPUNIT_ASSERT_EQUAL( currencyFF, money.getCurrency() );
}
void
MoneyTest::testEqual()
{
// Set up
const Money money123FF( 123, "FF" );
const Money money123USD( 123, "USD" );
const Money money12FF( 12, "FF" );
const Money money12USD( 12, "USD" );
// Process & Check
CPPUNIT_ASSERT( money123FF == money123FF ); // ==
CPPUNIT_ASSERT( money12FF != money123FF ); // != amount
CPPUNIT_ASSERT( money123USD != money123FF ); // != currency
CPPUNIT_ASSERT( money12USD != money123FF ); // != currency and != amount
}
void
MoneyTest::testAdd()
{
// Set up
const Money money12FF( 12, "FF" );
const Money expectedMoney( 135, "FF" );
// Process
Money money( 123, "FF" );
money += money12FF;
// Check
CPPUNIT_ASSERT_EQUAL( expectedMoney.getAmount() , money.getAmount() ); // += works
CPPUNIT_ASSERT( &money == &(money += money12FF) ); // += returns ref. on 'this'.
}
void
MoneyTest::testAddThrow()
{
// Set up
const Money money123FF( 123, "FF" );
// Process
Money money( 123, "USD" );
money += money123FF; // should throw an exception
}
/****************************************************************************/
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MoneyTest, "test_money_cppunit" );
// register test suite
CppUnit::Test *suite()
{
CppUnit::TestFactoryRegistry ®istry =
CppUnit::TestFactoryRegistry::getRegistry();
registry.registerFactory(
&CppUnit::TestFactoryRegistry::getRegistry( "test_money_cppunit" ) );
return registry.makeTest();
}
int main(int argc, char* argv[])
{
std::string testPath = (argc > 1) ? std::string(argv[1]) : "";
// Create the event manager and test controller
CppUnit::TestResult controller;
// Add a listener that colllects test result
CppUnit::TestResultCollector result;
controller.addListener( &result );
// Add a listener that print dots as test run.
//CppUnit::TextTestProgressListener progress;
// Add a listener that print name of methods as test run.
CppUnit::BriefTestProgressListener progress;
controller.addListener( &progress );
// Add the top suite to the test runner
CppUnit::TestRunner runner;
runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );
runner.addTest( suite() );
try {
std::cout << std::endl;
std::cout << "test Money class with cppunit" << std::endl;
std::cout << "=========================" << std::endl;
runner.run( controller, testPath );
std::cout << "=========================" << std::endl;
std::cout << std::endl;
// Print test in XML or compiler compatible format.
#if defined XMLOUT
CppUnit::XmlOutputter outputter( &result, std::cerr );
#else
CppUnit::CompilerOutputter outputter( &result, std::cerr );
#endif
outputter.write();
} catch ( std::invalid_argument& e ) { // Test path not resolved
std::cerr << std::endl
<< "ERROR: " << e.what()
<< std::endl;
return 0;
}
return result.wasSuccessful() ? 0 : 1;
}