/[escript]/branches/windows_from_1431_trunk/esysUtils/test/EsysExceptionTestCase.cpp
ViewVC logotype

Contents of /branches/windows_from_1431_trunk/esysUtils/test/EsysExceptionTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1444 - (show annotations)
Thu Feb 28 09:42:55 2008 UTC (15 years, 1 month ago) by trankine
File size: 6911 byte(s)
Completing the exception changes. Still fragile
on the unit tests.
going to continue on linux to fix some obvious bugs.
1
2 /* $Id$ */
3
4 /*******************************************************
5 *
6 * Copyright 2003-2007 by ACceSS MNRF
7 * Copyright 2007 by University of Queensland
8 *
9 * http://esscc.uq.edu.au
10 * Primary Business: Queensland, Australia
11 * Licensed under the Open Software License version 3.0
12 * http://www.opensource.org/licenses/osl-3.0.php
13 *
14 *******************************************************/
15
16 #include <iostream>
17
18 #include "EsysExceptionTestCase.h"
19 #include "esysUtils/EsysException.h"
20
21 using namespace std;
22 using namespace CppUnitTest;
23 using namespace esysUtils;
24
25 class DerivedEx : public EsysException {
26 public:
27
28 /// Default Constructor for Exception
29 DerivedEx() : EsysException() { updateMessage(); }
30
31 /// Constructor for Exception
32 DerivedEx(const char *cstr) : EsysException(cstr) { updateMessage(); }
33
34 /// Constructor for Exception
35 DerivedEx(const string &str) : EsysException(str) { updateMessage(); }
36
37 // Copy Constructor.
38 //DerivedEx(const EsysException &other): EsysException(other) {}
39
40 /// Return the exception name
41 virtual const string & exceptionName() const
42 {
43 return rhubarb;
44 }
45
46 static const string rhubarb;
47 };
48
49 const string DerivedEx::rhubarb("DerivedException");
50
51 void EsysExceptionTestCase::testCase0() {
52
53 assert(true);
54
55 }
56
57 void EsysExceptionTestCase::testCase1() {
58
59 EsysException defEx;
60
61 //
62 // default exception text should have contents of exceptionName near start
63 //
64 string defString = defEx.toString();
65 assert(defString.find(defEx.exceptionName()) != string::npos);
66 assert(defString.find(defEx.exceptionName()) < 4);
67
68 //
69 // default exception text shouldn't be much longer than contents of exception name
70 //
71 assert(defString.size() > defEx.exceptionName().size());
72 assert((defString.size() - defEx.exceptionName().size()) < 10);
73
74 string ex1Text("My first funny exception message.");
75 EsysException ex1(ex1Text);
76
77 //
78 // exception text should have contents of exceptionName near start
79 //
80 string ex1String = ex1.toString();
81 assert(ex1String.find(ex1.exceptionName()) != string::npos);
82 assert(defString.find(ex1.exceptionName()) < 4);
83
84 //
85 // exception text should contain entered exception message
86 //
87 assert(ex1String.find(ex1Text) != string::npos);
88
89 //
90 // copy constructed exception should match original
91 //
92 EsysException copyEx(ex1);
93 string copyString = copyEx.toString();
94 assert(ex1String == copyString);
95
96 //
97 // copy assigned exception should match original
98 //
99 EsysException assEx;
100 assEx = ex1;
101 string assString = assEx.toString();
102 assert(ex1String == assString);
103
104 //
105 // check throw/catch mechanism
106 //
107 string ex2Text("My second funny exception message.");
108 try {
109
110 EsysException ex2(ex2Text);
111 throw(ex2);
112
113 }
114
115 catch(EsysException& e) {
116
117 //
118 // exception text should have contents of exceptionName near start
119 //
120 string eString = e.toString();
121 assert(eString.find(e.exceptionName()) != string::npos);
122 assert(defString.find(e.exceptionName()) < 4);
123
124 //
125 // exception text should contain entered exception message
126 //
127 assert(eString.find(ex2Text) != string::npos);
128
129 }
130
131 }
132
133 //
134 // test derived EsysException
135 //
136 void EsysExceptionTestCase::testCase2() {
137
138 DerivedEx defEx;
139 //
140 // default exception text should have contents of exceptionName near start
141 //
142 string defString = defEx.toString();
143 assert(defString.find(defEx.exceptionName()) != string::npos);
144 assert(defString.find(defEx.exceptionName()) < 4);
145
146 //
147 // default exception text shouldn't be much longer than contents of exception name
148 //
149 assert(defString.size() > defEx.exceptionName().size());
150 assert((defString.size() - defEx.exceptionName().size()) < 10);
151
152 string ex1Text("asdjhieurncidhfjsnfkjefkjndfjkhsdrdfjksdhfweh");
153 DerivedEx ex1(ex1Text);
154 //
155 // exception text should have contents of exceptionName near start
156 //
157 string ex1String = ex1.toString();
158 assert(ex1String.find(ex1.exceptionName()) != string::npos);
159 assert(defString.find(ex1.exceptionName()) < 4);
160
161 //
162 // exception text should contain entered exception message
163 //
164 assert(ex1String.find(ex1Text) != string::npos);
165
166 //
167 // copy constructed exception should match original
168 //
169 DerivedEx copyEx(ex1);
170 string copyString = copyEx.toString();
171 std::cout << "\n==============================\n"
172 << ex1String << std::endl
173 << copyString << std::endl
174 << "==============================\n";
175 assert(ex1String == copyString);
176
177 //
178 // copy assigned exception should match original
179 //
180 DerivedEx assEx;
181 assEx = ex1;
182 string assString = assEx.toString();
183 assert(ex1String == assString);
184
185 //
186 // check throw/catch mechanism
187 //
188 string ex2Text("pjkkjhdfbnkjerbkjsduflfkjahalkgjlklhjhj");
189 try {
190
191 DerivedEx ex2(ex2Text);
192 throw(ex2);
193
194 }
195
196 catch(DerivedEx& e) {
197
198 //
199 // exception text should have contents of exceptionName near start
200 //
201 string eString = e.toString();
202 assert(eString.find(e.exceptionName()) != string::npos);
203 assert(defString.find(e.exceptionName()) < 4);
204
205 //
206 // exception text should contain entered exception message
207 //
208 assert(eString.find(ex2Text) != string::npos);
209 }
210
211 //
212 // check throw/catch mechanism
213 //
214 string ex3Text("irfjvniouf;iarhglAKSDIghlAKSDghladg");
215 try {
216
217 DerivedEx ex3(ex3Text);
218 throw(ex3);
219
220 }
221 catch(EsysException& e) {
222
223 //
224 // exception text should have contents of exceptionName near start
225 //
226 DerivedEx ex4;
227 std::string eString = e.toString();
228 assert(eString.find(ex4.exceptionName()) != string::npos);
229 assert(defString.find(ex4.exceptionName()) < 4);
230
231 //
232 // exception text should contain entered exception message
233 //
234 assert(eString.find(ex3Text) != string::npos);
235
236 }
237
238 //
239 // test to see if exception name gets lost on rethrow
240 //
241 try {
242 try {
243 DerivedEx ex4("D ex4 text.");
244 throw ex4;
245 }
246 catch (EsysException& e) {
247 cout << endl << e.toString() << endl;
248 throw;
249 }
250 }
251 catch (EsysException& e) {
252 cout << e.toString() << endl;
253 }
254
255 cout << "Test EsysException may be caught as a std::exception" << endl;
256 try {
257 DerivedEx ex4("Exception caught as std::exception");
258 throw ex4;
259 }
260 catch (exception& e) {
261 cout << e.what() << endl;
262 }
263 catch (...) {
264 //
265 // if the exception is caught here there is a problem
266 assert(false);
267 }
268 }
269
270 TestSuite* EsysExceptionTestCase::suite ()
271 {
272 //
273 // create the suite of tests to perform.
274 TestSuite *testSuite = new TestSuite ("EsysExceptionTestCase");
275
276 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase0",&EsysExceptionTestCase::testCase0));
277 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase1",&EsysExceptionTestCase::testCase1));
278 testSuite->addTest (new TestCaller<EsysExceptionTestCase>("testCase2",&EsysExceptionTestCase::testCase2));
279 return testSuite;
280 }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26