From a9ea686577d659d502b9b46e8817bc6c929b30fe Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sun, 20 Oct 2024 22:54:33 +0200 Subject: [PATCH] Fix unit tests --- CMakeLists.txt | 75 +++++++++++++++++---------- tests/CMakeLists.txt | 71 ++++++++++++------------- tests/src/main.cpp | 6 ++- tests/src/mock_networkaccessmanager.h | 3 +- tests/src/mock_playlistitem.h | 1 - tests/src/mock_settingsprovider.h | 4 +- tests/src/resources_env.h | 3 -- 7 files changed, 92 insertions(+), 71 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d5ad40e6b..825e846f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,6 +117,25 @@ endif() add_compile_options(${COMPILE_OPTIONS}) +add_definitions( + -DBOOST_BIND_NO_PLACEHOLDERS + -DQT_STRICT_ITERATORS + -DQT_NO_CAST_FROM_BYTEARRAY + -DQT_USE_QSTRINGBUILDER + -DQT_NO_URL_CAST_FROM_STRING + -DQT_NO_CAST_TO_ASCII + -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT + -DQT_NO_FOREACH + -DQT_ASCII_CAST_WARNINGS + -DQT_NO_CAST_FROM_ASCII + -DQT_NO_KEYWORDS + -DQT_NO_SIGNALS_SLOTS_KEYWORDS +) + +if(WIN32) + add_definitions(-DUNICODE) +endif() + if(BUILD_WERROR) set(CMAKE_COMPILE_WARNING_AS_ERROR ON) endif() @@ -188,7 +207,6 @@ else() endif() find_package(GTest) -find_library(GMOCK_LIBRARY gmock) set(QT_VERSION_MAJOR 6) set(QT_MIN_VERSION 6.4.0) @@ -384,28 +402,7 @@ if(WIN32 AND NOT ENABLE_WIN32_CONSOLE) set_target_properties(strawberry PROPERTIES WIN32_EXECUTABLE TRUE) endif() -target_compile_definitions(strawberry PRIVATE - -DBOOST_BIND_NO_PLACEHOLDERS - -DQT_STRICT_ITERATORS - -DQT_NO_CAST_FROM_BYTEARRAY - -DQT_USE_QSTRINGBUILDER - -DQT_NO_URL_CAST_FROM_STRING - -DQT_NO_CAST_TO_ASCII - -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT - -DQT_NO_FOREACH - -DQT_ASCII_CAST_WARNINGS - -DQT_NO_CAST_FROM_ASCII - -DQT_NO_KEYWORDS - -DQT_NO_SIGNALS_SLOTS_KEYWORDS -) - -if(WIN32) - target_compile_definitions(strawberry PRIVATE -DUNICODE) -endif() - set(SOURCES - src/main.cpp - src/core/logging.cpp src/core/mainwindow.cpp src/core/application.cpp @@ -1406,7 +1403,9 @@ qt_wrap_cpp(SOURCES ${HEADERS}) qt_wrap_ui(SOURCES ${UI}) qt_add_resources(SOURCES data/data.qrc data/icons.qrc) -target_sources(strawberry PRIVATE ${SOURCES}) +add_library(strawberry_lib ${SOURCES}) + +target_sources(strawberry PRIVATE src/main.cpp) if(WIN32) target_sources(strawberry PRIVATE windres.rc) @@ -1415,7 +1414,7 @@ endif() add_subdirectory(src) add_subdirectory(dist) -if(GTest_FOUND AND GMOCK_LIBRARY AND Qt${QT_VERSION_MAJOR}Test_FOUND) +if(TARGET GTest::GTest AND Qt${QT_VERSION_MAJOR}Test_FOUND) add_subdirectory(tests) endif() @@ -1440,15 +1439,22 @@ target_include_directories(strawberry PRIVATE ${CMAKE_BINARY_DIR}/src ) +target_include_directories(strawberry_lib PRIVATE + ${CMAKE_SOURCE_DIR} + ${CMAKE_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_BINARY_DIR}/src +) + if(SINGLEAPPLICATION_INCLUDE_DIRS) target_include_directories(strawberry SYSTEM PRIVATE ${SINGLEAPPLICATION_INCLUDE_DIRS}) endif() if(HAVE_QPA_QPLATFORMNATIVEINTERFACE_H) - target_include_directories(strawberry SYSTEM PRIVATE ${Qt${QT_VERSION_MAJOR}Gui_PRIVATE_INCLUDE_DIRS}) + target_include_directories(strawberry_lib SYSTEM PRIVATE ${Qt${QT_VERSION_MAJOR}Gui_PRIVATE_INCLUDE_DIRS}) endif() -target_link_libraries(strawberry PRIVATE +target_link_libraries(strawberry_lib PRIVATE ${CMAKE_THREAD_LIBS_INIT} PkgConfig::GLIB PkgConfig::GOBJECT @@ -1484,11 +1490,10 @@ target_link_libraries(strawberry PRIVATE $<$:execinfo> $<$:dsound dwmapi getopt-win::getopt> $<$:WindowsApp> - ${SINGLEAPPLICATION_LIBRARIES} ) if(APPLE) - target_link_libraries(strawberry PRIVATE + target_link_libraries(strawberry_lib PRIVATE "-framework Foundation" "-framework AppKit" "-framework Carbon" @@ -1500,6 +1505,20 @@ if(APPLE) ) endif() +target_link_libraries(strawberry PRIVATE + ${CMAKE_THREAD_LIBS_INIT} + PkgConfig::GLIB + PkgConfig::GOBJECT + Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::Network + Qt${QT_VERSION_MAJOR}::Sql + Qt${QT_VERSION_MAJOR}::Gui + Qt${QT_VERSION_MAJOR}::Widgets + $<$:Qt${QT_VERSION_MAJOR}::DBus> + ${SINGLEAPPLICATION_LIBRARIES} + strawberry_lib +) + if(NOT APPLE) install(TARGETS strawberry RUNTIME DESTINATION bin) endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b3a70adbd..4a5ade876 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,9 @@ enable_testing() +add_custom_target(strawberry_tests echo "Running Strawberry tests" WORKING_DIRECTORY ${CURRENT_BINARY_DIR}) +add_custom_target(build_tests WORKING_DIRECTORY ${CURRENT_BINARY_DIR}) +add_dependencies(strawberry_tests build_tests) + add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1) set(TESTUTILS-SOURCES @@ -14,21 +18,13 @@ set(TESTUTILS-MOC-HEADERS src/mock_networkaccessmanager.h src/test_utils.h src/t qt_wrap_cpp(TESTUTILS-SOURCES-MOC ${TESTUTILS-MOC-HEADERS}) add_library(test_utils STATIC EXCLUDE_FROM_ALL ${TESTUTILS-SOURCES} ${TESTUTILS-SOURCES-MOC}) -target_include_directories(test_utils SYSTEM PRIVATE - ${GTEST_INCLUDE_DIRS} - ${GMOCK_INCLUDE_DIRS} -) target_include_directories(test_utils PRIVATE ${CMAKE_BINARY_DIR}/src ${CMAKE_SOURCE_DIR}/src ) -target_link_directories(test_utils PRIVATE - ${SQLITE_LIBRARY_DIRS} - ${TAGLIB_LIBRARY_DIRS} -) target_link_libraries(test_utils PRIVATE - ${GTEST_BOTH_LIBRARIES} - ${GMOCK_LIBRARY} + GTest::GTest + GTest::gmock Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Network @@ -36,53 +32,58 @@ target_link_libraries(test_utils PRIVATE Qt${QT_VERSION_MAJOR}::Test ) -add_custom_target(strawberry_tests echo "Running Strawberry tests" WORKING_DIRECTORY ${CURRENT_BINARY_DIR}) -add_custom_target(build_tests WORKING_DIRECTORY ${CURRENT_BINARY_DIR}) -add_dependencies(strawberry_tests build_tests) - qt_add_resources(TEST-RESOURCE-SOURCES data/testdata.qrc ${CMAKE_SOURCE_DIR}/data/data.qrc ${CMAKE_SOURCE_DIR}/data/icons.qrc) -add_library(test_gui_main STATIC EXCLUDE_FROM_ALL ${TEST-RESOURCE-SOURCES} src/main.cpp) -target_include_directories(test_gui_main PRIVATE - ${CMAKE_BINARY_DIR}/src - ${CMAKE_SOURCE_DIR}/src -) - -target_include_directories(test_gui_main SYSTEM PRIVATE ${GSTREAMER_INCLUDE_DIRS}) - -target_link_libraries(test_gui_main PRIVATE strawberry_lib) -set_target_properties(test_gui_main PROPERTIES COMPILE_DEFINITIONS GUI) - add_library(test_main STATIC EXCLUDE_FROM_ALL ${TEST-RESOURCE-SOURCES} src/main.cpp) target_include_directories(test_main PRIVATE ${CMAKE_BINARY_DIR}/src ${CMAKE_SOURCE_DIR}/src ) -if(HAVE_GSTREAMER) - target_include_directories(test_main SYSTEM PRIVATE ${GSTREAMER_INCLUDE_DIRS}) -endif() -target_link_libraries(test_main PRIVATE strawberry_lib) +target_link_libraries(test_main PRIVATE + ${CMAKE_THREAD_LIBS_INIT} + PkgConfig::GLIB + PkgConfig::GOBJECT + PkgConfig::GSTREAMER_BASE + Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::Widgets + strawberry_lib +) + +add_library(test_gui_main STATIC EXCLUDE_FROM_ALL ${TEST-RESOURCE-SOURCES} src/main.cpp) +target_include_directories(test_gui_main PRIVATE + ${CMAKE_BINARY_DIR}/src + ${CMAKE_SOURCE_DIR}/src +) +target_link_libraries(test_gui_main PRIVATE + ${CMAKE_THREAD_LIBS_INIT} + PkgConfig::GLIB + PkgConfig::GOBJECT + PkgConfig::GSTREAMER_BASE + Qt${QT_VERSION_MAJOR}::Core + Qt${QT_VERSION_MAJOR}::Widgets + strawberry_lib +) +set_target_properties(test_gui_main PROPERTIES COMPILE_DEFINITIONS GUI) # Given a file foo_test.cpp, creates a target foo_test and adds it to the test target. macro(add_test_file test_source gui_required) get_filename_component(TEST_NAME ${test_source} NAME_WE) add_executable(${TEST_NAME} EXCLUDE_FROM_ALL ${test_source}) - target_include_directories(${TEST_NAME} SYSTEM PRIVATE - ${GTEST_INCLUDE_DIRS} - ${GMOCK_INCLUDE_DIRS} - ) target_include_directories(${TEST_NAME} PRIVATE ${CMAKE_BINARY_DIR}/src ${CMAKE_SOURCE_DIR}/src - ${TAGLIB_INCLUDE_DIRS} ) target_link_libraries(${TEST_NAME} PRIVATE + ${CMAKE_THREAD_LIBS_INIT} + PkgConfig::GLIB + PkgConfig::GOBJECT + PkgConfig::GSTREAMER_BASE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Concurrent - Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Sql Qt${QT_VERSION_MAJOR}::Test + Qt${QT_VERSION_MAJOR}::Widgets ) target_link_libraries(${TEST_NAME} PRIVATE test_utils) set(GUI_REQUIRED ${gui_required}) diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 1b7ff6c0b..dc9b0a1a1 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -20,7 +20,11 @@ #include -#include +#ifdef GUI +# include +#else +# include +#endif #include "logging_env.h" #include "metatypes_env.h" diff --git a/tests/src/mock_networkaccessmanager.h b/tests/src/mock_networkaccessmanager.h index b8c5d692e..d549860c3 100644 --- a/tests/src/mock_networkaccessmanager.h +++ b/tests/src/mock_networkaccessmanager.h @@ -21,6 +21,8 @@ #ifndef MOCK_NETWORKACCESSMANAGER_H #define MOCK_NETWORKACCESSMANAGER_H +#include + #include #include #include @@ -29,7 +31,6 @@ #include #include "test_utils.h" -#include "gmock/gmock.h" // Usage: // Create a MockNetworkAccessManager. diff --git a/tests/src/mock_playlistitem.h b/tests/src/mock_playlistitem.h index 5f1ead6be..5124dbac7 100644 --- a/tests/src/mock_playlistitem.h +++ b/tests/src/mock_playlistitem.h @@ -27,7 +27,6 @@ #include #include "core/song.h" -#include "core/settingsprovider.h" #include "core/sqlrow.h" #include "playlist/playlistitem.h" diff --git a/tests/src/mock_settingsprovider.h b/tests/src/mock_settingsprovider.h index c62da6779..dfa1b9d01 100644 --- a/tests/src/mock_settingsprovider.h +++ b/tests/src/mock_settingsprovider.h @@ -21,10 +21,10 @@ #ifndef MOCK_SETTINGSPROVIDER_H #define MOCK_SETTINGSPROVIDER_H -#include "core/settingsprovider.h" - #include +#include "core/settingsprovider.h" + // clazy:excludeall=function-args-by-value class MockSettingsProvider : public SettingsProvider { diff --git a/tests/src/resources_env.h b/tests/src/resources_env.h index dc6b54b68..27035db1f 100644 --- a/tests/src/resources_env.h +++ b/tests/src/resources_env.h @@ -34,9 +34,6 @@ class ResourcesEnvironment : public ::testing::Environment { Q_INIT_RESOURCE(data); Q_INIT_RESOURCE(icons); Q_INIT_RESOURCE(testdata); -#ifdef HAVE_TRANSLATIONS - Q_INIT_RESOURCE(translations); -#endif } private: Q_DISABLE_COPY(ResourcesEnvironment)