Merge pull request #590 from tbeu/restructure-cpp-build-targets

Restructure C++ CMake build targets
This commit is contained in:
Emily Bache 2025-02-12 05:43:26 +00:00 committed by GitHub
commit 6c59257f27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 6968 additions and 3004 deletions

View File

@ -1,37 +1,11 @@
cmake_minimum_required(VERSION 3.13)
project(gilded-rose-refactoring-kata-cpp)
# Load FetchContent module.
include(FetchContent)
# Declare GoogleTest as the content to fetch
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)
FetchContent_Declare(
catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.3.2
)
FetchContent_MakeAvailable(googletest catch2)
#set(gtest_force_shared_crt OFF CACHE BOOL "" FORCE)
# Force Google Test to link the C/C++ runtimes dynamically when
# building on Visual Studio
# Link:
# * https://github.com/google/googletest/tree/release-1.8.1/googletest#visual-studio-dynamic-vs-static-runtimes
if (MSVC)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# uncomment this line to enable coverage measurements using gcov
# set(CMAKE_CXX_FLAGS "--coverage")
enable_testing()
add_subdirectory(src)
add_subdirectory(lib)
add_subdirectory(test)

View File

@ -15,7 +15,7 @@ The `GildedRose.cc` file, i.e. the code under test, is identical in all four var
## Prerequisites
* CMake version >= 3.13
* C++ compiler that support C++11
* C++ compiler that supports C++14
## How to build and run tests in a terminal

View File

@ -1,4 +0,0 @@
#ifndef APPROVAL_TEST_1_APPROVALTESTS_HPP
#define APPROVAL_TEST_1_APPROVALTESTS_HPP
#include "ApprovalTests.v.6.0.0.hpp"
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +0,0 @@
set(LIB_NAME lib)
add_library(${LIB_NAME} INTERFACE)
target_include_directories(${LIB_NAME} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -1,4 +1,4 @@
set(SRC_LIB_NAME src)
set(SRC_LIB_NAME GildedRoseLib)
add_library(${SRC_LIB_NAME})
target_sources(${SRC_LIB_NAME} PRIVATE GildedRose.cc)
target_sources(${SRC_LIB_NAME} PRIVATE GildedRose.cc GildedRose.h)
target_include_directories(${SRC_LIB_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -1,3 +1,30 @@
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.16.0
GIT_SHALLOW TRUE
)
FetchContent_Declare(
catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.8.0
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(googletest catch2)
# Force Google Test to link the C/C++ runtimes dynamically
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Disable building GMock
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
# Do not install GTest
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
add_subdirectory(cpp_catch2_approvaltest)
add_subdirectory(cpp_catch2_unittest)
add_subdirectory(cpp_googletest_approvaltest)

View File

@ -1,9 +1,14 @@
set(TEST_NAME GildedRoseCatch2ApprovalTests)
add_executable(${TEST_NAME})
target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2ApprovalTests.cc)
target_link_libraries(${TEST_NAME} lib src Catch2::Catch2 Catch2::Catch2WithMain)
set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
target_include_directories(${TEST_NAME} PUBLIC ../third_party)
target_link_libraries(${TEST_NAME} GildedRoseLib Catch2::Catch2 Catch2::Catch2WithMain)
set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 14)
add_test(
NAME ${TEST_NAME}
COMMAND ${TEST_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path.
# The __FILE__ macro is used by Catch2 to get the path to current test file.

View File

@ -1,5 +1,5 @@
#define APPROVALS_CATCH
#include "ApprovalTests.hpp"
#define APPROVALS_CATCH2_V3
#include <ApprovalTests.hpp>
#include "GildedRose.h"
std::ostream& operator<<(std::ostream& os, const Item& obj)

View File

@ -1,9 +1,13 @@
set(TEST_NAME GildedRoseCatch2UnitTests)
add_executable(${TEST_NAME})
target_sources(${TEST_NAME} PRIVATE GildedRoseCatch2UnitTests.cc)
target_link_libraries(${TEST_NAME} lib src Catch2::Catch2 Catch2::Catch2WithMain )
set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
target_link_libraries(${TEST_NAME} GildedRoseLib Catch2::Catch2 Catch2::Catch2WithMain )
set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 14)
add_test(
NAME ${TEST_NAME}
COMMAND ${TEST_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path.
# The __FILE__ macro is used by Catch2 to get the path to current test file.

View File

@ -1,5 +1,4 @@
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch2/catch_all.hpp"
#include <catch2/catch_all.hpp>
#include "GildedRose.h"
TEST_CASE("GildedRoseUnitTest", "Foo")

View File

@ -1,9 +1,14 @@
set(TEST_NAME GildedRoseGoogletestApprovalTests)
add_executable(${TEST_NAME})
target_sources(${TEST_NAME} PRIVATE googletest_approval_main.cc GildedRoseGoogletestApprovalTests.cc)
target_link_libraries(${TEST_NAME} lib src gtest)
target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestApprovalTests.cc)
target_include_directories(${TEST_NAME} PUBLIC ../third_party)
target_link_libraries(${TEST_NAME} GildedRoseLib gtest)
set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
add_test(
NAME ${TEST_NAME}
COMMAND ${TEST_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path.
# The __FILE__ macro can be used to get the path to current test file.

View File

@ -1,8 +1,5 @@
// Include header files for test frameworks
#include <gtest/gtest.h>
#define APPROVALS_GOOGLETEST
#include <ApprovalTests.hpp>
// Include code to be tested
#include "GildedRose.h"
std::ostream& operator<<(std::ostream& os, const Item& obj)

View File

@ -1,2 +0,0 @@
#define APPROVALS_GOOGLETEST // This tells Approval Tests to provide a main() - only do this in one cpp file
#include "ApprovalTests.hpp"

View File

@ -1,9 +1,13 @@
set(TEST_NAME GildedRoseGoogletestUnitTests)
add_executable(${TEST_NAME})
target_sources(${TEST_NAME} PRIVATE GildedRoseGoogletestUnitTests.cc)
target_link_libraries(${TEST_NAME} src gtest gtest_main)
target_link_libraries(${TEST_NAME} GildedRoseLib gtest gtest_main)
set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
add_test(
NAME ${TEST_NAME}
COMMAND ${TEST_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path.
# The __FILE__ macro can be used to get the path to current test file.

View File

@ -1,9 +1,13 @@
set(TEST_NAME GildedRoseTextTests)
add_executable(${TEST_NAME} GildedRoseTextTests.cc)
target_sources(${TEST_NAME} PRIVATE GildedRoseTextTests.cc)
target_link_libraries(${TEST_NAME} lib src)
target_link_libraries(${TEST_NAME} GildedRoseLib)
set_property(TARGET ${TEST_NAME} PROPERTY CXX_STANDARD 11)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
add_test(
NAME ${TEST_NAME}
COMMAND ${TEST_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
# Set compiler option /FC for Visual Studio to to make the __FILE__ macro expand to full path.
# The __FILE__ macro is used by Catch2 to get the path to current test file.

6897
cpp/test/third_party/ApprovalTests.hpp vendored Normal file

File diff suppressed because it is too large Load Diff