Skip to content

Commit a3ddf73

Browse files
committed
cxx-qt-lib: add standalone unit tests for logging
1 parent 2bdb769 commit a3ddf73

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

tests/qt_types_standalone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ add_executable(${APP_NAME}
104104
cpp/qstringlist.h
105105
cpp/qtime.h
106106
cpp/qtimezone.h
107+
cpp/qtlogging.h
107108
cpp/qurl.h
108109
cpp/qvariant.h
109110
cpp/qvector.h

tests/qt_types_standalone/cpp/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "qstringlist.h"
4646
#include "qtime.h"
4747
#include "qtimezone.h"
48+
#include "qtlogging.h"
4849
#include "qurl.h"
4950
#include "qvariant.h"
5051
#include "qvector.h"
@@ -97,6 +98,7 @@ main(int argc, char* argv[])
9798
runTest(QScopedPointer<QObject>(new QStringListTest));
9899
runTest(QScopedPointer<QObject>(new QTimeTest));
99100
runTest(QScopedPointer<QObject>(new QTimeZoneTest));
101+
runTest(QScopedPointer<QObject>(new QtLoggingTest));
100102
runTest(QScopedPointer<QObject>(new QUrlTest));
101103
runTest(QScopedPointer<QObject>(new QVariantTest));
102104
runTest(QScopedPointer<QObject>(new QVectorTest));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// clang-format off
2+
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3+
// clang-format on
4+
// SPDX-FileContributor: Joshua Booth <[email protected]>
5+
//
6+
// SPDX-License-Identifier: MIT OR Apache-2.0
7+
#pragma once
8+
9+
#include <QtCore/qlogging.h>
10+
#include <QtTest/QTest>
11+
12+
#include "qt_types_standalone/src/qtlogging.cxx.h"
13+
14+
static QtMessageHandler originalHandler = nullptr;
15+
static QString loggedMessage{};
16+
17+
class QtLoggingTest : public QObject
18+
{
19+
Q_OBJECT
20+
21+
private:
22+
static void logAndStore(QtMsgType type,
23+
const QMessageLogContext& context,
24+
const QString& msg)
25+
{
26+
if (type == QtMsgType::QtInfoMsg) {
27+
loggedMessage = QStringLiteral("%1:%2 - %3")
28+
.arg(QString::fromUtf8(context.file))
29+
.arg(context.line)
30+
.arg(msg);
31+
}
32+
if (originalHandler) {
33+
(*originalHandler)(type, context, msg);
34+
}
35+
}
36+
37+
private Q_SLOTS:
38+
void log()
39+
{
40+
originalHandler = qInstallMessageHandler(logAndStore);
41+
const QString expectedMessage = log_info(QStringLiteral("test message"));
42+
qInstallMessageHandler(originalHandler);
43+
QCOMPARE(loggedMessage, expectedMessage);
44+
}
45+
};

tests/qt_types_standalone/rust/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fn main() {
4444
.file("src/qstringlist.rs")
4545
.file("src/qtime.rs")
4646
.file("src/qtimezone.rs")
47+
.file("src/qtlogging.rs")
4748
.file("src/qurl.rs")
4849
.file("src/qvariant.rs")
4950
.file("src/qvector.rs")

tests/qt_types_standalone/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod qstring;
4141
mod qstringlist;
4242
mod qtime;
4343
mod qtimezone;
44+
mod qtlogging;
4445
mod qurl;
4546
mod qvariant;
4647
mod qvector;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
2+
// SPDX-FileContributor: Joshua Booth <[email protected]>
3+
//
4+
// SPDX-License-Identifier: MIT OR Apache-2.0
5+
6+
use cxx_qt_lib::{q_info, QString};
7+
8+
#[cxx::bridge]
9+
mod ffi {
10+
extern "C++" {
11+
include!("cxx-qt-lib/qstring.h");
12+
type QString = cxx_qt_lib::QString;
13+
}
14+
15+
extern "Rust" {
16+
fn log_info(message: &QString) -> QString;
17+
}
18+
}
19+
20+
fn log_info(message: &QString) -> QString {
21+
q_info!("Message: {message}");
22+
QString::from(&format!("{}:{} - Message: {message}", file!(), line!() - 1))
23+
}

0 commit comments

Comments
 (0)