test-handlers use source location

This commit is contained in:
Klemens Morgenstern
2022-10-10 16:23:27 +08:00
committed by Klemens Morgenstern
parent 1a02413638
commit b30e4fbbda

View File

@@ -35,47 +35,55 @@ class handler
{ {
boost::optional<error_code> ec_; boost::optional<error_code> ec_;
bool pass_ = false; bool pass_ = false;
boost::source_location loc_{BOOST_CURRENT_LOCATION};
public: public:
handler() = default; handler(boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc) {}
explicit explicit
handler(error_code ec) handler(error_code ec, boost::source_location loc = BOOST_CURRENT_LOCATION)
: ec_(ec) : ec_(ec), loc_(loc)
{ {
} }
explicit explicit
handler(boost::none_t) handler(boost::none_t, boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc)
{ {
} }
handler(handler&& other) handler(handler&& other,boost::source_location loc = BOOST_CURRENT_LOCATION)
: ec_(other.ec_) : ec_(other.ec_)
, pass_(boost::exchange(other.pass_, true)) , pass_(boost::exchange(other.pass_, true))
, loc_(loc)
{ {
} }
~handler() ~handler()
{ {
BEAST_EXPECT(pass_); ::boost::beast::unit_test::suite::this_suite()->expect(pass_, loc_.file_name(), loc_.line());
} }
template<class... Args> template<class... Args>
void void
operator()(error_code ec, Args&&...) operator()(error_code ec, Args&&...)
{ {
BEAST_EXPECT(! pass_); // can't call twice ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
BEAST_EXPECTS(! ec_ || ec == *ec_, if (ec_ && ec != *ec_)
ec.message()); ::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; pass_ = true;
} }
void void
operator()() operator()()
{ {
BEAST_EXPECT(! pass_); // can't call twice ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
BEAST_EXPECT(! ec_); 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; pass_ = true;
} }
@@ -85,8 +93,11 @@ public:
void void
operator()(Arg0&&, Args&&...) operator()(Arg0&&, Args&&...)
{ {
BEAST_EXPECT(! pass_); // can't call twice ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
BEAST_EXPECT(! ec_); 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; pass_ = true;
} }
}; };
@@ -103,9 +114,9 @@ public:
*/ */
inline inline
handler handler
success_handler() noexcept success_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
{ {
return handler(error_code{}); return handler(error_code{}, loc);
} }
/** Return a test CompletionHandler which requires invocation. /** Return a test CompletionHandler which requires invocation.
@@ -117,9 +128,9 @@ success_handler() noexcept
*/ */
inline inline
handler handler
any_handler() noexcept any_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
{ {
return handler(boost::none); return handler(boost::none, loc);
} }
/** Return a test CompletionHandler which requires a specific error code. /** Return a test CompletionHandler which requires a specific error code.
@@ -136,9 +147,9 @@ any_handler() noexcept
*/ */
inline inline
handler handler
fail_handler(error_code ec) noexcept fail_handler(error_code ec,boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
{ {
return handler(ec); return handler(ec, loc);
} }
/** Run an I/O context. /** Run an I/O context.