Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cc99c5c

Browse files
committedApr 14, 2025
test: setup gtest for project
1 parent 34ae5ef commit cc99c5c

File tree

3 files changed

+67
-28
lines changed

3 files changed

+67
-28
lines changed
 

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bld/
2121
[Bb]in/
2222
[Oo]bj/
2323
[Ll]og/
24+
cmake-build-debug/
2425

2526
# Visual Studio 2015 cache/options directory
2627
.vs/

‎CMakeLists.txt

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
22

33
# ---- Project ----
4-
54
# Note: update this to your new project's name and version
65
project(
7-
Glob
8-
VERSION 1.0
9-
LANGUAGES CXX
6+
Glob
7+
VERSION 1.0
8+
LANGUAGES CXX
109
)
1110

1211
# ---- Options ----
1312
option(GLOB_USE_GHC_FILESYSTEM "Use ghc::filesystem instead of std::filesystem" OFF)
1413

1514
# ---- Include guards ----
1615

17-
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
18-
message(
19-
FATAL_ERROR
20-
"In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there."
21-
)
22-
endif()
16+
if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
17+
message(
18+
FATAL_ERROR
19+
"In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there."
20+
)
21+
endif ()
2322

2423
# ---- Add dependencies via CPM ----
2524
# see https://github.com/TheLartians/CPM.cmake for more info
@@ -28,10 +27,20 @@ include(cmake/CPM.cmake)
2827

2928
# PackageProject.cmake will be used to make our target installable
3029
CPMAddPackage(
31-
NAME PackageProject.cmake
32-
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
33-
VERSION 1.3
30+
NAME PackageProject.cmake
31+
GITHUB_REPOSITORY TheLartians/PackageProject.cmake
32+
VERSION 1.3
33+
)
34+
35+
CPMAddPackage(
36+
NAME googletest
37+
GITHUB_REPOSITORY google/googletest
38+
GIT_TAG v1.16.0
39+
VERSION 1.16.0
40+
OPTIONS "INSTALL_GTEST OFF" "gtest_force_shared_crt"
3441
)
42+
# For Windows: Prevent overriding the parent project's compiler/linker settings
43+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
3544

3645
# ---- Add source files ----
3746

@@ -47,23 +56,23 @@ file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/
4756
# INTERFACE_COMPILE_FEATURES cxx_std_17)
4857

4958
add_library(Glob ${headers} ${sources})
50-
SET_TARGET_PROPERTIES(Glob PROPERTIES OUTPUT_NAME glob)
59+
set_target_properties(Glob PROPERTIES OUTPUT_NAME glob)
5160
set_target_properties(Glob PROPERTIES CXX_STANDARD 17)
5261

53-
if ( GLOB_USE_GHC_FILESYSTEM )
54-
# Switch to ghc::filesystem.
55-
target_link_libraries(Glob PRIVATE ghcFilesystem::ghc_filesystem)
56-
target_compile_definitions(Glob PUBLIC GLOB_USE_GHC_FILESYSTEM)
57-
endif()
62+
if (GLOB_USE_GHC_FILESYSTEM)
63+
# Switch to ghc::filesystem.
64+
target_link_libraries(Glob PRIVATE ghcFilesystem::ghc_filesystem)
65+
target_compile_definitions(Glob PUBLIC GLOB_USE_GHC_FILESYSTEM)
66+
endif ()
5867

5968
# being a cross-platform target, we enforce standards conformance on MSVC
6069
target_compile_options(Glob PUBLIC "$<$<BOOL:${MSVC}>:/permissive->")
6170

6271
# Link dependencies (if required) target_link_libraries(Glob PUBLIC cxxopts)
6372

6473
target_include_directories(
65-
Glob PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
66-
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
74+
Glob PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
75+
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
6776
)
6877

6978
# ---- Create an installable target ----
@@ -74,11 +83,26 @@ target_include_directories(
7483
string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
7584

7685
packageProject(
77-
NAME ${PROJECT_NAME}
78-
VERSION ${PROJECT_VERSION}
79-
BINARY_DIR ${PROJECT_BINARY_DIR}
80-
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
81-
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
82-
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
83-
DEPENDENCIES ""
86+
NAME ${PROJECT_NAME}
87+
VERSION ${PROJECT_VERSION}
88+
BINARY_DIR ${PROJECT_BINARY_DIR}
89+
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
90+
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
91+
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
92+
DEPENDENCIES ""
8493
)
94+
95+
# --- setup tests ---
96+
enable_testing()
97+
98+
add_executable(glob_tests test/rglob_test.cpp)
99+
set_property(TARGET glob_tests PROPERTY CXX_STANDARD 17)
100+
target_link_libraries(glob_tests PRIVATE gtest_main ${PROJECT_NAME})
101+
add_test(NAME glob_tests COMMAND glob_tests)
102+
103+
add_executable(glob_tests_single test/rglob_test.cpp)
104+
set_property(TARGET glob_tests_single PROPERTY CXX_STANDARD 17)
105+
target_compile_definitions(glob_tests_single PRIVATE USE_SINGLE_HEADER=1)
106+
target_link_libraries(glob_tests_single PRIVATE gtest_main)
107+
target_include_directories(glob_tests_single PRIVATE single_include)
108+
add_test(NAME glob_tests_single COMMAND glob_tests_single)

‎test/rglob_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <gtest/gtest.h>
2+
3+
4+
#ifdef USE_SINGLE_HEADER
5+
#include "glob/glob.hpp"
6+
#else
7+
#include "glob/glob.h"
8+
#endif
9+
10+
11+
TEST(rglobTest, MatchNonExistent) {
12+
auto matches = glob::rglob("non-existent/**");
13+
EXPECT_EQ(matches.size(), 0);
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.