blob: 43f9d7fc2fa4fe7fc098fca43b27475d61b77931 (
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
<appendix id="test-examples">
<title>Unit Test Examples</title>
<para>
This appendix contains an example of a
<link linkend="test-header-example">unit test header file listing</link> and a
<link linkend="test-source-example">unit test source file listing</link> as well
as a
<link linkend="test-container-example">unit test container source file listing</link>.
</para>
<section id="test-header-example">
<title>Unit Test Header File Example</title>
<screen>
/***************************************************************************
mymoneyexceptiontest.h
-------------------
copyright : (C) 2002 by Thomas Baumgart
email : ipwizard@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* 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 __MYMONEYEXCEPTIONTEST_H__
#define __MYMONEYEXCEPTIONTEST_H__
#include <cppunit/extensions/HelperMacros.h>
#define private public
#include "mymoneyutils.h"
#include "mymoneyexception.h"
#undef private
class MyMoneyExceptionTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(MyMoneyExceptionTest);
CPPUNIT_TEST(testDefaultConstructor);
CPPUNIT_TEST(testConstructor);
CPPUNIT_TEST_SUITE_END();
protected:
public:
MyMoneyExceptionTest();
void setUp();
void tearDown();
void testDefaultConstructor();
void testConstructor();
};
#endif
</screen>
</section>
<section id="test-source-example">
<title>Unit Test Source File Example</title>
<screen>
/***************************************************************************
mymoneyexceptiontest.cpp
-------------------
copyright : (C) 2002 by Thomas Baumgart
email : ipwizard@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "mymoneyexceptiontest.h"
MyMoneyExceptionTest::MyMoneyExceptionTest()
{
}
void MyMoneyExceptionTest::setUp()
{
}
void MyMoneyExceptionTest::tearDown()
{
}
void MyMoneyExceptionTest::testDefaultConstructor()
{
MyMoneyException *e = new MYMONEYEXCEPTION("Message");
CPPUNIT_ASSERT(e->what() == "Message");
CPPUNIT_ASSERT(e->line() == __LINE__-2);
CPPUNIT_ASSERT(e->file() == __FILE__);
delete e;
}
void MyMoneyExceptionTest::testConstructor()
{
MyMoneyException *e = new MyMoneyException("New message",
"Joe's file", 1234);
CPPUNIT_ASSERT(e->what() == "New message");
CPPUNIT_ASSERT(e->line() == 1234);
CPPUNIT_ASSERT(e->file() == "Joe's file");
delete e;
}
</screen>
</section>
<section id="test-container-example">
<title>Unit Test Container Source File Example</title>
<para>
This test environment also contains a reference to a memory usage checker
which can safely be ignored. It is also contained in the &app; environment
and is a great help if looking for memory leaks. Also notice the usage of
the C++ preprocessor directive <emphasis>#ifdef HAVE_LIBCPPUNIT</emphasis>
to avoid compile errors when CPPUNIT is not installed.
</para>
<para>
Another specialty that is not required by CPPUNIT is the specific
TestProgressListener. It is used here to print the name of the fixture that
is currently running. Since this method is called upon start of each
test case, some logic is necessary to print the name only once.
</para>
<screen>
/***************************************************************************
autotest.cpp
-------------------
copyright : (C) 2002 by Thomas Baumgart
email : ipwizard@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "config.h"
#include <iostream>
#ifdef HAVE_LIBCPPUNIT
#include "cppunit/TextTestRunner.h"
#include "cppunit/TextTestResult.h"
#include "cppunit/TestSuite.h"
#include "cppunit/extensions/HelperMacros.h"
#include "mymoneyexceptiontest.h"
#include "cppunit/TextTestProgressListener.h"
class MyProgressListener : public CppUnit::TextTestProgressListener
{
void startTest(CppUnit::Test *test) {
QString name = test->getName().c_str();
name = name.mid(2); // cut off first 2 chars
name = name.left(name.find('.'));
if(m_name != name) {
if(m_name != "")
cout << endl;
cout << "Running: " << name << endl;
m_name = name;
}
}
private:
QString m_name;
};
#endif
int
main(int argc, char** argv)
{
#ifdef HAVE_LIBCPPUNIT
#ifdef _CHECK_MEMORY
_CheckMemory_Init(0);
#endif
CPPUNIT_TEST_SUITE_REGISTRATION(MyMoneyExceptionTest);
CppUnit::TestFactoryRegistry &registry =
CppUnit::TestFactoryRegistry::getRegistry();
CppUnit::Test *suite = registry.makeTest();
CppUnit::TextTestRunner* runner = new CppUnit::TextTestRunner();
runner->addTest(suite);
MyProgressListener progress;
runner->eventManager().addListener(&progress);
runner->run();
delete runner;
#ifdef _CHECK_MEMORY
chkmem.CheckMemoryLeak( true );
_CheckMemory_End();
#endif // _CHECK_MEMORY
#else
std::cout << "libcppunit not installed. no automatic tests available."
<< std::endl;
#endif // HAVE_LIBCPPUNIT
return 0;
}
</screen>
</section>
</appendix>
|