refactor: port base/UnicodeTest to QtTests

This commit is contained in:
sithlord48 2025-04-02 21:15:52 -04:00 committed by Nick Bolton
parent a1a7c8f3ff
commit 78cbe4b775
4 changed files with 96 additions and 46 deletions

View file

@ -1,46 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "arch/IArchString.h"
#include "base/Unicode.h"
#include <gtest/gtest.h>
TEST(UnicodeTests, doUTF32ToUTF8_will_convert_simple_string)
{
bool errors;
auto result = Unicode::UTF32ToUTF8(std::string("h\0\0\0e\0\0\0l\0\0\0l\0\0\0o\0\0\0", 20), &errors);
EXPECT_FALSE(errors);
EXPECT_STREQ(result.c_str(), "hello");
}
TEST(UnicodeTests, doUTF16ToUTF8_will_convert_simple_string)
{
bool errors;
auto result = Unicode::UTF16ToUTF8(std::string("h\0e\0l\0l\0o\0", 10), &errors);
EXPECT_FALSE(errors);
EXPECT_STREQ(result.c_str(), "hello");
}
TEST(UnicodeTests, doUCS2ToUTF8_will_convert_simple_string_kUCS2)
{
bool errors;
auto result = Unicode::textToUTF8("hello", &errors, IArchString::kUCS2);
EXPECT_FALSE(errors);
#ifdef _WIN32
EXPECT_EQ(result, std::string("hello", 5)); // mixed-platform expected result
#else
EXPECT_EQ(result, std::string("h\0e\0l", 5)); // mixed-platform expected result
#endif // _WIN32
}
TEST(UnicodeTests, doUCS2ToUTF8_will_convert_simple_string_any_platform)
{
bool errors;
auto result = Unicode::textToUTF8("hello", &errors);
EXPECT_FALSE(errors);
EXPECT_EQ(result, std::string("hello", 5)); // mixed-platform expected result
}

View file

@ -1,6 +1,10 @@
# SPDX-FileCopyrightText: 2025 Deskflow Developers
# SPDX-License-Identifier: MIT
if(WIN32)
set(extra_libs version)
endif()
create_test(
NAME PathTests
DEPENDS base
@ -16,3 +20,11 @@ create_test(
SOURCE StringTests.cpp
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/base"
)
create_test(
NAME UnicodeTests
DEPENDS base
LIBS arch ${extra_libs}
SOURCE UnicodeTests.cpp
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/base"
)

View file

@ -0,0 +1,60 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "UnicodeTests.h"
#include "../../lib/base/Unicode.h"
#include "arch/IArchString.h"
void UnicodeTests::initTestCase()
{
m_arch.init();
m_log.setFilter(kDEBUG2);
}
void UnicodeTests::UTF32ToUTF8()
{
bool errors;
auto result = Unicode::UTF32ToUTF8(std::string("h\0\0\0e\0\0\0l\0\0\0l\0\0\0o\0\0\0", 20), &errors);
QVERIFY(!errors);
QCOMPARE(result.c_str(), "hello");
}
void UnicodeTests::UTF16ToUTF8()
{
bool errors;
auto result = Unicode::UTF16ToUTF8(std::string("h\0e\0l\0l\0o\0", 10), &errors);
QVERIFY(!errors);
QCOMPARE(result.c_str(), "hello");
}
void UnicodeTests::UCS2ToUTF8_kUCS2()
{
bool errors;
auto result = Unicode::textToUTF8("hello", &errors, IArchString::kUCS2);
QVERIFY(!errors);
#ifdef _WIN32
QCOMPARE(result, std::string("hello", 5)); // mixed-platform expected result
#else
QCOMPARE(result, std::string("h\0e\0l", 5)); // mixed-platform expected result
#endif // _WIN32
}
void UnicodeTests::UCS2ToUTF8()
{
bool errors;
auto result = Unicode::textToUTF8("hello", &errors);
QVERIFY(!errors);
QCOMPARE(result, std::string("hello", 5)); // mixed-platform expected result
}
QTEST_MAIN(UnicodeTests)

View file

@ -0,0 +1,24 @@
/*
* 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 UnicodeTests : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void UTF32ToUTF8();
void UTF16ToUTF8();
void UCS2ToUTF8_kUCS2();
void UCS2ToUTF8();
private:
Arch m_arch;
Log m_log;
};