Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / spirit / phoenix / test / statement / exceptions.cpp
1 /*=============================================================================
2     Copyright (c) 2005-2007 Dan Marsden
3     Copyright (c) 2005-2007 Joel de Guzman
4
5     Distributed under the Boost Software License, Version 1.0. (See accompanying 
6     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8
9 #include <stdexcept>
10 #include <string>
11
12 #include <boost/spirit/include/phoenix_core.hpp>
13 #include <boost/spirit/include/phoenix_operator.hpp>
14 #include <boost/spirit/include/phoenix_statement.hpp>
15
16 #include <boost/detail/lightweight_test.hpp>
17
18 int main()
19 {
20     using namespace boost::phoenix;
21     using namespace boost::phoenix::arg_names;
22     using namespace std;
23
24     {
25         try
26         {
27             throw_(runtime_error("error"))();
28             BOOST_ERROR("exception should have been thrown");
29         }
30         catch(runtime_error& err)
31         {
32             BOOST_TEST(err.what() == string("error"));
33         }
34     }
35
36     {
37         try
38         {
39             try
40             {
41                 throw runtime_error("error");
42             }
43             catch(exception&)
44             {
45                 throw_()();
46                 BOOST_ERROR("exception should have been rethrown");
47             }
48         }
49         catch(exception& err)
50         {
51             BOOST_TEST(err.what() == string("error"));
52         }
53     }
54
55     {
56         bool caught_exception = false;
57
58         try_
59         [ throw_(runtime_error("error")) ]
60         .catch_<exception>()
61         [ ref(caught_exception) = true ]();
62
63         BOOST_TEST(caught_exception);
64     }
65
66     {
67         bool caught_exception = false;
68         try_
69         [ throw_(runtime_error("error")) ]
70         .catch_all
71         [ ref(caught_exception) = true ]();
72         BOOST_TEST(caught_exception);
73     }
74
75     {
76         bool caught_correct_exception = false;
77         try_
78             [ throw_(runtime_error("error")) ]
79         .catch_<string>()
80             [ ref(caught_correct_exception) = false ]
81         .catch_<exception>()
82             [ ref(caught_correct_exception) = true]();
83
84         BOOST_TEST(caught_correct_exception);
85     }
86
87     {
88         bool caught_correct_exception = false;
89         try_
90             [ throw_(runtime_error("error")) ]
91         .catch_<string>()
92             [ ref(caught_correct_exception) = false ]
93         .catch_all
94             [ ref(caught_correct_exception) = true]();
95
96         BOOST_TEST(caught_correct_exception);
97     }
98
99     return boost::report_errors();
100 }