From 6c3c619b35cc03e883f9d2b3326f906aedde9ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Wed, 18 Sep 2024 13:40:23 +0200 Subject: [PATCH] test: generalize HasReason and use it in FailFmtWithError Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com> --- src/test/util/setup_common.h | 8 +++----- src/test/util_string_tests.cpp | 5 ++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index 30d4280fa5..f5a3c0dcee 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -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; diff --git a/src/test/util_string_tests.cpp b/src/test/util_string_tests.cpp index 8b974ffe6f..1574fe2358 100644 --- a/src/test/util_string_tests.cpp +++ b/src/test/util_string_tests.cpp @@ -5,6 +5,7 @@ #include #include +#include using namespace util; @@ -21,9 +22,7 @@ inline void PassFmt(util::ConstevalFormatString fmt) template 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::Detail_CheckNumFormatSpecifiers(wrong_fmt), ErrType, check_throw); + BOOST_CHECK_EXCEPTION(util::ConstevalFormatString::Detail_CheckNumFormatSpecifiers(wrong_fmt), const char*, HasReason(error)); } BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)