Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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