diff --git a/SUMMARY.md b/SUMMARY.md index ecb43e2..c53f8a7 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -14,10 +14,14 @@ * [C++11 and Beyond](chapters/cpp11.md) * [Adding Features](chapters/features.md) * [How to Structure Your Project](chapters/structure.md) -* [Including Small Projects](chapters/smallinc.md) -* [Including Large Projects (X)](chapters/largeinc.md) +* [Including Projects](chapters/projects.md) + * [Submodule](chapters/projects/submodule.md) + * [DownloadProject](chapters/projects/download.md) + * [Fetch (3.11)](chapters/projects/fetch.md) * [Running Other Programs (X)](chapters/programs.md) -* [Testing (INCOMPLETE)](chapters/testing.md) +* [Testing](chapters/testing.md) + * [GoogleTest](chapters/testing/googletest.md) + * [Catch](chapters/testing/catch.md) * [Tidy and Format (X)](chapters/tidy.md) * [IDEs (X)](chapters/IDEs.md) * [Debugging (X)](chapters/debug.md) diff --git a/chapters/smallinc.md b/chapters/projects.md similarity index 100% rename from chapters/smallinc.md rename to chapters/projects.md diff --git a/chapters/projects/download.md b/chapters/projects/download.md new file mode 100644 index 0000000..0fb3536 --- /dev/null +++ b/chapters/projects/download.md @@ -0,0 +1,34 @@ +# GoogleTest: Download method + +You can use the downloader in my [CMake helper repository][CLIUtils/cmake], using CMake's `include` command. + +This is a downloader for [GoogleTest], based on the excellent [DownloadProject] tool. Downloading a copy for each project is the recommended way to use GoogleTest (so much so, in fact, that they have disabled the automatic CMake install target), so this respects that design decision. This method downloads the project at configure time, so that IDE's correctly find the libraries. Using it is simple: + +```cmake +cmake_minimum_required(VERSION 3.4) +project(MyProject CXX) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) + +enable_testing() # Must be in main file + +include(AddGoogleTest) # Could be in /tests/CMakeLists.txt +add_executable(SimpleTest SimpleTest.cu) +add_gtest(SimpleTest) +``` + +> Note: `add_gtest` is just a macro that adds `gtest`, `gmock`, and `gtest_main`, and then runs `add_test` to create a test with the same name: +> ```cmake +> target_link_libraries(SimpleTest gtest gmock gtest_main) +> add_test(SimpleTest SimpleTest) +> ``` + + +## Catch + + +[^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest. + + +[CLIUtils/cmake]: https://github.com/CLIUtils/cmake +[GoogleTest]: https://github.com/google/googletest +[DownloadProject]: https://github.com/Crascit/DownloadProject diff --git a/chapters/projects/fetch.md b/chapters/projects/fetch.md new file mode 100644 index 0000000..964cf49 --- /dev/null +++ b/chapters/projects/fetch.md @@ -0,0 +1,15 @@ +# Testing + +## General Testing Information + +## GoogleTest + + +## Fetch + + + + +[CLIUtils/cmake]: https://github.com/CLIUtils/cmake +[GoogleTest]: https://github.com/google/googletest +[DownloadProject]: https://github.com/Crascit/DownloadProject diff --git a/chapters/projects/submodule.md b/chapters/projects/submodule.md new file mode 100644 index 0000000..9da0328 --- /dev/null +++ b/chapters/projects/submodule.md @@ -0,0 +1,67 @@ +# GoogleTest: Submodule method (preferred) + +To use this method, just checkout GoogleTest as a submodule:[^1] + +```cmake +git submodule add --branch=release-1.8.0 ../../google/googletest.git extern/googletest +``` + +Then, in your main `CMakeLists.txt`: + +```cmake +option(PACKAGE_TESTS "Build the tests" ON) +if(PACKAGE_TESTS) + enable_testing() + add_subdirectory(tests) +endif() +``` + +As mentioned before, you have to do the `enable_testing` in your main CMakeLists. Now, in your tests directory: + +```cmake +add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest") +``` + +If you did this in your main CMakeLists, you could use a normal add_subdirectory; the extra path here is needed to correct the build path because we are calling it from a subdirectory. + +The next line is optional, but keeps your `CACHE` cleaner: + +```cmake +mark_as_advanced( + BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS + gmock_build_tests gtest_build_samples gtest_build_tests + gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols +) +``` + +If you are interested in keeping IDEs that support folders clean, I would also add these lines: + +```cmake +set_target_properties(gtest PROPERTIES FOLDER extern) +set_target_properties(gtest_main PROPERTIES FOLDER extern) +set_target_properties(gmock PROPERTIES FOLDER extern) +set_target_properties(gmock_main PROPERTIES FOLDER extern) +``` + +Then, to add a test, I'd recommend the following macro: + +```cmake +macro(package_add_test TESTNAME) + add_executable(${TESTNAME} ${ARGN}) + target_link_libraries(${TESTNAME} gtest gmock gtest_main) + add_test(${TESTNAME} ${TESTNAME}) + set_target_properties(${TESTNAME} PROPERTIES FOLDER tests) +endmacro() + +package_add_test(test1 test1.cpp) +``` + +This will allow you to quickly and simply add tests. Feel free to adjust to suit your needs. If you haven't seen it before, `ARGN` is "every argument after the listed ones". + + +[^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest. + + +[CLIUtils/cmake]: https://github.com/CLIUtils/cmake +[GoogleTest]: https://github.com/google/googletest +[DownloadProject]: https://github.com/Crascit/DownloadProject diff --git a/chapters/testing.md b/chapters/testing.md index 491f3f8..a905dab 100644 --- a/chapters/testing.md +++ b/chapters/testing.md @@ -2,100 +2,11 @@ ## General Testing Information - -## GoogleTest -### Submodule method (preferred) - -To use this method, just checkout GoogleTest as a submodule:[^1] +In your main CMakeLists.txt you need to add the following function call (not in a subfolder): ```cmake -git submodule add --branch=release-1.8.0 ../../google/googletest.git extern/googletest +enable_testing() ``` -Then, in your main `CMakeLists.txt`: -```cmake -option(PACKAGE_TESTS "Build the tests" ON) -if(PACKAGE_TESTS) - enable_testing() - add_subdirectory(tests) -endif() -``` - -As mentioned before, you have to do the `enable_testing` in your main CMakeLists. Now, in your tests directory: - -```cmake -add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest") -``` - -If you did this in your main CMakeLists, you could use a normal add_subdirectory; the extra path here is needed to correct the build path because we are calling it from a subdirectory. - -The next line is optional, but keeps your `CACHE` cleaner: - -```cmake -mark_as_advanced( - BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS - gmock_build_tests gtest_build_samples gtest_build_tests - gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols -) -``` - -If you are interested in keeping IDEs that support folders clean, I would also add these lines: - -```cmake -set_target_properties(gtest PROPERTIES FOLDER extern) -set_target_properties(gtest_main PROPERTIES FOLDER extern) -set_target_properties(gmock PROPERTIES FOLDER extern) -set_target_properties(gmock_main PROPERTIES FOLDER extern) -``` - -Then, to add a test, I'd recommend the following macro: - -```cmake -macro(package_add_test TESTNAME) - add_executable(${TESTNAME} ${ARGN}) - target_link_libraries(${TESTNAME} gtest gmock gtest_main) - add_test(${TESTNAME} ${TESTNAME}) - set_target_properties(${TESTNAME} PROPERTIES FOLDER tests) -endmacro() - -package_add_test(test1 test1.cpp) -``` - -This will allow you to quickly and simply add tests. Feel free to adjust to suit your needs. If you haven't seen it before, `ARGN` is "every argument after the listed ones". - - -### Download method - -You can use the downloader in my [CMake helper repository][CLIUtils/cmake], using CMake's `include` command. - -This is a downloader for [GoogleTest], based on the excellent [DownloadProject] tool. Downloading a copy for each project is the recommended way to use GoogleTest (so much so, in fact, that they have disabled the automatic CMake install target), so this respects that design decision. This method downloads the project at configure time, so that IDE's correctly find the libraries. Using it is simple: - -```cmake -cmake_minimum_required(VERSION 3.4) -project(MyProject CXX) -list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) - -enable_testing() # Must be in main file - -include(AddGoogleTest) # Could be in /tests/CMakeLists.txt -add_executable(SimpleTest SimpleTest.cu) -add_gtest(SimpleTest) -``` - -> Note: `add_gtest` is just a macro that adds `gtest`, `gmock`, and `gtest_main`, and then runs `add_test` to create a test with the same name: -> ```cmake -> target_link_libraries(SimpleTest gtest gmock gtest_main) -> add_test(SimpleTest SimpleTest) -> ``` - - -## Catch - - -[^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest. - - -[CLIUtils/cmake]: https://github.com/CLIUtils/cmake -[GoogleTest]: https://github.com/google/googletest -[DownloadProject]: https://github.com/Crascit/DownloadProject +Look at the subchapters for recipes for popular frameworks. diff --git a/chapters/testing/catch.md b/chapters/testing/catch.md new file mode 100644 index 0000000..1dc087f --- /dev/null +++ b/chapters/testing/catch.md @@ -0,0 +1,4 @@ +# Catch + + + diff --git a/chapters/testing/googletest.md b/chapters/testing/googletest.md new file mode 100644 index 0000000..97a88a3 --- /dev/null +++ b/chapters/testing/googletest.md @@ -0,0 +1,94 @@ +# GoogleTest + +## Submodule method (preferred) + +To use this method, just checkout GoogleTest as a submodule:[^1] + +```cmake +git submodule add --branch=release-1.8.0 ../../google/googletest.git extern/googletest +``` + +Then, in your main `CMakeLists.txt`: + +```cmake +option(PACKAGE_TESTS "Build the tests" ON) +if(PACKAGE_TESTS) + enable_testing() + add_subdirectory(tests) +endif() +``` + +As mentioned before, you have to do the `enable_testing` in your main CMakeLists. Now, in your tests directory: + +```cmake +add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest") +``` + +If you did this in your main CMakeLists, you could use a normal add_subdirectory; the extra path here is needed to correct the build path because we are calling it from a subdirectory. + +The next line is optional, but keeps your `CACHE` cleaner: + +```cmake +mark_as_advanced( + BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS + gmock_build_tests gtest_build_samples gtest_build_tests + gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols +) +``` + +If you are interested in keeping IDEs that support folders clean, I would also add these lines: + +```cmake +set_target_properties(gtest PROPERTIES FOLDER extern) +set_target_properties(gtest_main PROPERTIES FOLDER extern) +set_target_properties(gmock PROPERTIES FOLDER extern) +set_target_properties(gmock_main PROPERTIES FOLDER extern) +``` + +Then, to add a test, I'd recommend the following macro: + +```cmake +macro(package_add_test TESTNAME) + add_executable(${TESTNAME} ${ARGN}) + target_link_libraries(${TESTNAME} gtest gmock gtest_main) + add_test(${TESTNAME} ${TESTNAME}) + set_target_properties(${TESTNAME} PROPERTIES FOLDER tests) +endmacro() + +package_add_test(test1 test1.cpp) +``` + +This will allow you to quickly and simply add tests. Feel free to adjust to suit your needs. If you haven't seen it before, `ARGN` is "every argument after the listed ones". + +## Download method + +You can use the downloader in my [CMake helper repository][CLIUtils/cmake], using CMake's `include` command. + +This is a downloader for [GoogleTest], based on the excellent [DownloadProject] tool. Downloading a copy for each project is the recommended way to use GoogleTest (so much so, in fact, that they have disabled the automatic CMake install target), so this respects that design decision. This method downloads the project at configure time, so that IDE's correctly find the libraries. Using it is simple: + +```cmake +cmake_minimum_required(VERSION 3.4) +project(MyProject CXX) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) + +enable_testing() # Must be in main file + +include(AddGoogleTest) # Could be in /tests/CMakeLists.txt +add_executable(SimpleTest SimpleTest.cu) +add_gtest(SimpleTest) +``` + +> Note: `add_gtest` is just a macro that adds `gtest`, `gmock`, and `gtest_main`, and then runs `add_test` to create a test with the same name: +> ```cmake +> target_link_libraries(SimpleTest gtest gmock gtest_main) +> add_test(SimpleTest SimpleTest) +> ``` + + + +[^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest. + + +[CLIUtils/cmake]: https://github.com/CLIUtils/cmake +[GoogleTest]: https://github.com/google/googletest +[DownloadProject]: https://github.com/Crascit/DownloadProject diff --git a/examples/root-dict/CMakeLists.txt b/examples/root-dict/CMakeLists.txt index 1c41f18..031be0e 100644 --- a/examples/root-dict/CMakeLists.txt +++ b/examples/root-dict/CMakeLists.txt @@ -9,11 +9,11 @@ include("${ROOT_DIR}/modules/RootNewMacros.cmake") message(STATUS "Found ROOT: ${ROOT_VERSION} at ${ROOT_DIR}") -string(REPLACE " " ";" ROOT_CXX_FLAG_LIST "${ROOT_CXX_FLAGS}") +separate_arguments(ROOT_CXX_FLAGS) set_target_properties(ROOT::Core PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${ROOT_INCLUDE_DIRS}" - INTERFACE_COMPILE_OPTIONS "${ROOT_CXX_FLAG_LIST}" + INTERFACE_COMPILE_OPTIONS "${ROOT_CXX_FLAGS}" ) include_directories(ROOT_BUG) diff --git a/examples/root-simple-3.11/CMakeLists.txt b/examples/root-simple-3.11/CMakeLists.txt index b9e9362..f344e6c 100644 --- a/examples/root-simple-3.11/CMakeLists.txt +++ b/examples/root-simple-3.11/CMakeLists.txt @@ -7,11 +7,11 @@ project(RootSimpleExample311 LANGUAGES CXX) find_package(ROOT CONFIG REQUIRED) message(STATUS "Found ROOT: ${ROOT_VERSION} at ${ROOT_DIR}") -string(REPLACE " " ";" ROOT_CXX_FLAG_LIST "${ROOT_CXX_FLAGS}") +seperate_arguments(ROOT_CXX_FLAGS) ## [modern_fix] target_include_directories(ROOT::Core INTERFACE "${ROOT_INCLUDE_DIRS}") -target_compile_options(ROOT::Core INTERFACE "${ROOT_CXX_FLAG_LIST}") +target_compile_options(ROOT::Core INTERFACE "${ROOT_CXX_FLAGS}") ## [modern_fix] add_executable(RootSimpleExample311 SimpleExample.cxx) diff --git a/examples/root-simple/CMakeLists.txt b/examples/root-simple/CMakeLists.txt index 741c219..2e00890 100644 --- a/examples/root-simple/CMakeLists.txt +++ b/examples/root-simple/CMakeLists.txt @@ -13,12 +13,12 @@ message(STATUS "Found ROOT: ${ROOT_VERSION} at ${ROOT_DIR}") # ROOT targets are missing includes and flags ## [setup_properties] # Fix for ROOT_CXX_FLAGS not actually being a CMake list -string(REPLACE " " ";" ROOT_CXX_FLAG_LIST "${ROOT_CXX_FLAGS}") +separate_arguments(ROOT_CXX_FLAGS) set_property(TARGET ROOT::Core PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${ROOT_INCLUDE_DIRS}") set_property(TARGET ROOT::Core APPEND PROPERTY - INTERFACE_COMPILE_OPTIONS ${ROOT_CXX_FLAG_LIST}) + INTERFACE_COMPILE_OPTIONS ${ROOT_CXX_FLAGS}) ## [setup_properties] # Adding an exectuable program and linking to needed ROOT libraries