Compare commits

...

5 Commits

Author SHA1 Message Date
Alessio
cef5e9ca72
Merge ffb6156362361018061f4185d1d217958f0f958c into 1b96fa13f549387b7549cc89e1a785cf143a1a50 2025-11-16 17:36:36 +00:00
Alessio
ffb6156362
Merge branch 'google:main' into main 2025-11-16 18:36:32 +01:00
Alessio Attilio
92b54856cf
Merge branch 'google:main' into main 2025-09-03 23:20:27 +02:00
Alessio Attilio
b43258902b
Merge branch 'google:main' into main 2025-09-01 22:01:46 +02:00
SigAttilio
bc110c7ca3 fix: Add mock_type to MockObjectState for dynamic type registration
Signed-off-by: SigAttilio <226562783+SigAttilio@users.noreply.github.com>
2025-08-23 22:27:33 +02:00

View File

@ -456,7 +456,8 @@ static CallReaction intToCallReaction(int mock_behavior) {
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
// detecting leaked mock objects and explicitly verifying a mock's
@ -473,6 +474,8 @@ struct MockObjectState {
::std::string first_used_test;
bool leakable; // true if and only if it's OK to leak 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
@ -481,13 +484,22 @@ struct MockObjectState {
// is removed from the registry in the mock object's destructor.
class MockObjectRegistry {
public:
// Maps a mock object (identified by its address) to its state.
typedef std::map<const void*, MockObjectState> StateMap;
// New alias for StateMap
using StateMap = std::map<const void*, MockObjectState>;
// This destructor will be called when a program exits, after all
// tests in it have been run. By then, there should be no mock
// object alive. Therefore we report any living object as test
// failure, unless the user explicitly asked us to ignore it.
// New method to register the type of mock
template <typename MockClass>
void RegisterMockType(const void* mock_obj) {
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() {
if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return;
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.
continue;
// FIXME: Print the type of the leaked object.
// This can help the user identify the leaked object.
std::cout << "\n";
// Print the type of the leaked object, if available
std::string type_name = "<unknown>";
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;
std::cout << internal::FormatFileLocation(state.first_used_file,
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
// given mock object.
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);
}
// Tells Google Mock to fail uninteresting calls on the given mock
// object.
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);
}