Skip to content

Commit 18d4c43

Browse files
committed
Merge bitcoin/bitcoin#30921: test: generalize HasReason and use it in FailFmtWithError
6c3c619 test: generalize HasReason and use it in FailFmtWithError (Lőrinc) Pull request description: Standardized boost exception checking in recent tests introduced in bitcoin/bitcoin#30546 (comment) 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 6c3c619 hodlinator: ACK 6c3c619 Tree-SHA512: 740fb18b8fea78e4eb9740ceb0fe75d37246c28cfa2638b9d093e9514dd6d7926cc5be9ec57f8027cca3aa9d616e8c54322d2401cfa67fd25282f7816e63532d
2 parents d7fcc91 + 6c3c619 commit 18d4c43

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/test/util/setup_common.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,9 @@ std::ostream& operator<<(std::ostream& os, const uint256& num);
274274
class HasReason
275275
{
276276
public:
277-
explicit HasReason(const std::string& reason) : m_reason(reason) {}
278-
bool operator()(const std::exception& e) const
279-
{
280-
return std::string(e.what()).find(m_reason) != std::string::npos;
281-
};
277+
explicit HasReason(std::string_view reason) : m_reason(reason) {}
278+
bool operator()(std::string_view s) const { return s.find(m_reason) != std::string_view::npos; }
279+
bool operator()(const std::exception& e) const { return (*this)(e.what()); }
282280

283281
private:
284282
const std::string m_reason;

src/test/util_string_tests.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <util/string.h>
66

77
#include <boost/test/unit_test.hpp>
8+
#include <test/util/setup_common.h>
89

910
using namespace util;
1011

@@ -21,9 +22,7 @@ inline void PassFmt(util::ConstevalFormatString<NumArgs> fmt)
2122
template <unsigned WrongNumArgs>
2223
inline void FailFmtWithError(std::string_view wrong_fmt, std::string_view error)
2324
{
24-
using ErrType = const char*;
25-
auto check_throw{[error](const ErrType& str) { return str == error; }};
26-
BOOST_CHECK_EXCEPTION(util::ConstevalFormatString<WrongNumArgs>::Detail_CheckNumFormatSpecifiers(wrong_fmt), ErrType, check_throw);
25+
BOOST_CHECK_EXCEPTION(util::ConstevalFormatString<WrongNumArgs>::Detail_CheckNumFormatSpecifiers(wrong_fmt), const char*, HasReason(error));
2726
}
2827

2928
BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)

0 commit comments

Comments
 (0)