Skip to content

Commit ef29ae0

Browse files
committed
test: Introduce header-only test framework
Adds src/test/util/framework.hpp as a lightweight Boost.Test replacement, along with CMake and main.cpp changes needed to build against it. Includes: - `CHECK`, valid with any comparison operator, optional message - `REQUIRE`, valid with any comparison operator, optional message - `CHECK_EQUAL_RANGES`, better debugging for vectors - `THROW_*`, macros for checking throwing conditions - Info and warn messages Includes a scripted diff to replace all BOOST macros with with new counterparts.
1 parent 9b4993c commit ef29ae0

130 files changed

Lines changed: 9161 additions & 8664 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ipc/test/ipc_test.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
#include <kj/test.h>
2424
#include <stdexcept>
2525

26-
#include <boost/test/unit_test.hpp>
27-
26+
#include <test/util/framework.hpp>
2827
static_assert(ipc::capnp::messages::MAX_MONEY == MAX_MONEY);
2928
static_assert(ipc::capnp::messages::MAX_DOUBLE == std::numeric_limits<double>::max());
3029
static_assert(ipc::capnp::messages::DEFAULT_BLOCK_RESERVED_WEIGHT == DEFAULT_BLOCK_RESERVED_WEIGHT);
@@ -47,8 +46,8 @@ static std::string TempPath(std::string_view pattern)
4746
std::string temp{fs::PathToString(fs::path{fs::temp_directory_path()} / fs::PathFromString(std::string{pattern}))};
4847
temp.push_back('\0');
4948
int fd{mkstemp(temp.data())};
50-
BOOST_CHECK_GE(fd, 0);
51-
BOOST_CHECK_EQUAL(close(fd), 0);
49+
CHECK(fd >= 0);
50+
CHECK(close(fd) == 0);
5251
temp.resize(temp.size() - 1);
5352
fs::remove(fs::PathFromString(temp));
5453
return temp;
@@ -87,17 +86,17 @@ void IpcPipeTest()
8786
std::unique_ptr<mp::ProxyClient<gen::FooInterface>> foo{foo_promise.get_future().get()};
8887

8988
// Test: make sure arguments were sent and return value is received
90-
BOOST_CHECK_EQUAL(foo->add(1, 2), 3);
89+
CHECK(foo->add(1, 2) == 3);
9190

9291
COutPoint txout1{Txid::FromUint256(uint256{100}), 200};
9392
COutPoint txout2{foo->passOutPoint(txout1)};
94-
BOOST_CHECK(txout1 == txout2);
93+
CHECK((txout1 == txout2));
9594

9695
UniValue uni1{UniValue::VOBJ};
9796
uni1.pushKV("i", 1);
9897
uni1.pushKV("s", "two");
9998
UniValue uni2{foo->passUniValue(uni1)};
100-
BOOST_CHECK_EQUAL(uni1.write(), uni2.write());
99+
CHECK(uni1.write() == uni2.write());
101100

102101
CMutableTransaction mtx;
103102
mtx.version = 2;
@@ -106,15 +105,15 @@ void IpcPipeTest()
106105
mtx.vout.emplace_back(COIN, CScript());
107106
CTransactionRef tx1{MakeTransactionRef(mtx)};
108107
CTransactionRef tx2{foo->passTransaction(tx1)};
109-
BOOST_CHECK(*Assert(tx1) == *Assert(tx2));
108+
CHECK((*Assert(tx1) == *Assert(tx2)));
110109

111110
std::vector<char> vec1{'H', 'e', 'l', 'l', 'o'};
112111
std::vector<char> vec2{foo->passVectorChar(vec1)};
113-
BOOST_CHECK_EQUAL(std::string_view(vec1.begin(), vec1.end()), std::string_view(vec2.begin(), vec2.end()));
112+
CHECK(std::string_view(vec1.begin(), vec1.end()) == std::string_view(vec2.begin(), vec2.end()));
114113

115114
auto script1{CScript() << OP_11};
116115
auto script2{foo->passScript(script1)};
117-
BOOST_CHECK_EQUAL(HexStr(script1), HexStr(script2));
116+
CHECK(HexStr(script1) == HexStr(script2));
118117

119118
// Test cleanup: disconnect and join thread
120119
foo.reset();
@@ -125,7 +124,7 @@ void IpcPipeTest()
125124
void IpcSocketPairTest()
126125
{
127126
int fds[2];
128-
BOOST_CHECK_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
127+
CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
129128
std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()};
130129
std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol()};
131130
std::promise<void> promise;
@@ -135,10 +134,10 @@ void IpcSocketPairTest()
135134
promise.get_future().wait();
136135
std::unique_ptr<interfaces::Init> remote_init{protocol->connect(fds[1], "test-connect")};
137136
std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
138-
BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
137+
CHECK(remote_echo->echo("echo test") == "echo test");
139138
remote_echo.reset();
140139
remote_init->stop();
141-
BOOST_CHECK(static_cast<TestInit*>(init.get())->stop_called.load());
140+
CHECK(static_cast<TestInit*>(init.get())->stop_called.load());
142141
remote_init.reset();
143142
thread.join();
144143
}
@@ -151,24 +150,24 @@ void IpcSocketTest(const fs::path& datadir)
151150
std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};
152151

