wibble  1.1
exception.test.h
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * Generic base exception hierarchy
3  *
4  * Copyright (C) 2003,2004,2005,2006 Enrico Zini <enrico@debian.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #include <wibble/test.h>
22 #include <wibble/exception.h>
23 #include <errno.h>
24 #include <unistd.h>
25 
26 using namespace std;
27 namespace wex = wibble::exception;
28 
29 struct TestException {
30  Test generic()
31  {
32  try {
33  throw wex::Generic("antani");
34  } catch ( std::exception& e ) {
35  assert(string(e.what()).find("antani") != string::npos);
36  }
37 
38  try {
39  throw wex::Generic("antani");
40  } catch ( wex::Generic& e ) {
41  assert(e.fullInfo().find("antani") != string::npos);
42  }
43  }
44 
46  {
47 #ifdef POSIX
48  try {
49  assert_eq(access("does-not-exist", F_OK), -1);
50  throw wex::System("checking for existance of nonexisting file");
51  } catch ( wibble::exception::System& e ) {
52  // Check that we caught the right value of errno
53  assert_eq(e.code(), ENOENT);
54  }
55 
56  try {
57  assert_eq(access("does-not-exist", F_OK), -1);
58  throw wex::File("does-not-exist", "checking for existance of nonexisting file");
59  } catch ( wex::File& e ) {
60  // Check that we caught the right value of errno
61  assert_eq(e.code(), ENOENT);
62  assert(e.fullInfo().find("does-not-exist") != string::npos);
63  }
64 #endif
65  }
66 
68  {
69  int check = -1;
70  try {
71  check = 0;
73  check = 1;
74  } catch ( wex::BadCast& e ) {
75  assert_eq( e.fullInfo(),
76  "bad cast: from i to PKc. Context:\n test" );
77  check = 2;
78  }
79  assert_eq( check, 2 );
80  }
81 
83  wex::AddContext ctx( "toplevel context" );
84  int check = -1;
85  try {
86  wex::AddContext ctx( "first context" );
87  check = 0;
88  {
89  wex::AddContext ctx( "second context" );
90  throw wex::Generic( "foobar" );
91  }
92  } catch( wex::Generic &e ) {
93  assert_eq( e.formatContext(), "toplevel context, \n "
94  "first context, \n "
95  "second context, \n "
96  "foobar" );
97  check = 2;
98  }
99  assert_eq( check, 2 );
100  }
101 };
102 
103 // vim:set ts=4 sw=4:
std::string formatContext() const
Definition: exception.h:154
Base class for exceptions for file I/O.
Definition: exception.h:421
Base class for all exceptions.
Definition: exception.h:180
virtual const std::string & fullInfo() const
Format in a string all available information about the exception.
Definition: exception.h:205
Base class for system exceptions.
Definition: exception.h:397
virtual int code() const
Get the system error code associated to the exception.
Definition: exception.h:408
Definition: core.h:11
bool access(const std::string &s, int m)
access() a filename
Definition: exception.test.h:29
Test system()
Definition: exception.test.h:45
Test addContext()
Definition: exception.test.h:82
Test badCast()
Definition: exception.test.h:67
Definition: exception.h:106
Definition: exception.h:288
Definition: exception.h:274
void Test
Definition: test.h:178
#define assert_eq(x, y)
Definition: test.h:33
#define assert(x)
Definition: test.h:30