refactor: port gui/LoggerTests to QtTests

This commit is contained in:
sithlord48
2025-04-08 18:22:19 -04:00
committed by Nick Bolton
parent 8a80c52208
commit ae37543a37
4 changed files with 72 additions and 49 deletions

View File

@ -1,49 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2024 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "gui/Logger.h"
#include "gmock/gmock.h"
#include <gtest/gtest.h>
using namespace testing;
using namespace deskflow::gui;
TEST(LoggerTests, handleMessage_withDebugEnvVarOn_emitsNewLine)
{
Logger logger;
std::string newLineEmitted;
QObject::connect(
&logger, &Logger::newLine, //
[&newLineEmitted](const QString &line) { newLineEmitted = line.toStdString(); }
);
qputenv("DESKFLOW_GUI_DEBUG", "true");
logger.loadEnvVars();
logger.handleMessage(QtDebugMsg, "stub", "test");
EXPECT_THAT(newLineEmitted, HasSubstr("test"));
qputenv("DESKFLOW_GUI_DEBUG", "");
}
TEST(LoggerTests, handleMessage_withDebugEnvVarOff_doesNotEmitNewLine)
{
Logger logger;
bool newLineEmitted = false;
QObject::connect(
&logger, &Logger::newLine, //
[&newLineEmitted](const QString &line) { newLineEmitted = true; }
);
qputenv("DESKFLOW_GUI_DEBUG", "false");
logger.loadEnvVars();
logger.handleMessage(QtDebugMsg, "stub", "test");
EXPECT_FALSE(newLineEmitted);
qputenv("DESKFLOW_GUI_DEBUG", "");
}

View File

@ -9,3 +9,10 @@ create_test(
SOURCE DotEnvTests.cpp
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/gui"
)
create_test(
NAME LoggerTests
DEPENDS gui
SOURCE LoggerTests.cpp
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/gui"
)

View File

@ -0,0 +1,49 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-FileCopyrightText: (C) 2024 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "LoggerTests.h"
#include "../../lib/gui/Logger.h"
#include <QSignalSpy>
using namespace deskflow::gui;
void LoggerTests::newLine()
{
Logger logger;
QSignalSpy spy(&logger, &Logger::newLine);
QVERIFY(spy.isValid());
qputenv("DESKFLOW_GUI_DEBUG", "true");
logger.loadEnvVars();
logger.handleMessage(QtDebugMsg, "stub", "test");
QCOMPARE(spy.count(), 1);
QVERIFY(qvariant_cast<QString>(spy.takeFirst().at(0)).contains("test"));
qputenv("DESKFLOW_GUI_DEBUG", "");
}
void LoggerTests::noNewLine()
{
Logger logger;
bool newLineEmitted = false;
QSignalSpy spy(&logger, &Logger::newLine);
QVERIFY(spy.isValid());
qputenv("DESKFLOW_GUI_DEBUG", "false");
logger.loadEnvVars();
logger.handleMessage(QtDebugMsg, "stub", "test");
QCOMPARE(spy.count(), 0);
QVERIFY(!newLineEmitted);
qputenv("DESKFLOW_GUI_DEBUG", "");
}
QTEST_MAIN(LoggerTests)

View File

@ -0,0 +1,16 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include <QTest>
class LoggerTests : public QObject
{
Q_OBJECT
private slots:
// Test are run in order top to bottom
void newLine();
void noNewLine();
};