1 // MoneyTest.cpp
2
3 #include "StdAfx.h"
4 #include <cppunit/config/SourcePrefix.h>
5 #include "Money.h"
6 #include "MoneyTest.h"
7
8 // Registers the fixture into the 'registry'
9 CPPUNIT_TEST_SUITE_REGISTRATION( MoneyTest );
10
11
12 void
setUp()13 MoneyTest::setUp()
14 {
15 }
16
17
18 void
tearDown()19 MoneyTest::tearDown()
20 {
21 }
22
23
24 void
testConstructor()25 MoneyTest::testConstructor()
26 {
27 // Set up
28 const std::string currencyFF( "FF" );
29 const double longNumber = 1234.5678;
30
31 // Process
32 Money money( longNumber, currencyFF );
33
34 // Check
35 CPPUNIT_ASSERT_EQUAL( longNumber, money.getAmount() );
36 CPPUNIT_ASSERT_EQUAL( currencyFF, money.getCurrency() );
37 }
38
39
40 void
testEqual()41 MoneyTest::testEqual()
42 {
43 // Set up
44 const Money money123FF( 123, "FF" );
45 const Money money123USD( 123, "USD" );
46 const Money money12FF( 12, "FF" );
47 const Money money12USD( 12, "USD" );
48
49 // Process & Check
50 CPPUNIT_ASSERT( money123FF == money123FF ); // ==
51 CPPUNIT_ASSERT( money12FF != money123FF ); // != amount
52 CPPUNIT_ASSERT( money123USD != money123FF ); // != currency
53 CPPUNIT_ASSERT( money12USD != money123FF ); // != currency and != amount
54 }
55
56
57 void
testAdd()58 MoneyTest::testAdd()
59 {
60 // Set up
61 const Money money12FF( 12, "FF" );
62 const Money expectedMoney( 135, "FF" );
63
64 // Process
65 Money money( 123, "FF" );
66 money += money12FF;
67
68 // Check
69 CPPUNIT_ASSERT_EQUAL( expectedMoney, money ); // add works
70 CPPUNIT_ASSERT( &money == &(money += money12FF) ); // add returns ref. on 'this'.
71 }
72
73
74 void
testAddThrow()75 MoneyTest::testAddThrow()
76 {
77 // Set up
78 const Money money123FF( 123, "FF" );
79
80 // Process
81 Money money( 123, "USD" );
82 CPPUNIT_ASSERT_THROW( money += money123FF, IncompatibleMoneyError );
83 }
84