-
Notifications
You must be signed in to change notification settings - Fork 6
/
expectexception.h
47 lines (39 loc) · 1.5 KB
/
expectexception.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef EXPECTEXCEPTION_H
#define EXPECTEXCEPTION_H
#include "backtrace.h"
#include "demangle.h"
/**
* The EXPECT_EXCEPTION macro should ensure that an expression throws an exception of a given type.
*/
class expected_exception: virtual public boost::exception, virtual public std::exception {};
class unexpected_exception: virtual public boost::exception, virtual public std::exception {};
struct unexpected_exception_tag {};
typedef boost::error_info<struct unexpected_exception_tag,boost::exception_ptr> unexpected_exception_info;
struct tag_expected_exception_type {};
typedef boost::error_info<struct tag_expected_exception_type,std::type_info const *> expected_exception_type;
inline
std::string
to_string( expected_exception_type const & x )
{
return demangle(*x.value());
}
#define EXPECT_EXCEPTION(X,F) do { \
bool thrown = false; \
\
try { \
F; \
} catch (const X&) { \
thrown = true; \
} catch (const std::exception&) { \
BOOST_THROW_EXCEPTION(unexpected_exception {}\
<< unexpected_exception_info {boost::current_exception ()} \
<< Backtrace::make () \
<< expected_exception_type {&typeid(X)}); \
} \
\
if (!thrown) \
BOOST_THROW_EXCEPTION(expected_exception {} \
<< Backtrace::make () \
<< expected_exception_type {&typeid(X)}); \
} while(false);
#endif // EXPECTEXCEPTION_H