diff --git a/googlemock/CMakeLists.txt b/googlemock/CMakeLists.txt index 99b2411f3..1d5bc0941 100644 --- a/googlemock/CMakeLists.txt +++ b/googlemock/CMakeLists.txt @@ -141,7 +141,7 @@ if (gmock_build_tests) cxx_test(gmock-actions_test gmock_main) cxx_test(gmock-cardinalities_test gmock_main) cxx_test(gmock_ex_test gmock_main) - cxx_test(gmock-function-mocker_test gmock_main) + cxx_test(gmock-function-mocker_test gmock_main test/split-mock_test_helper.cc) cxx_test(gmock-internal-utils_test gmock_main) cxx_test(gmock-matchers-arithmetic_test gmock_main) cxx_test(gmock-matchers-comparisons_test gmock_main) diff --git a/googlemock/test/gmock-function-mocker_test.cc b/googlemock/test/gmock-function-mocker_test.cc index 05e26c8c4..a12dff2b0 100644 --- a/googlemock/test/gmock-function-mocker_test.cc +++ b/googlemock/test/gmock-function-mocker_test.cc @@ -53,6 +53,9 @@ GTEST_DISABLE_MSC_WARNINGS_PUSH_(4503) #include #include #include +#include + +#include "split-mock_test_helper.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -573,6 +576,34 @@ TEST(FunctionMockerTest, RefQualified) { EXPECT_EQ(std::move(mock_foo).RefQualifiedOverloaded(), 8); // NOLINT } +TEST(SplitMockTest, CallMockMethods) { + MockSplitDeclarationAndDefinition mock_split; + + EXPECT_CALL(mock_split, func(1)).WillOnce(Return(2)); + EXPECT_CALL(mock_split, func_const(3)).WillOnce(Return(4)); + + EXPECT_CALL(mock_split, func_overloaded()).WillOnce(Return(5)); + EXPECT_CALL(mock_split, func_overloaded(6)).WillOnce(Return(7)); + EXPECT_CALL(std::as_const(mock_split), func_overloaded(8)).WillOnce(Return(9)); + EXPECT_CALL(mock_split, func_overloaded(10, 11)).WillOnce(Return(12)); + EXPECT_CALL(std::move(mock_split), func_overloaded(13, 14)).WillOnce(Return(15)); + + EXPECT_CALL(mock_split, func_legacy(16, 17)).WillOnce(Return(18)); + EXPECT_CALL(mock_split, func_legacy_const(19)).WillOnce(Return(20)); + + EXPECT_EQ(mock_split.func(1), 2); + EXPECT_EQ(std::as_const(mock_split).func_const(3), 4); + + EXPECT_EQ(mock_split.func_overloaded(), 5); + EXPECT_EQ(mock_split.func_overloaded(6), 7); + EXPECT_EQ(std::as_const(mock_split).func_overloaded(8), 9); + EXPECT_EQ(mock_split.func_overloaded(10, 11), 12); + EXPECT_EQ(std::move(std::as_const(mock_split)).func_overloaded(13, 14), 15); + + EXPECT_EQ(mock_split.func_legacy(16, 17), 18); + EXPECT_EQ(std::as_const(mock_split).func_legacy_const(19), 20); +} + class MockB { public: MockB() = default; diff --git a/googlemock/test/split-mock_test_helper.cc b/googlemock/test/split-mock_test_helper.cc new file mode 100644 index 000000000..64537e566 --- /dev/null +++ b/googlemock/test/split-mock_test_helper.cc @@ -0,0 +1,57 @@ +// Copyright 2026, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// A test helper file defining the methods of a mock class declared in +// a header file. + +#include "split-mock_test_helper.h" + +MockSplitDeclarationAndDefinition::MockSplitDeclarationAndDefinition() = + default; +MockSplitDeclarationAndDefinition::~MockSplitDeclarationAndDefinition() = + default; + +DEFINE_MOCK_METHOD(MockSplitDeclarationAndDefinition, int, func, (int)); +DEFINE_MOCK_METHOD(MockSplitDeclarationAndDefinition, int, func_const, (int), + (const)); + +DEFINE_MOCK_METHOD(MockSplitDeclarationAndDefinition, int, func_overloaded, ()); +DEFINE_MOCK_METHOD(MockSplitDeclarationAndDefinition, int, func_overloaded, + (int)); +DEFINE_MOCK_METHOD(MockSplitDeclarationAndDefinition, int, func_overloaded, + (int), (const)); +DEFINE_MOCK_METHOD(MockSplitDeclarationAndDefinition, int, func_overloaded, + (int, int), (ref(&))); +DEFINE_MOCK_METHOD(MockSplitDeclarationAndDefinition, int, func_overloaded, + (int, int), (const, ref(&&))); + +DEFINE_MOCK_METHOD2(MockSplitDeclarationAndDefinition, func_legacy, + int(int, int)); +DEFINE_MOCK_CONST_METHOD1(MockSplitDeclarationAndDefinition, func_legacy_const, + int(int)); diff --git a/googlemock/test/split-mock_test_helper.h b/googlemock/test/split-mock_test_helper.h new file mode 100644 index 000000000..bea110c7a --- /dev/null +++ b/googlemock/test/split-mock_test_helper.h @@ -0,0 +1,55 @@ +// Copyright 2026, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// A test helper file declaring a mock class whose methods are defined in +// a .cc file. + +#ifndef GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_ +#define GOOGLEMOCK_TEST_GMOCK_LINK_TEST_H_ + +#include "gmock/gmock-function-mocker.h" + +struct MockSplitDeclarationAndDefinition { + MockSplitDeclarationAndDefinition(); + ~MockSplitDeclarationAndDefinition(); + + DECLARE_MOCK_METHOD(int, func, (int)); + DECLARE_MOCK_METHOD(int, func_const, (int), (const)); + + DECLARE_MOCK_METHOD(int, func_overloaded, ()); + DECLARE_MOCK_METHOD(int, func_overloaded, (int)); + DECLARE_MOCK_METHOD(int, func_overloaded, (int), (const)); + DECLARE_MOCK_METHOD(int, func_overloaded, (int, int), (ref(&))); + DECLARE_MOCK_METHOD(int, func_overloaded, (int, int), (const, ref(&&))); + + DECLARE_MOCK_METHOD2(func_legacy, int(int, int)); + DECLARE_MOCK_CONST_METHOD1(func_legacy_const, int(int)); +}; + +#endif // GOOGLEMOCK_TEST_SPLIT_MOCK_TEST_HELPER_H_