Skip to content

Commit

Permalink
add gtest unit tests for some of the gamelog API (#17)
Browse files Browse the repository at this point in the history
* add gtest unit tests for some of the gamelog API
* include gtest via cmake instead of find_package
  • Loading branch information
BigPeet authored Mar 18, 2021
1 parent 8f1c31c commit caf6ad9
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ endif()
if(ENABLE_TESTING)
message(STATUS "Enable testing.")
enable_testing()
message(STATUS "#################### Importing GTest. ####################")
include(cmake/GoogleTest.cmake)
message(STATUS "############### Finished Importing GTest. ################")
endif()

# Add source code directories
Expand Down
24 changes: 24 additions & 0 deletions cmake/GoogleTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Download and unpack googletest at configure time
configure_file(cmake/GoogleTest.cmake.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
15 changes: 15 additions & 0 deletions cmake/GoogleTest.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 2.8.12)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
32 changes: 32 additions & 0 deletions lib/gamelog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ set(LOG_HEADERS
include/gamelog/gamelog.h
)

set(LOG_TESTS
tests/test_main.cpp
tests/test_gamelog.cpp
)

add_library(
${LOG_LIB}
SHARED
Expand Down Expand Up @@ -43,3 +48,30 @@ install(TARGETS ${LOG_LIB}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${LOG_LIB}
)

# Unit Tests
if (ENABLE_TESTING)
add_executable(
${LOG_LIB}-test
${LOG_TESTS}
)
target_link_libraries(
${LOG_LIB}-test
PRIVATE
${LOG_LIB}
gtest
gtest_main
project_options
project_warnings
)

add_test(
NAME ${LOG_LIB}-test
COMMAND ${LOG_LIB}-test
)

install(
TARGETS ${LOG_LIB}-test
RUNTIME DESTINATION build
)
endif()
143 changes: 143 additions & 0 deletions lib/gamelog/tests/test_gamelog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include <filesystem>
#include "gtest/gtest.h"

#include "gamelog/gamelog.h"

class GameLogTest : public ::testing::Test
{
public:
constexpr static char const* m_sentinel{"@P"};

protected:
void SetUp() override {}

void TearDown() override {}
};


TEST_F(GameLogTest, IsMatchGameLog_Valid) // NOLINT
{
std::filesystem::path const valid_01{"Match_GameLog_1234.dat"};
std::filesystem::path const valid_02{"/some/path/Match_GameLog_1234.dat"};

ASSERT_TRUE(gl::IsMatchGameLog(valid_01));
ASSERT_TRUE(gl::IsMatchGameLog(valid_02));
}


TEST_F(GameLogTest, IsMatchGameLog_Invalid) // NOLINT
{
std::filesystem::path const invalid_01{"Match_GameLog_1234.txt"};
std::filesystem::path const invalid_02{"Match_GameLog_1234.log"};
std::filesystem::path const invalid_03{"MyGamelog.dat"};
std::filesystem::path const invalid_04{"Match_GameChat_1234.dat"};

ASSERT_FALSE(gl::IsMatchGameLog(invalid_01));
ASSERT_FALSE(gl::IsMatchGameLog(invalid_02));
ASSERT_FALSE(gl::IsMatchGameLog(invalid_03));
ASSERT_FALSE(gl::IsMatchGameLog(invalid_04));
}


TEST_F(GameLogTest, ParseGameLogFileStr_Empty_01) // NOLINT
{
std::vector<std::string> expected{};
ASSERT_EQ(gl::ParseGameLogFile(std::string{}), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_Empty_02) // NOLINT
{
std::string content{"Here is some nonsense.\nNo sentinel characters included."};
std::vector<std::string> expected{};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_Sentinel_01) // NOLINT
{
std::string s1{"Here is some text with just a"};
std::string s2{" single sentinel character."};
std::string content{s1 + m_sentinel + s2};
std::vector<std::string> expected{s2};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_Sentinel_02) // NOLINT
{
std::string s1{"Here is some text with "};
std::string s2{"multiple sentinel characters."};
std::string s3{"Another sentence."};
std::string content{s1 + m_sentinel + s2 + m_sentinel + s3};
std::vector<std::string> expected{s2, s3};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_Sentinel_03) // NOLINT
{
std::string s1{"Here is some text with "};
std::string s2{"multiple sentinel characters in a row."};
std::string s3{"Another sentence."};
std::string content{s1 + m_sentinel + s2 + m_sentinel + m_sentinel + s3};
std::vector<std::string> expected{s2, s3};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_CutOff_01) // NOLINT
{
std::string s1{"Here is some text with "};
std::string s2{"multiple sentinel characters."};
std::string s3{"Another sentence."};
std::string non_ascii{"ſþÖÄ"};
std::string content{s1 + m_sentinel + s2 + non_ascii + m_sentinel + s3};
std::vector<std::string> expected{s2, s3};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_CutOff_02) // NOLINT
{
std::string s1{"Here is some text with "};
std::string s2{"a single sentinel character but an additional sentence. Another sentence."};
std::string content{s1 + m_sentinel + s2};
std::vector<std::string> expected{s2};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_CutOff_03) // NOLINT
{
std::string s1{"Here is some text with "};
std::string s2{"a single sentinel character and a linebreak."};
std::string s3{"Another sentence which should not be in the result."};
std::string content{s1 + m_sentinel + s2 + '\n' + s3};
std::vector<std::string> expected{s2};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_CutOff_04) // NOLINT
{
std::string prefix1{"Player1 rolled a 2."};
std::string prefix2{"Player2 rolled a 4."};
std::string t1{"Turn 1: Player1"};
std::string t2{"Turn 1: Player2"};
std::string ignored{"asdaAsd123"};
std::string content{m_sentinel + prefix1 + m_sentinel + prefix2 + m_sentinel + t1 + m_sentinel +
t2 + ignored};
std::vector<std::string> expected{prefix1, prefix2, t1, t2};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}


TEST_F(GameLogTest, ParseGameLogFileStr_CutOff_05) // NOLINT
{
std::string s1{"Here is some text with "};
std::string s2{"a single sentinel character (and some reminder text example...)."};
std::string content{s1 + m_sentinel + s2};
std::vector<std::string> expected{s2};
ASSERT_EQ(gl::ParseGameLogFile(content), expected);
}
7 changes: 7 additions & 0 deletions lib/gamelog/tests/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "gtest/gtest.h"

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit caf6ad9

Please sign in to comment.