feat: make default mock behavior configurable via CMake option

This commit is contained in:
Manikandan K. S. 2026-07-04 22:16:38 +05:30
parent 973323ed64
commit eec840f0b6
3 changed files with 12 additions and 5 deletions

View File

@ -10,6 +10,9 @@
option(gmock_build_tests "Build all of Google Mock's own tests." OFF) option(gmock_build_tests "Build all of Google Mock's own tests." OFF)
set(gmock_default_mock_behavior "1" CACHE STRING
"Controls default mock behavior (0=Nice, 1=Naggy, 2=Strict).")
# A directory to find Google Test sources. # A directory to find Google Test sources.
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt") if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt")
set(gtest_dir gtest) set(gtest_dir gtest)
@ -102,6 +105,9 @@ else()
set_target_properties(gmock_main PROPERTIES VERSION ${GOOGLETEST_VERSION}) set_target_properties(gmock_main PROPERTIES VERSION ${GOOGLETEST_VERSION})
endif() endif()
target_compile_definitions(gmock PRIVATE
GMOCK_DEFAULT_MOCK_BEHAVIOR=${gmock_default_mock_behavior})
string(REPLACE ";" "$<SEMICOLON>" dirs "${gmock_build_include_dirs}") string(REPLACE ";" "$<SEMICOLON>" dirs "${gmock_build_include_dirs}")
target_include_directories(gmock SYSTEM INTERFACE target_include_directories(gmock SYSTEM INTERFACE
"$<BUILD_INTERFACE:${dirs}>" "$<BUILD_INTERFACE:${dirs}>"

View File

@ -38,10 +38,11 @@
// uninteresting calls as errors. // uninteresting calls as errors.
// //
// Currently a mock is naggy by default, so MockFoo and // Currently a mock is naggy by default, so MockFoo and
// NaggyMock<MockFoo> behave like the same. However, we will soon // NaggyMock<MockFoo> behave like the same. The default can be
// switch the default behavior of mocks to be nice, as that in general // overridden at build time via the GMOCK_DEFAULT_MOCK_BEHAVIOR
// leads to more maintainable tests. When that happens, MockFoo will // CMake option, or at runtime via the --gmock_default_mock_behavior
// stop behaving like NaggyMock<MockFoo> and start behaving like // flag. If the default is set to NiceMock, MockFoo will stop
// behaving like NaggyMock<MockFoo> and start behaving like
// NiceMock<MockFoo>. // NiceMock<MockFoo>.
// //
// NiceMock, NaggyMock, and StrictMock "inherit" the constructors of // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of

View File

@ -44,7 +44,7 @@ GMOCK_DEFINE_string_(verbose, testing::internal::kWarningVerbosity,
" warning - prints warnings and errors.\n" " warning - prints warnings and errors.\n"
" error - prints errors only."); " error - prints errors only.");
GMOCK_DEFINE_int32_(default_mock_behavior, 1, GMOCK_DEFINE_int32_(default_mock_behavior, GMOCK_DEFAULT_MOCK_BEHAVIOR,
"Controls the default behavior of mocks." "Controls the default behavior of mocks."
" Valid values:\n" " Valid values:\n"
" 0 - by default, mocks act as NiceMocks.\n" " 0 - by default, mocks act as NiceMocks.\n"