blob: 51ca90044a397a651dcab37536e9b99165942b1c (
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
|
/***************************************************************************
mymoneyobservertest.h
-------------------
copyright : (C) 2002 by Thomas Baumgart
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef __MYMONEYOBSERVERTEST_H__
#define __MYMONEYOBSERVERTEST_H__
#include <cppunit/extensions/HelperMacros.h>
class TestObserver : public MyMoneyObserver
{
public:
TestObserver() { m_updated = ""; }
void update(const TQString& id) { m_updated = id; };
const TQString& updated(void) { return m_updated; };
void reset(void) { m_updated = ""; };
private:
TQString m_updated;
};
class TestSubject : public MyMoneySubject
{
};
class MyMoneyObserverTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(MyMoneyObserverTest);
CPPUNIT_TEST(testEmptySubject);
CPPUNIT_TEST(testAddObserver);
CPPUNIT_TEST(testRemoveObserver);
CPPUNIT_TEST(testNotifyObserver);
CPPUNIT_TEST(testNotifyMultipleObserver);
CPPUNIT_TEST_SUITE_END();
protected:
TestSubject *subject;
TestObserver *observer1;
TestObserver *observer2;
public:
MyMoneyObserverTest () {}
void setUp () {
subject = new TestSubject;
observer1 = new TestObserver;
observer2 = new TestObserver;
}
void tearDown () {
delete observer1;
delete observer2;
delete subject;
}
void testEmptySubject() {
CPPUNIT_ASSERT(subject->m_observers.count() == 0);
}
void testAddObserver() {
subject->attach(observer1);
CPPUNIT_ASSERT(subject->m_observers.count() == 1);
CPPUNIT_ASSERT(subject->m_observers.at(0) == observer1);
}
void testRemoveObserver() {
testAddObserver();
subject->detach(observer1);
CPPUNIT_ASSERT(subject->m_observers.count() == 0);
}
void testNotifyObserver() {
testAddObserver();
CPPUNIT_ASSERT(observer1->updated() == "");
subject->notify("my id");
CPPUNIT_ASSERT(observer1->updated() == "my id");
}
void testNotifyMultipleObserver() {
testAddObserver();
subject->attach(observer2);
CPPUNIT_ASSERT(subject->m_observers.count() == 2);
CPPUNIT_ASSERT(subject->m_observers.at(0) == observer1);
CPPUNIT_ASSERT(subject->m_observers.at(1) == observer2);
CPPUNIT_ASSERT(observer1->updated() == "");
CPPUNIT_ASSERT(observer2->updated() == "");
subject->notify("my id");
CPPUNIT_ASSERT(observer1->updated() == "my id");
CPPUNIT_ASSERT(observer2->updated() == "my id");
}
};
#endif
|