wibble
1.1
wibble
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
#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
67
Test
badCast
()
68
{
69
int
check = -1;
70
try
{
71
check = 0;
72
throw
wex::BadCastExt< int, const char * >
(
"test"
);
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
82
Test
addContext
() {
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:
TestException::system
Test system()
Definition:
exception.test.h:45
wibble::exception::BadCast
Definition:
exception.h:273
wibble::exception::Context::formatContext
std::string formatContext() const
Definition:
exception.h:154
TestException::badCast
Test badCast()
Definition:
exception.test.h:67
wibble::exception::System::code
virtual int code() const
Get the system error code associated to the exception.
Definition:
exception.h:408
wibble::sys::fs::access
bool access(const std::string &s, int m)
access() a filename
test.h
assert_eq
#define assert_eq(x, y)
Definition:
test.h:33
wibble::exception::Generic::fullInfo
virtual const std::string & fullInfo() const
Format in a string all available information about the exception.
Definition:
exception.h:205
wibble::exception::System
Base class for system exceptions.
Definition:
exception.h:396
wibble::exception::Generic
Base class for all exceptions.
Definition:
exception.h:179
exception.h
wibble::exception::AddContext
Definition:
exception.h:106
TestException
Definition:
exception.test.h:29
wibble::exception::BadCastExt
Definition:
exception.h:287
Test
void Test
Definition:
test.h:178
wibble::exception
Definition:
core.h:11
TestException::addContext
Test addContext()
Definition:
exception.test.h:82
assert
#define assert(x)
Definition:
test.h:30
wibble::exception::File
Base class for exceptions for file I/O.
Definition:
exception.h:420
Generated by
1.8.17