wibble 0.1.28
|
00001 /* -*- C++ -*- 00002 * Generic base exception hierarchy 00003 * 00004 * Copyright (C) 2003,2004,2005,2006 Enrico Zini <enrico@debian.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 #include <wibble/test.h> 00022 #include <wibble/exception.h> 00023 #include <errno.h> 00024 00025 using namespace std; 00026 namespace wex = wibble::exception; 00027 00028 struct TestException { 00029 Test generic() 00030 { 00031 try { 00032 throw wex::Generic("antani"); 00033 } catch ( std::exception& e ) { 00034 assert(string(e.what()).find("antani") != string::npos); 00035 } 00036 00037 try { 00038 throw wex::Generic("antani"); 00039 } catch ( wex::Generic& e ) { 00040 assert(e.fullInfo().find("antani") != string::npos); 00041 } 00042 } 00043 00044 Test system() 00045 { 00046 try { 00047 assert_eq(access("does-not-exist", F_OK), -1); 00048 throw wex::System("checking for existance of nonexisting file"); 00049 } catch ( wibble::exception::System& e ) { 00050 // Check that we caught the right value of errno 00051 assert_eq(e.code(), ENOENT); 00052 } 00053 00054 try { 00055 assert_eq(access("does-not-exist", F_OK), -1); 00056 throw wex::File("does-not-exist", "checking for existance of nonexisting file"); 00057 } catch ( wex::File& e ) { 00058 // Check that we caught the right value of errno 00059 assert_eq(e.code(), ENOENT); 00060 assert(e.fullInfo().find("does-not-exist") != string::npos); 00061 } 00062 } 00063 00064 Test badCast() 00065 { 00066 int check = -1; 00067 try { 00068 check = 0; 00069 throw wex::BadCastExt< int, const char * >( "test" ); 00070 check = 1; 00071 } catch ( wex::BadCast& e ) { 00072 assert_eq( e.fullInfo(), 00073 "bad cast: from i to PKc. Context:\n test" ); 00074 check = 2; 00075 } 00076 assert_eq( check, 2 ); 00077 } 00078 00079 Test addContext() { 00080 wex::AddContext ctx( "toplevel context" ); 00081 int check = -1; 00082 try { 00083 wex::AddContext ctx( "first context" ); 00084 check = 0; 00085 { 00086 wex::AddContext ctx( "second context" ); 00087 throw wex::Generic( "foobar" ); 00088 } 00089 } catch( wex::Generic &e ) { 00090 assert_eq( e.formatContext(), "toplevel context, \n " 00091 "first context, \n " 00092 "second context, \n " 00093 "foobar" ); 00094 check = 2; 00095 } 00096 assert_eq( check, 2 ); 00097 } 00098 }; 00099 00100 // vim:set ts=4 sw=4: