test: refactor, decouple HasReason from test framework machinery

Avoid providing the entire unit test framework dependency to tests that only
require access to the HasReason utility class.

E.g. reverselock_tests.cpp, sync_tests.cpp, util_check_tests.cpp, util_string_tests.cpp,
and script_parse_tests.cpp only require access to HasReason and nothing else.
This commit is contained in:
furszy 2026-02-10 16:48:38 -05:00
parent dbbb780af0
commit d9c6769d03
No known key found for this signature in database
GPG Key ID: 5DD23CCC686AA623
12 changed files with 26 additions and 21 deletions

View File

@ -5,6 +5,7 @@
#include <ipc/process.h>
#include <ipc/test/ipc_test.h>
#include <test/util/common.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>

View File

@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <httpserver.h>
#include <test/util/common.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>

View File

@ -15,6 +15,7 @@
#include <serialize.h>
#include <span.h>
#include <streams.h>
#include <test/util/common.h>
#include <test/util/net.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>

View File

@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <sync.h>
#include <test/util/setup_common.h>
#include <test/util/common.h>
#include <boost/test/unit_test.hpp>

View File

@ -5,7 +5,7 @@
#include <core_io.h>
#include <script/script.h>
#include <util/strencodings.h>
#include <test/util/setup_common.h>
#include <test/util/common.h>
#include <boost/test/unit_test.hpp>

View File

@ -4,6 +4,7 @@
#include <common/system.h>
#include <compat/compat.h>
#include <test/util/common.h>
#include <test/util/setup_common.h>
#include <util/sock.h>
#include <util/threadinterrupt.h>

View File

@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <sync.h>
#include <test/util/setup_common.h>
#include <test/util/common.h>
#include <boost/test/unit_test.hpp>

View File

@ -6,6 +6,7 @@
#include <bitcoin-build-config.h> // IWYU pragma: keep
#include <common/run_command.h>
#include <test/util/common.h>
#include <test/util/setup_common.h>
#include <univalue.h>
#include <util/string.h>

View File

@ -9,6 +9,22 @@
#include <optional>
#include <string>
/**
* BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
* Use as
* BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
*/
class HasReason
{
public:
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;
};
// Make types usable in BOOST_CHECK_* @{
namespace std {
template <typename T> requires std::is_enum_v<T>

View File

@ -261,20 +261,4 @@ std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::
CBlock getBlock13b8a();
/**
* BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
* Use as
* BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
*/
class HasReason
{
public:
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;
};
#endif // BITCOIN_TEST_UTIL_SETUP_COMMON_H

View File

@ -5,7 +5,7 @@
#include <util/check.h>
#include <boost/test/unit_test.hpp>
#include <test/util/setup_common.h>
#include <test/util/common.h>
BOOST_AUTO_TEST_SUITE(util_check_tests)

View File

@ -8,7 +8,7 @@
#include <boost/test/unit_test.hpp>
#include <test/util/common.h>
#include <test/util/setup_common.h>
#include <tinyformat.h>
using namespace util;
using util::detail::CheckNumFormatSpecifiers;