Skip to content

Commit

Permalink
Adding unit test for tmSerialize class
Browse files Browse the repository at this point in the history
  • Loading branch information
lucsch committed Jul 6, 2024
1 parent dc40e2d commit 685e5ba
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 9 deletions.
26 changes: 17 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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}")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -102,22 +111,22 @@ 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}
MACOSX_BUNDLE WIN32
${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)
Expand Down Expand Up @@ -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()
Expand Down
118 changes: 118 additions & 0 deletions test/src/test_tmserialize.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 685e5ba

Please sign in to comment.