Skip to content

Commit

Permalink
googletestを使ったテスト
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishizaki committed Nov 12, 2022
1 parent ed43bb6 commit be0879a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cxx_cmake_ninja/CMakeLists.txt
Expand Up @@ -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.")
Expand Down Expand Up @@ -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 $<TARGET_FILE:bowling_game_test_gtest>
)

#################################################################################

add_executable(
bowling_game_cli
lib/bowling_game/src/bowling_game.cpp
Expand Down
@@ -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; i<n; i++){
pGame->roll(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();
}

0 comments on commit be0879a

Please sign in to comment.