wibble  0.1.28
exception.test.h
Go to the documentation of this file.
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 #include <unistd.h>
00025 
00026 using namespace std;
00027 namespace wex = wibble::exception;
00028 
00029 struct TestException {
00030     Test generic()
00031     {
00032         try {
00033             throw wex::Generic("antani");
00034         } catch ( std::exception& e ) {
00035             assert(string(e.what()).find("antani") != string::npos);
00036         }
00037         
00038         try {
00039             throw wex::Generic("antani");
00040         } catch ( wex::Generic& e ) {
00041             assert(e.fullInfo().find("antani") != string::npos);
00042         }
00043     }
00044 
00045     Test system() 
00046     {
00047         try {
00048             assert_eq(access("does-not-exist", F_OK), -1);
00049             throw wex::System("checking for existance of nonexisting file");
00050         } catch ( wibble::exception::System& e ) {
00051             // Check that we caught the right value of errno
00052             assert_eq(e.code(), ENOENT);
00053         }
00054         
00055         try {
00056             assert_eq(access("does-not-exist", F_OK), -1);
00057             throw wex::File("does-not-exist", "checking for existance of nonexisting file");
00058         } catch ( wex::File& e ) {
00059             // Check that we caught the right value of errno
00060             assert_eq(e.code(), ENOENT);
00061             assert(e.fullInfo().find("does-not-exist") != string::npos);
00062         }
00063     }
00064 
00065     Test badCast()
00066     {
00067         int check = -1;
00068         try {
00069             check = 0;
00070             throw wex::BadCastExt< int, const char * >( "test" );
00071             check = 1;
00072         } catch ( wex::BadCast& e ) {
00073             assert_eq( e.fullInfo(),
00074                        "bad cast: from i to PKc. Context:\n    test" );
00075             check = 2;
00076         }
00077         assert_eq( check, 2 );
00078     }
00079 
00080     Test addContext() {
00081         wex::AddContext ctx( "toplevel context" );
00082         int check = -1;
00083         try {
00084             wex::AddContext ctx( "first context" );
00085             check = 0;
00086             {
00087                 wex::AddContext ctx( "second context" );
00088                 throw wex::Generic( "foobar" );
00089             }
00090         } catch( wex::Generic &e ) {
00091             assert_eq( e.formatContext(), "toplevel context, \n    "
00092                        "first context, \n    "
00093                        "second context, \n    "
00094                        "foobar" );
00095             check = 2;
00096         }
00097         assert_eq( check, 2 );
00098     }
00099 };
00100 
00101 // vim:set ts=4 sw=4: