Bitcoin Core 28.99.0
P2P Digital Currency
result_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <util/result.h>
6
7#include <boost/test/unit_test.hpp>
8
9inline bool operator==(const bilingual_str& a, const bilingual_str& b)
10{
11 return a.original == b.original && a.translated == b.translated;
12}
13
14inline std::ostream& operator<<(std::ostream& os, const bilingual_str& s)
15{
16 return os << "bilingual_str('" << s.original << "' , '" << s.translated << "')";
17}
18
19BOOST_AUTO_TEST_SUITE(result_tests)
20
21struct NoCopy {
22 NoCopy(int n) : m_n{std::make_unique<int>(n)} {}
23 std::unique_ptr<int> m_n;
24};
25
26bool operator==(const NoCopy& a, const NoCopy& b)
27{
28 return *a.m_n == *b.m_n;
29}
30
31std::ostream& operator<<(std::ostream& os, const NoCopy& o)
32{
33 return os << "NoCopy(" << *o.m_n << ")";
34}
35
36util::Result<int> IntFn(int i, bool success)
37{
38 if (success) return i;
39 return util::Error{Untranslated(strprintf("int %i error.", i))};
40}
41
43{
44 if (success) return s;
45 return util::Error{Untranslated(strprintf("str %s error.", s.original))};
46}
47
48util::Result<NoCopy> NoCopyFn(int i, bool success)
49{
50 if (success) return {i};
51 return util::Error{Untranslated(strprintf("nocopy %i error.", i))};
52}
53
54template <typename T>
55void ExpectResult(const util::Result<T>& result, bool success, const bilingual_str& str)
56{
57 BOOST_CHECK_EQUAL(bool(result), success);
59}
60
61template <typename T, typename... Args>
62void ExpectSuccess(const util::Result<T>& result, const bilingual_str& str, Args&&... args)
63{
64 ExpectResult(result, true, str);
65 BOOST_CHECK_EQUAL(result.has_value(), true);
66 T expected{std::forward<Args>(args)...};
67 BOOST_CHECK_EQUAL(result.value(), expected);
68 BOOST_CHECK_EQUAL(&result.value(), &*result);
69}
70
71template <typename T, typename... Args>
72void ExpectFail(const util::Result<T>& result, const bilingual_str& str)
73{
74 ExpectResult(result, false, str);
75}
76
77BOOST_AUTO_TEST_CASE(check_returned)
78{
79 ExpectSuccess(IntFn(5, true), {}, 5);
80 ExpectFail(IntFn(5, false), Untranslated("int 5 error."));
81 ExpectSuccess(NoCopyFn(5, true), {}, 5);
82 ExpectFail(NoCopyFn(5, false), Untranslated("nocopy 5 error."));
83 ExpectSuccess(StrFn(Untranslated("S"), true), {}, Untranslated("S"));
84 ExpectFail(StrFn(Untranslated("S"), false), Untranslated("str S error."));
85}
86
87BOOST_AUTO_TEST_CASE(check_value_or)
88{
89 BOOST_CHECK_EQUAL(IntFn(10, true).value_or(20), 10);
90 BOOST_CHECK_EQUAL(IntFn(10, false).value_or(20), 20);
91 BOOST_CHECK_EQUAL(NoCopyFn(10, true).value_or(20), 10);
92 BOOST_CHECK_EQUAL(NoCopyFn(10, false).value_or(20), 20);
93 BOOST_CHECK_EQUAL(StrFn(Untranslated("A"), true).value_or(Untranslated("B")), Untranslated("A"));
94 BOOST_CHECK_EQUAL(StrFn(Untranslated("A"), false).value_or(Untranslated("B")), Untranslated("B"));
95}
96
ArgsManager & args
Definition: bitcoind.cpp:277
bool has_value() const noexcept
std::optional methods, so functions returning optional<T> can change to return Result<T> with minimal...
Definition: result.h:64
const T & value() const LIFETIMEBOUND
Definition: result.h:65
BOOST_AUTO_TEST_SUITE_END()
#define T(expected, seed, data)
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:93
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
util::Result< int > IntFn(int i, bool success)
void ExpectFail(const util::Result< T > &result, const bilingual_str &str)
void ExpectResult(const util::Result< T > &result, bool success, const bilingual_str &str)
bool operator==(const bilingual_str &a, const bilingual_str &b)
Definition: result_tests.cpp:9
std::ostream & operator<<(std::ostream &os, const bilingual_str &s)
void ExpectSuccess(const util::Result< T > &result, const bilingual_str &str, Args &&... args)
util::Result< bilingual_str > StrFn(bilingual_str s, bool success)
BOOST_AUTO_TEST_CASE(check_returned)
util::Result< NoCopy > NoCopyFn(int i, bool success)
std::unique_ptr< int > m_n
NoCopy(int n)
Bilingual messages:
Definition: translation.h:21
std::string translated
Definition: translation.h:23
std::string original
Definition: translation.h:22
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:51