Skip to content

Commit

Permalink
test: generalize HasReason and use it in FailFmtWithError
Browse files Browse the repository at this point in the history
Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com>
  • Loading branch information
l0rinc and hodlinator committed Sep 19, 2024
1 parent ab0b570 commit 6c3c619
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/test/util/setup_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/test/util_string_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <util/string.h>

#include <boost/test/unit_test.hpp>
#include <test/util/setup_common.h>

using namespace util;

Expand All @@ -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)
Expand Down

0 comments on commit 6c3c619

Please sign in to comment.