From e2008e2e36a823d06cc728714c3ca3ac9a2a6fba Mon Sep 17 00:00:00 2001 From: James Swan <122404367+swan-amazon@users.noreply.github.com> Date: Fri, 14 Jun 2024 22:44:32 +0000 Subject: [PATCH] Integrated gmock --- .gitmodules | 4 ++ BUILD.gn | 1 + build_overrides/googletest.gni | 18 ++++++ examples/build_overrides/googletest.gni | 18 ++++++ src/lib/support/tests/BUILD.gn | 3 + src/lib/support/tests/TestGmockUsage.cpp | 40 ++++++++++++ third_party/googletest/BUILD.gn | 78 ++++++++++++++++++++++++ third_party/googletest/repo | 1 + 8 files changed, 163 insertions(+) create mode 100644 build_overrides/googletest.gni create mode 100644 examples/build_overrides/googletest.gni create mode 100644 src/lib/support/tests/TestGmockUsage.cpp create mode 100644 third_party/googletest/BUILD.gn create mode 160000 third_party/googletest/repo diff --git a/.gitmodules b/.gitmodules index 55d44f9dba9e9f..e9857452412b17 100644 --- a/.gitmodules +++ b/.gitmodules @@ -324,3 +324,7 @@ url = https://github.com/Infineon/optiga-trust-m.git branch = matter_support platforms = infineon +[submodule "third_party/googletest/repo"] + path = third_party/googletest/repo + url = https://github.com/google/googletest.git + branch = v1.14.x diff --git a/BUILD.gn b/BUILD.gn index 5de6441925c6e6..2cc0b221426c8c 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -14,6 +14,7 @@ import("//build_overrides/build.gni") import("//build_overrides/chip.gni") +import("//build_overrides/googletest.gni") import("//build_overrides/mbedtls.gni") import("//build_overrides/nlassert.gni") import("//build_overrides/nlunit_test.gni") diff --git a/build_overrides/googletest.gni b/build_overrides/googletest.gni new file mode 100644 index 00000000000000..a16a342f1de996 --- /dev/null +++ b/build_overrides/googletest.gni @@ -0,0 +1,18 @@ +# Copyright (c) 2024 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +declare_args() { + # Root directory for googletest. + googletest_root = "//third_party/googletest" +} diff --git a/examples/build_overrides/googletest.gni b/examples/build_overrides/googletest.gni new file mode 100644 index 00000000000000..dd7eb268f085ea --- /dev/null +++ b/examples/build_overrides/googletest.gni @@ -0,0 +1,18 @@ +# Copyright (c) 2024 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +declare_args() { + # Root directory for nlassert. + googletest_root = "//third_party/googletest" +} diff --git a/src/lib/support/tests/BUILD.gn b/src/lib/support/tests/BUILD.gn index b8cdcfeb2e059f..b4036cdfa0ddf7 100644 --- a/src/lib/support/tests/BUILD.gn +++ b/src/lib/support/tests/BUILD.gn @@ -14,6 +14,7 @@ import("//build_overrides/build.gni") import("//build_overrides/chip.gni") +import("//build_overrides/googletest.gni") import("//build_overrides/nlunit_test.gni") import("//build_overrides/pigweed.gni") @@ -44,6 +45,7 @@ chip_test_suite("tests") { "TestErrorStr.cpp", "TestFixedBufferAllocator.cpp", "TestFold.cpp", + "TestGmockUsage.cpp", "TestIniEscaping.cpp", "TestIntrusiveList.cpp", "TestJsonToTlv.cpp", @@ -93,5 +95,6 @@ chip_test_suite("tests") { "${chip_root}/src/lib/support:testing", "${chip_root}/src/lib/support/jsontlv", "${chip_root}/src/platform", + "${googletest_root}:googlemock", ] } diff --git a/src/lib/support/tests/TestGmockUsage.cpp b/src/lib/support/tests/TestGmockUsage.cpp new file mode 100644 index 00000000000000..5823cfa9855532 --- /dev/null +++ b/src/lib/support/tests/TestGmockUsage.cpp @@ -0,0 +1,40 @@ +#include +#include + +using ::testing::Return; + +// Define the interface +class Foo +{ +public: + virtual ~Foo() = default; + virtual int DoSomething(int a) = 0; +}; + +// Create the mock class +class MockFoo : public Foo +{ +public: + MOCK_METHOD(int, DoSomething, (int a), (override)); +}; + +// Test function that uses Foo +int UseFoo(Foo * foo, int value) +{ + return foo->DoSomething(value); +} + +// Test case +TEST(GMockTest, BasicValidation) +{ + MockFoo mock_foo; + + // Set up the expectation + EXPECT_CALL(mock_foo, DoSomething(5)).WillOnce(Return(10)); + + // Call the function with the mock object + int result = UseFoo(&mock_foo, 5); + + // Validate the result + EXPECT_EQ(result, 11); +} diff --git a/third_party/googletest/BUILD.gn b/third_party/googletest/BUILD.gn new file mode 100644 index 00000000000000..a8cd71f6049833 --- /dev/null +++ b/third_party/googletest/BUILD.gn @@ -0,0 +1,78 @@ +# Copyright (c) 2024 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build_overrides/build.gni") +import("${build_root}/config/compiler/compiler.gni") + +config("googletest_config") { + include_dirs = [ + "repo/googletest/include", + "repo/googletest", + ] +} + +static_library("googletest") { + output_name = "libgoogletest" + + include_dirs = [ + "repo/googletest/include", + "repo/googletest", + ] + + sources = [ + "repo/googletest/src/gtest-all.cc", + "repo/googletest/src/gtest-assertion-result.cc", + "repo/googletest/src/gtest-death-test.cc", + "repo/googletest/src/gtest-filepath.cc", + "repo/googletest/src/gtest-internal-inl.h", + "repo/googletest/src/gtest-matchers.cc", + "repo/googletest/src/gtest-port.cc", + "repo/googletest/src/gtest-printers.cc", + "repo/googletest/src/gtest-test-part.cc", + "repo/googletest/src/gtest-typed-test.cc", + "repo/googletest/src/gtest.cc", + "repo/googletest/src/gtest_main.cc", + ] + + public_configs = [ ":googletest_config" ] +} + +config("googlemock_config") { + include_dirs = [ "repo/googlemock/include" ] +} + +static_library("googlemock") { + output_name = "libgooglemock" + + include_dirs = [ + "repo/googlemock/include", + "repo/googlemock", + ] + + sources = [ + "repo/googlemock/src/gmock-all.cc", + "repo/googlemock/src/gmock-cardinalities.cc", + "repo/googlemock/src/gmock-internal-utils.cc", + "repo/googlemock/src/gmock-matchers.cc", + "repo/googlemock/src/gmock-spec-builders.cc", + "repo/googlemock/src/gmock.cc", + "repo/googlemock/src/gmock_main.cc", + ] + + deps = [ ":googletest" ] + public_configs = [ + ":googlemock_config", + ":googletest_config", + ] +} diff --git a/third_party/googletest/repo b/third_party/googletest/repo new file mode 160000 index 00000000000000..f8d7d77c069363 --- /dev/null +++ b/third_party/googletest/repo @@ -0,0 +1 @@ +Subproject commit f8d7d77c06936315286eb55f8de22cd23c188571