File indexing completed on 2025-01-18 09:29:23
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_BEAST_TEST_HANDLER_HPP
0011 #define BOOST_BEAST_TEST_HANDLER_HPP
0012
0013 #include <boost/beast/_experimental/unit_test/suite.hpp>
0014 #include <boost/beast/core/error.hpp>
0015 #include <boost/asio/io_context.hpp>
0016 #include <boost/core/exchange.hpp>
0017 #include <boost/optional.hpp>
0018
0019 namespace boost {
0020 namespace beast {
0021 namespace test {
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 class handler
0035 {
0036 boost::optional<error_code> ec_;
0037 bool pass_ = false;
0038 boost::source_location loc_{BOOST_CURRENT_LOCATION};
0039 public:
0040 handler(boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc) {}
0041
0042 explicit
0043 handler(error_code ec, boost::source_location loc = BOOST_CURRENT_LOCATION)
0044 : ec_(ec), loc_(loc)
0045 {
0046 }
0047
0048 explicit
0049 handler(boost::none_t, boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc)
0050 {
0051 }
0052
0053 handler(handler&& other)
0054 : ec_(other.ec_)
0055 , pass_(boost::exchange(other.pass_, true))
0056 , loc_(other.loc_)
0057
0058 {
0059 }
0060
0061 ~handler()
0062 {
0063 ::boost::beast::unit_test::suite::this_suite()->expect(pass_, loc_.file_name(), loc_.line());
0064 }
0065
0066 template<class... Args>
0067 void
0068 operator()(error_code ec, Args&&...)
0069 {
0070 ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
0071 if (ec_ && ec != *ec_)
0072 ::boost::beast::unit_test::suite::this_suite()->fail(ec.message(), loc_.file_name(), loc_.line());
0073 else
0074 ::boost::beast::unit_test::suite::this_suite()->pass();
0075 pass_ = true;
0076 }
0077
0078 void
0079 operator()()
0080 {
0081 ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
0082 if (ec_ && ec_->failed())
0083 ::boost::beast::unit_test::suite::this_suite()->fail(ec_->message(), loc_.file_name(), loc_.line());
0084 else
0085 ::boost::beast::unit_test::suite::this_suite()->pass();
0086
0087 pass_ = true;
0088 }
0089
0090 template<class Arg0, class... Args,
0091 class = typename std::enable_if<
0092 ! std::is_convertible<Arg0, error_code>::value>::type>
0093 void
0094 operator()(Arg0&&, Args&&...)
0095 {
0096 ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
0097 if (ec_ && ec_->failed())
0098 ::boost::beast::unit_test::suite::this_suite()->fail(ec_->message(), loc_.file_name(), loc_.line());
0099 else
0100 ::boost::beast::unit_test::suite::this_suite()->pass();
0101 pass_ = true;
0102 }
0103 };
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 inline
0116 handler
0117 success_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
0118 {
0119 return handler(error_code{}, loc);
0120 }
0121
0122
0123
0124
0125
0126
0127
0128
0129 inline
0130 handler
0131 any_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
0132 {
0133 return handler(boost::none, loc);
0134 }
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 inline
0149 handler
0150 fail_handler(error_code ec,boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
0151 {
0152 return handler(ec, loc);
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164 inline
0165 void
0166 run(net::io_context& ioc)
0167 {
0168 ioc.run();
0169 ioc.restart();
0170 }
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185 template<class Rep, class Period>
0186 void
0187 run_for(
0188 net::io_context& ioc,
0189 std::chrono::duration<Rep, Period> elapsed)
0190 {
0191 ioc.run_for(elapsed);
0192 ioc.restart();
0193 }
0194
0195 }
0196 }
0197 }
0198
0199 #endif