From be0879acbe5b4c34a03d8637c13fcb16295c4be4 Mon Sep 17 00:00:00 2001 From: Nishizaki Date: Sat, 12 Nov 2022 21:26:16 +0900 Subject: [PATCH] =?UTF-8?q?googletest=E3=82=92=E4=BD=BF=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cxx_cmake_ninja/CMakeLists.txt | 29 ++++++ .../test/gtest/bowling_game_test_gtest.cpp | 89 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 cxx_cmake_ninja/lib/bowling_game/test/gtest/bowling_game_test_gtest.cpp diff --git a/cxx_cmake_ninja/CMakeLists.txt b/cxx_cmake_ninja/CMakeLists.txt index 2c80b00..9c1b8b6 100644 --- a/cxx_cmake_ninja/CMakeLists.txt +++ b/cxx_cmake_ninja/CMakeLists.txt @@ -18,6 +18,20 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR}) message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt. ") endif() +################################################################################# + +include(FetchContent) +FetchContent_Declare( + googletest + # Specify the commit you depend on and update it regularly. + URL https://github.com/google/googletest/archive/5376968f6948923e2411081fd9372e71a59d8e77.zip +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +################################################################################# + set(default_build_type "Debug") if(NOT CMAKE_BUILD_TYPE) message(STATUS "Setting build type to '${default_build_type}' as none was specified.") @@ -67,6 +81,21 @@ add_test( ################################################################################# +add_executable( + bowling_game_test_gtest + lib/bowling_game/src/bowling_game.cpp + lib/bowling_game/test/gtest/bowling_game_test_gtest.cpp +) + +target_link_libraries(bowling_game_test_gtest gtest_main) + +add_test( + NAME bowling_game_test_gtest + COMMAND ${TEST_LUNCHER} bowling_game_test_gtest $ +) + +################################################################################# + add_executable( bowling_game_cli lib/bowling_game/src/bowling_game.cpp diff --git a/cxx_cmake_ninja/lib/bowling_game/test/gtest/bowling_game_test_gtest.cpp b/cxx_cmake_ninja/lib/bowling_game/test/gtest/bowling_game_test_gtest.cpp new file mode 100644 index 0000000..499ded5 --- /dev/null +++ b/cxx_cmake_ninja/lib/bowling_game/test/gtest/bowling_game_test_gtest.cpp @@ -0,0 +1,89 @@ +#include "bowling_game.hpp" +#include "gtest/gtest.h" + +namespace my { +namespace project { +namespace { + +// The fixture for testing class bowling_game. +class bowling_game_test : public ::testing::Test { + protected: + // You can remove any or all of the following functions if their bodies would + // be empty. + + bowling_game_test() { + // You can do set-up work for each test here. + } + + ~bowling_game_test() override { + // You can do clean-up work that doesn't throw exceptions here. + } + + // If the constructor and destructor are not enough for setting up + // and cleaning up each test, you can define the following methods: + + void SetUp() override { + // Code here will be called immediately after the constructor (right + // before each test). + } + + void TearDown() override { + // Code here will be called immediately after each test (right + // before the destructor). + } + + // Class members declared here can be used by all tests in the test suite + // for bowling_game. + + static void roll_many(bowling_game* pGame, int n, int pins){ + int i; + for(i=0; iroll(pins); + } +} +}; + +TEST_F(bowling_game_test, gutter_game) { + bowling_game game; + roll_many(&game, 20, 0); + EXPECT_EQ(0, game.score()); +} + +TEST_F(bowling_game_test, all_ones) { + bowling_game game; + roll_many(&game, 20, 1); + EXPECT_EQ(20, game.score()); +} + +TEST_F(bowling_game_test, one_spare) { + bowling_game game; + game.roll(5); + game.roll(5); /* spare */ + game.roll(3); + roll_many(&game, 17, 0); + EXPECT_EQ(16, game.score()); +} + +TEST_F(bowling_game_test, one_strike) { + bowling_game game; + game.roll(10); + game.roll(3); + game.roll(4); + roll_many(&game, 16, 0); + EXPECT_EQ(24, game.score()); +} + +TEST_F(bowling_game_test, perfect_game) { + bowling_game game; + roll_many(&game, 12, 10); + EXPECT_EQ(300, game.score()); +} + +} // namespace +} // namespace project +} // namespace my + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}