Merge bitcoin/bitcoin#30921: test: generalize HasReason and use it in FailFmtWithError

6c3c619b35 test: generalize HasReason and use it in FailFmtWithError (Lőrinc)

Pull request description:

  Standardized boost exception checking in recent tests introduced in https://github.com/bitcoin/bitcoin/pull/30546#discussion_r1756493521 by extending `HasReason` to accept `const char*` through `string_view` in `operator()`.

  Note that `HasReason` only checks partial matches - but since we're specifying the whole error string, it doesn't affect us in this case.

ACKs for top commit:
  maflcko:
    review ACK 6c3c619b35
  hodlinator:
    ACK 6c3c619b35

Tree-SHA512: 740fb18b8fea78e4eb9740ceb0fe75d37246c28cfa2638b9d093e9514dd6d7926cc5be9ec57f8027cca3aa9d616e8c54322d2401cfa67fd25282f7816e63532d
This commit is contained in:
merge-script 2024-09-27 10:56:57 +01:00
commit 18d4c43cab
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1
2 changed files with 5 additions and 8 deletions

View File

@ -274,11 +274,9 @@ std::ostream& operator<<(std::ostream& os, const uint256& num);
class HasReason
{
public:
explicit HasReason(const std::string& reason) : m_reason(reason) {}
bool operator()(const std::exception& e) const
{
return std::string(e.what()).find(m_reason) != std::string::npos;
};
explicit HasReason(std::string_view reason) : m_reason(reason) {}
bool operator()(std::string_view s) const { return s.find(m_reason) != std::string_view::npos; }
bool operator()(const std::exception& e) const { return (*this)(e.what()); }
private:
const std::string m_reason;

View File

@ -5,6 +5,7 @@
#include <util/string.h>
#include <boost/test/unit_test.hpp>
#include <test/util/setup_common.h>
using namespace util;
@ -21,9 +22,7 @@ inline void PassFmt(util::ConstevalFormatString<NumArgs> fmt)
template <unsigned WrongNumArgs>
inline void FailFmtWithError(std::string_view wrong_fmt, std::string_view error)
{
using ErrType = const char*;
auto check_throw{[error](const ErrType& str) { return str == error; }};
BOOST_CHECK_EXCEPTION(util::ConstevalFormatString<WrongNumArgs>::Detail_CheckNumFormatSpecifiers(wrong_fmt), ErrType, check_throw);
BOOST_CHECK_EXCEPTION(util::ConstevalFormatString<WrongNumArgs>::Detail_CheckNumFormatSpecifiers(wrong_fmt), const char*, HasReason(error));
}
BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)