153152
std::string invalid_bind{"invalid:"};
154-
BOOST_CHECK_THROW(process->bind(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
155-
BOOST_CHECK_THROW(process->connect(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
153+
CHECK_THROWS_AS(process->bind(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
154+
CHECK_THROWS_AS(process->connect(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
156155

157156
auto bind_and_listen{[&](const std::string& bind_address) {
158157
std::string address{bind_address};
159158
int serve_fd = process->bind(datadir, "test_bitcoin", address);
160-
BOOST_CHECK_GE(serve_fd, 0);
161-
BOOST_CHECK_EQUAL(address, bind_address);
159+
CHECK(serve_fd >= 0);
160+
CHECK(address == bind_address);
162161
protocol->listen(serve_fd, "test-serve", *init);
163162
}};
164163

165164
auto connect_and_test{[&](const std::string& connect_address) {
166165
std::string address{connect_address};
167166
int connect_fd{process->connect(datadir, "test_bitcoin", address)};
168-
BOOST_CHECK_EQUAL(address, connect_address);
167+
CHECK(address == connect_address);
169168
std::unique_ptr<interfaces::Init> remote_init{protocol->connect(connect_fd, "test-connect")};
170169
std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
171-
BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
170+
CHECK(remote_echo->echo("echo test") == "echo test");
172171
}};
173172

174173
// Need to specify explicit socket addresses outside the data directory, because the data

src/ipc/test/ipc_tests.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@
77

88
#include <test/util/common.h>
99
#include <test/util/setup_common.h>
10-
#include <boost/test/unit_test.hpp>
11-
12-
BOOST_FIXTURE_TEST_SUITE(ipc_tests, BasicTestingSetup)
13-
BOOST_AUTO_TEST_CASE(ipc_tests)
10+
#include <test/util/framework.hpp>
11+
TEST_SUITE_BEGIN("ipc_tests")
12+
FIXTURE_TEST_CASE("ipc_tests", BasicTestingSetup)
1413
{
1514
IpcPipeTest();
1615
IpcSocketPairTest();
1716
IpcSocketTest(m_args.GetDataDirNet());
1817
}
1918

2019
// Test address parsing.
21-
BOOST_AUTO_TEST_CASE(parse_address_test)
20+
FIXTURE_TEST_CASE("parse_address_test", BasicTestingSetup)
2221
{
2322
std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};
2423
fs::path datadir{"/var/empty/notexist"};
2524
auto check_notexist{[](const std::system_error& e) { return e.code() == std::errc::no_such_file_or_directory; }};
2625
auto check_address{[&](std::string address, std::string expect_address, std::string expect_error) {
2726
if (expect_error.empty()) {
28-
BOOST_CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::system_error, check_notexist);
27+
CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::system_error, check_notexist);
2928
} else {
30-
BOOST_CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::invalid_argument, HasReason(expect_error));
29+
CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::invalid_argument, HasReason(expect_error));
3130
}
32-
BOOST_CHECK_EQUAL(address, expect_address);
31+
CHECK(address == expect_address);
3332
}};
3433
check_address("unix", "unix:/var/empty/notexist/test_bitcoin.sock", "");
3534
check_address("unix:", "unix:/var/empty/notexist/test_bitcoin.sock", "");
@@ -40,4 +39,4 @@ BOOST_AUTO_TEST_CASE(parse_address_test)
4039
check_address("invalid", "invalid", "Unrecognized address 'invalid'");
4140
}
4241

43-
BOOST_AUTO_TEST_SUITE_END()
42+
TEST_SUITE_END()

src/test/CMakeLists.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,21 @@ target_link_libraries(test_bitcoin
160160

161161
add_subdirectory(${PROJECT_SOURCE_DIR}/src/ipc/test ipc)
162162

163-
function(add_boost_test source_file)
163+
function(register_test_suite source_file)
164164
if(NOT EXISTS ${source_file})
165165
return()
166166
endif()
167167

168168
file(READ "${source_file}" source_file_content)
169169
string(REGEX
170-
MATCHALL "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(([A-Za-z0-9_]+)"
170+
MATCHALL "TEST_SUITE_BEGIN\\(\"[A-Za-z0-9_]+\""
171171
test_suite_macro "${source_file_content}"
172172
)
173-
list(TRANSFORM test_suite_macro
174-
REPLACE "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(" ""
175-
)
173+
list(TRANSFORM test_suite_macro REPLACE "^TEST_SUITE_BEGIN\\(\"" "")
174+
list(TRANSFORM test_suite_macro REPLACE "\"$" "")
176175
foreach(test_suite_name IN LISTS test_suite_macro)
177176
add_test(NAME ${test_suite_name}
178-
COMMAND test_bitcoin --run_test=${test_suite_name} --catch_system_error=no --log_level=test_suite -- -printtoconsole=1
177+
COMMAND test_bitcoin --run_test=${test_suite_name} -- -printtoconsole=1
179178
)
180179
set_property(TEST ${test_suite_name} PROPERTY
181180
SKIP_REGULAR_EXPRESSION
@@ -194,7 +193,7 @@ function(add_all_test_targets)
194193
if(result)
195194
cmake_path(APPEND test_source_dir ${test_source} OUTPUT_VARIABLE test_source)
196195
endif()
197-
add_boost_test(${test_source})
196+
register_test_suite(${test_source})
198197
endforeach()
199198
endfunction()
200199

0 commit comments

Comments
 (0)