Console: Add console logging documentation
[senf.git] / Utils / Exception.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Exception.test unit tests */
25
26 //#include "Exception.test.hh"
27 //#include "Exception.test.ih"
28
29 // Custom includes
30 #include "Exception.hh"
31
32 #include "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34 #include <boost/format.hpp>
35 #include <errno.h>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 BOOST_AUTO_UNIT_TEST(wrapException)
41 {
42     bool bad_cast (false);
43
44     try {
45         try {
46             try {
47                 try {
48                     try {
49                         throw std::bad_cast();
50                     }
51                     SENF_WRAP_EXC(std::bad_cast)
52                 }
53                 SENF_WRAP_EXC(std::bad_cast)
54             }
55             catch (senf::ExceptionMixin & ex) {
56                 ex << "\nspecial exception";
57                 throw;
58             }
59         }
60         catch (std::exception const & ex) {
61             BOOST_CHECK( std::string(ex.what()).find("-- \n") != std::string::npos );
62             BOOST_CHECK( std::string(ex.what()).find("special exception") != std::string::npos );
63             throw;
64         }
65     }
66     catch (std::bad_cast &) {
67         bad_cast = true;
68     }
69     BOOST_CHECK( bad_cast );
70 }
71
72 BOOST_AUTO_UNIT_TEST(errnoException)
73 {
74     try {
75         try {
76             throw senf::SystemException("::open()", ENOENT) << "\nmore";
77         }
78         catch(senf::Exception & e) {
79             e << "\nx=" << 1 << boost::format("\ny=%d") % 2;
80             throw;
81         }
82     }
83     catch (senf::SystemException & e) {
84         BOOST_CHECK_EQUAL( e.errorNumber(), ENOENT );
85         BOOST_CHECK_EQUAL( e.errorString(), "No such file or directory" );
86         std::string what (e.what());
87         std::string::size_type pos (what.find("-- \n"));
88         if (pos != std::string::npos)
89             what = std::string(what, pos+4);
90         BOOST_CHECK_EQUAL( what, "[No such file or directory] ::open()\nmore\nx=1\ny=2" );
91     }
92 }
93
94 ///////////////////////////////cc.e////////////////////////////////////////
95 #undef prefix_
96
97 \f
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // comment-column: 40
102 // c-file-style: "senf"
103 // indent-tabs-mode: nil
104 // ispell-local-dictionary: "american"
105 // compile-command: "scons -u test"
106 // End: