wibble  0.1.28
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 
45  Test system()
46  {
47  try {
48  assert_eq(access("does-not-exist", F_OK), -1);
49  throw wex::System("checking for existance of nonexisting file");
50  } catch ( wibble::exception::System& e ) {
51  // Check that we caught the right value of errno
52  assert_eq(e.code(), ENOENT);
53  }
54 
55  try {
56  assert_eq(access("does-not-exist", F_OK), -1);
57  throw wex::File("does-not-exist", "checking for existance of nonexisting file");
58  } catch ( wex::File& e ) {
59  // Check that we caught the right value of errno
60  assert_eq(e.code(), ENOENT);
61  assert(e.fullInfo().find("does-not-exist") != string::npos);
62  }
63  }
64 
65  Test badCast()
66  {
67  int check = -1;
68  try {
69  check = 0;
71  check = 1;
72  } catch ( wex::BadCast& e ) {
73  assert_eq( e.fullInfo(),
74  "bad cast: from i to PKc. Context:\n test" );
75  check = 2;
76  }
77  assert_eq( check, 2 );
78  }
79 
81  wex::AddContext ctx( "toplevel context" );
82  int check = -1;
83  try {
84  wex::AddContext ctx( "first context" );
85  check = 0;
86  {
87  wex::AddContext ctx( "second context" );
88  throw wex::Generic( "foobar" );
89  }
90  } catch( wex::Generic &e ) {
91  assert_eq( e.formatContext(), "toplevel context, \n "
92  "first context, \n "
93  "second context, \n "
94  "foobar" );
95  check = 2;
96  }
97  assert_eq( check, 2 );
98  }
99 };
100 
101 // vim:set ts=4 sw=4: