mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-03 09:43:55 +02:00
test: Add ASSERT_DEBUG_LOG to unit test framework
This commit is contained in:
32
src/test/lib/logging.cpp
Normal file
32
src/test/lib/logging.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2019 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <test/lib/logging.h>
|
||||
|
||||
#include <logging.h>
|
||||
#include <noui.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/memory.h>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
DebugLogHelper::DebugLogHelper(std::string message)
|
||||
: m_message{std::move(message)}
|
||||
{
|
||||
m_print_connection = LogInstance().PushBackCallback(
|
||||
[this](const std::string& s) {
|
||||
if (m_found) return;
|
||||
m_found = s.find(m_message) != std::string::npos;
|
||||
});
|
||||
noui_test_redirect();
|
||||
}
|
||||
|
||||
void DebugLogHelper::check_found()
|
||||
{
|
||||
noui_reconnect();
|
||||
LogInstance().DeleteCallback(m_print_connection);
|
||||
if (!m_found) {
|
||||
throw std::runtime_error(strprintf("'%s' not found in debug log\n", m_message));
|
||||
}
|
||||
}
|
||||
29
src/test/lib/logging.h
Normal file
29
src/test/lib/logging.h
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2019 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_TEST_LIB_LOGGING_H
|
||||
#define BITCOIN_TEST_LIB_LOGGING_H
|
||||
|
||||
#include <util/macros.h>
|
||||
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
class DebugLogHelper
|
||||
{
|
||||
const std::string m_message;
|
||||
bool m_found{false};
|
||||
std::list<std::function<void(const std::string&)>>::iterator m_print_connection;
|
||||
|
||||
void check_found();
|
||||
|
||||
public:
|
||||
DebugLogHelper(std::string message);
|
||||
~DebugLogHelper() { check_found(); }
|
||||
};
|
||||
|
||||
#define ASSERT_DEBUG_LOG(message) DebugLogHelper PASTE2(debugloghelper, __COUNTER__)(message)
|
||||
|
||||
#endif // BITCOIN_TEST_LIB_LOGGING_H
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <netaddress.h>
|
||||
#include <noui.h>
|
||||
#include <test/lib/logging.h>
|
||||
#include <test/setup_common.h>
|
||||
#include <timedata.h>
|
||||
#include <warnings.h>
|
||||
@@ -59,9 +60,10 @@ BOOST_AUTO_TEST_CASE(addtimedata)
|
||||
MultiAddTimeData(3, DEFAULT_MAX_TIME_ADJUSTMENT + 1);
|
||||
// Filter size is 1 + 3 = 4: It is always initialized with a single element (offset 0)
|
||||
|
||||
noui_suppress();
|
||||
MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5
|
||||
noui_reconnect();
|
||||
{
|
||||
ASSERT_DEBUG_LOG("Please check that your computer's date and time are correct!");
|
||||
MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5
|
||||
}
|
||||
|
||||
BOOST_CHECK(GetWarnings("gui").find("clock is wrong") != std::string::npos);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user