Merge 409017ae985aa7eef120d3e9d7c60dd66a2aac1a into 1b96fa13f549387b7549cc89e1a785cf143a1a50

This commit is contained in:
Alessio 2025-11-16 18:06:10 +00:00 committed by GitHub
commit 282de45a49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 12 deletions

1
.gitignore vendored
View File

@ -87,3 +87,4 @@ googlemock/gtest
/CMakeCache.txt /CMakeCache.txt
/ALL_BUILD.vcxproj.filters /ALL_BUILD.vcxproj.filters
/ALL_BUILD.vcxproj /ALL_BUILD.vcxproj
_codeql_*

View File

@ -456,7 +456,8 @@ static CallReaction intToCallReaction(int mock_behavior) {
namespace { namespace {
typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers; // Modern alias for FunctionMockers
using FunctionMockers = std::set<internal::UntypedFunctionMockerBase*>;
// The current state of a mock object. Such information is needed for // The current state of a mock object. Such information is needed for
// detecting leaked mock objects and explicitly verifying a mock's // detecting leaked mock objects and explicitly verifying a mock's
@ -473,6 +474,8 @@ struct MockObjectState {
::std::string first_used_test; ::std::string first_used_test;
bool leakable; // true if and only if it's OK to leak the object. bool leakable; // true if and only if it's OK to leak the object.
FunctionMockers function_mockers; // All registered methods of the object. FunctionMockers function_mockers; // All registered methods of the object.
const std::type_info* mock_type; // Dynamic mock type (if registered)
}; };
// A global registry holding the state of all mock objects that are // A global registry holding the state of all mock objects that are
@ -481,13 +484,22 @@ struct MockObjectState {
// is removed from the registry in the mock object's destructor. // is removed from the registry in the mock object's destructor.
class MockObjectRegistry { class MockObjectRegistry {
public: public:
// Maps a mock object (identified by its address) to its state. // New alias for StateMap
typedef std::map<const void*, MockObjectState> StateMap; using StateMap = std::map<const void*, MockObjectState>;
// This destructor will be called when a program exits, after all // New method to register the type of mock
// tests in it have been run. By then, there should be no mock template <typename MockClass>
// object alive. Therefore we report any living object as test void RegisterMockType(const void* mock_obj) {
// failure, unless the user explicitly asked us to ignore it. internal::MutexLock l(&internal::g_gmock_mutex);
StateMap::iterator it = states_.find(mock_obj);
if (it != states_.end()) {
it->second.mock_type = &typeid(*static_cast<const MockClass*>(mock_obj));
}
}
// This destructor will be called when the program exits, after all tests have run.
// At that point, there should be no live mock objects. Therefore, we report any still-live object as
// a test failure, unless the user has explicitly asked to ignore it.
~MockObjectRegistry() { ~MockObjectRegistry() {
if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return; if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return;
internal::MutexLock l(internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
@ -498,9 +510,12 @@ class MockObjectRegistry {
if (it->second.leakable) // The user said it's fine to leak this object. if (it->second.leakable) // The user said it's fine to leak this object.
continue; continue;
// FIXME: Print the type of the leaked object. // Print the type of the leaked object, if available
// This can help the user identify the leaked object. std::string type_name = "<unknown>";
std::cout << "\n"; if (it->second.mock_type) {
type_name = it->second.mock_type->name();
}
::std::cerr << "ERROR: Leaked mock object of type: " << type_name << "\n";
const MockObjectState& state = it->second; const MockObjectState& state = it->second;
std::cout << internal::FormatFileLocation(state.first_used_file, std::cout << internal::FormatFileLocation(state.first_used_file,
state.first_used_line); state.first_used_line);
@ -575,14 +590,14 @@ void Mock::AllowUninterestingCalls(uintptr_t mock_obj)
// Tells Google Mock to warn the user about uninteresting calls on the // Tells Google Mock to warn the user about uninteresting calls on the
// given mock object. // given mock object.
void Mock::WarnUninterestingCalls(uintptr_t mock_obj) void Mock::WarnUninterestingCalls(uintptr_t mock_obj)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::gmock_mutex) {
SetReactionOnUninterestingCalls(mock_obj, internal::kWarn); SetReactionOnUninterestingCalls(mock_obj, internal::kWarn);
} }
// Tells Google Mock to fail uninteresting calls on the given mock // Tells Google Mock to fail uninteresting calls on the given mock
// object. // object.
void Mock::FailUninterestingCalls(uintptr_t mock_obj) void Mock::FailUninterestingCalls(uintptr_t mock_obj)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::gmock_mutex) {
SetReactionOnUninterestingCalls(mock_obj, internal::kFail); SetReactionOnUninterestingCalls(mock_obj, internal::kFail);
} }

View File

@ -561,6 +561,15 @@ GTEST_API_ TestInfo* MakeAndRegisterTestInfo(
TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc, TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc,
TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory); TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory);
// Backward-compatible overload for ABI compatibility with v1.14.0 and earlier.
// This version takes const char* instead of std::string to avoid ABI issues
// when passing std::string by value across library boundaries.
GTEST_API_ TestInfo* MakeAndRegisterTestInfo(
const char* test_suite_name, const char* name, const char* type_param,
const char* value_param, CodeLocation code_location,
TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc,
TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory);
// If *pstr starts with the given prefix, modifies *pstr to be right // If *pstr starts with the given prefix, modifies *pstr to be right
// past the prefix and returns true; otherwise leaves *pstr unchanged // past the prefix and returns true; otherwise leaves *pstr unchanged
// and returns false. None of pstr, *pstr, and prefix can be NULL. // and returns false. None of pstr, *pstr, and prefix can be NULL.

View File

@ -2829,6 +2829,19 @@ TestInfo* MakeAndRegisterTestInfo(
return test_info; return test_info;
} }
// Backward-compatible overload for ABI compatibility with v1.14.0 and earlier.
TestInfo* MakeAndRegisterTestInfo(
const char* test_suite_name, const char* name, const char* type_param,
const char* value_param, CodeLocation code_location,
TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc,
TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory) {
// Forward to the std::string version
return MakeAndRegisterTestInfo(std::string(test_suite_name), name, type_param,
value_param, std::move(code_location),
fixture_class_id, set_up_tc, tear_down_tc,
factory);
}
void ReportInvalidTestSuiteType(const char* test_suite_name, void ReportInvalidTestSuiteType(const char* test_suite_name,
const CodeLocation& code_location) { const CodeLocation& code_location) {
Message errors; Message errors;