123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #ifndef BOOST_BEAST_TEST_HANDLER_HPP
- #define BOOST_BEAST_TEST_HANDLER_HPP
- #include <boost/beast/_experimental/unit_test/suite.hpp>
- #include <boost/beast/core/error.hpp>
- #include <boost/asio/io_context.hpp>
- #include <boost/core/exchange.hpp>
- #include <boost/optional.hpp>
- namespace boost {
- namespace beast {
- namespace test {
- class handler
- {
- boost::optional<error_code> ec_;
- bool pass_ = false;
- boost::source_location loc_{BOOST_CURRENT_LOCATION};
- public:
- handler(boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc) {}
- explicit
- handler(error_code ec, boost::source_location loc = BOOST_CURRENT_LOCATION)
- : ec_(ec), loc_(loc)
- {
- }
- explicit
- handler(boost::none_t, boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc)
- {
- }
- handler(handler&& other)
- : ec_(other.ec_)
- , pass_(boost::exchange(other.pass_, true))
- , loc_(other.loc_)
- {
- }
- ~handler()
- {
- ::boost::beast::unit_test::suite::this_suite()->expect(pass_, loc_.file_name(), loc_.line());
- }
- template<class... Args>
- void
- operator()(error_code ec, Args&&...)
- {
- ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
- if (ec_ && ec != *ec_)
- ::boost::beast::unit_test::suite::this_suite()->fail(ec.message(), loc_.file_name(), loc_.line());
- else
- ::boost::beast::unit_test::suite::this_suite()->pass();
- pass_ = true;
- }
- void
- operator()()
- {
- ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
- if (ec_ && ec_->failed())
- ::boost::beast::unit_test::suite::this_suite()->fail(ec_->message(), loc_.file_name(), loc_.line());
- else
- ::boost::beast::unit_test::suite::this_suite()->pass();
- pass_ = true;
- }
- template<class Arg0, class... Args,
- class = typename std::enable_if<
- ! std::is_convertible<Arg0, error_code>::value>::type>
- void
- operator()(Arg0&&, Args&&...)
- {
- ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
- if (ec_ && ec_->failed())
- ::boost::beast::unit_test::suite::this_suite()->fail(ec_->message(), loc_.file_name(), loc_.line());
- else
- ::boost::beast::unit_test::suite::this_suite()->pass();
- pass_ = true;
- }
- };
- inline
- handler
- success_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
- {
- return handler(error_code{}, loc);
- }
- inline
- handler
- any_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
- {
- return handler(boost::none, loc);
- }
- inline
- handler
- fail_handler(error_code ec,boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
- {
- return handler(ec, loc);
- }
- inline
- void
- run(net::io_context& ioc)
- {
- ioc.run();
- ioc.restart();
- }
- template<class Rep, class Period>
- void
- run_for(
- net::io_context& ioc,
- std::chrono::duration<Rep, Period> elapsed)
- {
- ioc.run_for(elapsed);
- ioc.restart();
- }
- }
- }
- }
- #endif
|