Bitcoin Core 30.99.0
P2P Digital Currency
common.h
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_TEST_UTIL_COMMON_H
6#define BITCOIN_TEST_UTIL_COMMON_H
7
8#include <ostream>
9#include <optional>
10#include <string>
11
18{
19public:
20 explicit HasReason(std::string_view reason) : m_reason(reason) {}
21 bool operator()(std::string_view s) const { return s.find(m_reason) != std::string_view::npos; }
22 bool operator()(const std::exception& e) const { return (*this)(e.what()); }
23
24private:
25 const std::string m_reason;
26};
27
28// Make types usable in BOOST_CHECK_* @{
29namespace std {
30template <typename T> requires std::is_enum_v<T>
31inline std::ostream& operator<<(std::ostream& os, const T& e)
32{
33 return os << static_cast<std::underlying_type_t<T>>(e);
34}
35
36template <typename T>
37inline std::ostream& operator<<(std::ostream& os, const std::optional<T>& v)
38{
39 return v ? os << *v
40 : os << "std::nullopt";
41}
42} // namespace std
43
44template <typename T>
45concept HasToString = requires(const T& t) { t.ToString(); };
46
47template <HasToString T>
48inline std::ostream& operator<<(std::ostream& os, const T& obj)
49{
50 return os << obj.ToString();
51}
52
53// @}
54
55#endif // BITCOIN_TEST_UTIL_COMMON_H
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: common.h:18
bool operator()(std::string_view s) const
Definition: common.h:21
const std::string m_reason
Definition: common.h:25
bool operator()(const std::exception &e) const
Definition: common.h:22
HasReason(std::string_view reason)
Definition: common.h:20
#define T(expected, seed, data)
Definition: common.h:29
std::ostream & operator<<(std::ostream &os, const T &e)
Definition: common.h:31
std::ostream & operator<<(std::ostream &os, const T &obj)
Definition: common.h:48