Line data Source code
1 : // Copyright (c) 2012-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/libexcept
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : // self
21 : //
22 : #include "catch_main.h"
23 :
24 :
25 : // eventdispatcher lib
26 : //
27 : #include <libexcept/exception.h>
28 :
29 :
30 :
31 :
32 :
33 : namespace
34 : {
35 :
36 :
37 :
38 :
39 :
40 : }
41 :
42 :
43 3 : CATCH_TEST_CASE("trace_mode", "[trace][exception]")
44 : {
45 2 : CATCH_START_SECTION("trace mode")
46 : {
47 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
48 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_YES);
49 :
50 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
51 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
52 :
53 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
54 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_NO);
55 : }
56 : CATCH_END_SECTION()
57 1 : }
58 :
59 :
60 5 : CATCH_TEST_CASE("user_exception", "[trace][exception]")
61 : {
62 6 : CATCH_START_SECTION("logic exception")
63 : {
64 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_NO);
65 :
66 6 : DECLARE_LOGIC_ERROR(test_logic_exception);
67 :
68 : {
69 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
70 1 : test_logic_exception e(test_logic_exception(
71 : "this is the what message"
72 2 : ));
73 :
74 1 : CATCH_CHECK(strcmp(e.what(), "test_logic_exception: this is the what message") == 0);
75 :
76 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
77 1 : CATCH_CHECK(stack.size() == 0);
78 : }
79 :
80 : {
81 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
82 1 : test_logic_exception e(test_logic_exception(
83 : "try with \"yes\" for the stack"
84 2 : ));
85 :
86 1 : CATCH_CHECK(strcmp(e.what(), "test_logic_exception: try with \"yes\" for the stack") == 0);
87 :
88 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
89 1 : CATCH_CHECK(stack.size() > 0);
90 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
91 : }
92 :
93 : {
94 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
95 1 : test_logic_exception e(test_logic_exception(
96 : "try with \"complete\" for the stack"
97 2 : ));
98 :
99 1 : CATCH_CHECK(strcmp(e.what(), "test_logic_exception: try with \"complete\" for the stack") == 0);
100 :
101 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
102 1 : CATCH_CHECK(stack.size() > 0);
103 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
104 : }
105 :
106 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
107 : }
108 : CATCH_END_SECTION()
109 :
110 6 : CATCH_START_SECTION("range exception")
111 : {
112 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_NO);
113 :
114 6 : DECLARE_OUT_OF_RANGE(test_range_exception);
115 :
116 : {
117 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
118 1 : test_range_exception e(test_range_exception(
119 : "range error"
120 2 : ));
121 :
122 1 : CATCH_CHECK(strcmp(e.what(), "test_range_exception: range error") == 0);
123 :
124 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
125 1 : CATCH_CHECK(stack.size() == 0);
126 : }
127 :
128 : {
129 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
130 1 : test_range_exception e(test_range_exception(
131 : "range error \"yes\" basic stack"
132 2 : ));
133 :
134 1 : CATCH_CHECK(strcmp(e.what(), "test_range_exception: range error \"yes\" basic stack") == 0);
135 :
136 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
137 1 : CATCH_CHECK(stack.size() > 0);
138 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
139 : }
140 :
141 : {
142 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
143 1 : test_range_exception e(test_range_exception(
144 : "range error \"complete\" complete stack"
145 2 : ));
146 :
147 1 : CATCH_CHECK(strcmp(e.what(), "test_range_exception: range error \"complete\" complete stack") == 0);
148 :
149 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
150 1 : CATCH_CHECK(stack.size() > 0);
151 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
152 : }
153 :
154 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
155 : }
156 : CATCH_END_SECTION()
157 :
158 6 : CATCH_START_SECTION("run-time exception")
159 : {
160 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_NO);
161 :
162 6 : DECLARE_MAIN_EXCEPTION(run_time_exception);
163 6 : DECLARE_EXCEPTION(run_time_exception, test_run_time_exception);
164 :
165 : {
166 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
167 1 : test_run_time_exception e(test_run_time_exception(
168 : "run time error"
169 2 : ));
170 :
171 1 : CATCH_CHECK(strcmp(e.what(), "run_time_exception: run time error") == 0);
172 :
173 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
174 1 : CATCH_CHECK(stack.size() == 0);
175 : }
176 :
177 : {
178 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
179 1 : test_run_time_exception e(test_run_time_exception(
180 : "range error \"yes\" basic stack"
181 2 : ));
182 :
183 1 : CATCH_CHECK(strcmp(e.what(), "run_time_exception: range error \"yes\" basic stack") == 0);
184 :
185 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
186 1 : CATCH_CHECK(stack.size() > 0);
187 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
188 : }
189 :
190 : {
191 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
192 1 : test_run_time_exception e(test_run_time_exception(
193 : "range error \"complete\" complete stack"
194 2 : ));
195 :
196 1 : CATCH_CHECK(strcmp(e.what(), "run_time_exception: range error \"complete\" complete stack") == 0);
197 :
198 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
199 1 : CATCH_CHECK(stack.size() > 0);
200 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
201 : }
202 :
203 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
204 : }
205 : CATCH_END_SECTION()
206 3 : }
207 :
208 :
209 5 : CATCH_TEST_CASE("direct_exception", "[trace][exception]")
210 : {
211 6 : CATCH_START_SECTION("direct logic exception")
212 : {
213 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_NO);
214 :
215 : {
216 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
217 1 : libexcept::logic_exception_t e(libexcept::logic_exception_t(
218 : "direct logic exception"
219 2 : ));
220 :
221 1 : CATCH_CHECK(strcmp(e.what(), "direct logic exception") == 0);
222 :
223 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
224 1 : CATCH_CHECK(stack.size() == 0);
225 : }
226 :
227 : {
228 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
229 1 : libexcept::logic_exception_t e(libexcept::logic_exception_t(
230 : "direct logic exception with \"yes\" for the stack"
231 2 : ));
232 :
233 1 : CATCH_CHECK(strcmp(e.what(), "direct logic exception with \"yes\" for the stack") == 0);
234 :
235 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
236 1 : CATCH_CHECK(stack.size() > 0);
237 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
238 : }
239 :
240 : {
241 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
242 1 : libexcept::logic_exception_t e(libexcept::logic_exception_t(
243 : "direct logic exception with \"yes\" for the stack"
244 : , 3
245 2 : ));
246 :
247 1 : CATCH_CHECK(strcmp(e.what(), "direct logic exception with \"yes\" for the stack") == 0);
248 :
249 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
250 1 : CATCH_CHECK(stack.size() == 3);
251 : }
252 :
253 : {
254 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
255 1 : libexcept::logic_exception_t e(libexcept::logic_exception_t(
256 : "direct logic exception with \"complete\" for the stack"
257 2 : ));
258 :
259 1 : CATCH_CHECK(strcmp(e.what(), "direct logic exception with \"complete\" for the stack") == 0);
260 :
261 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
262 1 : CATCH_CHECK(stack.size() > 0);
263 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
264 : }
265 :
266 : {
267 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
268 1 : libexcept::logic_exception_t e(libexcept::logic_exception_t(
269 : "direct logic exception with \"complete\" for the stack"
270 : , 2
271 2 : ));
272 :
273 1 : CATCH_CHECK(strcmp(e.what(), "direct logic exception with \"complete\" for the stack") == 0);
274 :
275 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
276 1 : CATCH_CHECK(stack.size() == 2);
277 : }
278 :
279 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
280 : }
281 : CATCH_END_SECTION()
282 :
283 6 : CATCH_START_SECTION("direct range exception")
284 : {
285 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_NO);
286 :
287 : {
288 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
289 1 : libexcept::out_of_range_t e(libexcept::out_of_range_t(
290 : "direct range error"
291 2 : ));
292 :
293 1 : CATCH_CHECK(strcmp(e.what(), "direct range error") == 0);
294 :
295 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
296 1 : CATCH_CHECK(stack.size() == 0);
297 : }
298 :
299 : {
300 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
301 1 : libexcept::out_of_range_t e(libexcept::out_of_range_t(
302 : "direct range error \"yes\" basic stack"
303 2 : ));
304 :
305 1 : CATCH_CHECK(strcmp(e.what(), "direct range error \"yes\" basic stack") == 0);
306 :
307 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
308 1 : CATCH_CHECK(stack.size() > 0);
309 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
310 : }
311 :
312 : {
313 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
314 1 : libexcept::out_of_range_t e(libexcept::out_of_range_t(
315 : "direct range error \"complete\" complete stack"
316 2 : ));
317 :
318 1 : CATCH_CHECK(strcmp(e.what(), "direct range error \"complete\" complete stack") == 0);
319 :
320 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
321 1 : CATCH_CHECK(stack.size() > 0);
322 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
323 : }
324 :
325 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
326 : }
327 : CATCH_END_SECTION()
328 :
329 6 : CATCH_START_SECTION("direct run-time exception")
330 : {
331 1 : CATCH_CHECK(libexcept::get_collect_stack() == libexcept::collect_stack_t::COLLECT_STACK_NO);
332 :
333 : {
334 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
335 1 : libexcept::exception_t e(libexcept::exception_t(
336 : "direct run time error"
337 2 : ));
338 :
339 1 : CATCH_CHECK(strcmp(e.what(), "direct run time error") == 0);
340 :
341 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
342 1 : CATCH_CHECK(stack.size() == 0);
343 : }
344 :
345 : {
346 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
347 1 : libexcept::exception_t e(libexcept::exception_t(
348 : "direct range error \"yes\" basic stack"
349 2 : ));
350 :
351 1 : CATCH_CHECK(strcmp(e.what(), "direct range error \"yes\" basic stack") == 0);
352 :
353 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
354 1 : CATCH_CHECK(stack.size() > 0);
355 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
356 : }
357 :
358 : {
359 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_YES);
360 1 : libexcept::exception_t e(libexcept::exception_t(
361 : "direct range error \"yes\" basic stack"
362 : , 0
363 2 : ));
364 :
365 1 : CATCH_CHECK(strcmp(e.what(), "direct range error \"yes\" basic stack") == 0);
366 :
367 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
368 1 : CATCH_CHECK(stack.size() == 0);
369 : }
370 :
371 : {
372 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
373 1 : libexcept::exception_t e(libexcept::exception_t(
374 : "direct range error \"complete\" complete stack"
375 2 : ));
376 :
377 1 : CATCH_CHECK(strcmp(e.what(), "direct range error \"complete\" complete stack") == 0);
378 :
379 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
380 1 : CATCH_CHECK(stack.size() > 0);
381 1 : CATCH_CHECK(stack.size() < libexcept::STACK_TRACE_DEPTH);
382 : }
383 :
384 : {
385 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_COMPLETE);
386 1 : libexcept::exception_t e(libexcept::exception_t(
387 : "direct range error \"complete\" complete stack"
388 : , 2
389 2 : ));
390 :
391 1 : CATCH_CHECK(strcmp(e.what(), "direct range error \"complete\" complete stack") == 0);
392 :
393 2 : libexcept::stack_trace_t const stack(e.get_stack_trace());
394 1 : CATCH_CHECK(stack.size() == 2);
395 : }
396 :
397 1 : libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO);
398 : }
399 : CATCH_END_SECTION()
400 3 : }
401 :
402 :
403 3 : CATCH_TEST_CASE("exception_parameters", "[parameters][exception]")
404 : {
405 2 : CATCH_START_SECTION("exception parameters")
406 : {
407 2 : libexcept::exception_t basic_exception("something bad happened");
408 :
409 1 : CATCH_CHECK(strcmp(basic_exception.what(), "something bad happened") == 0);
410 :
411 1 : basic_exception.set_parameter("filename", "/etc/aliases");
412 1 : basic_exception.set_parameter(std::string(), "ignored");
413 :
414 1 : CATCH_CHECK(basic_exception.get_parameter("filename") == "/etc/aliases");
415 1 : CATCH_CHECK(basic_exception.get_parameter("undefined").empty());
416 1 : CATCH_CHECK(basic_exception.get_parameter(std::string()).empty());
417 1 : CATCH_CHECK(basic_exception.get_parameters().size() == 1);
418 :
419 : try
420 : {
421 1 : throw basic_exception;
422 : }
423 2 : catch(libexcept::exception_t const & e)
424 : {
425 1 : CATCH_CHECK(strcmp(e.what(), "something bad happened") == 0);
426 1 : CATCH_CHECK(e.get_parameter("filename") == "/etc/aliases");
427 1 : CATCH_CHECK(e.get_parameter("undefined").empty());
428 1 : CATCH_CHECK(e.get_parameter(std::string()).empty());
429 1 : CATCH_CHECK(e.get_parameters().size() == 1);
430 : }
431 : }
432 : CATCH_END_SECTION()
433 7 : }
434 :
435 :
436 : // vim: ts=4 sw=4 et
|