refactor: port ConfigTests to QtTest
This commit is contained in:
parent
245f9db5c8
commit
810322623b
5 changed files with 188 additions and 122 deletions
|
|
@ -1,122 +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 "deskflow/Config.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace deskflow;
|
||||
|
||||
const auto kTestFilename = "tmp/test/test.toml";
|
||||
|
||||
TEST(ConfigTests, load_fileExists_loadsConfig)
|
||||
{
|
||||
std::ofstream testFile(kTestFilename);
|
||||
testFile << "[test.args]\n"
|
||||
R"(test-arg = "test opt")";
|
||||
testFile.close();
|
||||
Config config(kTestFilename, "test");
|
||||
|
||||
const auto result = config.load("test");
|
||||
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(config.argc(), 3);
|
||||
ASSERT_STREQ(config.argv()[0], "test");
|
||||
ASSERT_STREQ(config.argv()[1], "--test-arg");
|
||||
ASSERT_STREQ(config.argv()[2], "test opt");
|
||||
}
|
||||
|
||||
TEST(ConfigTests, load_filenameEmpty_throwsException)
|
||||
{
|
||||
EXPECT_THROW(
|
||||
{
|
||||
Config config("", "test");
|
||||
|
||||
config.load("test");
|
||||
},
|
||||
Config::NoConfigFilenameError
|
||||
);
|
||||
}
|
||||
|
||||
TEST(ConfigTests, load_fileDoesNotExist_returnsFalse)
|
||||
{
|
||||
Config config("nonexistent.toml", "test");
|
||||
|
||||
const auto result = config.load("test");
|
||||
|
||||
ASSERT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST(ConfigTests, load_invalidConfig_throwsException)
|
||||
{
|
||||
EXPECT_THROW(
|
||||
{
|
||||
std::ofstream testFile(kTestFilename);
|
||||
testFile << "foobar";
|
||||
testFile.close();
|
||||
|
||||
Config config(kTestFilename, "test");
|
||||
|
||||
config.load("test");
|
||||
},
|
||||
Config::ParseError
|
||||
);
|
||||
}
|
||||
|
||||
TEST(ConfigTests, load_sectionMissing_returnsFalse)
|
||||
{
|
||||
std::ofstream testFile(kTestFilename);
|
||||
testFile.close();
|
||||
Config config(kTestFilename, "missing");
|
||||
|
||||
const auto result = config.load("test");
|
||||
|
||||
ASSERT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST(ConfigTests, load_notTable_returnsFalse)
|
||||
{
|
||||
std::ofstream testFile(kTestFilename);
|
||||
testFile << "[test]";
|
||||
testFile.close();
|
||||
Config config(kTestFilename, "test");
|
||||
|
||||
const auto result = config.load("test");
|
||||
|
||||
ASSERT_FALSE(result);
|
||||
}
|
||||
|
||||
TEST(ConfigTests, load_lastArg_returnsLast)
|
||||
{
|
||||
std::ofstream testFile(kTestFilename);
|
||||
testFile << "[test.args]\n"
|
||||
R"(_last = "test last")"
|
||||
"\n"
|
||||
R"(test-second = true)";
|
||||
testFile.close();
|
||||
Config config(kTestFilename, "test");
|
||||
|
||||
const auto result = config.load("test-first");
|
||||
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(config.argc(), 3);
|
||||
ASSERT_STREQ(config.argv()[0], "test-first");
|
||||
ASSERT_STREQ(config.argv()[1], "--test-second");
|
||||
ASSERT_STREQ(config.argv()[2], "test last");
|
||||
}
|
||||
|
||||
TEST(ConfigTests, load_noArgs_returnsFalse)
|
||||
{
|
||||
std::ofstream testFile(kTestFilename);
|
||||
testFile << "[test.args]";
|
||||
testFile.close();
|
||||
Config config(kTestFilename, "test");
|
||||
|
||||
const auto result = config.load("");
|
||||
|
||||
ASSERT_FALSE(result);
|
||||
}
|
||||
|
|
@ -42,6 +42,7 @@ enable_testing()
|
|||
find_package(Qt6 ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Test)
|
||||
|
||||
add_subdirectory(common)
|
||||
add_subdirectory(deskflow)
|
||||
add_subdirectory(gui)
|
||||
|
||||
#make sure to use CI only plugin on Unix
|
||||
|
|
|
|||
14
src/unittests/deskflow/CMakeLists.txt
Normal file
14
src/unittests/deskflow/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# SPDX-FileCopyrightText: 2025 Deskflow Developers
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
if(WIN32)
|
||||
set(extra_libs version)
|
||||
endif()
|
||||
|
||||
create_test(
|
||||
NAME ConfigTests
|
||||
DEPENDS app
|
||||
LIBS arch base ${extra_libs}
|
||||
SOURCE ConfigTests.cpp
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/deskflow"
|
||||
)
|
||||
144
src/unittests/deskflow/ConfigTests.cpp
Normal file
144
src/unittests/deskflow/ConfigTests.cpp
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
/*
|
||||
* 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 "ConfigTests.h"
|
||||
|
||||
#include "../../lib/deskflow/Config.h"
|
||||
|
||||
using namespace deskflow;
|
||||
const auto kTestFilename = "tmp/test/test.toml";
|
||||
|
||||
void ConfigTests::initTestCase()
|
||||
{
|
||||
QDir dir;
|
||||
QVERIFY(dir.mkpath("tmp/test"));
|
||||
|
||||
m_arch.init();
|
||||
m_log.setFilter(kDEBUG2);
|
||||
}
|
||||
|
||||
void ConfigTests::loadFile()
|
||||
{
|
||||
QFile testFile(kTestFilename);
|
||||
QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Text));
|
||||
|
||||
QTextStream out(&testFile);
|
||||
out << "[test.args]\n"
|
||||
R"(test-arg = "test opt")";
|
||||
|
||||
testFile.close();
|
||||
QVERIFY(!testFile.isOpen());
|
||||
|
||||
Config config(kTestFilename, "test");
|
||||
|
||||
const auto result = config.load("test");
|
||||
QVERIFY(result);
|
||||
QCOMPARE(config.argc(), 3);
|
||||
QCOMPARE(config.argv()[0], "test");
|
||||
QCOMPARE(config.argv()[1], "--test-arg");
|
||||
QCOMPARE(config.argv()[2], "test opt");
|
||||
}
|
||||
|
||||
void ConfigTests::load_EmptyFile()
|
||||
{
|
||||
Config config("", "test");
|
||||
QVERIFY_THROWS_EXCEPTION(Config::NoConfigFilenameError, config.load("test"));
|
||||
}
|
||||
|
||||
void ConfigTests::load_NonExsitingFile()
|
||||
{
|
||||
Config config("nonexistent.toml", "test");
|
||||
|
||||
const auto result = config.load("test");
|
||||
QVERIFY(!result);
|
||||
}
|
||||
|
||||
void ConfigTests::load_InvalidConfig()
|
||||
{
|
||||
QFile testFile(kTestFilename);
|
||||
QVERIFY(testFile.open(QIODevice::WriteOnly));
|
||||
|
||||
QVERIFY(testFile.write("foobar"));
|
||||
|
||||
testFile.close();
|
||||
QVERIFY(!testFile.isOpen());
|
||||
|
||||
Config config(kTestFilename, "test");
|
||||
QVERIFY_THROWS_EXCEPTION(Config::ParseError, config.load("test"));
|
||||
}
|
||||
|
||||
void ConfigTests::load_missingSections()
|
||||
{
|
||||
QFile testFile(kTestFilename);
|
||||
QVERIFY(testFile.open(QIODevice::WriteOnly));
|
||||
|
||||
testFile.close();
|
||||
QVERIFY(!testFile.isOpen());
|
||||
|
||||
Config config(kTestFilename, "missing");
|
||||
const auto result = config.load("test");
|
||||
QVERIFY(!result);
|
||||
}
|
||||
|
||||
void ConfigTests::load_badTable()
|
||||
{
|
||||
QFile testFile(kTestFilename);
|
||||
QVERIFY(testFile.open(QIODevice::WriteOnly));
|
||||
|
||||
QVERIFY(testFile.write("[test]"));
|
||||
|
||||
testFile.close();
|
||||
QVERIFY(!testFile.isOpen());
|
||||
|
||||
Config config(kTestFilename, "test");
|
||||
|
||||
const auto result = config.load("test");
|
||||
QVERIFY(!result);
|
||||
}
|
||||
|
||||
void ConfigTests::load_lastArg()
|
||||
{
|
||||
QFile testFile(kTestFilename);
|
||||
QVERIFY(testFile.open(QIODevice::WriteOnly));
|
||||
|
||||
QVERIFY(testFile.write(
|
||||
"[test.args]\n"
|
||||
R"(_last = "test last")"
|
||||
"\n"
|
||||
R"(test-second = true)"
|
||||
));
|
||||
|
||||
testFile.close();
|
||||
QVERIFY(!testFile.isOpen());
|
||||
|
||||
deskflow::Config config(kTestFilename, "test");
|
||||
|
||||
const auto result = config.load("test-first");
|
||||
QVERIFY(result);
|
||||
QCOMPARE(config.argc(), 3);
|
||||
QCOMPARE(config.argv()[0], "test-first");
|
||||
QCOMPARE(config.argv()[1], "--test-second");
|
||||
QCOMPARE(config.argv()[2], "test last");
|
||||
}
|
||||
|
||||
void ConfigTests::load_noArgs()
|
||||
{
|
||||
QFile testFile(kTestFilename);
|
||||
QVERIFY(testFile.open(QIODevice::WriteOnly));
|
||||
|
||||
QVERIFY(testFile.write("[test.args]"));
|
||||
|
||||
testFile.close();
|
||||
QVERIFY(!testFile.isOpen());
|
||||
|
||||
deskflow::Config config(kTestFilename, "test");
|
||||
|
||||
const auto result = config.load("");
|
||||
QVERIFY(!result);
|
||||
}
|
||||
|
||||
QTEST_MAIN(ConfigTests)
|
||||
29
src/unittests/deskflow/ConfigTests.h
Normal file
29
src/unittests/deskflow/ConfigTests.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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 "base/Log.h"
|
||||
|
||||
#include <QTest>
|
||||
|
||||
class ConfigTests : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
// Test are run in order top to bottom
|
||||
void initTestCase();
|
||||
void loadFile();
|
||||
void load_EmptyFile();
|
||||
void load_NonExsitingFile();
|
||||
void load_InvalidConfig();
|
||||
void load_missingSections();
|
||||
void load_badTable();
|
||||
void load_lastArg();
|
||||
void load_noArgs();
|
||||
|
||||
private:
|
||||
Arch m_arch;
|
||||
Log m_log;
|
||||
};
|
||||
Loading…
Reference in a new issue