refactor: LogTests are now built with QTest
This commit is contained in:
committed by
Chris Rizzitello
parent
34c439b3de
commit
fb144a5e66
@ -29,6 +29,14 @@ create_test(
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/base"
|
||||
)
|
||||
|
||||
create_test(
|
||||
NAME LogTests
|
||||
DEPENDS base
|
||||
LIBS arch ${extra_libs}
|
||||
SOURCE LogTests.cpp
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/base"
|
||||
)
|
||||
|
||||
create_test(
|
||||
NAME BaseExceptionTests
|
||||
DEPENDS base
|
||||
|
||||
139
src/unittests/base/LogTests.cpp
Normal file
139
src/unittests/base/LogTests.cpp
Normal file
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* 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 "LogTests.h"
|
||||
|
||||
#include <QTextStream>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#define LEVEL_PRINT "%z\057"
|
||||
#define LEVEL_ERR "%z\061"
|
||||
#define LEVEL_INFO "%z\064"
|
||||
|
||||
QString sanitizeBuffer(const std::stringstream &in)
|
||||
{
|
||||
static QRegularExpression timestampRegex("\\[\\S+\\] ");
|
||||
QString rtn = QString::fromStdString(in.str()).simplified();
|
||||
rtn.remove(timestampRegex);
|
||||
return rtn;
|
||||
}
|
||||
|
||||
void LogTests::initTestCase()
|
||||
{
|
||||
m_arch.init();
|
||||
m_log.setFilter(LogLevel::Debug2);
|
||||
}
|
||||
|
||||
void LogTests::printWithErrorValidOutput()
|
||||
{
|
||||
Log log(false);
|
||||
|
||||
std::stringstream buffer;
|
||||
std::streambuf *old = std::cerr.rdbuf(buffer.rdbuf());
|
||||
|
||||
log.print(nullptr, 0, LEVEL_ERR "test message");
|
||||
|
||||
std::cerr.rdbuf(old);
|
||||
auto string = sanitizeBuffer(buffer);
|
||||
|
||||
QCOMPARE(string, "ERROR: test message");
|
||||
}
|
||||
|
||||
void LogTests::printTestPrintLevel()
|
||||
{
|
||||
Log log(false);
|
||||
|
||||
std::stringstream buffer;
|
||||
std::streambuf *old = std::cout.rdbuf(buffer.rdbuf());
|
||||
|
||||
log.print(nullptr, 0, LEVEL_PRINT "test message");
|
||||
|
||||
std::cout.rdbuf(old);
|
||||
auto string = sanitizeBuffer(buffer);
|
||||
|
||||
QCOMPARE(string, "test message");
|
||||
}
|
||||
|
||||
void LogTests::printTestWithArgs()
|
||||
{
|
||||
Log log(false);
|
||||
|
||||
std::stringstream buffer;
|
||||
std::streambuf *old = std::cout.rdbuf(buffer.rdbuf());
|
||||
|
||||
log.print(nullptr, 0, LEVEL_INFO "test %d %.2f %s", 1, 1.234, "test arg");
|
||||
|
||||
std::cout.rdbuf(old);
|
||||
auto string = sanitizeBuffer(buffer);
|
||||
|
||||
QCOMPARE(string, "INFO: test 1 1.23 test arg");
|
||||
}
|
||||
|
||||
void LogTests::printTestLogString()
|
||||
{
|
||||
Log log(false);
|
||||
|
||||
std::stringstream buffer;
|
||||
std::streambuf *old = std::cout.rdbuf(buffer.rdbuf());
|
||||
|
||||
auto longString = QString(10000, 'a');
|
||||
log.print(nullptr, 0, LEVEL_INFO "%s", qPrintable(longString));
|
||||
|
||||
std::cout.rdbuf(old);
|
||||
auto string = sanitizeBuffer(buffer);
|
||||
|
||||
QCOMPARE(string, QString("INFO: %1").arg(longString));
|
||||
}
|
||||
|
||||
void LogTests::printLevelToHigh()
|
||||
{
|
||||
Log log(false);
|
||||
|
||||
std::stringstream buffer;
|
||||
std::streambuf *old = std::cout.rdbuf(buffer.rdbuf());
|
||||
|
||||
log.print(CLOG_DEBUG5 "test message");
|
||||
|
||||
std::cout.rdbuf(old);
|
||||
auto string = sanitizeBuffer(buffer);
|
||||
|
||||
QCOMPARE(string, QString{});
|
||||
}
|
||||
|
||||
void LogTests::printInfoWithFileAndLine()
|
||||
{
|
||||
Log log(false);
|
||||
|
||||
std::stringstream buffer;
|
||||
std::streambuf *old = std::cout.rdbuf(buffer.rdbuf());
|
||||
|
||||
log.print("test file", 123, LEVEL_INFO "test message");
|
||||
|
||||
std::cout.rdbuf(old);
|
||||
auto string = sanitizeBuffer(buffer);
|
||||
|
||||
QCOMPARE(string, "INFO: test message test file:123");
|
||||
}
|
||||
|
||||
void LogTests::printErrWithFileAndLine()
|
||||
{
|
||||
Log log(false);
|
||||
|
||||
std::stringstream buffer;
|
||||
std::streambuf *old = std::cerr.rdbuf(buffer.rdbuf());
|
||||
|
||||
log.print("test file", 123, LEVEL_ERR "test message");
|
||||
|
||||
std::cerr.rdbuf(old);
|
||||
auto string = sanitizeBuffer(buffer);
|
||||
|
||||
QCOMPARE(string, "ERROR: test message test file:123");
|
||||
}
|
||||
|
||||
QTEST_MAIN(LogTests)
|
||||
28
src/unittests/base/LogTests.h
Normal file
28
src/unittests/base/LogTests.h
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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 "../../lib/arch/ArchString.h"
|
||||
#include "base/Log.h"
|
||||
#include <QTest>
|
||||
|
||||
class LogTests : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private Q_SLOTS:
|
||||
void initTestCase();
|
||||
// Test are run in order top to bottom
|
||||
void printWithErrorValidOutput();
|
||||
void printTestPrintLevel();
|
||||
void printTestWithArgs();
|
||||
void printTestLogString();
|
||||
void printLevelToHigh();
|
||||
void printInfoWithFileAndLine();
|
||||
void printErrWithFileAndLine();
|
||||
|
||||
private:
|
||||
Arch m_arch;
|
||||
Log m_log;
|
||||
};
|
||||
@ -1,103 +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 "base/Log.h"
|
||||
|
||||
#include "gmock/gmock-matchers.h"
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#define LEVEL_PRINT "%z\057"
|
||||
#define LEVEL_ERR "%z\061"
|
||||
#define LEVEL_INFO "%z\064"
|
||||
|
||||
using testing::EndsWith;
|
||||
using testing::HasSubstr;
|
||||
using testing::internal::CaptureStderr;
|
||||
using testing::internal::CaptureStdout;
|
||||
using testing::internal::GetCapturedStderr;
|
||||
using testing::internal::GetCapturedStdout;
|
||||
|
||||
TEST(LogTests, print_withErrorLevel_outputIsValid)
|
||||
{
|
||||
CaptureStderr();
|
||||
Log log(false);
|
||||
|
||||
log.print(nullptr, 0, LEVEL_ERR "test message");
|
||||
|
||||
EXPECT_THAT(GetCapturedStderr(), EndsWith("ERROR: test message\n"));
|
||||
}
|
||||
|
||||
TEST(LogTests, print_simpleString_outputIsValid)
|
||||
{
|
||||
CaptureStdout();
|
||||
Log log(false);
|
||||
|
||||
log.print(nullptr, 0, LEVEL_PRINT "test message");
|
||||
|
||||
EXPECT_THAT(GetCapturedStdout(), EndsWith("test message\n"));
|
||||
}
|
||||
|
||||
TEST(LogTests, print_withArgs_outputIsValid)
|
||||
{
|
||||
CaptureStdout();
|
||||
Log log(false);
|
||||
|
||||
log.print(nullptr, 0, LEVEL_INFO "test %d %.2f %s", 1, 1.234, "test arg");
|
||||
|
||||
EXPECT_THAT(GetCapturedStdout(), HasSubstr("INFO: test 1 1.23 test arg\n"));
|
||||
}
|
||||
|
||||
TEST(LogTests, print_withPrintLevel_outputIsValid)
|
||||
{
|
||||
CaptureStdout();
|
||||
Log log(false);
|
||||
|
||||
log.print(nullptr, 0, LEVEL_PRINT "test message");
|
||||
|
||||
EXPECT_THAT(GetCapturedStdout(), "test message\n");
|
||||
}
|
||||
|
||||
TEST(LogTests, print_longMessage_outputIsValid)
|
||||
{
|
||||
CaptureStdout();
|
||||
Log log(false);
|
||||
|
||||
auto longString = std::string(10000, 'a');
|
||||
log.print(nullptr, 0, LEVEL_INFO "%s", longString.c_str());
|
||||
|
||||
EXPECT_THAT(GetCapturedStdout(), HasSubstr("INFO: " + longString + "\n"));
|
||||
}
|
||||
|
||||
TEST(LogTests, print_highestLevel_noOutput)
|
||||
{
|
||||
CaptureStdout();
|
||||
Log log(false);
|
||||
|
||||
log.print(CLOG_DEBUG5 "test message");
|
||||
|
||||
EXPECT_EQ(GetCapturedStdout(), "");
|
||||
}
|
||||
|
||||
TEST(LogTests, print_infoWithFileAndLine_outputIsValid)
|
||||
{
|
||||
CaptureStdout();
|
||||
Log log(false);
|
||||
|
||||
log.print("test file", 123, LEVEL_INFO "test message");
|
||||
|
||||
EXPECT_THAT(GetCapturedStdout(), EndsWith("INFO: test message\n\ttest file:123\n"));
|
||||
}
|
||||
|
||||
TEST(LogTests, print_errorWithFileAndLine_outputIsValid)
|
||||
{
|
||||
CaptureStderr();
|
||||
Log log(false);
|
||||
|
||||
log.print("test file", 123, LEVEL_ERR "test message");
|
||||
|
||||
EXPECT_THAT(GetCapturedStderr(), EndsWith("ERROR: test message\n\ttest file:123\n"));
|
||||
}
|
||||
Reference in New Issue
Block a user