aboutsummaryrefslogtreecommitdiff
path: root/src/test/lib
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2019-08-02 17:06:37 -0400
committerMarcoFalke <falke.marco@gmail.com>2019-11-04 10:42:33 -0500
commitfa2c44c3ccc3e7a54e2afc862addd555d5a6e51b (patch)
tree25dcc1cf23640475cff8321843dba35499436e5c /src/test/lib
parentfa1936f57bbf5aebb1f8fc18701441d79219d443 (diff)
downloadbitcoin-fa2c44c3ccc3e7a54e2afc862addd555d5a6e51b.tar.xz
test: Add ASSERT_DEBUG_LOG to unit test framework
Diffstat (limited to 'src/test/lib')
-rw-r--r--src/test/lib/logging.cpp32
-rw-r--r--src/test/lib/logging.h29
2 files changed, 61 insertions, 0 deletions
diff --git a/src/test/lib/logging.cpp b/src/test/lib/logging.cpp
new file mode 100644
index 0000000000..4cfebf63df
--- /dev/null
+++ b/src/test/lib/logging.cpp
@@ -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));
+ }
+}
diff --git a/src/test/lib/logging.h b/src/test/lib/logging.h
new file mode 100644
index 0000000000..ea1b0ad6f0
--- /dev/null
+++ b/src/test/lib/logging.h
@@ -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