Skip to content

Commit be37037

Browse files
committed
Merge bitcoin-core/gui#337: test: Use Regex Search in Apptests
6969b2b qt, test: use regex search in apptests (Jarol Rodriguez) d09d1cf qt, test: introduce FindInConsole function (Jarol Rodriguez) Pull request description: This PR refactors our GUI `apptests` so that it uses regex search to find values in our console/qtextedit output regardless if it is in `plaintext`, `html`, or `markdown`. This introduces a new function `FindInConsole` which uses [QRegularExpression](https://doc.qt.io/qt-5/qregularexpression.html) to search the output of the console. The function must be provided with a [perl compatible regex](https://www.debuggex.com/cheatsheet/regex/pcre) pattern which wants to match a single group. The function then returns the matched group. If no match is found, an empty `QString` is returned. We then use this new function in `TestRpcCommand` to find the current `chain` value instead of reading with univalue. This approach can apply to a wider variety of testing scenarios as we can reuse this function to search for values when the console output is exported in a different format than `plaintext`. As an example, A follow up PR will add tests for console resizing and needs to look for the size in `html` tags after exporting the console text with `toHtml()`. ACKs for top commit: hebasto: ACK 6969b2b ShaMan239: ACK 6969b2b Tree-SHA512: 4db8bcd4a1acc4539ca64bbd7de572fe7dd6afc3e95108235abfc2891585bc4db3a56a33928fa38e8d44ac87023ce0dee3abcfadfbcd4440e3a21a52fef02536
2 parents 4f1a75b + 6969b2b commit be37037

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/qt/test/apptests.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <qt/rpcconsole.h>
1313
#include <shutdown.h>
1414
#include <test/util/setup_common.h>
15-
#include <univalue.h>
1615
#include <validation.h>
1716

1817
#if defined(HAVE_CONFIG_H)
@@ -21,15 +20,24 @@
2120

2221
#include <QAction>
2322
#include <QLineEdit>
23+
#include <QRegularExpression>
2424
#include <QScopedPointer>
2525
#include <QSignalSpy>
26+
#include <QString>
2627
#include <QTest>
2728
#include <QTextEdit>
2829
#include <QtGlobal>
2930
#include <QtTest/QtTestWidgets>
3031
#include <QtTest/QtTestGui>
3132

3233
namespace {
34+
//! Regex find a string group inside of the console output
35+
QString FindInConsole(const QString& output, const QString& pattern)
36+
{
37+
const QRegularExpression re(pattern);
38+
return re.match(output).captured(1);
39+
}
40+
3341
//! Call getblockchaininfo RPC and check first field of JSON output.
3442
void TestRpcCommand(RPCConsole* console)
3543
{
@@ -41,10 +49,9 @@ void TestRpcCommand(RPCConsole* console)
4149
QTest::keyClick(lineEdit, Qt::Key_Return);
4250
QVERIFY(mw_spy.wait(1000));
4351
QCOMPARE(mw_spy.count(), 4);
44-
QString output = messagesWidget->toPlainText();
45-
UniValue value;
46-
value.read(output.right(output.size() - output.lastIndexOf(QChar::ObjectReplacementCharacter) - 1).toStdString());
47-
QCOMPARE(value["chain"].get_str(), std::string("regtest"));
52+
const QString output = messagesWidget->toPlainText();
53+
const QString pattern = QStringLiteral("\"chain\": \"(\\w+)\"");
54+
QCOMPARE(FindInConsole(output, pattern), QString("regtest"));
4855
}
4956
} // namespace
5057

0 commit comments

Comments
 (0)