-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add gtest unit tests for some of the gamelog API (#17)
* add gtest unit tests for some of the gamelog API * include gtest via cmake instead of find_package
- Loading branch information
Showing
6 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |