From 3735d4b32746500ad26296ad12ac8ec349fddade Mon Sep 17 00:00:00 2001 From: Xavier Michelon Date: Wed, 5 Apr 2023 10:13:49 +0200 Subject: [PATCH] feat(GODT-2239): unit tests for BridgeUtils.cpp in bridgepp. --- .../bridge-gui/bridgepp/CMakeLists.txt | 5 +- .../Test/Exception/TestBridgeUtils.cpp | 111 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 internal/frontend/bridge-gui/bridgepp/Test/Exception/TestBridgeUtils.cpp diff --git a/internal/frontend/bridge-gui/bridgepp/CMakeLists.txt b/internal/frontend/bridge-gui/bridgepp/CMakeLists.txt index 12df7a85..9c5eb38f 100644 --- a/internal/frontend/bridge-gui/bridgepp/CMakeLists.txt +++ b/internal/frontend/bridge-gui/bridgepp/CMakeLists.txt @@ -185,7 +185,10 @@ enable_testing() #***************************************************************************************************************************************************** # Tests #***************************************************************************************************************************************************** -add_executable(bridgepp-test Test/Exception/TestException.cpp) +add_executable(bridgepp-test + Test/Exception/TestBridgeUtils.cpp + Test/Exception/TestException.cpp + ) add_dependencies(bridgepp-test bridgepp) target_precompile_headers(bridgepp-test PRIVATE Pch.h) target_link_libraries(bridgepp-test diff --git a/internal/frontend/bridge-gui/bridgepp/Test/Exception/TestBridgeUtils.cpp b/internal/frontend/bridge-gui/bridgepp/Test/Exception/TestBridgeUtils.cpp new file mode 100644 index 00000000..a990eb66 --- /dev/null +++ b/internal/frontend/bridge-gui/bridgepp/Test/Exception/TestBridgeUtils.cpp @@ -0,0 +1,111 @@ +// Copyright (c) 2023 Proton AG +// +// This file is part of Proton Mail Bridge. +// +// Proton Mail Bridge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Proton Mail Bridge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Proton Mail Bridge. If not, see . + + +#include +#include + + +using namespace bridgepp; + + +//**************************************************************************************************************************************************** +// +//**************************************************************************************************************************************************** +TEST(BridgeUtils, OS) { +#ifdef Q_OS_MACOS + EXPECT_EQ(os(), OS::MacOS); + EXPECT_FALSE(onLinux()); + EXPECT_TRUE(onMacOS()); + EXPECT_FALSE(onWindows()); + EXPECT_EQ(goos(), "darwin"); + return; +#endif + +#ifdef Q_OS_WIN + EXPECT_EQ(os(), OS::Windows); + EXPECT_FALSE(onLinux()); + EXPECT_FALSE(onMacOS()); + EXPECT_TRUE(onWindows()); + EXPECT_EQ(goos(), "windows"); + return; +#endif + +#ifdef Q_OS_LINUX + EXPECT_EQ(os(), OS::Linux); + EXPECT_TRUE(onLinux()); + EXPECT_FALSE(onMacOS()); + EXPECT_FALSE(onWindows()); + EXPECT_EQ(goos(), "linux"); + return; +#endif + + EXPECT_TRUE(false); // should be unreachable. +} + +//**************************************************************************************************************************************************** +// +//**************************************************************************************************************************************************** +TEST(BridgeUtils, UserFolders) { + typedef QString (*dirFunction)(); + QList functions = { userConfigDir, userCacheDir, userDataDir, sentryCacheDir }; + QString path; + for (dirFunction f: functions) { + EXPECT_NO_THROW(path = f()); + EXPECT_FALSE(path.isEmpty()); + EXPECT_TRUE(QDir(path).exists()); + } +} + + +//**************************************************************************************************************************************************** +// +//**************************************************************************************************************************************************** +TEST(BridgeUtils, Random) { + qint32 repeatCount = 1000; + qint32 const maxValue = 5; + for (qint32 i = 0; i < repeatCount; ++i) { + qint64 n = 0; + EXPECT_NO_THROW(n = randN(maxValue)); + EXPECT_TRUE((n >= 0) && (n < maxValue)); + QString name; + EXPECT_NO_THROW(name = randomFirstName()); + EXPECT_FALSE(name.isEmpty()); + EXPECT_NO_THROW(name = randomLastName()); + EXPECT_FALSE(name.isEmpty()); + EXPECT_NO_THROW(randomUser()); + } +} + + +//**************************************************************************************************************************************************** +// +//**************************************************************************************************************************************************** +TEST(BridgeUtils, ElideLongString) { + std::function const test = [](QString const &input, qint32 maxLength, QString const &expected) -> bool { + QString output; + EXPECT_NO_THROW(output = elideLongString(input, maxLength)); + return output == expected; + }; + + EXPECT_TRUE(test( "", 0, "")); + EXPECT_TRUE(test("1234", 4, "1234")); + EXPECT_TRUE(test("123", 2, "...")); + EXPECT_TRUE(test("1234567890", 8, "12...90")); + EXPECT_TRUE(test("1234567890", 10, "1234567890")); + EXPECT_TRUE(test("1234567890", 100, "1234567890")); +}