From 78cbe4b7752a327df8870fee6c93438a8e76a646 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Wed, 2 Apr 2025 21:15:52 -0400 Subject: [PATCH] refactor: port base/UnicodeTest to QtTests --- src/test/unittests/base/UnicodeTests.cpp | 46 ------------------ src/unittests/base/CMakeLists.txt | 12 +++++ src/unittests/base/UnicodeTests.cpp | 60 ++++++++++++++++++++++++ src/unittests/base/UnicodeTests.h | 24 ++++++++++ 4 files changed, 96 insertions(+), 46 deletions(-) delete mode 100644 src/test/unittests/base/UnicodeTests.cpp create mode 100644 src/unittests/base/UnicodeTests.cpp create mode 100644 src/unittests/base/UnicodeTests.h diff --git a/src/test/unittests/base/UnicodeTests.cpp b/src/test/unittests/base/UnicodeTests.cpp deleted file mode 100644 index 92bc30cb3..000000000 --- a/src/test/unittests/base/UnicodeTests.cpp +++ /dev/null @@ -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 - -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 -} diff --git a/src/unittests/base/CMakeLists.txt b/src/unittests/base/CMakeLists.txt index 30ae0d00b..cc9cd01cd 100644 --- a/src/unittests/base/CMakeLists.txt +++ b/src/unittests/base/CMakeLists.txt @@ -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" +) diff --git a/src/unittests/base/UnicodeTests.cpp b/src/unittests/base/UnicodeTests.cpp new file mode 100644 index 000000000..cfaea4015 --- /dev/null +++ b/src/unittests/base/UnicodeTests.cpp @@ -0,0 +1,60 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * 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) diff --git a/src/unittests/base/UnicodeTests.h b/src/unittests/base/UnicodeTests.h new file mode 100644 index 000000000..15bba08a0 --- /dev/null +++ b/src/unittests/base/UnicodeTests.h @@ -0,0 +1,24 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "base/Log.h" + +#include + +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; +};