From 685e5ba4fd9234f94606b69f527b128abb5d49c2 Mon Sep 17 00:00:00 2001 From: Lucien Schreiber Date: Sat, 6 Jul 2024 13:29:57 +0200 Subject: [PATCH] Adding unit test for tmSerialize class --- CMakeLists.txt | 26 +++++--- test/src/test_tmserialize.cpp | 118 ++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 9 deletions(-) create mode 100644 test/src/test_tmserialize.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c7dc3527..e32a6a8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,10 +18,10 @@ set(CMAKE_PROJECT_VERSION_MINOR ${${PROJECT_NAME}_MINOR_VERSION}) set(CMAKE_PROJECT_VERSION_PATCH ${GIT_NUMBER}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) -if(UNIX AND NOT APPLE) +if (UNIX AND NOT APPLE) find_package(PNG REQUIRED) message(STATUS "PNG_LIBRARIES: ${PNG_LIBRARY}") -endif() +endif () include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup() @@ -34,6 +34,15 @@ if (MSVC) add_definitions(-D_CRT_SECURE_NO_WARNINGS) endif () +## Code coverage for IDE, see https://www.jetbrains.com/help/clion/code-coverage-clion.html#compiler-flags +SET(USE_CODECOVERAGE_IDE OFF CACHE BOOL "Should we do code coverage for IDE ?") +if (USE_CODECOVERAGE_IDE) + message(STATUS "Code coverage for IDE") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage") + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage") + message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") +endif (USE_CODECOVERAGE_IDE) + # Enable tests SET(USE_UNITTEST CACHE BOOL "Should we create the tests for ${CMAKE_PROJECT_NAME}") @@ -65,7 +74,7 @@ if (WITH_TOOLBASVIEW) CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_DIR} -DCMAKE_BUILD_TYPE=Release BUILD_COMMAND conan build ../ToolBasView INSTALL_COMMAND "" - ) + ) endif () # Sources and headers @@ -102,9 +111,9 @@ set_source_files_properties(${ICON_PATH} PROPERTIES MACOSX_PACKAGE_LOCATION Reso # Create a library with all sources files mainly for unit testing ADD_LIBRARY("${CMAKE_PROJECT_NAME}_lib" STATIC ${SRC_FILES}) target_link_libraries("${CMAKE_PROJECT_NAME}_lib" ${CONAN_LIBS}) -if(UNIX AND NOT APPLE) +if (UNIX AND NOT APPLE) target_link_libraries("${CMAKE_PROJECT_NAME}_lib" ${PNG_LIBRARY} xkbcommon fontconfig) -endif(UNIX AND NOT APPLE) +endif (UNIX AND NOT APPLE) # Create the Toolmap binary ADD_EXECUTABLE(${CMAKE_PROJECT_NAME} @@ -112,12 +121,12 @@ ADD_EXECUTABLE(${CMAKE_PROJECT_NAME} ${MAIN_SRC_FILE} ${ICON_PATH} src/gui/resource.rc - ) +) TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${CMAKE_PROJECT_NAME}_lib) -if(UNIX AND NOT APPLE) +if (UNIX AND NOT APPLE) message(STATUS "PNG lib is : ${PNG_LIBRARY}") target_link_libraries(${CMAKE_PROJECT_NAME} ${PNG_LIBRARY} xkbcommon fontconfig) -endif(UNIX AND NOT APPLE) +endif (UNIX AND NOT APPLE) #IF (WIN32) # SOURCE_GROUP(resource FILES src/resource.rc) @@ -152,7 +161,6 @@ IF (MSVC) MESSAGE(STATUS "Adding multiprocessor support for Visual Studio (/MP Flag)") ENDIF (MSVC) -## Code coverage if (USE_CODECOV) include(CodeCoverage) append_coverage_compiler_flags() diff --git a/test/src/test_tmserialize.cpp b/test/src/test_tmserialize.cpp new file mode 100644 index 00000000..ce23d38f --- /dev/null +++ b/test/src/test_tmserialize.cpp @@ -0,0 +1,118 @@ +#include "gtest/gtest.h" +#include "../../src/components/wxserialize/tmserialize.h" + +TEST(tmSerializeTest, SerializeBool) { + tmSerialize serializer; + serializer << true; + serializer << false; + EXPECT_EQ(serializer.GetString(), _T("1|0|")); +} + +TEST(tmSerializeTest, SerializeString) { + tmSerialize serializer; + serializer << _T("Hello"); + serializer << _T("World"); + EXPECT_EQ(serializer.GetString(), _T("Hello|World|")); +} + +TEST(tmSerializeTest, SerializeInt) { + tmSerialize serializer; + serializer << 42; + serializer << -1337; + EXPECT_EQ(serializer.GetString(), _T("42|-1337|")); +} + +TEST(tmSerializeTest, DeserializeBool) { + tmSerialize serializer(_T("1|0|")); + bool value; + serializer >> value; + EXPECT_EQ(value, true); + serializer >> value; + EXPECT_EQ(value, false); +} + +TEST(tmSerializeTest, DeserializeString) { + tmSerialize serializer(_T("Hello|World|")); + wxString value; + serializer >> value; + EXPECT_EQ(value, _T("Hello")); + serializer >> value; + EXPECT_EQ(value, _T("World")); +} + +TEST(tmSerializeTest, DeserializeInt) { + tmSerialize serializer(_T("42|-1337|")); + int value; + serializer >> value; + EXPECT_EQ(value, 42); + serializer >> value; + EXPECT_EQ(value, -1337); +} + +TEST(tmSerializeTest, CanRead){ + tmSerialize serializer; + int value = 0; + serializer >> value; + EXPECT_EQ(value, 0); + + tmSerialize serialize2(wxEmptyString); + int value2 = 0; + serialize2 >> value2; + EXPECT_EQ(value2, 0); + +} + +TEST(tmSerialize, StoreReadLong){ + long value = 123456789; + long result = 0; + tmSerialize serializer; + serializer << value; + serializer << value; + + tmSerialize serializer2(_T("123456789|123456789|")); + serializer2 >> result; + EXPECT_EQ(value, result); + serializer2 >> result; + EXPECT_EQ(value, result); +} + +TEST(tmSerialize, StoreReadColour){ + wxColour red (255, 0, 0); + wxColour blue (0, 0, 255); + wxColour result; + tmSerialize serializer; + serializer << red; + serializer << blue; + + tmSerialize serializer2(_T("rgb(255,0,0)|rgb(0,0,255)|")); + serializer2 >> result; + EXPECT_EQ(red, result); + serializer2 >> result; + EXPECT_EQ(blue, result); +} + +TEST(tmSerialize, WrongConvert) { + wxString value = _T("Hello"); + int result = 0; + tmSerialize serializer(value); + serializer >> result; + EXPECT_EQ(result, 0); +} + +TEST(tmSerialize, StringSerialize) { + wxString value = _T("Hello"); + wxString result = wxEmptyString; + tmSerialize serialize2; + serialize2 << value; + serialize2 << value; + + tmSerialize serialize3(_T("Hello|Hello|")); + serialize3 >> result; + EXPECT_EQ(result, value); + serialize3 >> result; + EXPECT_EQ(result, value); + + tmSerialize serializer(value); + serializer >> result; + EXPECT_EQ(result, value); +}